| 1 | package Mojolicious::Plugin::AssetPack::Pipe::Riotjs; | ||
| 2 | use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe'; | ||
| 3 | |||
| 4 | use Mojo::File 'path'; | ||
| 5 | use Mojolicious::Plugin::AssetPack::Util qw(diag $CWD DEBUG); | ||
| 6 | |||
| 7 | has _riotjs => sub { [shift->_find_app([qw(nodejs node)]), path(__FILE__)->dirname->child('riot.js')] }; | ||
| 8 | |||
| 9 | sub process { | ||
| 10 | my ($self, $assets) = @_; | ||
| 11 | my $store = $self->assetpack->store; | ||
| 12 | my $file; | ||
| 13 | |||
| 14 | $assets->each(sub { | ||
| 15 | my ($asset, $index) = @_; | ||
| 16 | my $attrs = $asset->TO_JSON; | ||
| 17 | $attrs->{key} = 'riot'; | ||
| 18 | $attrs->{format} = 'js'; | ||
| 19 | return unless $asset->format eq 'tag'; | ||
| 20 | return $asset->content($file)->FROM_JSON($attrs) if $file = $store->load($attrs); | ||
| 21 | local $CWD = $self->app->home->to_string; | ||
| 22 | local $ENV{NODE_PATH} = $self->app->home->rel_file('node_modules'); | ||
| 23 | $self->run([qw(riot --version)], undef, \undef) unless $self->{installed}++; | ||
| 24 | $self->run($self->_riotjs, \$asset->content, \my $js); | ||
| 25 | $asset->content($store->save(\$js, $attrs))->FROM_JSON($attrs); | ||
| 26 | }); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub _install_riot { | ||
| 30 | my $self = shift; | ||
| 31 | my $path = $self->app->home->rel_file('node_modules/.bin/riot'); | ||
| 32 | return $path if -e $path; | ||
| 33 | local $CWD = $self->app->home->to_string; | ||
| 34 | $self->app->log->warn('Installing riot... Please wait. (npm install riot)'); | ||
| 35 | $self->run([qw(npm install riot)]); | ||
| 36 | return $path; | ||
| 37 | } | ||
| 38 | |||
| 39 | 1; | ||
| 40 | |||
| 41 | =encoding utf8 | ||
| 42 | |||
| 43 | =head1 NAME | ||
| 44 | |||
| 45 | Mojolicious::Plugin::AssetPack::Pipe::Riotjs - Process Riotjs .tag files | ||
| 46 | |||
| 47 | =head1 SYNOPSIS | ||
| 48 | |||
| 49 | use Mojolicious::Lite; | ||
| 50 | plugin AssetPack => {pipes => [qw(Riotjs JavaScript)]}; | ||
| 51 | |||
| 52 | Note that the above will not load the other default pipes, such as | ||
| 53 | L<Mojolicious::Plugin::AssetPack::Pipe::Css>. | ||
| 54 | |||
| 55 | =head1 DESCRIPTION | ||
| 56 | |||
| 57 | L<Mojolicious::Plugin::AssetPack::Pipe::Riotjs> will process | ||
| 58 | L<http://riotjs.com/> ".tag" files. | ||
| 59 | |||
| 60 | This module require L<https://www.npmjs.com/> to compile Riotjs tag files. | ||
| 61 | |||
| 62 | =head1 METHODS | ||
| 63 | |||
| 64 | =head2 process | ||
| 65 | |||
| 66 | See L<Mojolicious::Plugin::AssetPack::Pipe/process>. | ||
| 67 | |||
| 68 | =head1 SEE ALSO | ||
| 69 | |||
| 70 | L<Mojolicious::Plugin::AssetPack>. | ||
| 71 | |||
| 72 | =cut |