| 1 | # Copyright Infomation | ||
| 2 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 3 | # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D. | ||
| 4 | # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com | ||
| 5 | # Email : mewsoft@cpan.org, support@mewsoft.com | ||
| 6 | # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved. | ||
| 7 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 8 | package Nile::Plugin::Minify::Perl; | ||
| 9 | |||
| 10 | our $VERSION = '0.55'; | ||
| 11 | our $AUTHORITY = 'cpan:MEWSOFT'; | ||
| 12 | |||
| 13 | =pod | ||
| 14 | |||
| 15 | =encoding utf8 | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Nile::Plugin::Minify::Perl - Perl code minifier plugin for the Nile framework. | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Nile::Plugin::Minify::Perl - Perl code minifier plugin for the Nile framework. | ||
| 26 | |||
| 27 | $app->plugin->minify->perl($output_file => $file1, $file2, $url1, $url2, ...); | ||
| 28 | |||
| 29 | $app->plugin->minify->perl('app.pm' => '/file1.pm', '/file2.pm'); | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | use Nile::Base; | ||
| 34 | use Perl::Tidy; | ||
| 35 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 36 | sub process { | ||
| 37 | |||
| 38 | my ($self, $out, @files) = @_; | ||
| 39 | |||
| 40 | my $content = ""; | ||
| 41 | |||
| 42 | foreach my $file (@files) { | ||
| 43 | $content .= $self->process_file($file); | ||
| 44 | } | ||
| 45 | |||
| 46 | $self->app->file->put($out, $content); | ||
| 47 | } | ||
| 48 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 49 | sub process_file { | ||
| 50 | |||
| 51 | my ($self, $file) = @_; | ||
| 52 | |||
| 53 | my $source_string = $self->app->file->get($file); | ||
| 54 | |||
| 55 | my $dest_string; | ||
| 56 | my $stderr_string; | ||
| 57 | my $errorfile_string; | ||
| 58 | |||
| 59 | my $argv = "-npro"; # Ignore any .perltidyrc at this site | ||
| 60 | |||
| 61 | $argv .= " -pbp"; # Format according to perl best practices | ||
| 62 | $argv .= " -nst"; # Must turn off -st in case -pbp is specified | ||
| 63 | $argv .= " -se"; # -se appends the errorfile to stderr | ||
| 64 | $argv .= " -ci=0 -cti=0 -dac -sil=0"; | ||
| 65 | |||
| 66 | #-pbp is an abbreviation for the parameters in the book B<Perl Best Practices> by Damian Conway: | ||
| 67 | # -l=78 -i=4 -ci=4 -st -se -vt=2 -cti=0 -pt=1 -bt=1 -sbt=1 -bbt=1 -nsfs -nolq | ||
| 68 | # -wbb='% + - * / x != == >= <= =~ !~ < > | & = **= += *= &= <<= &&= -= /= |= >>= ||= //= .= %= ^= x=' | ||
| 69 | |||
| 70 | $argv = "-vt=0 -nolq -i=0 -sil=0 -ce -l=1024 -nbl -pt=2 -bt=2 -sbt=2 -bbt=2 -bvt=1 -sbvt=1 -pvtc=1 -cti=0 -ci=0 -nsfs -nsts -bar -dac -dbc -dsc -dp -sob -ce -mbl=0 -dws -ple "; | ||
| 71 | $argv .= " -nsak='my our local' -naws "; | ||
| 72 | |||
| 73 | my $error = Perl::Tidy::perltidy( | ||
| 74 | argv => $argv, | ||
| 75 | source => \$source_string, | ||
| 76 | destination => \$dest_string, | ||
| 77 | stderr => \$stderr_string, | ||
| 78 | errorfile => \$errorfile_string, # ignored when -se flag is set | ||
| 79 | ); | ||
| 80 | |||
| 81 | if ($error) { | ||
| 82 | # serious error in input parameters, no tidied output | ||
| 83 | $self->app->abort("Perl::Minifier Error $stderr_string"); | ||
| 84 | } | ||
| 85 | |||
| 86 | return $dest_string; | ||
| 87 | } | ||
| 88 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 89 | |||
| 90 | =pod | ||
| 91 | |||
| 92 | =head1 Bugs | ||
| 93 | |||
| 94 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||
| 95 | |||
| 96 | =head1 HOMEPAGE | ||
| 97 | |||
| 98 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||
| 99 | |||
| 100 | =head1 SOURCE | ||
| 101 | |||
| 102 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||
| 103 | |||
| 104 | =head1 SEE ALSO | ||
| 105 | |||
| 106 | See L<Nile> for details about the complete framework. | ||
| 107 | |||
| 108 | =head1 AUTHOR | ||
| 109 | |||
| 110 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||
| 111 | Website: http://www.mewsoft.com | ||
| 112 | |||
| 113 | =head1 COPYRIGHT AND LICENSE | ||
| 114 | |||
| 115 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||
| 116 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||
| 117 | |||
| 118 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | 1; |