File Coverage

blib/lib/Dist/Zilla/Plugin/AlienBase/Wrapper/Bundle.pm
Criterion Covered Total %
statement 56 58 96.5
branch 10 14 71.4
condition 2 2 100.0
subroutine 10 10 100.0
pod 0 3 0.0
total 78 87 89.6


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::AlienBase::Wrapper::Bundle 0.30 {
2              
3 1     1   232884 use 5.014;
  1         9  
4 1     1   571 use Moose;
  1         465729  
  1         6  
5 1     1   8274 use Path::Tiny ();
  1         11432  
  1         25  
6 1     1   8 use List::Util ();
  1         2  
  1         1009  
7              
8             # ABSTRACT: Bundle a copy of Alien::Base::Wrapper with your dist
9             # VERSION
10              
11              
12             with 'Dist::Zilla::Role::FileGatherer',
13             'Dist::Zilla::Role::PrereqSource',
14             'Dist::Zilla::Role::FileMunger';
15              
16             has filename => (
17             is => 'ro',
18             isa => 'Str',
19             default => 'inc/Alien/Base/Wrapper.pm',
20             );
21              
22             has system_check => (
23             is => 'ro',
24             isa => 'ArrayRef[Str]',
25             );
26              
27             has system => (
28             is => 'ro',
29             isa => 'ArrayRef[Str]',
30             );
31              
32             has alien => (
33             is => 'ro',
34             isa => 'ArrayRef[Str]',
35             default => sub { [] },
36             );
37              
38             around mvp_multivalue_args => sub {
39             my($orig, $self) = @_;
40             ($self->$orig, 'alien', 'system', 'system_check' );
41             };
42              
43             sub gather_files
44             {
45 5     5 0 238998 my($self) = @_;
46              
47 5         53 require Alien::Base::Wrapper;
48 5 50       91 unless(Alien::Base::Wrapper->VERSION('1.28'))
49             {
50 0         0 $self->log_fatal("requires Alien::Base::Wrapper 1.28, but we have @{[ Alien::Base::Wrapper->VERSION ]}");
  0         0  
51             }
52              
53 5         106 my $content = Path::Tiny->new($INC{'Alien/Base/Wrapper.pm'})->slurp_utf8;
54              
55 5         1723 my $file;
56              
57 5     19   39 $file = List::Util::first { $_->name eq $self->filename } @{ $self->zilla->files };
  19         265  
  5         217  
58              
59 5 100       61 if($file)
60             {
61 1         13 $file->content($content);
62             }
63             else
64             {
65 4         225 $file = Dist::Zilla::File::InMemory->new({
66             name => $self->filename,
67             content => $content,
68             });
69 4         1451 $self->add_file($file);
70             }
71             }
72              
73             my $comment_begin = "# BEGIN code inserted by @{[ __PACKAGE__ ]}\n";
74             my $comment_end = "# END code inserted by @{[ __PACKAGE__ ]}\n";
75              
76             sub _string ($$)
77             {
78 2     2   7 my($indent, $array) = @_;
79 2         5 join "\n", map { "$indent$_" } map { s/^\| //r } @$array;
  5         19  
  5         27  
80             }
81              
82             sub munge_files
83             {
84 5     5 0 15514 my($self) = @_;
85 5 100       237 return unless defined $self->system_check;
86              
87 1 50       41 $self->log_fatal("system_check, but no system block") unless defined $self->system;
88              
89 1         39 my $system_check = _string '', $self->system_check;
90 1         78 my $system = _string ' ', $self->system;
91              
92 1     3   8 my $file = List::Util::first { $_->name eq 'Makefile.PL' } @{ $self->zilla->files };
  3         142  
  1         38  
93 1 50       53 $self->log_fatal("Makefile.PL not found") unless $file;
94              
95 1         10 my $code = "my \$system_install = $system_check;\n" .
96             "if(\$system_install) {\n" .
97             "$system\n" .
98             "} else {\n";
99              
100 1         34 my @aliens;
101 1         3 foreach my $spec (@{ $self->alien })
  1         44  
102             {
103 2         12 my ($alien, $version) = split /\@/, $spec;
104 2   100     12 $version //= "0";
105 2         10 $code .= " \$WriteMakefileArgs{BUILD_REQUIRES}->{'$alien'} = \"$version\";\n";
106 2         8 push @aliens, $alien;
107             }
108              
109 1         4 $code .= " \$WriteMakefileArgs{CC} = '\$(FULLPERL) -Iinc -MAlien::Base::Wrapper=@{[ join ',', @aliens ]} -e cc --';\n" .
  1         7  
110 1         9 " \$writeMakefileArgs{LD} = '\$(FULLPERL) -Iinc -MAlien::Base::Wrapper=@{[ join ',', @aliens ]} -e ld --';\n" .
111             "}\n";
112              
113 1         9 my $content = $file->content;
114              
115 1         107 my $ok = $content =~ s/(unless \( eval \{ ExtUtils::MakeMaker)/"$comment_begin$code$comment_end\n\n$1"/e;
  1         12  
116 1 50       5 $self->log_fatal('unable to find the correct location to insert prereqs')
117             unless $ok;
118              
119 1         4 $file->content($content);
120             }
121              
122             sub register_prereqs
123             {
124 5     5 0 11083 my($self) = @_;
125              
126             $self->zilla->prereqs->requirements_for($_, 'requires')->clear_requirement('Alien::Base::Wrapper')
127 5         164 for qw( configure build );
128 5 100       1463 return unless defined $self->system_check;
129 1         29 $self->zilla->register_prereqs({ type => 'requires', phase => 'configure' }, 'ExtUtils::MakeMaker' => '6.52');
130             }
131              
132             __PACKAGE__->meta->make_immutable;
133              
134             }
135              
136             1;
137              
138             __END__
139              
140             =pod
141              
142             =encoding UTF-8
143              
144             =head1 NAME
145              
146             Dist::Zilla::Plugin::AlienBase::Wrapper::Bundle - Bundle a copy of Alien::Base::Wrapper with your dist
147              
148             =head1 VERSION
149              
150             version 0.30
151              
152             =head1 SYNOPSIS
153              
154             [AlienBase::Wrapper::Bundle]
155             system_check = | do { # check for libfoo!
156             system_check = | use Devel::CheckLib qw( check_lib );
157             system_check = | check_lib( lib => [ 'foo' ] );
158             system_check = | }
159            
160             system = | # use libfoo!
161             system = | $WriteMakefileArgs{LIBS} = [ '-lfoo' ];
162            
163             alien = Alien::libfoo
164              
165             =head1 DESCRIPTION
166              
167             B<NOTE>: This technique and plugin is EXPERIMENTAL. Please visit us at #native on irc.perl.org
168             if you want to use this technique.
169              
170             This module bundled L<Alien::Base::Wrapper> with your distribution, which allows for
171             late-binding fallback of an alien when a system probe fails. It removes C<Alien::Base::Wrapper>
172             as a configure or build prerequisite if found, in case you have a plugin automatically computing
173             it as a prereq. (Note that if the prereq is added after this plugin it won't be removed, so
174             be sure to use this plugin AFTER any auto prereqs plugin).
175              
176             =head1 ATTRIBUTES
177              
178             =head2 filename
179              
180             This specifies the name of the bundled L<Alien::Base::Wrapper>, the default is
181             C<inc/Alien/Base/Wrapper.pm>.
182              
183             =head1 AUTHOR
184              
185             Graham Ollis <plicease@cpan.org>
186              
187             =head1 COPYRIGHT AND LICENSE
188              
189             This software is copyright (c) 2017 by Graham Ollis.
190              
191             This is free software; you can redistribute it and/or modify it under
192             the same terms as the Perl 5 programming language system itself.
193              
194             =cut