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