File Coverage

blib/lib/Dist/Zilla/Plugin/Alt.pm
Criterion Covered Total %
statement 55 58 94.8
branch 10 16 62.5
condition 4 8 50.0
subroutine 11 11 100.0
pod 0 3 0.0
total 80 96 83.3


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