| 1 | package Mojolicious::Plugin::AssetPack::Pipe::Less; | ||
| 2 | use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe'; | ||
| 3 | |||
| 4 | use Mojolicious::Plugin::AssetPack::Util qw(diag $CWD DEBUG); | ||
| 5 | |||
| 6 | sub process { | ||
| 7 | my ($self, $assets) = @_; | ||
| 8 | my $store = $self->assetpack->store; | ||
| 9 | my $file; | ||
| 10 | |||
| 11 | $assets->each(sub { | ||
| 12 | my ($asset, $index) = @_; | ||
| 13 | return if $asset->format ne 'less'; | ||
| 14 | my $attrs = $asset->TO_JSON; | ||
| 15 | @$attrs{qw(format key)} = qw(css less); | ||
| 16 | return $asset->content($file)->FROM_JSON($attrs) if $file = $store->load($attrs); | ||
| 17 | diag 'Process "%s" with checksum %s.', $asset->url, $attrs->{checksum} if DEBUG; | ||
| 18 | my @args = qw(lessc --no-color); | ||
| 19 | my $file = $asset->path ? $asset : Mojo::Asset::File->new->add_chunk($asset->content); | ||
| 20 | push @args, '--include-path=' . $asset->path->dirname if $asset->path; | ||
| 21 | push @args, $file->path; | ||
| 22 | $self->run(\@args, undef, \my $css); | ||
| 23 | $asset->content($store->save(\$css, $attrs))->FROM_JSON($attrs); | ||
| 24 | }); | ||
| 25 | } | ||
| 26 | |||
| 27 | sub _install_lessc { | ||
| 28 | my $self = shift; | ||
| 29 | my $bin = $self->app->home->rel_file('node_modules/.bin/lessc'); | ||
| 30 | return $bin if -e $bin; | ||
| 31 | local $CWD = $self->app->home->to_string; | ||
| 32 | $self->app->log->warn('Installing lessc... Please wait. (npm install less)'); | ||
| 33 | $self->run([qw(npm install less)]); | ||
| 34 | return $bin; | ||
| 35 | } | ||
| 36 | |||
| 37 | 1; | ||
| 38 | |||
| 39 | =encoding utf8 | ||
| 40 | |||
| 41 | =head1 NAME | ||
| 42 | |||
| 43 | Mojolicious::Plugin::AssetPack::Pipe::Less - Process Less CSS files | ||
| 44 | |||
| 45 | =head1 DESCRIPTION | ||
| 46 | |||
| 47 | L<Mojolicious::Plugin::AssetPack::Pipe::Less> will process | ||
| 48 | L<http://lesscss.org/> files into JavaScript. | ||
| 49 | |||
| 50 | This module require the C<less> executable to be installed. C<less> will be | ||
| 51 | automatically installed using L<https://www.npmjs.com/> unless already | ||
| 52 | installed. | ||
| 53 | |||
| 54 | =head1 METHODS | ||
| 55 | |||
| 56 | =head2 process | ||
| 57 | |||
| 58 | See L<Mojolicious::Plugin::AssetPack::Pipe/process>. | ||
| 59 | |||
| 60 | =head1 SEE ALSO | ||
| 61 | |||
| 62 | L<Mojolicious::Plugin::AssetPack>. | ||
| 63 | |||
| 64 | =cut |