App-depak
view release on metacpan or search on metacpan
lib/App/depak.pm view on Meta::CPAN
}
}
if ($self->{exclude_dist}) {
if (!$excluded_distmods) {
$excluded_distmods = [];
for (@{ $self->{exclude_dist} }) {
push @$excluded_distmods, Dist::Util::list_dist_modules($_);
}
}
if (grep { $_ eq $mod } @$excluded_distmods) {
log_info("Excluding %s (by dist): skipped", $mod);
next MOD_TO_FILTER;
}
}
if (defined(my $file = $self->{exclude_list})) {
if (!$excluded_list) {
$excluded_list = [];
log_debug(" Reading excludes listed in: %s", $file);
open my($fh), "<", $file
or die "Can't open modules list file '$file': $!\n";
my $linenum = 0;
while (my $line = <$fh>) {
$linenum++;
next unless $line =~ /\S/;
$line =~ s/^\s+//;
$line =~ s/(\w+(?:::\w+)*)\s*// or do {
warn "Invalid syntax in $file:$linenum: can't find valid module name, skipped\n";
next;
};
my $emod = $1;
log_debug(" Adding excluded module: %s", $emod);
push @$excluded_list, $emod;
}
}
if (grep { $_ eq $mod } @$excluded_list) {
log_info("Excluding %s (by list): skipped", $mod);
}
}
$fmod_paths{$mod} = $mod_paths{$mod};
}
%mod_paths = %fmod_paths;
require Module::XSOrPP;
MOD_TO_ADD:
for my $mod (sort keys %mod_paths) {
my $mpath = $mod_paths{$mod};
unless ($mpath) {
if (Module::XSOrPP::is_xs($mod)) {
unless (!$self->{allow_xs} || grep { $_ eq $mod } @{ $self->{allow_xs} } ||
grep { $_ eq $mod } @ALLOW_XS) {
die "Can't add XS module: $mod\n";
}
}
}
$mpath //= Module::Path::More::module_path(module=>$mod);
unless (defined $mpath) {
if ($self->{skip_not_found}) {
log_info("Path for module '%s' not found, skipped", $mod);
next MOD_TO_ADD;
} else {
die "Can't find path for $mod\n";
}
}
my $modp = $mod; $modp =~ s!::!/!g; $modp .= ".pm";
my ($dir) = $modp =~ m!(.+)/(.+)!;
if ($dir) {
my $dir_to_make = "$tempdir/lib/$dir";
unless (-d $dir_to_make) {
File::Path::make_path($dir_to_make) or die "Can't make_path: $dir_to_make\n";
}
}
if ($self->{stripper}) {
my $stripper = do {
require Perl::Stripper;
Perl::Stripper->new(
maintain_linum => $self->{stripper_maintain_linum},
strip_ws => $self->{stripper_ws},
strip_comment => $self->{stripper_comment},
strip_pod => $self->{stripper_pod},
strip_log => $self->{stripper_log},
);
};
log_debug(" Stripping $mpath --> $modp ...");
my $src = read_binary($mpath);
my $stripped = $stripper->strip($src);
write_binary("$tempdir/lib/$modp", $stripped);
} elsif ($self->{strip}) {
require Perl::Strip;
my $strip = Perl::Strip->new;
log_debug(" Stripping $mpath --> $modp ...");
my $src = read_binary($mpath);
my $stripped = $strip->strip($src);
write_binary("$tempdir/lib/$modp", $stripped);
} elsif ($self->{squish}) {
log_debug(" Squishing $mpath --> $modp ...");
require Perl::Squish;
my $squish = Perl::Squish->new;
$squish->file($mpath, "$tempdir/lib/$modp");
} else {
log_debug(" Copying $mpath --> $tempdir/lib/$modp ...");
File::Copy::copy($mpath, "$tempdir/lib/$modp");
}
$totfiles++;
$totsize += (-s $mpath);
}
log_info(" Added %d files (%.1f KB)", $totfiles, $totsize/1024);
}
sub _pack {
require ExtUtils::MakeMaker;
require File::Find;
my $self = shift;
lib/App/depak.pm view on Meta::CPAN
schema => ['str*', {
default => 'fatpacker',
in=>[@$trace_methods, 'none'],
}],
description => <<'_',
The default is `fatpacker`, which is the same as what `fatpack trace` does.
Different tracing methods have different pro's and con's, one method might
detect required modules that another method does not, and vice versa. There are
several methods available, please see `App::tracepm` for more details.
A special value of `none` is also provided. If this is selected, then depak will
not perform any tracing. Usually used in conjunction with `--include-from`.
_
cmdline_aliases => { t=>{} },
tags => ['category:module-selection', 'category:tracing'],
},
trace_extra_opts => {
schema => ['hash*'],
summary => 'Pass more options to `App::tracepm`',
tags => ['category:module-selection'],
},
include_prereq => {
'summary.alt.plurality.singular' => 'Include module and its recursive dependencies for packing',
schema => ['array*', of=>'str*'],
description => <<'_',
This option can be used to include a module, as well as other modules in the
same distribution as that module, as well as the distribution's recursive
dependencies, for packing. Dependencies will be searched using a local CPAN
index. This is a convenient alternative to tracing a module. So you might want
to use this option together with setting `trace_method` to `none`.
This option requires that `lcpan` is installed and a fairly recent lcpan index
is available.
_
tags => ['category:module-selection'],
},
exclude_prereq => {
'summary.alt.plurality.singular' => 'Allow script to depend on a module instead of packing it',
schema => ['array*', of=>'str*'],
description => <<'_',
This option can be used to express that script will depend on a specified
module, instead of including it packed. The prereq-ed module, as well as other
modules in the same distribution, as well as its prereqs and so on recursively,
will be excluded from packing as well.
This option can be used to express dependency to an XS module, since XS modules
cannot be packed.
To query dependencies, a local CPAN index is used for querying speed. Thus, this
option requires that `lcpan` is installed and a fairly recent lcpan index is
available.
_
tags => ['category:module-selection'],
},
skip_not_found => {
summary => 'Instead of dying, skip when module to add is not found',
'summary.alt.bool.not' => 'Instead of skipping, die when module to add is not found',
schema => ['bool'],
description => <<'_',
This option is useful when you use `include_prereq`, because modules without its
own .pm files will also be included (CPAN indexes packages, including those that
do not have their own .pm files).
By default, this option is turned off unless when you use `include_prereq` where
this option is by default turned on. You can of course override the default by
explicitly specify this option.
_
tags => ['category:module-selection'],
},
allow_xs => {
'summary.alt.plurality.singular' => 'Allow adding a specified XS module',
schema => ['array*', of=>'str*'],
tags => ['category:module-selection'],
},
use => {
summary => 'Additional modules to "use"',
'summary.alt.plurality.singular' => 'Additional module to "use"',
schema => ['array*' => of => 'perl::modname*'],
description => <<'_',
Will be passed to the tracer. Will currently only affect the `fatpacker` and
`require` methods (because those methods actually run your script).
_
tags => ['category:module-selection'],
},
args => {
summary => 'Script arguments',
'x.name.is_plural' => 1,
'summary.alt.plurality.singular' => 'Script argument',
description => <<'_',
Will be used when running your script, e.g. when `trace_method` is `fatpacker`
or `require`. For example, if your script requires three arguments: `--foo`,
`2`, `"bar baz"` then you can either use:
% depak script output --args --foo --args 2 --args "bar baz"
or:
% depak script output --args-json '["--foo",2,"bar baz"]'
_
schema => ['array*' => of => 'str*'],
tags => ['category:tracing'],
},
multiple_runs => {
summary => 'Pass to tracepm',
schema => ['array*' => of => ['hash*']],
tags => ['category:tracing'],
},
shebang => {
lib/App/depak.pm view on Meta::CPAN
squish => {
summary => 'Whether to squish included modules using Perl::Squish',
'summary.alt.bool.yes' => 'Squish included modules using Perl::Squish',
schema => ['bool' => default=>0],
tags => ['category:stripping'],
},
strip => {
summary => 'Whether to strip included modules using Perl::Strip',
'summary.alt.bool.yes' => 'Strip included modules using Perl::Strip',
schema => ['bool' => default=>0],
tags => ['category:stripping'],
},
debug_keep_tempdir => {
summary => 'Keep temporary directory for debugging',
schema => ['bool'],
tags => ['category:debugging'],
},
test => {
schema => ['bool', is=>1],
summary => 'Test the resulting output',
cmdline_aliases => {T=>{}},
description => <<'_',
Testing is done by running the resulting packed script with perl. To test, at
least one test case is required (see `--test-case-json`). Test cases specify
what arguments to give to program, what exit code we expect, and what the output
should contain.
_
tags => ['category:testing'],
},
test_cases => {
schema => ['array*', of=>'hash*'],
'x.name.is_plural' => 1,
description => <<'_',
Example case:
{"args":["--help"], "exit_code":0, "perl_args":["-Mlib::core::only"], "output_like":"Usage:"}
_
tags => ['category:testing'],
},
},
};
sub depak {
require Cwd;
require File::Util::Test;
require File::Spec;
require File::Temp;
my %args = @_;
my $self = __PACKAGE__->new(%args);
$self->{debug_keep_tempdir} //= $ENV{DEBUG_KEEP_TEMPDIR} // 0;
$self->{skip_not_found} //= $self->{include_prereq} ? 1:0;
my $tempdir = File::Temp::tempdir(CLEANUP => 0);
log_debug("Created tempdir %s", $tempdir);
$self->{tempdir} = $tempdir;
# for convenience of completion in bash, we allow / to separate namespace.
# we convert it back to :: here.
for (@{ $self->{exclude_module} // [] },
@{ $self->{exclude_dist} // [] },
@{ $self->{include_module} // [] },
@{ $self->{include_dist} // [] },
@{ $self->{use} // [] },
) {
s!/!::!g;
s/\.pm\z//;
}
mkdir "$tempdir/lib";
$self->{perl_version} //= $^V;
$self->{perl_version} = version->parse($self->{perl_version});
log_debug("Will be targetting perl %s", $self->{perl_version});
if ($self->{input_file} eq '-') {
$self->{input_file_is_stdin} = 1;
$self->{input_file} = $self->{abs_input_file} = (File::Temp::tempfile())[1];
open my($fh), ">", $self->{abs_input_file}
or return [500, "Can't write temporary input file '$self->{abs_input_file}': $!"];
local $_; while (<STDIN>) { print $fh $_ }
$self->{output_file} //= '-';
} else {
(-f $self->{input_file})
or return [500, "No such input file: $self->{input_file}"];
$self->{abs_input_file} = Cwd::abs_path($self->{input_file}) or return
[500, "Can't find absolute path of input file $self->{input_file}"];
}
if ($self->{output_file} eq '-') {
$self->{output_file_is_stdout} = 1;
$self->{output_file} = $self->{abs_output_file} = (File::Temp::tempfile())[1];
} else {
return [412, "Output file '$self->{output_file}' exists, won't overwrite (see --overwrite)"]
if File::Util::Test::file_exists($self->{output_file}) && !$self->{overwrite};
return [500, "Can't write to output file '$self->{output_file}': $!"]
unless open my($fh), ">", $self->{output_file};
}
$self->{abs_output_file} //= Cwd::abs_path($self->{output_file}) or return
[500, "Can't find absolute path of output file '$self->{output_file}'"];
unless ($self->{trace_method} eq 'none') {
log_info("Tracing dependencies ...");
$self->_trace;
}
log_info("Building lib/ ...");
$self->_build_lib;
log_info("Packing ...");
$self->_pack;
lib/App/depak.pm view on Meta::CPAN
is available.
=item * B<input_file> => I<filename> (default: "-")
Path to input file (script to be packed).
C<-> (or if unspecified) means to take from standard input (internally, a
temporary file will be created to handle this).
=item * B<multiple_runs> => I<array[hash]>
Pass to tracepm.
=item * B<output_file> => I<filename> (default: "-")
Path to output file.
C<-> (or if unspecified) means to output to stdout.
=item * B<overwrite> => I<bool> (default: 0)
Whether to overwrite output if previously exists.
=item * B<pack_method> => I<str> (default: "fatpack")
Packing method to use.
Either C<fatpack> (the default) or C<datapack>. Fatpack puts packed modules inside
Perl variables and load them via require hook. Datapack puts packed modules in
B<DATA> section. For more details about each method, please consult
C<Module::FatPack> and C<Module::DataPack>.
One thing to remember is, with datapack, your script cannot load modules during
compile-time (C<use>): all modules must be loaded during run-time (C<require>)
when data section is already available. Also, your script currently cannot
contain data section of its own.
=item * B<perl_version> => I<str>
Perl version to target, defaults to current running version.
This is for determining which modules are considered core and should be skipped
by default (when C<exclude_core> option is enabled). Different perl versions have
different sets of core modules as well as different versions of the modules.
=item * B<put_hook_at_the_end> => I<bool>
Prefer modules from other sources (filesystem) first.
Normally, the C<fatpack> or C<datapack> require hook will be put at the front of
C<@INC>. If this option is set to true, the require hook will be put at the end.
This means Perl will search modules from the regular sources (filesystem) first.
This is useful for example if you want your application to use the possibly
newer version of modules on the filesystem before using the packed version as
the fallback when some modules are not installed.
=item * B<shebang> => I<str> (default: "/usr/bin/perl")
Set shebang lineE<sol>path.
=item * B<skip_not_found> => I<bool>
Instead of dying, skip when module to add is not found.
This option is useful when you use C<include_prereq>, because modules without its
own .pm files will also be included (CPAN indexes packages, including those that
do not have their own .pm files).
By default, this option is turned off unless when you use C<include_prereq> where
this option is by default turned on. You can of course override the default by
explicitly specify this option.
=item * B<squish> => I<bool> (default: 0)
Whether to squish included modules using Perl::Squish.
=item * B<strip> => I<bool> (default: 0)
Whether to strip included modules using Perl::Strip.
=item * B<stripper> => I<bool> (default: 0)
Whether to strip included modules using Perl::Stripper.
=item * B<stripper_comment> => I<bool> (default: 1)
Set strip_comment=1 (strip comments) in Perl::Stripper.
=item * B<stripper_log> => I<bool> (default: 0)
Set strip_log=1 (strip log statements) in Perl::Stripper.
=item * B<stripper_maintain_linum> => I<bool> (default: 0)
Set maintain_linum=1 in Perl::Stripper.
=item * B<stripper_pod> => I<bool> (default: 1)
Set strip_pod=1 (strip POD) in Perl::Stripper.
=item * B<stripper_ws> => I<bool> (default: 1)
Set strip_ws=1 (strip whitespace) in Perl::Stripper.
=item * B<test> => I<bool>
Test the resulting output.
Testing is done by running the resulting packed script with perl. To test, at
least one test case is required (see C<--test-case-json>). Test cases specify
what arguments to give to program, what exit code we expect, and what the output
should contain.
=item * B<test_cases> => I<array[hash]>
Example case:
{"args":["--help"], "exit_code":0, "perl_args":["-Mlib::core::only"], "output_like":"Usage:"}
=item * B<trace_extra_opts> => I<hash>
( run in 1.976 second using v1.01-cache-2.11-cpan-99c4e6809bf )