| 1 |
package App::Yath::Script::V1; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
my ($RUN_SUB, @DEVLIBS, $NO_PLUGINS); |
| 8 |
|
| 9 |
sub do_begin { |
| 10 |
my $class = shift; |
| 11 |
my %params = @_; |
| 12 |
|
| 13 |
my $script = $params{script}; |
| 14 |
my $argv = $params{argv}; |
| 15 |
my $config_file = $params{config}; |
| 16 |
my $user_config_file = $params{user_config}; |
| 17 |
|
| 18 |
local $.; |
| 19 |
|
| 20 |
my $ORIG_TMP; |
| 21 |
my $ORIG_TMP_PERMS; |
| 22 |
my %ORIG_SIG = map { defined($SIG{$_}) ? ($_ => "$SIG{$_}") : () } keys %SIG; |
| 23 |
my @ORIG_ARGV = @$argv; |
| 24 |
# Skip @INC hook refs (coderef / arrayref / blessed); they can't cross exec via -I nor round-trip JSON. |
| 25 |
my @ORIG_INC = grep { !ref($_) } @INC; |
| 26 |
my %CONFIG; |
| 27 |
|
| 28 |
@ARGV = @$argv; |
| 29 |
|
| 30 |
# ==START TESTABLE CODE PARSE_CONFIG_FILES== |
| 31 |
|
| 32 |
my (@CONFIG_ARGS, @TO_CLEAN); |
| 33 |
for my $file ($config_file, $user_config_file) { |
| 34 |
next unless $file && -f $file; |
| 35 |
|
| 36 |
my $cmd; |
| 37 |
open(my $fh, '<', $file) or die "Could not open config file '$file' for reading: $!"; |
| 38 |
while (my $line = <$fh>) { |
| 39 |
chomp($line); |
| 40 |
$cmd = $1 and next if $line =~ m/^\[(.*)\]$/; |
| 41 |
$line =~ s/;.*$//g; |
| 42 |
$line =~ s/^\s*//g; |
| 43 |
$line =~ s/\s*$//g; |
| 44 |
next unless length($line); |
| 45 |
|
| 46 |
my ($key, $eq, $val); |
| 47 |
if ($line =~ m/^(-\S)((?:rel|glob|relglob)\(.*\))$/) { # Handle things like -Irel(...) |
| 48 |
$key = $1; |
| 49 |
$eq = ''; |
| 50 |
$val = $2; |
| 51 |
} |
| 52 |
else { |
| 53 |
($key, $eq, $val) = split /(=|\s+)/, $line, 2; # Covers most cases |
| 54 |
} |
| 55 |
|
| 56 |
my $is_pre; |
| 57 |
if ($key =~ m/^-D/ || $key eq '--dev-lib') { |
| 58 |
$eq = '=' if $val; |
| 59 |
$is_pre = 1; |
| 60 |
} |
| 61 |
|
| 62 |
if ($key eq '--no-scan-plugins') { |
| 63 |
$is_pre = 1; |
| 64 |
} |
| 65 |
|
| 66 |
my $need_to_clean; |
| 67 |
if ($val && $val =~ s/(^|=)\s*rel\(\s*//) { |
| 68 |
die "Syntax error in $file line $.: Expected ')'\n" unless $val =~ s/\s*\)$//; |
| 69 |
my $path = $file; |
| 70 |
$path =~ s{[^/]*$}{}g; |
| 71 |
$val = "${path}${val}"; |
| 72 |
$need_to_clean = 1; |
| 73 |
} |
| 74 |
|
| 75 |
my @all; |
| 76 |
|
| 77 |
if ($val && $val =~ s/(^|=)\s*(rel)?glob\(\s*//) { |
| 78 |
my $rel = $2; |
| 79 |
|
| 80 |
die "Syntax error in $file line $.: Expected ')'\n" unless $val =~ s/\s*\)$//; |
| 81 |
|
| 82 |
my $path = ''; |
| 83 |
if ($rel) { |
| 84 |
$path = $file; |
| 85 |
$path =~ s{[^/]*$}{}g; |
| 86 |
} |
| 87 |
|
| 88 |
# Avoid loading File::Glob in this process... |
| 89 |
my $out = `$^X -e 'print join "\\n" => glob("${path}${val}")'`; |
| 90 |
my @vals = split /\n/, $out; |
| 91 |
@all = map {[$key, $eq, $_, 1]} @vals; |
| 92 |
} |
| 93 |
else { |
| 94 |
@all = ([$key, $eq, $val, $need_to_clean]); |
| 95 |
} |
| 96 |
|
| 97 |
for my $set (@all) { |
| 98 |
my ($key, $eq, $val, $need_to_clean) = @$set; |
| 99 |
$eq //= ''; |
| 100 |
|
| 101 |
my @parts = $eq eq '=' ? ("${key}${eq}${val}") : (grep { defined $_ } $key, $val); |
| 102 |
|
| 103 |
if ($is_pre) { |
| 104 |
push @CONFIG_ARGS => @parts; |
| 105 |
} |
| 106 |
else { |
| 107 |
$cmd //= '~'; |
| 108 |
push @{$CONFIG{$cmd}} => @parts; |
| 109 |
push @TO_CLEAN => [$cmd, $#{$CONFIG{$cmd}}, $key, $eq, $val] if $need_to_clean; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
close($fh); |
| 114 |
} |
| 115 |
|
| 116 |
unshift @ARGV => @CONFIG_ARGS; |
| 117 |
|
| 118 |
# ==END TESTABLE CODE PARSE_CONFIG_FILES== |
| 119 |
# ==START TESTABLE CODE PRE_PARSE_D_ARGS== |
| 120 |
|
| 121 |
my (@libs, %done, @args, $maybe_exec); |
| 122 |
while (@ARGV) { |
| 123 |
my $arg = shift @ARGV; |
| 124 |
|
| 125 |
if ($arg eq '--' || $arg eq '::') { |
| 126 |
push @args => $arg; |
| 127 |
last; |
| 128 |
} |
| 129 |
|
| 130 |
if ($arg eq '--no-dev-lib') { |
| 131 |
@libs = (); |
| 132 |
%done = (); |
| 133 |
next; |
| 134 |
} |
| 135 |
|
| 136 |
if ($arg =~ m{^(?:(?:-D=?|--dev-lib=)(.*)|--dev-lib)$}) { |
| 137 |
my @add = $1 ? ($1) : (); |
| 138 |
unless (@add) { |
| 139 |
@add = ('lib', 'blib/lib', 'blib/arch'); |
| 140 |
$maybe_exec = $arg; |
| 141 |
} |
| 142 |
|
| 143 |
push @libs => grep { !$done{$_}++ } @add; |
| 144 |
next; |
| 145 |
} |
| 146 |
|
| 147 |
if ($arg eq '--no-scan-plugins') { |
| 148 |
$NO_PLUGINS = 1; |
| 149 |
next; |
| 150 |
} |
| 151 |
|
| 152 |
push @args => $arg; |
| 153 |
} |
| 154 |
@ARGV = (@args, @ARGV); |
| 155 |
|
| 156 |
unshift @INC => @libs; |
| 157 |
@DEVLIBS = @libs; |
| 158 |
|
| 159 |
# ==END TESTABLE CODE PRE_PARSE_D_ARGS== |
| 160 |
|
| 161 |
# Now it is safe/ok to load things. |
| 162 |
require Cwd; |
| 163 |
require File::Spec; |
| 164 |
|
| 165 |
$ORIG_TMP = File::Spec->tmpdir(); |
| 166 |
$ORIG_TMP_PERMS = ((stat($ORIG_TMP))[2] & 07777); |
| 167 |
|
| 168 |
# ==START TESTABLE CODE CLEANUP_PATHS== |
| 169 |
|
| 170 |
if (@libs || @TO_CLEAN) { |
| 171 |
for (my $i = 0; $i < @libs; $i++) { |
| 172 |
$DEVLIBS[$i] = $INC[$i] = Cwd::realpath($INC[$i]) // File::Spec->rel2abs($INC[$i]); |
| 173 |
} |
| 174 |
|
| 175 |
for my $clean (@TO_CLEAN) { |
| 176 |
my ($cmd, $idx, $key, $eq, $val) = @$clean; |
| 177 |
$val = Cwd::realpath($val) // File::Spec->rel2abs($val); |
| 178 |
|
| 179 |
if ($eq eq '=') { |
| 180 |
$CONFIG{$cmd}->[$idx] = "${key}${eq}${val}"; |
| 181 |
} |
| 182 |
else { |
| 183 |
$CONFIG{$cmd}->[$idx] = $val; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
# ==END TESTABLE CODE CLEANUP_PATHS== |
| 189 |
# ==START TESTABLE CODE CREATE_APP== |
| 190 |
|
| 191 |
require App::Yath; |
| 192 |
require Time::HiRes; |
| 193 |
require Test2::Harness::Settings; |
| 194 |
|
| 195 |
my %mixin = (config_file => '', user_config_file => ''); |
| 196 |
$mixin{config_file} = Cwd::realpath($config_file) // File::Spec->rel2abs($config_file) if $config_file; |
| 197 |
$mixin{user_config_file} = Cwd::realpath($user_config_file) // File::Spec->rel2abs($user_config_file) if $user_config_file; |
| 198 |
|
| 199 |
my $settings = Test2::Harness::Settings->new( |
| 200 |
harness => { |
| 201 |
orig_tmp => $ORIG_TMP, |
| 202 |
orig_tmp_perms => $ORIG_TMP_PERMS, |
| 203 |
orig_sig => \%ORIG_SIG, |
| 204 |
orig_argv => \@ORIG_ARGV, |
| 205 |
orig_inc => \@ORIG_INC, |
| 206 |
script => $script, |
| 207 |
no_scan_plugins => $NO_PLUGINS, |
| 208 |
dev_libs => \@DEVLIBS, |
| 209 |
start => Time::HiRes::time(), |
| 210 |
version => $App::Yath::VERSION, |
| 211 |
cwd => Cwd::getcwd(), |
| 212 |
%mixin, |
| 213 |
}, |
| 214 |
); |
| 215 |
|
| 216 |
my $app = App::Yath->new( |
| 217 |
argv => \@ARGV, |
| 218 |
config => \%CONFIG, |
| 219 |
settings => $settings, |
| 220 |
); |
| 221 |
|
| 222 |
$app->generate_run_sub('App::Yath::Script::V1::_run'); |
| 223 |
|
| 224 |
# ==END TESTABLE CODE CREATE_APP== |
| 225 |
|
| 226 |
# Reset these if we got this far. |
| 227 |
$? = 0; |
| 228 |
$@ = ''; |
| 229 |
} |
| 230 |
|
| 231 |
sub do_runtime { |
| 232 |
my $class = shift; |
| 233 |
return _run(); |
| 234 |
} |
| 235 |
|
| 236 |
1; |
| 237 |
|
| 238 |
__END__ |
| 239 |
|
| 240 |
=pod |
| 241 |
|
| 242 |
=encoding UTF-8 |
| 243 |
|
| 244 |
=head1 NAME |
| 245 |
|
| 246 |
App::Yath::Script::V1 - V1 (Legacy) yath script handler for Test2::Harness |
| 247 |
|
| 248 |
=head1 DESCRIPTION |
| 249 |
|
| 250 |
This is the V1 script handler for L<Test2::Harness>. It is used by |
| 251 |
L<App::Yath::Script> when a C<.yath.rc> file with no version marker (or an |
| 252 |
explicit C<# V1> marker) is found, or when V1 is the highest installed version. |
| 253 |
|
| 254 |
This module contains the logic that was previously embedded directly in the |
| 255 |
C<scripts/yath> script of L<Test2::Harness>. |
| 256 |
|
| 257 |
=head1 METHODS |
| 258 |
|
| 259 |
=over 4 |
| 260 |
|
| 261 |
=item $class->do_begin(%params) |
| 262 |
|
| 263 |
Called during the C<BEGIN> phase. Parses configuration files, processes C<-D> |
| 264 |
and C<--no-scan-plugins> arguments, sets up C<@INC>, and initializes |
| 265 |
L<App::Yath>. |
| 266 |
|
| 267 |
Parameters: |
| 268 |
|
| 269 |
=over 4 |
| 270 |
|
| 271 |
=item script => $path |
| 272 |
|
| 273 |
Path to the executing script. |
| 274 |
|
| 275 |
=item argv => \@argv |
| 276 |
|
| 277 |
Original command line arguments. |
| 278 |
|
| 279 |
=item config => $path |
| 280 |
|
| 281 |
Path to the C<.yath.rc> file, or C<undef>. |
| 282 |
|
| 283 |
=item user_config => $path |
| 284 |
|
| 285 |
Path to the C<.yath.user.rc> file, or C<undef>. |
| 286 |
|
| 287 |
=back |
| 288 |
|
| 289 |
=item $exit = $class->do_runtime() |
| 290 |
|
| 291 |
Called after C<BEGIN>. Executes the yath command and returns the exit code. |
| 292 |
|
| 293 |
=back |
| 294 |
|
| 295 |
=head1 SOURCE |
| 296 |
|
| 297 |
The source code repository for Test2-Harness can be found at |
| 298 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 299 |
|
| 300 |
=head1 MAINTAINERS |
| 301 |
|
| 302 |
=over 4 |
| 303 |
|
| 304 |
=item Chad Granum E<lt>[email protected]<gt> |
| 305 |
|
| 306 |
=back |
| 307 |
|
| 308 |
=head1 AUTHORS |
| 309 |
|
| 310 |
=over 4 |
| 311 |
|
| 312 |
=item Chad Granum E<lt>[email protected]<gt> |
| 313 |
|
| 314 |
=back |
| 315 |
|
| 316 |
=head1 COPYRIGHT |
| 317 |
|
| 318 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 319 |
|
| 320 |
This program is free software; you can redistribute it and/or |
| 321 |
modify it under the same terms as Perl itself. |
| 322 |
|
| 323 |
See F<http://dev.perl.org/licenses/> |
| 324 |
|
| 325 |
=cut |