File Coverage

blib/lib/Dist/Zilla/Plugin/Alt.pm
Criterion Covered Total %
statement 63 66 95.4
branch 10 16 62.5
condition 4 8 50.0
subroutine 12 12 100.0
pod 0 3 0.0
total 89 105 84.7


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::Alt 0.08 {
2              
3 1     1   3544485 use 5.020;
  1         4  
4 1     1   7 use Moose;
  1         2  
  1         8  
5 1     1   6953 use experimental qw( postderef signatures );
  1         2  
  1         9  
6 1     1   247 use List::Util qw( first );
  1         3  
  1         83  
7 1     1   8 use File::Find ();
  1         2  
  1         19  
8 1     1   557 use File::chdir;
  1         1997  
  1         1187  
9              
10             # ABSTRACT: Create Alt distributions with Dist::Zilla
11              
12              
13             with 'Dist::Zilla::Role::FileMunger';
14             with 'Dist::Zilla::Role::MetaProvider';
15             with 'Dist::Zilla::Role::NameProvider';
16              
17             my @diagnostic = (q{print "!!! This is an Alt:: distribution that will not actually install unless you !!!\\n";},
18             q{print "!!! set PERL_ALT_INSTALL=OVERWRITE. See https://metacpan.org/pod/Alt !!!\\n";});
19              
20             sub munge_files ($self)
21 2     2 0 261098 {
  2         6  
  2         4  
22 2 100   8   28 if(my $file = first { $_->name eq 'Makefile.PL' } @{ $self->zilla->files })
  8 50       334  
  2         68  
23             {
24 1         50 my $content = $file->content;
25 1   50     77 my $extra = join "\n", qq{# begin inserted by @{[blessed $self ]} @{[ $self->VERSION || 'dev' ]}},
  1         9  
  1         14  
26             @diagnostic,
27             q{my $alt = $ENV{PERL_ALT_INSTALL} || '';},
28             q{$WriteMakefileArgs{DESTDIR} =},
29             q{ $alt ? $alt eq 'OVERWRITE' ? '' : $alt : 'no-install-alt';},
30             q<if($^O eq 'MSWin32' && $WriteMakefileArgs{DESTDIR}) {>,
31             q{ # Windows is a precious snowflake that can't handle DESTDIR},
32             q{ # Caveat: this probably ignores any PREFIX specified by the user},
33             q{ require Config;},
34             q{ require File::Spec;},
35             q{ my @prefix = split /:/, $Config::Config{prefix};},
36             q{ $WriteMakefileArgs{PREFIX} = File::Spec->catdir($WriteMakefileArgs{DESTDIR}, @prefix);},
37             q{ delete $WriteMakefileArgs{DESTDIR};},
38             q{ # DO NOT DO THIS SORT OF THING},
39             q{ # THIS IS PRETTY UGLY AND PROBABLY BAD},
40             q{ # DO AS I SAY AND NOT AS I DO},
41             q< package>,
42             q< ExtUtils::MM_Any;>,
43             q< my $orig = \&init_INSTALL;>,
44             q< *init_INSTALL = sub {>,
45             q< my($self, @args) = @_;>,
46             q{ delete $self->{ARGS}{INSTALL_BASE} if $self->{ARGS}{PREFIX};},
47             q{ $self->$orig(@args);},
48             q< }>,
49             q< }>,
50 1   50     8 qq{# end inserted by @{[blessed $self ]} @{[ $self->VERSION || 'dev' ]}},
  1         14  
51             q{};
52 1 50       15 if($content =~ s{^WriteMakefile}{${extra}WriteMakefile}m)
53             {
54 1         48 $file->content($content);
55             }
56             else
57             {
58 0         0 $self->log_fatal('unable to find WriteMakefile in Makefile.PL');
59             }
60             }
61 4     4   226 elsif($file = first { $_->name eq 'Build.PL' } $self->zilla->files->@*)
62             {
63 1         48 my $content = $file->content;
64 1   50     77 my $extra = join "\n", qq{# begin inserted by @{[blessed $self ]} @{[ $self->VERSION || 'dev' ]}},
  1         9  
  1         16  
65             @diagnostic,
66             q{my $alt = $ENV{PERL_ALT_INSTALL} || '';},
67             q{$module_build_args{destdir} =},
68             q{ $alt ? $alt eq 'OVERWRITE' ? '' : $alt : 'no-install-alt';},
69             q<if($^O eq 'MSWin32' && $module_build_args{destdir}) {>,
70             q{ # Windows is a precious snowflake that can't handle destdir},
71             q{ # Caveat: this probably ignores any PREFIX specified by the user},
72             q{ require Config;},
73             q{ require File::Spec;},
74             q{ my @prefix = split /:/, $Config::Config{prefix};},
75             q{ $module_build_args{PREFIX} = File::Spec->catdir($module_build_args{destdir}, @prefix);},
76             q{ delete $module_build_args{destdir};},
77             q<}>,
78 1   50     6 qq{# end inserted by @{[blessed $self ]} @{[ $self->VERSION || 'dev' ]}},
  1         17  
79             q{};
80 1 50       9 if($content =~ s{^(my \$build =)}{$extra . "\n" . $1}me)
  1         10  
81             {
82 1         4 $file->content($content);
83             }
84             else
85             {
86 0         0 $self->log_fatal('unable to find Module::Build->new in Build.PL');
87             }
88             }
89             else
90             {
91 0         0 $self->log_fatal('unable to find Makefile.PL or Build.PL');
92             }
93             }
94              
95             sub metadata ($self)
96 4     4 0 89815 {
  4         10  
  4         6  
97             return {
98             no_index => {
99 4         10 file => [ grep !/^lib\/Alt\//, grep /^lib.*\.pm$/, map { $_->name } @{ $self->zilla->files } ],
  16         679  
  4         143  
100             },
101             };
102             }
103              
104             sub provide_name ($self)
105 2     2 0 2579 {
  2         5  
  2         4  
106 2         71 local $CWD = $self->zilla->root;
107 2 50       233 return unless -d 'lib/Alt';
108 2         6 my @files;
109 2 100   8   146 File::Find::find(sub { return unless -f; push @files, $File::Find::name }, "lib/Alt");
  8         598  
  2         64  
110 2 50       18 return unless @files;
111 2 50       7 $self->log_fatal("found too many Alt modules!") if @files > 1;
112 2         7 my $name = $files[0];
113 2         20 $name =~ s/^lib\///;
114 2         11 $name =~ s/\.pm$//;
115 2         10 $name =~ s/\//-/g;
116 2         14 $name;
117             }
118              
119             }
120              
121             1;
122              
123             __END__
124              
125             =pod
126              
127             =encoding UTF-8
128              
129             =head1 NAME
130              
131             Dist::Zilla::Plugin::Alt - Create Alt distributions with Dist::Zilla
132              
133             =head1 VERSION
134              
135             version 0.08
136              
137             =head1 SYNOPSIS
138              
139             Your dist.ini:
140              
141             [GatherDir]
142             [MakeMaker]
143             [Alt]
144              
145             =head1 DESCRIPTION
146              
147             This L<Dist::Zilla> plugin can be added to an existing dist.ini file to
148             turn your (or someone else's) distribution into an L<Alt> distribution.
149             What it does is:
150              
151             =over 4
152              
153             =item Modifies C<Makefile.PL> or C<Build.PL>
154              
155             Adds code to change the install location so that your dist won't
156             be installed unless the environment variable C<PERL_ALT_INSTALL>
157             is set to C<OVERWRITE>.
158              
159             [version 0.08]
160              
161             Will also add a diagnostic warning that will display when
162             C<Makefile.PL> or C<Build.PL> is run, with a link to L<Alt> to help
163             de-confuse those unfamiliar with the L<Alt> namespace.
164              
165             =item Updates the no_index meta
166              
167             So that only C<.pm> files in your lib directory that are in the
168             C<Alt::> namespace will be indexed.
169              
170             =item Sets the dist name property
171              
172             If the name isn't already set in your C<dist.ini> by some other
173             means, this plugin will set the name based on the Alt module.
174             If you have more than one Alt module (which would be unusual)
175             then it is an error unless you set the name by some other means.
176              
177             =back
178              
179             =head1 CAVEATS
180              
181             This plugin should appear in your C<dist.ini> toward the end, or at
182             least after your C<[GatherDir]> and C<[MakeMaker]> plugins (or equivalent).
183              
184             =head1 SEE ALSO
185              
186             =over 4
187              
188             =item L<Alt>
189              
190             =item L<Dist::Zilla>
191              
192             =back
193              
194             =head1 AUTHOR
195              
196             Graham Ollis <plicease@cpan.org>
197              
198             =head1 COPYRIGHT AND LICENSE
199              
200             This software is copyright (c) 2016 by Graham Ollis.
201              
202             This is free software; you can redistribute it and/or modify it under
203             the same terms as the Perl 5 programming language system itself.
204              
205             =cut