| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Dist::Zilla::App::Command::bundle; | ||
| 5 | $Dist::Zilla::App::Command::bundle::VERSION = '0.0.10'; | ||
| 6 | # ABSTRACT: concatenate individual files from your dist into bundles | ||
| 7 | |||
| 8 | use Dist::Zilla::App -command; | ||
| 9 | use Dist::Zilla::Plugin::Web::Bundle; | ||
| 10 | use Path::Class; | ||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | sub abstract { 'concatenate individual files from your dist into bundles' } | ||
| 15 | |||
| 16 | |||
| 17 | sub opt_spec { | ||
| 18 | [ 'filename=s' => 'a file from which to take the bundling information', { default => 'components.json' } ], | ||
| 19 | [ 'lib_dir=s' => 'a name for lib dir', { default => 'lib' } ], | ||
| 20 | } | ||
| 21 | |||
| 22 | |||
| 23 | sub execute { | ||
| 24 | my ($self, $opt, $args) = @_; | ||
| 25 | |||
| 26 | my $zilla = $self->zilla; | ||
| 27 | my $filename = $opt->filename; | ||
| 28 | |||
| 29 | die "File with bundles information not found: $filename" unless -e $filename; | ||
| 30 | |||
| 31 | my $plugin = Dist::Zilla::Plugin::Web::Bundle->new({ | ||
| 32 | plugin_name => ':Bundler', | ||
| 33 | zilla => $zilla, | ||
| 34 | filename => $filename, | ||
| 35 | lib_dir => $opt->lib_dir | ||
| 36 | }); | ||
| 37 | |||
| 38 | |||
| 39 | $plugin->process_components(); | ||
| 40 | |||
| 41 | for my $file (@{$zilla->files}) { | ||
| 42 | |||
| 43 | my $file_path = file($file->name); | ||
| 44 | my $to_dir = file($file->name)->dir; | ||
| 45 | |||
| 46 | $to_dir->mkpath unless -e $to_dir; | ||
| 47 | |||
| 48 | open my $out_fh, '>', "$file_path" or die "couldn't open $file_path to write: $!"; | ||
| 49 | |||
| 50 | # This is needed, or \n is translated to \r\n on win32. | ||
| 51 | # Maybe :raw:utf8 is needed, but not sure. | ||
| 52 | # -- Kentnl - 2010-06-10 | ||
| 53 | binmode( $out_fh , ":raw" ); | ||
| 54 | |||
| 55 | print { $out_fh } $file->content; | ||
| 56 | close $out_fh or die "error closing $file_path: $!"; | ||
| 57 | |||
| 58 | chmod $file->mode, "$file_path" or die "couldn't chmod $file_path: $!"; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | 1; | ||
| 63 | |||
| 64 | __END__ |