-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperlbrewise-spec
executable file
·5860 lines (4191 loc) · 191 KB
/
perlbrewise-spec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# -*- mode: perl; -*-
# This chunk of stuff was generated by App::FatPacker. To find the original
# file's code, look for the end of this BEGIN block or the string 'FATPACK'
BEGIN {
my %fatpacked;
$fatpacked{"Applify.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'APPLIFY';
package Applify;
use strict;
use warnings;
use Carp ();
use File::Basename ();
use constant SUB_NAME_IS_AVAILABLE => $INC{'App/FatPacker/Trace.pm'}
? 0 # this will be true when running under "fatpack"
: eval 'use Sub::Name; 1' ? 1 : 0;
our $VERSION = '0.14';
our $PERLDOC = 'perldoc';
our $SUBCMD_PREFIX = "command";
my $ANON = 1;
sub app {
my $self = shift;
my $code = $self->{app} ||= shift;
my $parser = $self->_option_parser;
my (%options, @options_spec, $application_class, $app);
# has to be run before calculating option spec.
# cannot do ->can() as application_class isn't created yet.
if ($self->_subcommand_activate($ARGV[0])) { shift @ARGV; }
for my $option (@{$self->{options}}) {
my $switch = $self->_attr_to_option($option->{name});
push @options_spec, $self->_calculate_option_spec($option);
$options{$switch} = $option->{default} if exists $option->{default};
$options{$switch} = [@{$options{$switch}}] if ref($options{$switch}) eq 'ARRAY';
}
unless ($parser->getoptions(\%options, @options_spec, $self->_default_options)) {
$self->_exit(1);
}
if ($options{help}) {
$self->print_help;
$self->_exit('help');
}
elsif ($options{man}) {
system $PERLDOC => $self->documentation;
$self->_exit($? >> 8);
}
elsif ($options{version}) {
$self->print_version;
$self->_exit('version');
}
$application_class = $self->{application_class} ||= $self->_generate_application_class($code);
$app = $application_class->new(
{map { my $k = $self->_option_to_attr($_); $k => $self->_upgrade($k, $options{$_}) } keys %options});
return $app if defined wantarray; # $app = do $script_file;
$self->_exit($app->run(@ARGV));
}
sub documentation {
return $_[0]->{documentation} if @_ == 1;
$_[0]->{documentation} = $_[1] or die 'Usage: documentation $file|$module_name;';
return $_[0];
}
sub extends {
my $self = shift;
$self->{extends} = [@_];
return $self;
}
sub import {
my ($class, %args) = @_;
my @caller = caller;
my $self = $class->new({caller => \@caller});
my $ns = $caller[0] . '::';
my %export;
strict->import;
warnings->import;
$self->{skip_subs} = {app => 1, option => 1, version => 1, documentation => 1, extends => 1, subcommand => 1};
no strict 'refs';
for my $name (keys %$ns) {
$self->{'skip_subs'}{$name} = 1;
}
for my $k (qw(app extends option version documentation subcommand)) {
my $name = $args{$k} // $k;
next unless $name;
$export{$k} = $name =~ /::/ ? $name : "$caller[0]\::$name";
}
no warnings 'redefine'; # need to allow redefine when loading a new app
*{$export{app}} = sub (&) { $self->app(@_) };
*{$export{option}} = sub { $self->option(@_) };
*{$export{version}} = sub { $self->version(@_) };
*{$export{documentation}} = sub { $self->documentation(@_) };
*{$export{extends}} = sub { $self->extends(@_) };
*{$export{subcommand}} = sub { $self->subcommand(@_) };
}
sub new {
my ($class, $args) = @_;
my $self = bless $args, $class;
$self->{options} ||= [];
$self->{caller} or die 'Usage: $self->new({ caller => [...], ... })';
return $self;
}
sub option {
my $self = shift;
my $type = shift or die 'Usage: option $type => ...';
my $name = shift or die 'Usage: option $type => $name => ...';
my $documentation = shift or die 'Usage: option $type => $name => $documentation, ...';
my ($default, %args);
if (@_ % 2) {
$default = shift;
%args = @_;
}
else {
%args = @_;
}
if ($args{alias} and !ref $args{alias}) {
$args{alias} = [$args{alias}];
}
push @{$self->{options}}, {default => $default, %args, type => $type, name => $name, documentation => $documentation};
return $self;
}
sub options { $_[0]->{options} }
sub print_help {
my $self = shift;
my @options = @{$self->{options}};
my $width = 0;
push @options, {name => ''};
push @options, {name => 'help', documentation => 'Print this help text'};
push @options, {name => 'man', documentation => 'Display manual for this application'} if $self->documentation;
push @options, {name => 'version', documentation => 'Print application name and version'} if $self->version;
push @options, {name => ''};
$self->_print_synopsis;
OPTION:
for my $option (@options) {
my $length = length $option->{name};
$width = $length if $width < $length;
}
print "Usage:\n";
if (%{$self->{subcommands} || {}}) {
my $subcmds = [sort { $a->{name} cmp $b->{name} } values %{$self->{subcommands}}];
my ($width) = sort { $b <=> $a } map { length($_->{name}) } @$subcmds;
print "\n ", File::Basename::basename($0), " [command] [options]\n";
print "\ncommands:\n";
printf(" %-${width}s %s\n", @{$_}{'name', 'desc'}) for @$subcmds;
print "\noptions:\n";
}
OPTION:
for my $option (@options) {
my $name = $self->_attr_to_option($option->{name}) or do { print "\n"; next OPTION };
printf(
" %s %2s%-${width}s %s\n",
$option->{required} ? '*' : ' ',
length($name) > 1 ? '--' : '-',
$name, $option->{documentation},
);
}
return $self;
}
sub print_version {
my $self = shift;
my $version = $self->version or die 'Cannot print version without version()';
unless ($version =~ m!^\d!) {
eval "require $version; 1" or die "Could not load $version: $@";
$version = $version->VERSION;
}
printf "%s version %s\n", File::Basename::basename($0), $version;
}
sub subcommand {
my ($self, $name) = (shift, shift);
return $self->{subcommand} unless @_;
$self->{subcommands}{$name} = {name => $name, desc => $_[0], adaptation => $_[1]};
return $self;
}
sub version {
return $_[0]->{version} if @_ == 1;
$_[0]->{version} = $_[1] or die 'Usage: version $module_name|$num;';
return $_[0];
}
sub _attr_to_option {
local $_ = $_[1] or return;
s!_!-!g;
$_;
}
sub _calculate_option_spec {
my ($self, $option) = @_;
my $spec = $self->_attr_to_option($option->{name});
if (ref $option->{alias} eq 'ARRAY') {
$spec .= join '|', '', @{$option->{alias}};
}
if ($option->{type} =~ /^(?:bool|flag)/i) { $spec .= '!' }
elsif ($option->{type} =~ /^inc/) { $spec .= '+' }
elsif ($option->{type} =~ /^str/) { $spec .= '=s' }
elsif ($option->{type} =~ /^int/i) { $spec .= '=i' }
elsif ($option->{type} =~ /^num/i) { $spec .= '=f' }
elsif ($option->{type} =~ /^file/) { $spec .= '=s' } # TODO
elsif ($option->{type} =~ /^dir/) { $spec .= '=s' } # TODO
else { die 'Usage: option {bool|flag|inc|str|int|num|file|dir} ...' }
if (my $n_of = $option->{n_of}) {
$spec .= $n_of eq '@' ? $n_of : "{$n_of}";
$option->{default}
and ref $option->{default} ne 'ARRAY'
and die 'Usage option ... default => [Need to be an array ref]';
$option->{default} ||= [];
}
return $spec;
}
sub _default_options {
my $self = shift;
my @default;
push @default, 'help';
push @default, 'man' if $self->documentation;
push @default, 'version' if $self->version;
return @default;
}
sub _exit {
my ($self, $reason) = @_;
exit 0 unless ($reason =~ /^\d+$/); # may change without warning...
exit $reason;
}
sub _generate_application_class {
my ($self, $code) = @_;
my $application_class = $self->{caller}[1];
my $extends = $self->{extends} || [];
my ($meta, @required);
$application_class =~ s!\W!_!g;
$application_class = join '::', ref($self), "__ANON__${ANON}__", $application_class;
$ANON++;
eval qq[
package $application_class;
use base qw(@$extends);
1;
] or die "Failed to generate application class: $@";
{
no strict 'refs';
_sub("$application_class\::new" => sub { my $class = shift; bless shift, $class })
unless grep { $_->can('new') } @$extends;
_sub("$application_class\::_script" => sub {$self});
_sub(
"$application_class\::run" => sub {
my ($app, @extra) = @_;
if (@required = grep { not defined $app->{$_} } @required) {
my $required = join ', ', map { '--' . $self->_attr_to_option($_) } @required;
$app->_script->print_help;
die "Required attribute missing: $required\n";
}
# get subcommand code - which should have a registered subroutine
# or fallback to app {} block.
$code = $app->_script->_subcommand_code($app) || $code;
return $app->$code(@extra);
}
);
for ('app', $self->{caller}[0]) {
my $ns = \%{"$_\::"};
for my $name (keys %$ns) {
$self->{skip_subs}{$name} and next;
my $code = eval { ref $ns->{$name} eq 'CODE' ? $ns->{$name} : *{$ns->{$name}}{CODE} } or next;
my $fqn = join '::', $application_class, $name;
_sub($fqn => $code);
delete $ns->{$name}; # may be a bit too destructive?
}
}
$meta = $application_class->meta if $application_class->isa('Moose::Object') and $application_class->can('meta');
for my $option (@{$self->{options}}) {
my $name = $option->{name};
my $fqn = join '::', $application_class, $name;
if ($meta) {
$meta->add_attribute($name => {is => 'rw', default => $option->{default}});
}
else {
_sub($fqn => sub { @_ == 2 and $_[0]->{$name} = $_[1]; $_[0]->{$name} });
}
push @required, $name if $option->{required};
}
}
return $application_class;
}
sub _load_class {
my $class = shift or return undef;
return $class if $class->can('new');
return eval "require $class; 1" ? $class : "";
}
sub _option_parser {
$_[0]->{_option_parser} ||= do {
require Getopt::Long;
Getopt::Long::Parser->new(config => [qw(no_auto_help no_auto_version pass_through)]);
};
}
sub _option_to_attr {
local $_ = $_[1] or return;
s!-!_!g;
$_;
}
sub _print_synopsis {
my $self = shift;
my $documentation = $self->documentation or return;
my $print;
unless (-e $documentation) {
eval "use $documentation; 1" or die "Could not load $documentation: $@";
$documentation =~ s!::!/!g;
$documentation = $INC{"$documentation.pm"};
}
open my $FH, '<', $documentation or die "Failed to read synopsis from $documentation: $@";
while (<$FH>) {
last if $print and /^=(?:cut|head1)/;
print if $print;
$print = 1 if /^=head1 SYNOPSIS/;
}
}
sub _sub {
my ($fqn, $code) = @_;
no strict 'refs';
return if *$fqn{CODE};
*$fqn = SUB_NAME_IS_AVAILABLE ? Sub::Name::subname($fqn, $code) : $code;
}
sub _subcommand_activate {
my ($self, $name) = @_;
return undef unless $name and $name =~ /^\w+/;
return undef unless $self->{subcommands}{$name};
$self->{subcommand} = $name;
{
no warnings 'redefine';
local *Applify::app = sub {
Carp::confess(
"Looks like you have a typo in your script! Cannot have app{} inside a subcommand options block.");
};
$self->{subcommands}{$name}{adaptation}->($self);
}
return 1;
}
sub _subcommand_code {
my ($self, $app, $name) = (shift, shift);
return undef unless $name = $self->subcommand;
return $app->can("${SUBCMD_PREFIX}_${name}");
}
sub _upgrade {
my ($self, $name, $input) = @_;
return $input unless defined $input;
my ($option) = grep { $_->{name} eq $name } @{$self->{options}};
return $input unless my $class = _load_class($option->{isa});
return ref $input eq 'ARRAY' ? [map { $class->new($_) } @$input] : $class->new($input);
}
1;
=encoding utf8
=head1 NAME
Applify - Write object oriented scripts with ease
=head1 VERSION
0.14
=head1 DESCRIPTION
This module should keep all the noise away and let you write scripts
very easily. These scripts can even be unittested even though they
are define directly in the script file and not in a module.
=head1 SYNOPSIS
#!/usr/bin/perl
use Applify;
option file => input_file => 'File to read from';
option dir => output_dir => 'Directory to write files to';
option flag => dry_run => 'Use --no-dry-run to actually do something', 1;
documentation __FILE__;
version 1.23;
sub generate_exit_value {
return int rand 100;
}
app {
my($self, @extra) = @_;
my $exit_value = 0;
print "Extra arguments: @extra\n" if(@extra);
print "Will read from: ", $self->input_file, "\n";
print "Will write files to: ", $self->output_dir, "\n";
if($self->dry_run) {
die 'Will not run script';
}
return $self->generate_exit_value;
};
=head1 APPLICATION CLASS
This module will generate an application class, which C<$self> inside the
L</app> block refere to. This class will have:
=over 4
=item * new()
An object constructor. This method will not be auto generated if any of
the classes given to L</extends> has the method C<new()>.
=item * run()
This method is basically the code block given to L</app>.
=item * Other methods
Other methods defined in the script file will be accesible from C<$self>
inside C<app{}>.
=item * _script()
This is an accessor which return the L<Applify> object which
is refered to as C<$self> in this documentation.
NOTE: This accessor starts with an underscore to prevent conflicts
with L</options>.
=item * Other accessors
Any L</option> (application switch) will be available as an accessor on the
application object.
=back
=head1 EXPORTED FUNCTIONS
=head2 option
option $type => $name => $documentation;
option $type => $name => $documentation, $default;
option $type => $name => $documentation, $default, @args;
option $type => $name => $documentation, @args;
This function is used to define options which can be given to this
application. See L</SYNOPSIS> for example code. This function can also be
called as a method on C<$self>.
=over 4
=item * $type
Used to define value types for this input.
=over 4
=item bool, flag
=item inc
=item str
=item int
=item num
=item file (TODO)
=item dir (TODO)
=back
=item * $name
The name of an application switch. This name will also be used as
accessor name inside the application. Example:
# define an application switch:
option file => some_file => '...';
# call the application from command line:
> myapp.pl --some-file /foo/bar
# run the application code:
app {
my $self = shift;
print $self->some_file # prints "/foo/bar"
return 0;
};
=item * C<$documentation>
Used as description text when printing the usage text.
=item * C<@args>
=over 4
=item * C<required>
The script will not start if a required field is omitted.
=item * C<n_of>
Allow the option to hold a list of values. Examples: "@", "4", "1,3".
See L<Getopt::Long/Options-with-multiple-values> for details.
=item * C<isa>
Specify the class an option should be instantiated as. Example:
option file => output => "output file", isa => "Mojo::File";
The C<output()> attribute will then later return an object of L<Mojo::File>,
instead of just a plain string.
=item * Other
Any other L<Moose> attribute argument may/will be supported in
future release.
=back
=back
=head2 documentation
documentation __FILE__; # current file
documentation '/path/to/file';
documentation 'Some::Module';
Specifies where to retrieve documentaion from when giving the C<--man>
switch to your script.
=head2 version
version 'Some::Module';
version $num;
Specifies where to retrieve the version number from when giving the
C<--version> switch to your script.
=head2 extends
extends @classes;
Specify which classes this application should inherit from. These
classes can be L<Moose> based.
=head2 subcommand
subcommand list => 'provide a listing objects' => sub {
option flag => long => 'long listing';
option flag => recursive => 'recursively list objects';
};
subcommand create => 'create a new object' => sub {
option str => name => 'name of new object', required => 1;
option str => description => 'description for the object', required => 1;
};
sub command_create {
my ($self, @extra) = @_;
## do creating
return 0;
}
sub command_list {
my ($self, @extra) = @_;
## do listing
return 0;
}
app {
my ($self, @extra) = @_;
## fallback when no command given.
$self->_script->print_help;
return 0;
};
This function allows for creating multiple related sub commands within the same
script in a similar fashion to C<git>. The L</option>, L</extends> and
L</documentation> exported functions may sensibly be called within the
subroutine. Calling the function with no arguments will return the running
subcommand, i.e. a valid C<$ARGV[0]>. Non valid values for the subcommand given
on the command line will result in the help being displayed.
=head2 app
app CODE;
This function will define the code block which is called when the application
is started. See L</SYNOPSIS> for example code. This function can also be
called as a method on C<$self>.
IMPORTANT: This function must be the last function called in the script file
for unittests to work. Reason for this is that this function runs the
application in void context (started from command line), but returns the
application object in list/scalar context (from L<perlfunc/do>).
=head1 ATTRIBUTES
=head2 options
$array_ref = $self->options;
Holds the application options given to L</option>.
=head1 METHODS
=head2 new
$self = $class->new({ options => $array_ref, ... });
Object constructor. Creates a new object representing the script meta
information.
=head2 print_help
Will print L</options> to selected filehandle (STDOUT by default) in
a normalized matter. Example:
Usage:
--foo Foo does this and that
* --bar Bar does something else
--help Print this help text
--man Display manual for this application
--version Print application name and version
=head2 print_version
Will print L</version> to selected filehandle (STDOUT by default) in
a normalized matter. Example:
some-script.pl version 1.23
=head2 import
Will export the functions listed under L</EXPORTED FUNCTIONS>. The functions
will act on a L<Applify> object created by this method.
=head1 COPYRIGHT & LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHORS
Jan Henning Thorsen - C<[email protected]>
Roy Storey - C<[email protected]>
=cut
APPLIFY
$fatpacked{"Devel/IPerl/Plugin/CpanMinus.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'DEVEL_IPERL_PLUGIN_CPANMINUS';
package Devel::IPerl::Plugin::CpanMinus;
use strict;
use warnings;
our $VERSION = '0.01';
sub register {
my ($class, $iperl) = @_;
my $options = {
cpanm => [],
cpanm_info => [cmd => 'info'],
cpanm_installdeps => [installdeps => 1],
};
$iperl->load_plugin('Perlbrew') unless $iperl->can('perlbrew');
for my $name (qw{cpanm cpanm_info cpanm_installdeps}) {
$iperl->helper($name => sub {
my ($ip, $ret, %env) = (shift, -1);
eval 'require App::cpanminus::fatscript; 1;';
return $ret if $@;
return $ret if 0 == @_; # nothing to do?
my @filtered = @_;
my $cpanm = App::cpanminus::script->new(
@{$options->{$name}},
argv => [@filtered]);
$cpanm->parse_options();
$cpanm->{interactive} = 0;
delete $cpanm->{action}
if exists $cpanm->{action} and $cpanm->{action} =~ m/upgrade/;
return $cpanm->doit;
});
}
return 1;
}
1;
=pod
=head1 NAME
Devel::IPerl::Plugin::CpanMinus - cpanm client
=head1 DESCRIPTION
This plugin enables users to curate L<local::lib> as set up in
L<Devel::IPerl::Plugin::Perlbrew>.
Once users have access to L<Devel::IPerl::Plugin::Perlbrew> they wish to have
the ability to curate the libraries they
L<create|Devel::IPerl::Plugin::Perlbrew#perlbrew_lib_create>. While this is easy
to achieve at the command line, the notebook is an excellent place to document
this workflow as well.
=head1 SYNOPSIS
IPerl->load_plugin('CpanMinus') unless IPerl->can('cpanm');
# create and use a library
IPerl->perlbrew_lib_create('cpanm-test');
IPerl->perlbrew('cpanm-test');
# install dependencies for notebook
IPerl->cpanm_installdeps('.');
# install a specific module
IPerl->cpanm('Test::Pod');
=head1 IPerl Interface Method
=head2 register
Called by C<<< IPerl->load_plugin('CpanMinus') >>>.
=head1 REGISTERED METHODS
These all take as arguments any arguments that are accepted by the command line
client L<cpanm>.
=head2 cpanm
# install a specific module
IPerl->cpanm('Test::Pod');
Use L<cpanm> to install the given module.
=head2 cpanm_info
IPerl->cpanm_info('Test::Pod');
Displays the distribution information in "AUTHOR/Dist-Name-ver.tar.gz" format.
=head2 cpanm_installdeps
# install dependencies as listed in cpanfile in current directory
IPerl->cpanm_installdeps('.');
# install dependencies for a module
IPerl->cpanm_installdeps('-n', '--quiet', 'Test::Pod');
=cut
DEVEL_IPERL_PLUGIN_CPANMINUS
$fatpacked{"Devel/IPerl/Plugin/Perlbrew.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'DEVEL_IPERL_PLUGIN_PERLBREW';
package Devel::IPerl::Plugin::Perlbrew;
use strict;
use warnings;
use feature 'say';
use Symbol 'delete_package';
use constant DEBUG => $ENV{IPERL_PLUGIN_PERLBREW_DEBUG} ? 1 : 0;
use constant PERLBREW_CLASS => $ENV{IPERL_PLUGIN_PERLBREW_CLASS}
? $ENV{IPERL_PLUGIN_PERLBREW_CLASS}
: 'App::perlbrew';
use constant PERLBREW_INSTALLED => eval 'use '. PERLBREW_CLASS.'; 1' ? 1 : 0;
our $VERSION = '0.03';
sub brew {
my $self = shift;
my %env = %{$self->env || {}};
my %save = ();
for my $var(_filtered_env_keys(\%env)) {
say STDERR "@$self{name} ", join " = ", $var, $env{$var} if DEBUG;
$save{$var} = $ENV{$var} if exists $ENV{$var};
$ENV{$var} = $env{$var};
}
if ($env{PERL5LIB}) {
say STDERR join " = ", 'PERL5LIB', $env{'PERL5LIB'} if DEBUG;
eval "use lib split ':', q[$env{PERL5LIB}];";
warn $@ if $@; ## uncoverable branch true
}
return $self->saved(\%save);
}
sub env { return $_[0]{env} if @_ == 1; $_[0]{env} = $_[1]; $_[0]; }
sub new {
my $class = shift;
bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class;
}
sub name { return $_[0]{name} if @_ == 1; $_[0]{name} = $_[1]; $_[0]; }
sub register {
my ($class, $iperl) = @_;
my $domain = sub {
my $instance = $_[0]->instance;
return $instance->{'perlbrew_domain'} if @_ == 1;
$instance->{'perlbrew_domain'} = $_[1];
$instance;
};
$domain->($iperl, $ENV{'PERLBREW_HOME'});
for my $name (qw{perlbrew}) {
my $current = $class->new->name('@@@'); ## impossible name
$iperl->helper($name => sub {
my ($ip, $lib, $unload, $ret) = (shift, shift, shift || 0, -1);
return $ret if not defined $lib;
return $ret if 0 == PERLBREW_INSTALLED;
my $new = $class->new->name($class->_make_name($lib));
if ($current->unload($unload)->name ne $new->name) {
my $pb = PERLBREW_CLASS->new();
$pb->home($domain->($ip));
$new->env({ $pb->perlbrew_env($new->name) });
## ensure the timing of the DESTROY, spoil
undef($current = $current->spoil);
$current = $new->brew;
}
return $new->success;
});
}
for my $name (qw{list list_modules}) {
$iperl->helper("perlbrew_$name" => sub {
my ($ip, $ret) = (shift, -1);
return $ret if 0 == PERLBREW_INSTALLED;
my $pb = PERLBREW_CLASS->new();
$pb->home($domain->($ip));
local $App::perlbrew::PERLBREW_HOME = $pb->home
if ($name eq 'list_modules');
return $pb->run_command($name, @_);
});
}
for my $name (qw{lib_create}) {
$iperl->helper("perlbrew_$name" => sub {
my ($ip, $lib, $ret) = (shift, shift, -1);
return $ret if not defined $lib;
return $ret if 0 == PERLBREW_INSTALLED;
my $pb = PERLBREW_CLASS->new();
$pb->home($domain->($ip));
eval { $pb->run_command_lib_create($class->_make_name($lib)); };
return $@ ? 0 : 1;
});
}
$iperl->helper('perlbrew_domain' => sub {
my ($ip, $dir) = (shift, shift);
return $domain->($ip) unless $dir && -d $dir;
return $domain->($ip, $dir)->{'perlbrew_domain'};
});
return 1;
}
sub saved { return $_[0]{saved} if @_ == 1; $_[0]{saved} = $_[1]; $_[0]; }
sub spoil {
my $self = shift;
my %env = %{$self->env || {}};
my %save = %{$self->saved || {}};
for my $var(_filtered_env_keys(\%env)) {
if (exists $save{$var}) {
say STDERR "revert ", join " = ", $var, $save{$var} if DEBUG;
$ENV{$var} = $save{$var};
} else {
say STDERR "unset ", $var if DEBUG;
delete $ENV{$var};
}
}
if ($env{PERL5LIB}) {
say STDERR join " = ", 'PERL5LIB', $env{'PERL5LIB'} if DEBUG;
eval "no lib split ':', q[$env{PERL5LIB}];";
warn $@ if $@; ## uncoverable branch true
if ($self->unload) {
my $path_re = qr{\Q$env{PERL5LIB}\E};
for my $module_path(keys %INC) {
## autosplit modules
next if $module_path =~ m{\.(al|ix)$} && delete $INC{$module_path};
## global destruction ?
next if not defined $INC{$module_path};
## FatPacked ?
next if ref($INC{$module_path});
## Not part of this PERL5LIB
next if $INC{$module_path} !~ m{^$path_re};
## translate to class_path
(my $class = $module_path) =~ s{/}{::}g;
$class =~ s/\.pm//;
## notify and unload
say "unloading $class ($module_path) from $INC{$module_path}";
_teardown( $class );
delete $INC{$module_path};
}
}
}
# no need to revert again.
return $self->env({})->saved({});
}
sub success { scalar(keys %{$_[0]->{env}}) ? 1 : 0; }
sub unload { return $_[0]{unload} if @_ == 1; $_[0]{unload} = $_[1]; $_[0]; }
sub _check_env_perl {
my ($env_perl, $path_perl) = (shift, _from_binary_path());
$ENV{PERLBREW_PERL} = $env_perl = $path_perl unless $env_perl;
return $env_perl unless $path_perl;
return ($env_perl eq $path_perl ? $env_perl : $ENV{PERLBREW_PERL} = $path_perl);
}
sub _filtered_env_keys {
return (sort grep { m/^PERL/i && $_ ne "PERL5LIB" } keys %{+pop});
}
sub _from_binary_path {
say STDERR $^X if DEBUG;
if ($^X =~ m{/perls/([^/]+)/bin/perl}) { return $1; }
(my $v = $^V->normal) =~ s/v/perl-/;
return $v;
}
sub _make_name {
my ($class, $name, $current) =
(shift, shift, _check_env_perl($ENV{PERLBREW_PERL}));
my $pb = PERLBREW_CLASS->new();