| 1 | package Dist::Zilla::Plugin::Depak; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | our $DATE = '2017-07-14'; # DATE | |||||||||||
| 4 | 1 | our $VERSION = '0.21'; # VERSION | |||||||||||
| 5 | |||||||||||||
| 6 | 1 | ▶ | use 5.010001; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 1 | ▶ | use App::lcpan::Call qw(call_lcpan_script); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | ▶ | use Data::Dmp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use Dist::Zilla::File::InMemory; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use ExtUtils::MakeMaker; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 1 | ▶ | use File::Path qw(make_path); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 1 | ▶ | use File::Slurper qw(read_binary write_binary); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 1 | ▶ | use File::Temp qw(tempfile tempdir); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 1 | ▶ | use File::Which; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 1 | ▶ | use IPC::System::Options qw(system); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 1 | ▶ | use JSON::MaybeXS; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 1 | ▶ | use List::Util qw(first); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | |||||||||||||
| 22 | 1 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 1 | with ( | |||||||||||
| 24 | 'Dist::Zilla::Role::FileFinderUser' => { | ||||||||||||
| 25 | default_finders => [':ExecFiles', ':InstallModules'], | ||||||||||||
| 26 | }, | ||||||||||||
| 27 | 'Dist::Zilla::Role::FileGatherer', | ||||||||||||
| 28 | 'Dist::Zilla::Role::FileMunger', | ||||||||||||
| 29 | #'Dist::Zilla::Role::MetaProvider', | ||||||||||||
| 30 | 'Dist::Zilla::Role::PERLANCAR::WriteModules', | ||||||||||||
| 31 | ); | ||||||||||||
| 32 | |||||||||||||
| 33 | 1 | has include_script => (is => 'rw'); | |||||||||||
| 34 | 1 | has exclude_script => (is => 'rw'); | |||||||||||
| 35 | 1 | has put_hook_at_the_end => (is => 'rw'); | |||||||||||
| 36 | |||||||||||||
| 37 | 1 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 38 | |||||||||||||
| 39 | 0 | ▶ | sub mvp_multivalue_args { qw(include_script exclude_script) } | ||||||||||
|
Subroutine
mvp_multivalue_args: not called CC=1 SCAR=6.9
| |||||||||||||
| 40 | |||||||||||||
| 41 | 0 | ▶ | sub munge_files { | ||||||||||
|
Subroutine
munge_files: not called CC=14 SCAR=53.5
Pod
undocumented
| |||||||||||||
| 42 | #use experimental 'smartmatch'; | ||||||||||||
| 43 | |||||||||||||
| 44 | 0 | my $self = shift; | |||||||||||
| 45 | |||||||||||||
| 46 | |||||||||||||
| 47 | 0 | $self->log_debug(["Initializing _mods and _dists"]); | |||||||||||
| 48 | 0 | ▶ | $self->{_mods} //= {}; | ||||||||||
|
Condition0%0/2
$self->{"_mods"} //= {}
MC/DC0%0/1
$self->{"_mods"} //= {}
$self->{"_mods"}
Truth table0%0/2
A
A= $self->{"_mods"}
| |||||||||||||
| 49 | |||||||||||||
| 50 | # let's check whether there are other FileMunger plugins and warn it. due to | ||||||||||||
| 51 | # the nature of this plugin (combines several module files of potentially | ||||||||||||
| 52 | # different encodings into a single perl file, and thus using the 'bytes' | ||||||||||||
| 53 | # encoding to combine them all) it might conflict with the other file | ||||||||||||
| 54 | # mungers, for example PodWeaver which does text munging and uses text | ||||||||||||
| 55 | # encoding. so, it's best to put this plugin as the last file munger. | ||||||||||||
| 56 | 0 | my $found_me; | |||||||||||
| 57 | my @other_file_mungers; | ||||||||||||
| 58 | 0 | for my $p (@{ $self->zilla->plugins }) { | |||||||||||
| 59 | 0 | ▶ | if (!$found_me) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | 0 | ▶ | if ($p eq $self) { $found_me++ } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | 0 | next; | |||||||||||
| 62 | } | ||||||||||||
| 63 | 0 | ▶ | if ($p->does("Dist::Zilla::Role::FileMunger")) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 0 | push @other_file_mungers, $p->plugin_name; | |||||||||||
| 65 | } | ||||||||||||
| 66 | } | ||||||||||||
| 67 | 0 | ▶ | $self->log(["There are other FileMunger plugins (%s) after this plugin, ". | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | "this is not recommended because it might cause encoding problems", | ||||||||||||
| 69 | \@other_file_mungers]) if @other_file_mungers; | ||||||||||||
| 70 | |||||||||||||
| 71 | 0 | my @scripts0 = grep { $_->name =~ m!^(bin|scripts?)! } @{ $self->found_files }; | |||||||||||
| 72 | 0 | my @scripts; | |||||||||||
| 73 | 0 | ▶ | if ($self->include_script) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 0 | for my $item (@{ $self->include_script }) { | |||||||||||
| 75 | 0 | ▶ | my $file = first { $item eq $_->name } @scripts0; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 76 | 0 | ▶ | $self->log_fatal(["included '%s' not in list of available scripts", $item]) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | unless $file; | ||||||||||||
| 78 | 0 | push @scripts, $file; | |||||||||||
| 79 | } | ||||||||||||
| 80 | } else { | ||||||||||||
| 81 | 0 | @scripts = @scripts0; | |||||||||||
| 82 | } | ||||||||||||
| 83 | 0 | ▶ | if ($self->exclude_script) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | 0 | for my $item (@{ $self->exclude_script }) { | |||||||||||
| 85 | 0 | @scripts = grep { $_->name ne $item } @scripts; | |||||||||||
| 86 | } | ||||||||||||
| 87 | } | ||||||||||||
| 88 | 0 | $self->munge_script($_) for @scripts; | |||||||||||
| 89 | |||||||||||||
| 90 | 0 | my @modules = grep { $_->name =~ m!^(lib)! } @{ $self->found_files }; | |||||||||||
| 91 | 0 | $self->munge_module($_) for @modules; | |||||||||||
| 92 | |||||||||||||
| 93 | # we usually do this in metadata(), but for some reason it is now executed | ||||||||||||
| 94 | # before munge_files() so _mods is still empty | ||||||||||||
| 95 | 0 | $self->log_debug(["Setting dist metadata x_Dist_Zilla_Plugin_Depak"]); | |||||||||||
| 96 | $self->zilla->distmeta->{x_Dist_Zilla_Plugin_Depak} = { | ||||||||||||
| 97 | packed_modules => $self->{_mods}, | ||||||||||||
| 98 | packed_dists => $self->{_dists}, | ||||||||||||
| 99 | 0 | }; | |||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | 0 | ▶ | sub munge_script { | ||||||||||
|
Subroutine
munge_script: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||
| 103 | 0 | my ($self, $file) = @_; | |||||||||||
| 104 | |||||||||||||
| 105 | 0 | ▶ | $self->log_fatal(["Can't find depak in PATH"]) unless which("depak"); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 106 | |||||||||||||
| 107 | # we use the depak CLI instead of App::depak because we want to use | ||||||||||||
| 108 | # --config-profile. | ||||||||||||
| 109 | 0 | my $profile = $file->name; | |||||||||||
| 110 | 0 | $profile =~ s!.+[\\/]!!; | |||||||||||
| 111 | |||||||||||||
| 112 | # since we're dealing with CLI, we need actual files. even a modified | ||||||||||||
| 113 | # DZF:OnDisk might not have the actual file located in $file->name, so we | ||||||||||||
| 114 | # write to tempfile first. | ||||||||||||
| 115 | |||||||||||||
| 116 | 0 | my $source; | |||||||||||
| 117 | { | ||||||||||||
| 118 | 0 | my ($fh, $filename) = tempfile(); | |||||||||||
| 119 | 0 | $source = $filename; | |||||||||||
| 120 | 0 | write_binary($filename, $file->content); | |||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | 0 | my $target; | |||||||||||
| 124 | { | ||||||||||||
| 125 | 0 | my ($fh, $filename) = tempfile(); | |||||||||||
| 126 | 0 | $target = $filename; | |||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | 0 | $self->write_modules_to_dir; | |||||||||||
| 130 | 0 | my $mods_tempdir = $self->written_modules_dir; | |||||||||||
| 131 | |||||||||||||
| 132 | # the --json output is so that we can read the list of included modules | ||||||||||||
| 133 | 0 | my @depak_cmd = ( | |||||||||||
| 134 | "depak", | ||||||||||||
| 135 | "--include-dir", $mods_tempdir, | ||||||||||||
| 136 | "-i", $source, "-o", $target, "--overwrite", | ||||||||||||
| 137 | "--json", | ||||||||||||
| 138 | ); | ||||||||||||
| 139 | |||||||||||||
| 140 | 0 | ▶ | if (-f "depak.conf") { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 141 | 0 | push @depak_cmd, ( | |||||||||||
| 142 | "--config-profile", $profile, | ||||||||||||
| 143 | "--config-path", "depak.conf", | ||||||||||||
| 144 | ); | ||||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | 0 | ▶ | if ($self->put_hook_at_the_end) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | 0 | push @depak_cmd, "--put-hook-at-the-end"; | |||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | 0 | $self->log_debug(["Depak-ing %s: %s", $file->{name}, \@depak_cmd]); | |||||||||||
| 152 | 0 | my $stdout; | |||||||||||
| 153 | 0 | system({die=>1, log=>1, shell=>0, capture_stdout=>\$stdout}, @depak_cmd); | |||||||||||
| 154 | |||||||||||||
| 155 | 0 | my $depak_res = JSON::MaybeXS::decode_json($stdout); | |||||||||||
| 156 | 0 | ▶ | $self->log_fatal(["depak failed: %s", $depak_res]) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 157 | unless $depak_res->[0] == 200; | ||||||||||||
| 158 | |||||||||||||
| 159 | 0 | my $content = read_binary($target); | |||||||||||
| 160 | |||||||||||||
| 161 | $self->log_debug(["depak output: %s (%s, %d bytes)", | ||||||||||||
| 162 | 0 | $file->{name}, $target, length($content)]); | |||||||||||
| 163 | |||||||||||||
| 164 | #$self->log_debug(["depak result: %s", $depak_res]); | ||||||||||||
| 165 | |||||||||||||
| 166 | 0 | my $im = $depak_res->[3]{'func.included_modules'}; | |||||||||||
| 167 | 0 | for (keys %$im) { $self->{_mods}{$_} = $im->{$_} } | |||||||||||
| 168 | |||||||||||||
| 169 | # re-add the file instead of changing the content, so we can re-set the | ||||||||||||
| 170 | # encoding to 'bytes' | ||||||||||||
| 171 | my $newfile = Dist::Zilla::File::InMemory->new( | ||||||||||||
| 172 | 0 | encoding=>'bytes', name=>$file->{name}, content => $content); | |||||||||||
| 173 | 0 | $self->zilla->prune_file($file); | |||||||||||
| 174 | 0 | $self->add_file($newfile); | |||||||||||
| 175 | } | ||||||||||||
| 176 | |||||||||||||
| 177 | 0 | ▶ | sub munge_module { | ||||||||||
|
Subroutine
munge_module: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||
| 178 | 0 | my ($self, $file) = @_; | |||||||||||
| 179 | |||||||||||||
| 180 | 0 | my $munged; | |||||||||||
| 181 | 0 | my $content = $file->content; | |||||||||||
| 182 | 0 | my @pod; | |||||||||||
| 183 | |||||||||||||
| 184 | 0 | ▶ | if ($content =~ /^#\s*(PACKED_MODULES|PACKED_CONTENTS_POD)\s*$/m) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 185 | 0 | $munged++; | |||||||||||
| 186 | 0 | ▶ | $self->{_mods} //= {}; | ||||||||||
|
Condition0%0/2
$self->{"_mods"} //= {}
MC/DC0%0/1
$self->{"_mods"} //= {}
$self->{"_mods"}
Truth table0%0/2
A
A= $self->{"_mods"}
| |||||||||||||
| 187 | 0 | $content =~ s/(^#\s*PACKED_MODULES\s*$)/ | |||||||||||
| 188 | 0 | "our \%PACKED_MODULES = %{ " . dmp($self->{_mods}) . " }; $1"/em; | |||||||||||
| 189 | push @pod, "Modules packed into this distribution:\n\n=over\n\n", | ||||||||||||
| 190 | 0 | (map {"=item * $_\n\n"} sort keys %{$self->{_mods}}), | |||||||||||
| 191 | "\n=back\n\n"; | ||||||||||||
| 192 | } | ||||||||||||
| 193 | |||||||||||||
| 194 | 0 | ▶ | if ($content =~ /^#\s*(PACKED_DISTS|PACKED_CONTENTS_POD)\s*$/m) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 195 | 0 | $munged++; | |||||||||||
| 196 | 0 | ▶ | unless ($self->{_dists}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 197 | 0 | ▶ | if (!keys %{ $self->{_mods} }) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 198 | 0 | $self->{_dists} = {}; | |||||||||||
| 199 | } else { | ||||||||||||
| 200 | my $res = call_lcpan_script( | ||||||||||||
| 201 | 0 | argv => ["mod2dist", keys %{$self->{_mods}}], | |||||||||||
| 202 | ); | ||||||||||||
| 203 | 0 | ▶ | $self->log_fatal(["Can't lcpan mod2dist: %s - %res", $res->[0], $res->[1]]) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 204 | unless $res->[0] == 200; | ||||||||||||
| 205 | 0 | ▶ | if (ref $res->[2]) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 206 | 0 | for (values %{$res->[2]}) { | |||||||||||
| 207 | 0 | $self->{_dists}{$_} = 0; | |||||||||||
| 208 | } | ||||||||||||
| 209 | } else { | ||||||||||||
| 210 | # single result | ||||||||||||
| 211 | 0 | $self->{_dists}{$res->[2]} = 0; | |||||||||||
| 212 | } | ||||||||||||
| 213 | } | ||||||||||||
| 214 | } | ||||||||||||
| 215 | 0 | $content =~ s/(^#\s*PACKED_DISTS\s*$)/ | |||||||||||
| 216 | 0 | "our \@PACKED_DISTS = \@{" . dmp([sort keys %{$self->{_dists}}]) . "}; $1"/em; | |||||||||||
| 217 | push @pod, "Distributions packed into this distribution:\n\n=over\n\n", | ||||||||||||
| 218 | 0 | (map {"=item * $_\n\n"} sort keys %{$self->{_dists}}), | |||||||||||
| 219 | "\n=back\n\n"; | ||||||||||||
| 220 | } | ||||||||||||
| 221 | |||||||||||||
| 222 | 0 | ▶ | if ($content =~ /^#\s*PACKED_CONTENTS_POD\s*$/m) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 223 | 0 | $munged++; | |||||||||||
| 224 | 0 | $content =~ s/(^#\s*PACKED_CONTENTS_POD\s*$)/ | |||||||||||
| 225 | 0 | join("", @pod)/em; | |||||||||||
| 226 | push @pod, "Distributions packed inside this script:\n\n=over\n\n", | ||||||||||||
| 227 | 0 | (map {"=item * $_\n\n"} sort keys %{$self->{_dists}}), | |||||||||||
| 228 | "\n=back\n\n"; | ||||||||||||
| 229 | } | ||||||||||||
| 230 | |||||||||||||
| 231 | 0 | ▶ | if ($munged) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 232 | 0 | $self->log_debug(["Setting %PACKED_MODULES / \@PACKED_DISTS / PACKED_CONTENTS_POD in %s", $file->{name}]); | |||||||||||
| 233 | 0 | $file->content($content); | |||||||||||
| 234 | } | ||||||||||||
| 235 | } | ||||||||||||
| 236 | |||||||||||||
| 237 | 0 | ▶ | sub gather_files { | ||||||||||
|
Subroutine
gather_files: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 238 | } | ||||||||||||
| 239 | |||||||||||||
| 240 | 0 | ▶ | sub metadata { | ||||||||||
|
Subroutine
metadata: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 241 | 0 | my $self = shift; | |||||||||||
| 242 | |||||||||||||
| 243 | # weird, why is metadata() now executed before munge_files()? so let's use | ||||||||||||
| 244 | # distmeta directly. | ||||||||||||
| 245 | } | ||||||||||||
| 246 | |||||||||||||
| 247 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 248 | 1 | 1; | |||||||||||
| 249 | # ABSTRACT: Pack dependencies onto scripts during build using 'depak' | ||||||||||||
| 250 | |||||||||||||
| 251 | __END__ | ||||||||||||