Xrepo/MM.pm

filter: Statementstmt 84.6% 11 / 13 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 88.8% 16 / 18 CC 1 SCAR 0.6
CC
1
Cov
85%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Xrepo/Build.pm ↑ Summary
1 1 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Alien::Xrepo::MM v1.0.10 {
4 1 ▶ use Cwd qw[getcwd];
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶ use JSON::PP qw[decode_json encode_json];
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶ use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶ use Alien::Xrepo::Build;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Alien::Xrepo::Build::Dist;
9 our $CONFIG = 'xrepo-mm.json'; # build-time config, re-read by run() during make
10  
11 sub new ( $class, %attr ) {
12 my %self = map { $_ => $attr{$_} } qw[
13 module_name dist_abstract dist_author dist_version license
14 base_dir xrepo_snapshot xrepo_share_dir xrepo_cache xrepo_update_repo
15 xrepo_verbose xrepo_diagnosis
16 ];
17 $self{dist_version} //= 'v1.0.0';
18 $self{license} //= 'artistic_2';
19 $self{xrepo_cache} //= 0;
20 $self{xrepo_update_repo} //= 0;
21 $self{xrepo_verbose} //= 0;
22 $self{xrepo_diagnosis} //= 0;
23 $self{base_dir} //= Cwd::getcwd();
24 $self{requires} = { %{ $attr{requires} // {} } };
25 $self{configure_requires} = { %{ $attr{configure_requires} // {} } };
26 $self{build_requires} = { %{ $attr{build_requires} // {} } };
27 $self{test_requires} = { %{ $attr{test_requires} // {} } };
28  
29 # Thing the dist must own: it cannot be reverse-engineered from the dir name.
30 die 'Alien::Xrepo::MM: module_name is required' unless defined $self{module_name};
31 return bless \%self, $class;
32 }
33 sub module_name { $_[0]{module_name} }
34 sub dist_abstract { $_[0]{dist_abstract} }
35 sub dist_author { $_[0]{dist_author} }
36 sub dist_version { $_[0]{dist_version} }
37 sub license { $_[0]{license} }
38 sub xrepo_snapshot { $_[0]{xrepo_snapshot} }
39 sub xrepo_share_dir { $_[0]{xrepo_share_dir} }
40 sub xrepo_cache { $_[0]{xrepo_cache} }
41 sub xrepo_update_repo { $_[0]{xrepo_update_repo} }
42 sub xrepo_verbose { $_[0]{xrepo_verbose} }
43 sub xrepo_diagnosis { $_[0]{xrepo_diagnosis} }
44 sub base_dir { $_[0]{base_dir} }
45 sub requires { $_[0]{requires} }
46 sub configure_requires { $_[0]{configure_requires} }
47 sub build_requires { $_[0]{build_requires} }
48 sub test_requires { $_[0]{test_requires} }
49  
50 # The record of facts the two builders share: this dist root, the module, and
51 # the build script whose mtime counts as a freshness input.
52 sub _dist ($self) {
53 Alien::Xrepo::Build::Dist->new( script => 'Makefile.PL', map { $_ => $self->{$_} } qw[base_dir module_name] );
54 }
55  
56 # Engine modules that count as recipe inputs when loaded into this process.
57 sub _engine_mods { (qw[Alien/Xrepo/MM.pm Alien/Xrepo/Build.pm Alien/Xrepo/Runtime.pm Alien/Xrepo/Build/Recipe.pm]) }
58  
59 # The Makefile gets the xrepo freshness check as a pure_all dependency. The
60 # rule itself is a tiny perl one-liner so every `make` decision happens in
61 # Perl, next to the same staleness logic Module::Build uses. -Ilib is for the
62 # source tree (some dists build the build script from a fresh checkout); the
63 # config is a file, never shell-quoted.
64 # make requires the recipe to start with a real tab. ``<<~`` dedents on the
65 # closing delimiter and can only strip spaces, which MakeMaker then writes
66 # space-indented and GNU make rejects as a malformed rule, so the tab is
67 # built explicitly instead.
68 sub _postamble {
69 my $tab = "\t";
70 my $run = "Alien::Xrepo::MM::run(q{$CONFIG})";
71 return "pure_all :: xrepo\n\nxrepo :\n$tab\$(NOECHO) \$(PERLRUN) -Ilib -MAlien::Xrepo::MM -e \"$run\";\n";
72 }
73 package #
74 MY {
75  
76 sub postamble ( $self = undef, @args ) {
77 my $prev = '';
78 if ( ref $self ) {
79 my $out = eval { $self->SUPER::postamble(@args) };
80 $prev = $out if defined $out;
81 }
82 return ( length $prev ? "$prev\n" : '' ) . Alien::Xrepo::MM::_postamble();
83 }
84 }
85  
86 # Generate the Makefile. The config below is written first so the pure_all hook the recipe emits has something to read when `make` runs.
87 sub Write ($self) {
88 my $base = path( $self->{base_dir} );
89 $base->mkpath unless -d $base;
90 my $cwd = getcwd();
91 chdir $base or die "Alien::Xrepo::MM: chdir $base: $!";
92 my $dist = $self->_dist;
93 my $snapshot = path( defined $self->{xrepo_snapshot} ? $self->{xrepo_snapshot} : $dist->snapshot_path )->absolute;
94 my $share = defined $self->{xrepo_share_dir} ? path( $self->{xrepo_share_dir} )->absolute : $snapshot->parent;
95 $base->child($CONFIG)->spew_utf8(
96 encode_json(
97 { module_name => $self->{module_name},
98 snapshot => $snapshot->stringify,
99 share_dir => $share->stringify,
100 cache => $self->{xrepo_cache},
101 update_repo => $self->{xrepo_update_repo},
102 verbose => $self->{xrepo_verbose},
103 diagnosis => $self->{xrepo_diagnosis}
104 }
105 )
106 );
107 my %wm = $self->_write_makefile_args;
108 require ExtUtils::MakeMaker;
109 ExtUtils::MakeMaker::WriteMakefile(%wm);
110 chdir $cwd;
111 return;
112 }
113  
114 # ExtUtils::MakeMaker arguments mirror Alarm::Xrepo::MB's properties: test/build requires union into PREREQ_PM (EUMM treats PREREQ_PM as runtime), configure_requires separate, and the config cleaned with `make realclean`.
115 sub _write_makefile_args ($self) {
116 my %wm = (
117 NAME => $self->{module_name},
118 VERSION => $self->{dist_version},
119 ABSTRACT => ( $self->{dist_abstract} // $self->{module_name} ),
120 AUTHOR => $self->{dist_author},
121 LICENSE => $self->{license},
122 PREREQ_PM => { %{ $self->{build_requires} }, %{ $self->{test_requires} }, %{ $self->{requires} } },
123 CONFIGURE_REQUIRES => { %{ $self->{configure_requires} } },
124 clean => { FILES => $CONFIG }
125 );
126 return %wm;
127 }
128  
129 # What `make` executes on the xrepo target: re-read the config written by Write(), rebuild the snapshot when stale, and do nothing (successfully) when the file is absent so a dist that merely loaded us but never used Write is unharmed.
130 sub run ( $config //= $CONFIG ) {
131 my $cfg = path($config);
132 $cfg = $cfg->absolute unless $cfg->is_absolute;
133 my $dist_root = $cfg->parent;
134 return !print "Alien::Xrepo::MM: no $config found; skipping xrepo build\n" unless -e $cfg;
135 my $data = decode_json( $cfg->slurp_utf8 );
136 my $dist = Alien::Xrepo::Build::Dist->new( base_dir => $dist_root->stringify, module_name => $data->{module_name}, script => 'Makefile.PL' );
137 my $snapshot = path( $data->{snapshot} );
138 $snapshot = $dist_root->child( $data->{snapshot} ) unless $snapshot->is_absolute;
139 return !print "=> xrepo: $snapshot is up to date\n" unless $dist->snapshot_stale( $snapshot, _engine_mods() );
140 my $share = ( defined $data->{share_dir} && $data->{share_dir} ne '' ) ? path( $data->{share_dir} ) : $snapshot->parent;
141 $share->mkpath;
142 print "=> xrepo: writing $snapshot\n";
143 my $build = Alien::Xrepo::Build->new(
144 recipe => $dist->recipe,
145 snapshot => $snapshot,
146 share_dir => $share->stringify,
147 cache => ( $data->{cache} // 0 ),
148 update_repo => ( $data->{update_repo} // 0 ),
149 verbose => ( $data->{verbose} // 0 ),
150 diagnosis => ( $data->{diagnosis} // 0 )
151 );
152 $build->run;
153 $build->assert_installed;
154 return;
155 }
156 };
157 #
158 1;
159 __END__
« Xrepo/Build.pm ↑ Summary