Skip to content

Commit 31e69fc

Browse files
authored
Merge branch 'shadowcat-mst:master' into plenv
2 parents c1bf8a4 + 2578b01 commit 31e69fc

File tree

5 files changed

+300
-99
lines changed

5 files changed

+300
-99
lines changed

Changes

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1+
0.902003 - 2021-11-25
2+
- Restore 'plx t/foo.t' feature
3+
4+
0.902002 - 2021-11-09
5+
- Fix pod typo
6+
7+
0.902001 - 2021-11-09
8+
- Add support for prepending environment variables to --cmd/--perl/--exec
19
- Make plx-packed handle being invoked via | perl -
10+
- Set PERL_PLX_BASE env var inside invocations
11+
- Handle falsish config values
212

313
0.901001 - 2020-05-30
414
- Add extra commands and user bootstrap code

README.md

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ environments, using them can still be a little on the lumpy side. With
3030
perl -Ilocal/lib/perl -Ilib bin/myapp
3131
carton exec perl -Ilib bin/myapp
3232

33-
With [App::perlbrew](https://metacpan.org/pod/App::perlbrew),
33+
With [App::perlbrew](https://metacpan.org/pod/App%3A%3Aperlbrew),
3434

3535
perlbrew switch perl-5.28.0@libname
3636
perl -Ilib bin/myapp
@@ -40,7 +40,7 @@ With [https://github.com/tokuhirom/plenv](https://github.com/tokuhirom/plenv),
4040
plenv exec perl -Ilib bin/myapp
4141

4242
and if you have more than one distinct layer of dependencies, while
43-
[local::lib](https://metacpan.org/pod/local::lib) will happily handle that, integrating it with everything else
43+
[local::lib](https://metacpan.org/pod/local%3A%3Alib) will happily handle that, integrating it with everything else
4444
becomes a pain in the buttocks.
4545

4646
As a result of this, your not-so-humble author found himself regularly having
@@ -62,7 +62,7 @@ However, much like back in 2007 frustration with explaining to other
6262
developers how to set up [CPAN](https://metacpan.org/pod/CPAN) to install into `~/perl5` and how to
6363
set up one's environment variables to then find the modules so installed
6464
led to the exercise in rage driven development that first created
65-
[local::lib](https://metacpan.org/pod/local::lib), walking newbies through the creation and subsequent use of
65+
[local::lib](https://metacpan.org/pod/local%3A%3Alib), walking newbies through the creation and subsequent use of
6666
such a script was not the most enjoyable experience for anybody involved.
6767

6868
Thus, the creation of this module to reduce the setup process to:
@@ -77,7 +77,7 @@ Follwed by being able to immediately (and even more concisely) run:
7777
plx myapp
7878

7979
which will execute `perl -Ilib bin/myapp` with the correct `perl` and the
80-
relevant [local::lib](https://metacpan.org/pod/local::lib) already in scope.
80+
relevant [local::lib](https://metacpan.org/pod/local%3A%3Alib) already in scope.
8181

8282
If this seems of use to you, the ["QUICKSTART"](#quickstart) is next and the ["ACTIONS"](#actions)
8383
section of this document lists the full capabilities of plx. Onwards!
@@ -114,7 +114,7 @@ library, then we'd also want to run:
114114

115115
plx --config libspec add 40otherlib.dir ../Other-Lib/lib
116116

117-
If we want our ~/perl [local::lib](https://metacpan.org/pod/local::lib) available within the plx environment, we
117+
If we want our ~/perl [local::lib](https://metacpan.org/pod/local%3A%3Alib) available within the plx environment, we
118118
can add that as the least significant libspec with:
119119

120120
plx --config libspec add 00tilde.ll $HOME/perl5
@@ -145,9 +145,36 @@ installer available, you can run:
145145

146146
to get the current latest packed version.
147147

148-
The packed version bundled [local::lib](https://metacpan.org/pod/local::lib) and [File::Which](https://metacpan.org/pod/File::Which), and also includes
148+
The packed version bundles [local::lib](https://metacpan.org/pod/local%3A%3Alib) and [File::Which](https://metacpan.org/pod/File%3A%3AWhich), and also includes
149149
a modified `--cpanm` action that uses an inline `App::cpanminus`.
150150

151+
# ENVIRONMENT
152+
153+
`plx` actions that execute external commands all clear any existing
154+
environment variables that start with `PERL` to keep an encapsulated setup
155+
for commands being run within the layouts - and also set `PERL5OPT` to
156+
exclude `site_perl` (but not `vendor_perl`) to avoid locally installed
157+
modules causing unexpected effects.
158+
159+
Having done so, `plx` then loads each env config entry and sets those
160+
variables - then prepends the `plx` specific entries to both `PATH` and
161+
`PERL5LIB`. You can add env config entries with ["--config"](#config):
162+
163+
plx --config env add NAME VALUE
164+
165+
The changes that will be made to your environment can be output by calling
166+
the ["--env"](#env) command.
167+
168+
Additionally, environment variable overrides may be provided to the
169+
["--cmd"](#cmd), ["--exec"](#exec) and ["--perl"](#perl) commands by providing them in
170+
`NAME=VALUE` format:
171+
172+
# do not do this, it will be deleted
173+
PERL_RL=Perl5 plx <something>
174+
175+
# do this instead, it will provide the environment variable to the command
176+
plx PERL_RL=Perl5 <something>
177+
151178
# ACTIONS
152179

153180
plx --help # Print synopsis
@@ -280,11 +307,11 @@ syntax that is (hopefully) correct for your current shell.
280307
Finds the `cpanm` binary in the `PATH` that `plx` was executed _from_,
281308
and executes it using the layout's perl binary and environment variables.
282309

283-
Requires the user to specify a [local::lib](https://metacpan.org/pod/local::lib) to install into via `-l` or
310+
Requires the user to specify a [local::lib](https://metacpan.org/pod/local%3A%3Alib) to install into via `-l` or
284311
`-L` in order to avoid installing modules into unexpected places.
285312

286313
Note that this action exists primarily for bootstrapping, and if you want
287-
to use a different installer such as [App::cpm](https://metacpan.org/pod/App::cpm), you'd install it with:
314+
to use a different installer such as [App::cpm](https://metacpan.org/pod/App%3A%3Acpm), you'd install it with:
288315

289316
plx --cpanm -ldevel App::cpm
290317

@@ -324,7 +351,7 @@ layout's perl with the given options and arguments.
324351
exists bin/<name> -> --perl bin/<name> <args>
325352
else -> --exec <name> <args>
326353

327-
**Note**: Much like the `devel` [local::lib](https://metacpan.org/pod/local::lib) is created to allow for the
354+
**Note**: Much like the `devel` [local::lib](https://metacpan.org/pod/local%3A%3Alib) is created to allow for the
328355
installation of out-of-band dependencies that aren't going to be needed in
329356
production, the `dev` directory is supported to allow for the easy addition
330357
of development time only sugar commands. Note that since `perl` will re-exec
@@ -390,7 +417,7 @@ The part of the name before the last `.` is not semantically significant to
390417
plx, but is used for asciibetical sorting of the libspec entries to determine
391418
in which order to apply them.
392419

393-
The part after must be either `ll` for a [local::lib](https://metacpan.org/pod/local::lib), or `dir` for a bare
420+
The part after must be either `ll` for a [local::lib](https://metacpan.org/pod/local%3A%3Alib), or `dir` for a bare
394421
[lib](https://metacpan.org/pod/lib) directory.
395422

396423
When loaded, the spec is (if relative) resolved to an absolute path relative
@@ -438,8 +465,8 @@ libspec pointing at `$HOME/perl5`.
438465

439466
## --installself
440467

441-
Installs [App::plx](https://metacpan.org/pod/App::plx) and [App::cpanminus](https://metacpan.org/pod/App::cpanminus) into the highest-numbered
442-
[local::lib](https://metacpan.org/pod/local::lib) within the layout.
468+
Installs [App::plx](https://metacpan.org/pod/App%3A%3Aplx) and [App::cpanminus](https://metacpan.org/pod/App%3A%3Acpanminus) into the highest-numbered
469+
[local::lib](https://metacpan.org/pod/local%3A%3Alib) within the layout.
443470

444471
## --installenv
445472

bin/plx

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
package App::plx;
44

5-
our $VERSION = '0.901001'; # 0.901.1
5+
our $VERSION = '0.902003'; # 0.902.3
66

77
$VERSION = eval $VERSION;
88

@@ -345,20 +345,27 @@ sub run_action_init {
345345
}
346346

347347
sub _which {
348-
my ($self, $cmd, @args) = @_;
348+
my ($self, @args) = @_;
349349
$self->ensure_layout_config_dir;
350+
351+
my @env;
352+
353+
push @env, shift @args while $args[0] =~ /^\w+=/;
354+
355+
my $cmd = shift @args;
356+
350357
barf "--cmd <cmd> <args>" unless $cmd;
351358

352359
if ($fs->file_name_is_absolute($cmd)) {
353-
return (exec => $cmd => @args);
360+
return (exec => @env => $cmd => @args);
354361
}
355362

356363
if ($cmd eq 'perl') {
357-
return (perl => @args);
364+
return (perl => @env => @args);
358365
}
359366

360367
if ($cmd =~ m{/}) {
361-
return (perl => $cmd, @args);
368+
return (perl => @env => $cmd, @args);
362369
}
363370

364371
if ($cmd =~ /^-/) {
@@ -373,16 +380,16 @@ sub _which {
373380
}
374381
last;
375382
}
376-
return (perl => @optargs);
383+
return (perl => @env => @optargs);
377384
}
378385

379386
foreach my $dirname ($self->cmd_search_path) {
380387
if (-f (my $file = $self->layout_file($dirname => $cmd))) {
381-
return (perl => $file, @args);
388+
return (perl => @env => $file, @args);
382389
}
383390
}
384391

385-
return (exec => $cmd, @args);
392+
return (exec => @env => $cmd, @args);
386393
}
387394

388395
sub run_action_which {
@@ -391,7 +398,7 @@ sub run_action_which {
391398
say join(' ', 'plx', "--${action}", @call);
392399
}
393400

394-
sub run_action_run {
401+
sub run_action_cmd {
395402
my ($self, @args) = @_;
396403
my ($action, @call) = $self->_which(@args);
397404
$self->${\"run_action_${action}"}(@call);
@@ -401,13 +408,18 @@ sub run_action_perl {
401408
my ($self, @call) = @_;
402409
$self->ensure_layout_config_dir;
403410
return $self->show_config_perl unless @call;
404-
$self->run_action_exec($self->layout_perl, @call);
411+
my @env;
412+
push @env, shift @call while $call[0] =~ /^\w+=/;
413+
$self->run_action_exec(@env, $self->layout_perl, @call);
405414
}
406415

407416
sub run_action_exec {
408417
my ($self, @exec) = @_;
409418
$self->ensure_layout_config_dir;
410419
$self->setup_env;
420+
421+
shift @exec and $ENV{$1} = $2 while $exec[0] =~ /^(\w+)=(.*)$/;
422+
411423
exec(@exec) or barf "exec of (".join(' ', @exec).") failed: $!";
412424
}
413425

@@ -605,12 +617,11 @@ sub _parse_multi {
605617
my ($self, @args) = @_;
606618
my @multi;
607619
MULTI: while (@args) {
608-
barf "Expected multi arg {, got: $args[0]" unless $args[0] eq '{';
620+
barf "Expected multi arg [, got: $args[0]" unless $args[0] eq '[';
609621
shift @args;
610622
my @action;
611-
/^--/ or s/^/--/ for @args ? $args[0] : ();
612623
while (my $el = shift @args) {
613-
push @multi, \@action and next MULTI if $el eq '}';
624+
push @multi, \@action and next MULTI if $el eq ']';
614625
push @action, $el;
615626
}
616627
barf "Missing closing ] for multi";
@@ -643,7 +654,7 @@ sub run_action_showmulti {
643654
sub run {
644655
my ($self, $cmd, @args) = @_;
645656
$cmd ||= '--help';
646-
if ($cmd eq '{') {
657+
if ($cmd eq '[') {
647658
return $self->run_action_multi($cmd, @args);
648659
}
649660
if ($cmd =~ s/^--//) {
@@ -657,7 +668,7 @@ sub run {
657668
$cmd = shift @args;
658669
}
659670
$self->ensure_layout_config_dir;
660-
return $self->run_action_run($cmd, @args);
671+
return $self->run_action_cmd($cmd, @args);
661672
}
662673

663674
caller() ? 1 : __PACKAGE__->new->run(@ARGV);
@@ -809,9 +820,36 @@ installer available, you can run:
809820
810821
to get the current latest packed version.
811822
812-
The packed version bundled L<local::lib> and L<File::Which>, and also includes
823+
The packed version bundles L<local::lib> and L<File::Which>, and also includes
813824
a modified C<--cpanm> action that uses an inline C<App::cpanminus>.
814825
826+
=head1 ENVIRONMENT
827+
828+
C<plx> actions that execute external commands all clear any existing
829+
environment variables that start with C<PERL> to keep an encapsulated setup
830+
for commands being run within the layouts - and also set C<PERL5OPT> to
831+
exclude C<site_perl> (but not C<vendor_perl>) to avoid locally installed
832+
modules causing unexpected effects.
833+
834+
Having done so, C<plx> then loads each env config entry and sets those
835+
variables - then prepends the C<plx> specific entries to both C<PATH> and
836+
C<PERL5LIB>. You can add env config entries with L</--config>:
837+
838+
plx --config env add NAME VALUE
839+
840+
The changes that will be made to your environment can be output by calling
841+
the L</--env> command.
842+
843+
Additionally, environment variable overrides may be provided to the
844+
L</--cmd>, L</--exec> and L</--perl> commands by providing them in
845+
C<NAME=VALUE> format:
846+
847+
# do not do this, it will be deleted
848+
PERL_RL=Perl5 plx <something>
849+
850+
# do this instead, it will provide the environment variable to the command
851+
plx PERL_RL=Perl5 <something>
852+
815853
=head1 ACTIONS
816854
817855
plx --help # Print synopsis
@@ -843,7 +881,7 @@ a modified C<--cpanm> action that uses an inline C<App::cpanminus>.
843881
plx --exec <cmd> <args> # exec()s with env vars set
844882
plx --perl <args> # Run perl with args
845883
846-
plx --run <cmd> <args> # DWIM command:
884+
plx --cmd <cmd> <args> # DWIM command:
847885
848886
cmd = perl -> --perl <args>
849887
cmd = -<flag> -> --perl -<flag> <args>
@@ -855,15 +893,15 @@ a modified C<--cpanm> action that uses an inline C<App::cpanminus>.
855893
exists bin/<name> -> --perl bin/<name> <args>
856894
else -> --exec <name> <args>
857895
858-
plx --which <cmd> # Expands --run <cmd> without running
896+
plx --which <cmd> # Expands --cmd <cmd> without running
859897
860-
plx <something> <args> # Shorthand for plx --run
898+
plx <something> <args> # Shorthand for plx --cmd
861899
862900
plx --commands <filter>? # List available commands
863901
864-
plx --multi { <cmd1> <args1> } { ... } # Run multiple actions
865-
plx --showmulti { ... } { ... } # Show multiple action running
866-
plx { ... } { ... } # Shorthand for plx --multi
902+
plx --multi [ <cmd1> <args1> ] [ ... ] # Run multiple actions
903+
plx --showmulti [ ... ] [ ... ] # Show multiple action running
904+
plx [ ... ] [ ... ] # Shorthand for plx --multi
867905
868906
plx --userinit <perl> # Init ~/.plx with ~/perl5 ll
869907
plx --installself # Installs plx and cpanm into layout
@@ -974,9 +1012,9 @@ Without arguments, sugar for C<--config perl>.
9741012
Otherwise, sets up the layout's environment variables and C<exec>s the
9751013
layout's perl with the given options and arguments.
9761014
977-
=head2 --run
1015+
=head2 --cmd
9781016
979-
plx --run <cmd> <args>
1017+
plx --cmd <cmd> <args>
9801018
9811019
cmd = perl -> --perl <args>
9821020
cmd = -<flag> -> --perl -<flag> <args>
@@ -1002,7 +1040,7 @@ anything with a non-perl shebang, one can add wrappers here ala:
10021040
10031041
plx --which <cmd>
10041042
1005-
Outputs the expanded form of a C<--run> invocation without running it.
1043+
Outputs the expanded form of a C<--cmd> invocation without running it.
10061044
10071045
=head2 --config
10081046
@@ -1085,15 +1123,13 @@ stripped and the filter is treated as a regexp.
10851123
10861124
=head2 --multi
10871125
1088-
plx --multi { --init } { --config perl set 5.28.0 }
1089-
plx --multi { init } { config perl set 5.28.0 }
1126+
plx --multi [ --init ] [ --config perl set 5.28.0 ]
10901127
10911128
Runs multiple plx commands from a single invocation delimited by C<[ ... ]>.
10921129
10931130
=head2 --showmulti
10941131
1095-
plx --showmulti { --init } { --config perl set 5.28.0 }
1096-
plx --showmulti { init } { config perl set 5.28.0 }
1132+
plx --showmulti [ --init ] [ --config perl set 5.28.0 ]
10971133
10981134
Outputs approximate plx invocations that would be run by C<--multi>.
10991135

0 commit comments

Comments
 (0)