lib/CPAN/Static/Install.pm

filter: Statementstmt 45.4% 70 / 154 Branchbran 6.8% 3 / 44 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 66.6% 20 / 30 Podpod 50.0% 7 / 14 totaltotal 37.7% 100 / 265 CC 34 SCAR 29.6
CC
34
Cov
34%
build
72.0
manify
42.0
supports_static_install
30.0
CRAP 19.4
↑ Summary blib/lib/CPAN/Static.pm »
1 package CPAN::Static::Install;
2 1 $CPAN::Static::Install::VERSION = '0.006';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 our @EXPORT_OK = qw/configure build test install supports_static_install opts_from_args_list opts_from_args_string/;
8 1 our %EXPORT_TAGS = (
9 'all' => \@EXPORT_OK,
10 );
11  
12 1 ▶use CPAN::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use ExtUtils::Config 0.003;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use ExtUtils::Helpers 0.020 qw/make_executable split_like_shell man1_pagename man3_pagename detildefy/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use ExtUtils::Install qw/pm_to_blib/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use ExtUtils::InstallPaths 0.002;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Basename qw/dirname/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use File::Path qw/mkpath/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use File::Spec::Functions qw/catfile catdir rel2abs abs2rel splitdir curdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Getopt::Long 2.36 qw/GetOptionsFromArray/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use JSON::PP 2 qw/encode_json decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 0 ▶sub write_file {
Subroutine write_file: not called CC=2 SCAR=17.9
Pod undocumented
26 0 my ($filename, $content) = @_;
27 0 ▶ open my $fh, '>', $filename or die "Could not open $filename: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or die "Could not open ${filename}: $!\n" 0 0 0
28 0 print $fh $content;
29 }
30 1 ▶sub read_file {
Subroutine read_file: called CC=2 SCAR=7.0
Pod undocumented
31 1 my ($filename) = @_;
32 1 ▶ open my $fh, '<', $filename or die "Could not open $filename: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or die "Could not open ${filename}: $!\n" 1 0 1
33 1 return do { local $/; <$fh> };
34 }
35  
36 1 my @getopt_flags = qw/install_base=s install_path=s% installdirs=s destdir=s prefix=s config=s%
37 uninst:1 verbose:1 dry_run:1 pureperl-only:1 create_packlist=i jobs=i/;
38  
39 1 ▶sub opts_from_args_list {
Subroutine opts_from_args_list: called CC=1 SCAR=0.0
Pod documented
40 1 my (@args) = @_;
41 1 GetOptionsFromArray(\@args, \my %result, @getopt_flags);
42 1 return %result;
43 }
44  
45 0 ▶sub opts_from_args_string {
Subroutine opts_from_args_string: not called CC=2 SCAR=17.9
Pod documented
46 0 my $arg = shift;
47 0 ▶ my @args = defined $arg ? split_like_shell($arg) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 0 0 0
48 0 return opts_from_args_list(@args);
49 }
50  
51 0 ▶sub supports_static_install {
Subroutine supports_static_install: not called CC=5 SCAR=34.0
Pod documented
52 0 my $meta = shift;
53 0 ▶ if (!$meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta) 0 0 0
54 0 ▶ return undef unless -f 'META.json';
Branch: 1 branch
BranchTrueFalseTotal
unless -f "META.json" 0 0 0
55 0 $meta = CPAN::Meta->load_file('META.json');
56 }
57 0 ▶ my $static_version = $meta->custom('x_static_install') || 0;
Condition0%0/2
$meta->custom('x_static_install') || 0
l!l
00
MC/DC0%0/1
$meta->custom('x_static_install') || 0
$meta->custom('x_static_install')
Truth table0%0/2
A
A= $meta->custom('x_static_install')
Aresult
11
00
58 0 ▶ return $static_version == 1 ? $static_version : undef;
Branch: 1 branch
BranchTrueFalseTotal
$static_version == 1 ? : 0 0 0
59 }
60  
61 0 ▶sub configure {
Subroutine configure: not called CC=4 SCAR=30.0
Pod documented
62 0 my %args = @_;
63 0 ▶ die "Unsupported static install version" if defined $args{static_version} and int $args{static_version} != 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"static_version"} and int $args{"static_version"} != 1 0 0 0
Condition0%0/3
defined $args{"static_version"} and int $args{"static_version"} != 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"static_version"} and int $args{"static_version"} != 1
defined $args{"static_version"} int $args{"static_version"} != 1
Truth table0%0/3
A and B
A= defined $args{"static_version"}B= int $args{"static_version"} != 1
ABresult
0X0
100
111
64 0 ▶ $args{config} = $args{config}->values_set if blessed($args{config});
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($args{"config"}) 0 0 0
65 0 my $meta = CPAN::Meta->load_file('META.json');
66 0 my %env = opts_from_args_string($ENV{PERL_MB_OPT});
67 0 printf "Saving configuration for '%s' version '%s'\n", $meta->name, $meta->version;
68 0 write_file('_static_build_params', encode_json([ \%env, \%args ]));
69 0 $meta->save('MYMETA.json');
70 }
71  
72 0 ▶sub manify {
Subroutine manify: not called CC=6 SCAR=37.4
Pod undocumented
73 0 my ($input_file, $output_file, $section, $opts) = @_;
74 0 ▶ return if -e $output_file && -M $input_file <= -M $output_file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $output_file and -M $input_file <= -M $output_file 0 0 0
Condition0%0/3
-e $output_file and -M $input_file <= -M $output_file
!ll&&!rl&&r
000
MC/DC0%0/2
-e $output_file and -M $input_file <= -M $output_file
-e $output_file -M $input_file <= -M $output_file
Truth table0%0/3
A and B
A= -e $output_fileB= -M $input_file <= -M $output_file
ABresult
0X0
100
111
75 0 my $dirname = dirname($output_file);
76 0 ▶ mkpath($dirname, $opts->{verbose}) if not -d $dirname;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dirname 0 0 0
77 0 require Pod::Man;
78 0 Pod::Man->new(section => $section)->parse_from_file($input_file, $output_file);
79 0 ▶ print "Manifying $output_file\n" if $opts->{verbose} && $opts->{verbose} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"verbose"} and $opts->{"verbose"} > 0 0 0 0
Condition0%0/3
$opts->{"verbose"} and $opts->{"verbose"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"verbose"} and $opts->{"verbose"} > 0
$opts->{"verbose"} $opts->{"verbose"} > 0
Truth table0%0/3
A and B
A= $opts->{"verbose"}B= $opts->{"verbose"} > 0
ABresult
0X0
100
111
80 0 return;
81 }
82  
83 0 ▶sub find {
Subroutine find: not called CC=2 SCAR=17.9
Pod undocumented
84 0 my ($pattern, $dir) = @_;
85 0 my @result;
86 0 ▶ File::Find::find(sub { push @result, $File::Find::name if /$pattern/ && -f }, $dir) if -d $dir;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
if -d $dir 0 0 0
if /$pattern/ and -f $_ 0 0 0
Condition0%0/3
/$pattern/ and -f $_
!ll&&!rl&&r
000
MC/DC0%0/2
/$pattern/ and -f $_
/$pattern/ -f $_
Truth table0%0/3
A and B
A= /$pattern/B= -f $_
ABresult
0X0
100
111
87 0 return @result;
88 }
89  
90 0 ▶sub contains_pod {
Subroutine contains_pod: not called CC=2 SCAR=17.9
Pod undocumented
91 0 my ($file) = @_;
92 0 ▶ return unless -T $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -T $file 0 0 0
93 0 return read_file($file) =~ /^\=(?:head|pod|item)/m;
94 }
95  
96 2 ▶sub hash_merge {
Subroutine hash_merge: called CC=4 SCAR=13.9
Pod undocumented
97 2 my ($left, @others) = @_;
98 2 my %result = %{$left};
99 1 for my $right (@others) {
100 2 for my $key (keys %$right) {
101 2 ▶ $result{$key} = ref($right->{$key}) eq 'HASH' ? hash_merge($result{key}, $right->{key}) : $right->{$key};
Branch: 1 branch
BranchTrueFalseTotal
ref $right->{$key} eq 'HASH' ? : 1 1 2
102 }
103 }
104 0 return %result;
105 }
106  
107 1 ▶sub get_opts {
Subroutine get_opts: called CC=2 SCAR=11.1
Pod undocumented
108 1 my %extra_opts = @_;
109 1 my ($env, $bargv) = @{ decode_json(read_file('_static_build_params')) };
110 1 my %options = hash_merge($env, $bargv, \%extra_opts);
111 0 $_ = detildefy($_) for grep { defined } @options{qw/install_base destdir prefix/}, values %{ $options{install_path} };
112 0 $options{meta} = CPAN::Meta->load_file('MYMETA.json');
113 0 $options{config} = ExtUtils::Config->new($options{config});
114 0 $options{install_paths} = ExtUtils::InstallPaths->new(%options, dist_name => $options{meta}->name);
115 0 return %options;
116 }
117  
118 0 ▶sub build {
Subroutine build: not called CC=8 SCAR=42.8
Pod documented
119 0 my %extra_opts = @_;
120 0 my %opt = get_opts(%extra_opts);
121 0 my %modules = map { $_ => catfile('blib', $_) } find(qr/\.pm$/, 'lib');
122 0 my %docs = map { $_ => catfile('blib', $_) } find(qr/\.pod$/, 'lib');
123 0 my %scripts = map { $_ => catfile('blib', $_) } find(qr/(?:)/, 'script');
124 0 my %sdocs = map { $_ => delete $scripts{$_} } grep { /.pod$/ } keys %scripts;
125 0 my %dist_shared = map { $_ => catfile(qw/blib lib auto share dist/, $opt{meta}->name, abs2rel($_, 'share')) } find(qr/(?:)/, 'share');
126 0 my %module_shared = map { $_ => catfile(qw/blib lib auto share module/, abs2rel($_, 'module-share')) } find(qr/(?:)/, 'module-share');
127 0 pm_to_blib({ %modules, %docs, %scripts, %dist_shared, %module_shared }, catdir(qw/blib lib auto/));
128 0 make_executable($_) for values %scripts;
129 0 mkpath(catdir(qw/blib arch/), $opt{verbose});
130  
131 0 ▶ if ($opt{install_paths}->is_default_installable('bindoc')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"install_paths"}->is_default_installable("bindoc")) 0 0 0
132 0 my $section = $opt{config}->get('man1ext');
133 0 for my $input (keys %scripts, keys %sdocs) {
134 0 ▶ next unless contains_pod($input);
Branch: 1 branch
BranchTrueFalseTotal
unless contains_pod($input) 0 0 0
135 0 my $output = catfile('blib', 'bindoc', man1_pagename($input));
136 0 manify($input, $output, $section, \%opt);
137 }
138 }
139 0 ▶ if ($opt{install_paths}->is_default_installable('libdoc')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"install_paths"}->is_default_installable("libdoc")) 0 0 0
140 0 my $section = $opt{config}->get('man3ext');
141 0 for my $input (keys %modules, keys %docs) {
142 0 ▶ next unless contains_pod($input);
Branch: 1 branch
BranchTrueFalseTotal
unless contains_pod($input) 0 0 0
143 0 my $output = catfile('blib', 'libdoc', man3_pagename($input));
144 0 manify($input, $output, $section, \%opt);
145 }
146 }
147 }
148  
149 1 ▶sub test {
Subroutine test: called CC=3 SCAR=21.1
Pod documented
150 1 my %extra_opts = @_;
151 1 my %opt = get_opts(%extra_opts);
152 0 ▶ die "Must run `./Build build` first\n" if not -d 'blib';
Branch: 1 branch
BranchTrueFalseTotal
unless -d "blib" 0 0 0
153 0 require TAP::Harness::Env;
154 my %test_args = (
155 (verbosity => $opt{verbose}) x!! exists $opt{verbose},
156 (jobs => $opt{jobs}) x!! exists $opt{jobs},
157 (color => 1) x !!-t STDOUT,
158 0 lib => [ map { rel2abs(catdir(qw/blib/, $_)) } qw/arch lib/ ],
159 );
160 0 my $tester = TAP::Harness::Env->create(\%test_args);
161 0 ▶ $tester->runtests(sort +find(qr/\.t$/, 't'))->has_errors and die "Tests failed";
Branch: 1 branch
BranchTrueFalseTotal
$tester->runtests(sort(find(qr/\.t$/, "t")))->has_errors and die "Tests failed" 0 0 0
162 }
163  
164 0 ▶sub install {
Subroutine install: not called CC=2 SCAR=17.9
Pod documented
165 0 my (%extra_opts) = @_;
166 0 my %opt = get_opts(%extra_opts);
167 0 ▶ die "Must run `./Build build` first\n" if not -d 'blib';
Branch: 1 branch
BranchTrueFalseTotal
unless -d "blib" 0 0 0
168 0 ExtUtils::Install::install($opt{install_paths}->install_map, @opt{qw/verbose dry_run uninst/});
169 }
170  
171 1 1;
172  
173 # ABSTRACT: static CPAN installation reference implementation
174  
175 __END__
↑ Summary blib/lib/CPAN/Static.pm »