File Coverage

blib/lib/Mojolicious/Plugin/AssetPack/Preprocessors.pm
Criterion Covered Total %
statement 28 64 43.7
branch 0 16 0.0
condition 1 2 50.0
subroutine 10 16 62.5
pod 5 5 100.0
total 44 103 42.7


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::AssetPack::Preprocessors;
2 1     1   14402 use Mojo::Base 'Mojo::EventEmitter';
  1         7249  
  1         6  
3 1     1   1801 use Mojo::Util ();
  1         77592  
  1         30  
4 1     1   405 use Mojolicious::Plugin::AssetPack::Preprocessor;
  1         4  
  1         11  
5 1     1   30 use File::Basename;
  1         3  
  1         59  
6 1     1   351 use File::Which;
  1         726  
  1         53  
7 1     1   407 use IPC::Run3 ();
  1         13236  
  1         35  
8 1   50 1   8 use constant DEBUG => $ENV{MOJO_ASSETPACK_DEBUG} || 0;
  1         2  
  1         586  
9              
10             our $VERSION = '0.01';
11              
12             my %PREPROCESSORS = (
13             coffee => 'Mojolicious::Plugin::AssetPack::Preprocessor::CoffeeScript',
14             css => 'Mojolicious::Plugin::AssetPack::Preprocessor::Css',
15             js => 'Mojolicious::Plugin::AssetPack::Preprocessor::JavaScript',
16             jsx => 'Mojolicious::Plugin::AssetPack::Preprocessor::Jsx',
17             less => 'Mojolicious::Plugin::AssetPack::Preprocessor::Less',
18             sass => 'Mojolicious::Plugin::AssetPack::Preprocessor::Sass',
19             scss => 'Mojolicious::Plugin::AssetPack::Preprocessor::Scss',
20             );
21              
22             has fallback => sub {
23             require Mojolicious::Plugin::AssetPack::Preprocessor::Fallback;
24             Mojolicious::Plugin::AssetPack::Preprocessor::Fallback->new;
25             };
26              
27             sub add {
28 0     0 1 0 my ($self, $extension, $arg) = @_;
29              
30             # create object
31 0 0       0 if (@_ == 4) {
32 0 0       0 my $class
33             = $arg =~ /::/ ? $arg : "Mojolicious::Plugin::AssetPack::Preprocessor::$arg";
34 0 0       0 eval "require $class;1" or die "Could not load $class: $@\n";
35 0         0 warn "[AssetPack] Adding $class preprocessor for $extension.\n" if DEBUG;
36 0         0 my $object = $class->new(pop);
37 0         0 return $self->add($extension => $object);
38             }
39              
40             # back compat
41 0 0       0 if (ref $arg eq 'CODE') {
42 0         0 $arg = Mojolicious::Plugin::AssetPack::Preprocessor->new(processor => $arg);
43             }
44              
45 0         0 $self->on($extension => $arg);
46             }
47              
48             sub can_process {
49 0     0 1 0 my ($self, $extension) = @_;
50              
51 0         0 for my $p ($self->_preprocessors($extension)) {
52 0 0       0 return 1 if $p->can_process;
53             }
54              
55 0         0 return 0;
56             }
57              
58             sub checksum {
59 0     0 1 0 my ($self, $extension, $text, $filename) = @_;
60 0         0 my $cwd = Mojolicious::Plugin::AssetPack::Preprocessors::CWD->new(dirname $filename);
61 0         0 my @checksum;
62              
63 0         0 for my $p ($self->_preprocessors($extension)) {
64 0         0 $p->cwd($cwd->[0]);
65 0         0 push @checksum, $p->checksum($text, $filename);
66             }
67              
68 0 0       0 return @checksum == 1 ? $checksum[0] : Mojo::Util::md5_sum(join '', @checksum);
69             }
70              
71             sub process {
72 0     0 1 0 my ($self, $extension, $assetpack, $text, $filename) = @_;
73 0         0 my $cwd = Mojolicious::Plugin::AssetPack::Preprocessors::CWD->new(dirname $filename);
74 0         0 my @err;
75              
76 0         0 for my $p ($self->_preprocessors($extension)) {
77 0         0 $p->cwd($cwd->[0]);
78 0         0 $p->($p, $assetpack, $text, $filename);
79             }
80              
81 0         0 return $self;
82             }
83              
84 0     0 1 0 sub remove { shift->unsubscribe(@_) }
85              
86             sub _preprocessors {
87 0     0   0 my ($self, $extension) = @_;
88 0         0 my @preprocessors = @{$self->subscribers($extension)};
  0         0  
89              
90 0 0       0 return @preprocessors if @preprocessors;
91 0         0 my $class = $PREPROCESSORS{$extension};
92 0 0       0 return $self->add($extension => $class => {}) if $class;
93 0         0 return $self->fallback;
94             }
95              
96             package Mojolicious::Plugin::AssetPack::Preprocessors::CWD;
97 1     1   8 use Cwd;
  1         2  
  1         120  
98 1     1   776 sub new { my $self = bless [getcwd], $_[0]; chdir $_[1]; return $self; }
  1         12  
  1         3  
99 1     1   300 sub DESTROY { chdir $_[0]->[0]; }
100              
101             1;
102              
103             =encoding utf8
104              
105             =head1 NAME
106              
107             Mojolicious::Plugin::AssetPack::Preprocessors - DEPRECATED
108              
109             =head1 DESCRIPTION
110              
111             L will be DEPRECATED.
112              
113             =head1 ATTRIBUTES
114              
115             =head2 fallback
116              
117             =head1 METHODS
118              
119             =head2 add
120              
121             =head2 can_process
122              
123             =head2 checksum
124              
125             =head2 process
126              
127             =head2 map_type
128              
129             =head2 remove
130              
131             =head1 SEE ALSO
132              
133             L
134              
135             =cut