File Coverage

blib/lib/Module/Release.pm
Criterion Covered Total %
statement 376 424 88.6
branch 111 148 75.0
condition 18 31 58.0
subroutine 81 84 96.4
pod 52 52 100.0
total 638 739 86.3


line stmt bran cond sub pod time code
1 23     23   7968562 use v5.16;
  23         97  
2              
3             package Module::Release;
4              
5             =encoding utf8
6              
7             =head1 NAME
8              
9             Module::Release - Automate software releases
10              
11             =head1 SYNOPSIS
12              
13             use Module::Release;
14              
15             my $release = Module::Release->new( %params );
16              
17             # call methods to automate your release process
18             $release->check_vcs;
19             ...
20              
21             =cut
22              
23 23     23   150 use strict;
  23         98  
  23         820  
24              
25 23     23   155 use warnings;
  23         66  
  23         1616  
26 23     23   124 no warnings;
  23         49  
  23         1812  
27              
28             our $VERSION = '2.137';
29              
30 23     23   167 use Carp qw(carp croak);
  23         41  
  23         1867  
31 23     23   154 use File::Basename qw(dirname);
  23         37  
  23         1699  
32 23     23   142 use File::Spec;
  23         84  
  23         696  
33 23     23   11836 use IPC::Open3;
  23         87804  
  23         1529  
34 23     23   178 use Scalar::Util qw(blessed);
  23         81  
  23         1239  
35 23     23   127 use Symbol 'gensym';
  23         40  
  23         91812  
36              
37             my %Loaded_mixins = ( );
38              
39             =head1 DESCRIPTION
40              
41             C automates your software release process. It started as
42             a script that automated my release process, so it has bits to
43             talk to PAUSE (CPAN) and SourceForge, and to use C and
44             C. Other people have extended this in other modules under the same
45             namespace so you can use C, C, and many other things.
46              
47             The methods represent a step in the release process. Some of them check a
48             condition (e.g. all tests pass) and die if that doesn't work.
49             C doesn't let you continue if something is wrong. Once
50             you have checked everything, use the upload features to send your files
51             to the right places.
52              
53             The included C script is a good starting place. Don't be afraid to
54             edit it for your own purposes.
55              
56             The included C script is a variation on C aimed at
57             pre-release testing against multiple perl versions.
58              
59             =head2 Configuration
60              
61             C looks at several sources for configuration information.
62              
63             =head3 Perl setup
64              
65             C looks at C to get the values it needs for
66             certain operations.
67              
68             =over 4
69              
70             =item make
71              
72             The name of the program to run for the C steps
73              
74             =back
75              
76             =head3 Environment variables
77              
78             =over 4
79              
80             =item PERL
81              
82             Use this value as the perl interpreter, otherwise use the value in C<$^X>.
83              
84             =item RELEASE_DEBUG
85              
86             Do you want debugging output? Set this to a true value
87              
88             =item CPAN_PASS
89              
90             Your CPAN password. If you don't set this and you want to upload to
91             PAUSE, you should be prompted for it.
92              
93             =item CPAN_PASS_
94              
95             Append the CPAN user ID to C and use that value to interact
96             with PAUSE. This allows people to deal with more than one CPAN account
97             (for example, work and personal). If this has no value, it is used
98             preferentially to C.
99              
100             The CPAN user value comes from the C release config value.
101              
102             =back
103              
104             =head3 C<.releaserc>
105              
106             C looks for either C<.releaserc> or C in
107             the current working directory. It reads that with
108             C to get these values:
109              
110             =over 4
111              
112             =item cpan_user
113              
114             Your PAUSE user id.
115              
116             =item cpan_pass
117              
118             Your PAUSE password, but don't use this. Put it in the environment
119             variable.
120              
121             =item http_proxy
122              
123             =item https_proxy
124              
125             =item ignore_prereqs
126              
127             A whitespace separated list of modules for C to ignore.
128             But, don't use C. It was not a good idea.
129              
130             =item makefile_PL
131              
132             The name of the file to run as F. The default is
133             C<"Makefile.PL">, but you can set it to C<"Build.PL"> to use a
134             C-based system.
135              
136             If this is set to C<"Build.PL">, this his will also cause
137             C to use C instead of
138             C.
139              
140             =item makefile
141              
142             The name of the file created by C above. The default is
143             C<"Makefile">, but you can set it to C<"Build"> for
144             C-based systems.
145              
146             =item module_name
147              
148             C tries to guess the module name from the distro name, but if
149             you don't like that, set the module name in the config file.
150              
151             =back
152              
153             =head2 Methods
154              
155             If you don't like what any of these methods do, override them in a subclass.
156              
157             =over 4
158              
159             =item new()
160              
161             Create the C object. It reads the configuration
162             and initializes everything.
163              
164             =cut
165              
166             sub new {
167 19     19 1 164639 my( $class, %params ) = @_;
168              
169 19         294 my $self = bless {}, $class;
170              
171 19         138 my $config = $self->_read_configuration;
172              
173 18         97 $self->init( $config, %params );
174              
175 18         346 return $self;
176             }
177              
178              
179             =item init()
180              
181             Set up the C object. By default, it expects something
182             using MakeMaker, but if it sees a F it configures itself for
183             C.
184              
185             The values in the configuration file override any settings here, so if you
186             have both a F and a F then you can override the
187             C preference by setting the C and C
188             configuration values.
189              
190             =cut
191              
192             sub init {
193 18     18 1 62 my( $self, $config, %params ) = @_;
194              
195 18         118 $self->_set_defaults( %params );
196              
197             # $config comes in as a parameter
198 18         418 $self->_process_configuration( $config );
199              
200             # defer $self->_set_up_web_client;
201              
202 18         314 1;
203             }
204              
205 36 50   36   11776 sub _select_config_file_name { -e ".releaserc" ? ".releaserc" : "releaserc" }
206              
207             sub _set_defaults {
208 18     18   180 require Config;
209 18         6373 require IO::Null;
210              
211 18         8734 my( $self, %params ) = @_;
212              
213             my $defaults = {
214             'Makefile.PL' => 'Makefile.PL',
215             'Makefile' => 'Makefile',
216             make => $Config::Config{make},
217             manifest => 'MANIFEST',
218             debug => $ENV{RELEASE_DEBUG} || 0,
219             local_file => undef,
220             remote_file => undef,
221             input_fh => *STDIN{IO},
222             output_fh => *STDOUT{IO},
223             debug_fh => *STDERR{IO},
224 18   100     1684 null_fh => IO::Null->new(),
225             quiet => 0,
226             devnull => File::Spec->devnull,
227             ignore_prereqs => '',
228             module_name => undef,
229             %params,
230             };
231              
232 18         790 foreach my $key ( keys %$defaults ) {
233 270         596 $self->{$key} = $defaults->{$key};
234             }
235              
236 18         129 $self->set_perl( $^X );
237 18         384 $self->add_a_perl( $^X );
238              
239             # setup for Module::Build. This is a kludge. There isn't a
240             # programmatic interface to Makemaker, and I don't want to
241             # treat Makemaker and Module::Build differently. I'm stuck
242             # with a fancy shell script.
243 18 50       1058 if( -e 'Build.PL' ) {
244 0         0 $self->{'make'} = File::Spec->catfile(qw{. Build});
245 0         0 $self->{'Makefile.PL'} = 'Build.PL';
246 0         0 $self->{'Makefile'} = '_build';
247             }
248 18         1145 1;
249             }
250              
251             sub _read_configuration {
252 19     19   8564 require ConfigReader::Simple;
253              
254             # NOTE: I have to read the configuration to see if I should
255             # call the subclass, but I haven't called init yet.
256             # Don't set up anything in _read_configuration!
257 19         67766 my $self = shift;
258              
259 19         89 my $conf_file = $self->_select_config_file_name;
260              
261             # Read the configuration
262 19 100       339 $self->_die( "Could not find conf file $conf_file\n" )
263             unless -e $conf_file;
264 18         157 my $config = $self->{config} = ConfigReader::Simple->new( $conf_file );
265 18 50       3136 $self->_die( "Could not get configuration data\n" ) unless ref $config;
266              
267 18         56 $config;
268             }
269              
270             sub _process_configuration {
271 18     18   237 my $self = shift;
272              
273             # Figure out options
274 18 50       241 $self->{cpan} = $self->config->cpan_user eq '' ? 0 : 1;
275              
276             {
277 18         933 my @pairs = (
  18         260  
278             [ qw(Makefile.PL makefile_PL) ],
279             [ qw(Makefile makefile) ],
280             [ qw(make make) ],
281             [ qw(module_name module_name) ],
282             );
283              
284 18         115 foreach my $pair ( @pairs ) {
285 72         576 my( $key, $config ) = @$pair;
286              
287 72 50       208 $self->{$key} = $self->config->get($config)
288             if $self->config->exists($config);
289             }
290             }
291              
292 18         197 my @required = qw( );
293              
294 18         67 my $ok = 1;
295 18         122 for( @required ) {
296 0 0       0 unless( length $self->config->$_() ) {
297 0         0 $ok = 0;
298 0         0 $self->_warn( "Missing configuration data: $_; Aborting!\n" );
299             }
300             }
301 18 50       125 $self->_die( "Missing configuration data" ) unless $ok;
302              
303 18 50       85 if( $self->config->perls ) {
304 0         0 my @paths = split /:/, $self->config->perls;
305              
306 0         0 foreach my $path ( @paths ) {
307 0         0 $self->add_a_perl( $path );
308             }
309             }
310             }
311              
312             sub _handle_subclass {
313 0     0   0 my( $self, $subclass, %params ) = @_;
314              
315              
316             # This is a bit tricky. We have to be able to use the subclass, but
317             # we don't know if it is defined or not. It might be in a .pm file
318             # we haven't loaded, it might be in another file the user already
319             # loaded, or the user might have defined it inline inside
320             # the script. We'll try loading it if it fails can()
321 0 0       0 unless( eval { $subclass->can( 'new' ) } ) {
  0         0  
322             # I don't care if this fails because loading the file
323             # might not be the problem
324 0         0 eval { require File::Spec->catfile( split '::', $subclass ) . '.pm' };
  0         0  
325             }
326              
327             # If it's not defined by now, we're screwed and we give up
328             $self->_die( "$subclass does not have a new()!" )
329 0 0       0 unless eval { $subclass->can( 'new' ) };
  0         0  
330              
331 0         0 my $new_self = eval { $subclass->new( %params ) };
  0         0  
332 0         0 my $at = $@;
333              
334 0 0       0 return $new_self if blessed $new_self;
335              
336 0         0 $self->_die( "Could not create object with $subclass: $at!" );
337             }
338              
339              
340             =item load_mixin( MODULE )
341              
342             EXPERIMENTAL!!
343              
344             Load MODULE through require (so no importing), without caring what it does.
345             My intent is that MODULE adds methods to the C namespace
346             so a release object can see it. This should probably be some sort of
347             delegation.
348              
349             Added in 1.21
350              
351             =cut
352              
353             sub load_mixin {
354 3     3 1 7 my( $self, $module ) = @_;
355              
356 3 100       13 return 1 if $self->mixin_loaded( $module );
357              
358 2     1   3 { local $^W = 0; eval "use $module" };
  2         7  
  2         130  
  1         6  
  1         1  
  1         62  
359              
360 2 100       12 $self->_die( "Could not load [$module]! $@" ) if $@;
361              
362 1         5 ++$Loaded_mixins{ $module };
363             }
364              
365             =item loaded_mixins
366              
367             Returns a list of the loaded mixins
368              
369             Added in 1.21
370              
371             =cut
372              
373 1     1 1 4 sub loaded_mixins { keys %Loaded_mixins }
374              
375             =item mixin_loaded( MODULE )
376              
377             Returns true if the mixin class is loaded
378              
379             =cut
380              
381 7     7 1 2078 sub mixin_loaded { exists $Loaded_mixins{ $_[1] } }
382              
383             =back
384              
385             =head2 Methods for configuration and settings
386              
387             =over 4
388              
389             =item config
390              
391             Get the configuration object. By default this is a C
392             object;
393              
394             =cut
395              
396 123     123 1 14771 sub config { $_[0]->{config} }
397              
398             =item local_file( FILENAME )
399              
400             Returns or sets the name of the local distribution file. You can use
401             the literal argument C to clear the value.
402              
403             =cut
404              
405             sub local_file {
406 40 100   40 1 5641 $_[0]->{local_file} = $_[1] if @_ > 1;
407              
408 40         441 $_[0]->{local_file};
409             }
410              
411             =item remote_file
412              
413             Returns the name of the file on the remote side. You can use the
414             literal argument C to clear the value.
415              
416             =cut
417              
418             sub remote_file {
419 53 100   53 1 104 $_[0]->{remote_file} = $_[1] if @_ > 1;
420              
421 53         276 $_[0]->{remote_file};
422             }
423              
424             =back
425              
426             =head2 Methods for multiple perl testing
427              
428             =over 4
429              
430             =item set_perl
431              
432             Set the current path for the perl binary that C should
433             use for general tasks. This is not related to the list of perls used to
434             test multiple binaries unless you use one of those binaries to set a new
435             value.
436              
437             If PATH looks like a perl binary, C uses it as the new setting
438             for perl and returns the previous value.
439              
440             Added in 1.21.
441              
442             =cut
443              
444             sub set_perl {
445 20     20 1 658 my( $self, $path ) = @_;
446              
447             # resolve a path, especially on Windows, like
448             # C:\STRAWB~1\perl\bin\perl.exe
449 20 100       83 unless( my $version = $self->_looks_like_perl( $path ) ) {
450 1         36 $self->_die( "Does not look like a perl [$path]" );
451             }
452              
453 19         496 my $old_perl = $self->get_perl;
454              
455 19         196 $self->{perl} = $path;
456              
457 19         129 $old_perl;
458             }
459              
460             sub _looks_like_perl {
461 42     42   17250 my( $self, $path ) = @_;
462              
463             # resolve a path, especially on Windows, like
464             # C:\STRAWB~1\perl\bin\perl.exe
465 42 50       341 return 1 if $path =~ /\bperl.exe\z/;
466              
467 42         14442968 my $version = `$path -e "print \$\]" 2>&1`;
468              
469 42 100       2962 $version =~ m/^\d+\.[\d_]+$/ ? $version : ();
470             }
471              
472             =item get_perl
473              
474             Returns the current path for the perl binary that C should
475             use for general tasks. This is not related to the list of perls used to
476             test multiple binaries.
477              
478             Added in 1.21.
479              
480             =cut
481              
482 23     23 1 8945 sub get_perl { $_[0]->{perl} }
483              
484             =item perls()
485              
486             Return the list of perl binaries Module::Release will use to test the
487             distribution.
488              
489             Added in 1.21.
490              
491             =cut
492              
493             sub perls {
494 9     9 1 2432 my $self = shift;
495              
496 9         25 my @perls = keys %{$self->{perls}};
  9         45  
497 9         116 $self->_debug( "perls at the start [@perls]" );
498              
499             # Sort them
500             @perls =
501 6         21 map { $_->[0] }
502 0 0 0     0 sort { $a->[2] <=> $b->[2] || $a->[3] <=> $b->[3] || $a->[0] cmp $b->[0] }
503 6         31 map { [ $_->[0], ( $_->[1] =~ m/(perl5\.(?|([0-9]{3})_?([0-9]{2})|([0-9]{1,2})\.([0-9]+)))/) ] }
504 6         73 map { [ $_, (m{.*/(.*)}) ] }
505 9         37 grep { -x $_ }
  6         181  
506             @perls;
507              
508 9         55 $self->_debug( "perls after filtering [@perls]" );
509 9         49 $self->_debug( "Testing with " . @perls . " perls" );
510              
511 9         47 return @perls;
512             }
513              
514             =item add_a_perl( PATH )
515              
516             Add a perl binary to the list of perls to use for testing. If PATH
517             is not executable or cannot run C, this method returns
518             nothing and does not add PATH. Otherwise, it returns true. If the
519             same path was already in the list, it returns true but does not
520             create a duplicate.
521              
522             Added in 1.21.
523              
524             =cut
525              
526             sub add_a_perl {
527 22     22 1 8299 my( $self, $path ) = @_;
528              
529 22 100       309 return 1 if exists $self->{perls}{$path};
530              
531 21 100       1431 unless( -x $path ) {
532 1 50 33     15 if( $path =~ m/[*?[]/ && $self->config->allow_glob_in_perls ) {
533 0         0 $self->add_a_perl( $_ ) for glob $path;
534             }
535             else {
536 1         13 $self->_warn( "$path is not executable" );
537             }
538 1         9 return;
539             }
540              
541 20         200 my $version = $self->_looks_like_perl( $path );
542              
543 20 100       514 unless( $version ) {
544 1         106 $self->_warn( "$path does not appear to be perl!" );
545 1         17 return;
546             }
547              
548 19         535 return $self->{perls}{$path} = $version;
549             }
550              
551             =item remove_a_perl( PATH )
552              
553             Delete PATH from the list of perls used for testing
554              
555             Added in 1.21.
556              
557             =cut
558              
559             sub remove_a_perl {
560 2     2 1 4374 my( $self, $path ) = @_;
561              
562 2         13 return delete $self->{perls}{$path}
563             }
564              
565             =item reset_perls
566              
567             Reset the list of perl interpreters to just the one running C.
568              
569             Added in 1.21.
570              
571             =cut
572              
573             sub reset_perls {
574 1     1 1 968 my $self = shift;
575              
576 1         7 $self->{perls} = {};
577              
578 1         5 return $self->{perls}{$^X} = $];
579             }
580              
581              
582             =item input_fh
583              
584             Return the value of input_fh.
585              
586             =cut
587              
588             sub input_fh {
589 1     1 1 4 return $_[0]->{input_fh};
590             }
591              
592             =item output_fh
593              
594             If quiet is off, return the value of output_fh. If output_fh is not
595             set, return STDOUT. If quiet is on, return the value of null_fh.
596              
597             =cut
598              
599             sub output_fh {
600             $_[0]->quiet
601             ?
602             $_[0]->null_fh
603             :
604             ( $_[0]->{output_fh} || *STDOUT{IO} )
605 94 100 100 94 1 287 }
606              
607             =item null_fh
608              
609             Return the null filehandle. So far that's something set up in C and I
610             haven't provided a way to set it. Any subclass can make their C
611             return whatever they like.
612              
613             =cut
614              
615             sub null_fh {
616 132     132 1 7034 require IO::Null;
617 132   66     7323 $_[0]->{null_fh} //= IO::Null->new;
618             }
619              
620             =item quiet
621              
622             Get the value of quiet mode (true or false).
623              
624             =item turn_quiet_on
625              
626             Turn on quiet mode
627              
628             =item turn_quiet_off
629              
630             Turn off quiet mode
631              
632             =cut
633              
634 11     11 1 9822 sub turn_quiet_on { $_[0]->{quiet} = 1 }
635 13     13 1 45153 sub turn_quiet_off { $_[0]->{quiet} = 0 }
636              
637 105     105 1 7345 sub quiet { $_[0]->{quiet} }
638              
639             =item debug
640              
641             Get the value of the debugging flag (true or false).
642              
643             =item turn_debug_on
644              
645             Turn on debugging
646              
647             =item turn_debug_off
648              
649             Turn off debugging
650              
651             =item debug_fh
652              
653             If debugging is on, return the value of debug_fh. If debug_fh is not
654             set, return STDERR. If debugging is off, return the value of null_fh.
655              
656             =cut
657              
658 7     7 1 4164 sub turn_debug_on { $_[0]->{debug} = 1 }
659 5     5 1 1185 sub turn_debug_off { $_[0]->{debug} = 0 }
660              
661 117     117 1 5616 sub debug { $_[0]->{debug} }
662              
663             sub debug_fh {
664 110 100   110 1 1592 if( $_[0]->debug ) {
665             $_[0]->{debug_fh} || *STDERR{IO}
666 18 100       105 }
667             else {
668 92         1507 $_[0]->null_fh
669             }
670             }
671              
672             =back
673              
674             =head2 Methods for building
675              
676             =over 4
677              
678             =item clean
679              
680             Run `make realclean`
681              
682             =cut
683              
684             sub clean {
685 3     3 1 14433 my $self = shift;
686 3         30 $self->_print( "Cleaning directory... " );
687              
688 3 100       66 unless( -e $self->{Makefile} ) {
689 2         15 $self->_print( " no $self->{Makefile}---skipping\n" );
690 2         17 return;
691             }
692              
693 1         12 $self->run( "$self->{make} realclean 2>&1" );
694              
695 1         6 $self->_print( "done\n" );
696             }
697              
698             =item distclean
699              
700             Run `make distclean`
701              
702             =cut
703              
704             sub distclean {
705 3     3 1 11468 my $self = shift;
706 3         20 $self->_print( "Cleaning directory... " );
707              
708 3 100       66 unless( -e $self->{Makefile} ) {
709 2         16 $self->_print( " no $self->{Makefile}---skipping\n" );
710 2         22 return;
711             }
712              
713 1         12 $self->run( "$self->{make} distclean 2>&1" );
714              
715 1         6 $self->_print( "done\n" );
716             }
717              
718              
719             =item build_makefile()
720              
721             Runs `perl Makefile.PL 2>&1`.
722              
723             This step ensures that we start off fresh and pick up any changes in
724             C.
725              
726             =cut
727              
728             sub build_makefile {
729 9     9 1 17557 my $self = shift;
730 9         36 $self->_print( "Recreating make file... " );
731              
732 9 100       211 unless( -e $self->{'Makefile.PL'} ) {
733 2         63 $self->_print( " no $self->{'Makefile.PL'}---skipping\n" );
734 2         14 return;
735             }
736              
737 7         105 $self->run( "$self->{perl} $self->{'Makefile.PL'} 2>&1 0<&-" );
738              
739 7         40 $self->_print( "done\n" );
740             }
741              
742             =item make()
743              
744             Run a plain old `make`.
745              
746             =cut
747              
748             sub make {
749 3     3 1 11062 my $self = shift;
750 3         17 $self->_print( "Running make... " );
751              
752 3 100       62 unless( -e $self->{'Makefile'} )
753             {
754 2         15 $self->_print( " no $self->{'Makefile'}---skipping\n" );
755 2         594 return;
756             }
757              
758 1         13 my $tests = $self->run( "$self->{make} 2>&1" );
759              
760 1         8 $self->_print( "done\n" );
761             }
762              
763             =item test()
764              
765             Run `make test`. If any tests fail, it dies.
766              
767             =cut
768              
769             sub test {
770 5     5 1 11063 my $self = shift;
771 5         24 $self->_print( "Checking make test... " );
772              
773 5 100       122 unless( -e $self->{'Makefile'} ) {
774 2         15 $self->_print( " no $self->{'Makefile'}---skipping\n" );
775 2         16 return;
776             }
777              
778 3         25 my $tests = $self->run( "$self->{make} test 2>&1" );
779              
780 3 100       76 unless ($tests =~ m/All tests successful/) {
781 2 50       12 if( $self->debug ) { # from H.Merijn Brand
782 0         0 my $prove = File::Spec->catfile(
783             dirname( $self->get_perl ),
784             'prove'
785             );
786              
787 0 0       0 if( -x $prove ) {
    0          
788             my $prove_out =
789             join "\n\n",
790 0         0 map { scalar qx"$prove -wvb $_" }
  0         0  
791             ($tests =~ m{^(t/\w+\.t)\s+[0-9]+}gm);
792 0         0 $prove_out =~ s/^.*\r//gm;
793 0         0 $self->_warn( $prove_out );
794             }
795             elsif( $self->debug ) {
796 0         0 $self->_print( "prove [$prove] was not executable!" );
797             }
798             }
799              
800 2         11 $self->_die( "\nERROR: Tests failed!\n$tests\n\nAborting release\n" )
801             }
802              
803 1         4 $self->_print( "all tests pass\n" );
804             }
805              
806             =item dist()
807              
808             Run `make dist`. As a side effect determines the distribution
809             name if not set on the command line.
810              
811             =cut
812              
813             sub dist {
814 6     6 1 5179 my $self = shift;
815 6         35 $self->_print( "Making dist...\n" );
816              
817 6         46 $self->build_makefile;
818              
819 6         43 my $messages = $self->run( "$self->{make} dist 2>&1 < $self->{devnull}" );
820 6         72 $self->_debug( "messages are [$messages]\n" );
821              
822             # If the distro isn't already set, try to guess it
823 6 100       21 unless( $self->local_file ) {
824 4         14 $self->_debug( ", guessing local distribution name\n" );
825 4         44 my( $guess ) = $messages =~ /(?:\s|')(\S+\.tar)/;
826 4         20 $self->_debug( "guessed [$guess]\n" );
827 4         15 $self->local_file( $guess );
828              
829 4 100       47 $self->_die( "Couldn't guess distname from dist output\n" )
830             unless $self->local_file;
831              
832 2         9 $self->local_file( $self->local_file() . '.gz' );
833 2         17 $self->remote_file( $self->local_file );
834             }
835              
836             # local_file should exist now
837 4 100       10 $self->_die( "Local file '$self->{local_file}' does not exist\n" )
838             unless -f $self->local_file;
839              
840 1         6 $self->_print( "Done making dist\n" );
841             }
842              
843             =item disttest
844              
845             Run `make disttest`. If the tests fail, it dies.
846              
847             =cut
848              
849             sub disttest {
850 6     6 1 11363 my $self = shift;
851              
852 6         33 $self->_print( "Checking make disttest... " );
853              
854 6 100       154 unless( -e $self->{'Makefile'} ) {
855 3         26 $self->_print( " no $self->{'Makefile'}---skipping\n" );
856 3         22 return;
857             }
858              
859 3         22 my $tests = $self->run( "$self->{make} disttest 2>&1" );
860              
861 3 100       100 $self->_die( "\nERROR: Tests failed!\n$tests\n\nAborting release\n" )
862             unless $tests =~ /All tests successful/;
863              
864 1         4 $self->_print( "all tests pass\n" );
865             }
866              
867             =item dist_test
868              
869             This was the old name for the method, but was inconsistent with
870             other method names. It still works, but is deprecated and will
871             give a warning.
872              
873             =cut
874              
875             sub dist_test {
876 1     1 1 14400 $_[0]->_warn( "dist_test is deprecated. Use disttest instead." );
877              
878 1         12 goto &disttest;
879             }
880              
881             =item dist_version
882              
883             Return the distribution version ( set in dist() )
884              
885             =cut
886              
887             sub _parse_version {
888 18     18   23 my( $self ) = @_;
889 23     23   196 no warnings 'uninitialized';
  23         58  
  23         8303  
890              
891 18 100       66 $self->_die( "Can't get dist_version! It's not set (did you run dist first?)" )
892             unless defined $self->remote_file;
893              
894 17 100       25 my( $version_str, $vee, $version, $dev ) = $self->remote_file
895             =~ / ( (v?) ([\d.]+) (?: _ (\d+) )? ) (?:\. (?: tar \. gz | zip ) )? $/xi
896             or return { components => [], string => '', multipart => 0 };
897              
898 15         44 my @components = split /[.]/, $version;
899 15         18 my $count = @components;
900              
901 15   100     44 my $multipart = length($vee) || ($count > 2);
902              
903 15         101 my $hash = {
904             components => \@components,
905             count => $count,
906             string => $version_str,
907             vee => lc $vee,
908             version => $version,
909             v_version => lc($vee) . $version,
910             dev => $dev,
911             multipart => $multipart,
912             };
913              
914 23     23   14376 $self->_debug( Dumper( $self->remote_file, $hash ) ); use Data::Dumper;
  23         202016  
  23         10152  
  15         28  
915              
916 15         47 return $hash;
917             }
918              
919             sub dist_version {
920 18     18 1 23620 my( $self ) = @_;
921              
922 18         46 my $v = $self->_parse_version;
923              
924 17 100       82 if( $v->{multipart} ) {
    100          
    100          
925 9         18 $self->_debug( "Choosing multipart version" );
926             # This is a multi-part version
927             # We assume that version.pm is available if multi-part
928             # versions are in use.
929             eval {
930 9         30 require version;
931             }
932 9 50       12 or do { # Fall back to using $version_str verbatim
933 0         0 warn $@;
934 0         0 return $v->{version_str};
935             };
936              
937             # There are pre- and post-0.77 versions of version.pm.
938             # The former are deprecated, but I assume we must
939             # gracefully use what we have available.
940 9 50       20 eval {
941 9         15 my $string = $v->{v_version};
942 9 50       147 $v->{version} = version->VERSION >= 0.77 ?
943             version->parse ($string)->normal : # latest and best
944             ''.version->new($string) ; # legacy
945 9         33 1;
946             }
947             or
948             $self->_die( "Couldn't parse version '$v->{string}' from '".
949             $self->remote_file. "': $@");
950              
951 9         38 return $v->{version};
952             }
953             elsif( $v->{count} == 1 ) {
954 1         3 $self->_debug( "Choosing single component version" );
955             # some versions might be a single number, such as those
956             # that use dates as integers with no dot.
957 1         6 return $v->{components}[0];
958             }
959             elsif( $v->{string} =~ /(\d+) \. (\d+)(_\d+)? $/x ) {
960 5         12 $self->_debug( "Choosing major.minor_dev? version" );
961             # Else, use the older implementation for backward-compatibility
962             # Note the lack of an initial ^ matcher is deliberate.
963 5         32 my( $major, $minor, $dev ) = ( $1, $2, $3 );
964 5         16 return $self->dist_version_format( $major, $minor, $dev );
965             }
966             else {
967 2         6 $self->_debug( "Unhandled version" );
968 2         6 return '';
969             }
970             }
971              
972             =item dist_version_format
973              
974             Return the distribution version ( set in dist() )
975              
976             # XXX make this configurable
977              
978             =cut
979              
980             sub dist_version_format {
981 23     23   207 no warnings 'uninitialized';
  23         44  
  23         60768  
982 7     7 1 3529 my $self = shift;
983 7         65 my( $major, $minor, $dev ) = @_;
984              
985 7         62 sprintf "%s.%s%s", $major, $minor, $dev;
986             }
987              
988             =item module_name
989              
990             Returns the module name. This either takes it from the config file
991             or tries to guess it from the distro name.
992              
993             =cut
994              
995             sub module_name {
996 0     0 1 0 my $self = shift;
997 0 0       0 return $self->{module_name} if $self->{module_name};
998              
999 0         0 my $name = $self->local_file;
1000 0         0 $self->_debug( "Guessing name. Local file is <$name>\n" );
1001              
1002 0         0 $name =~ s/-\d.*//g;
1003 0         0 $name =~ s/-/::/g;
1004 0         0 $self->_debug( "Guessing name. Module name is <$name>\n" );
1005              
1006 0         0 $self->{module_name} = $name;
1007             }
1008              
1009             =item check_manifest
1010              
1011             Run `make manifest` and report anything it finds. If it gives output,
1012             die. You should check C to ensure it has the things it needs.
1013             If files that shouldn't show up do, put them in MANIFEST.SKIP.
1014              
1015             Since `make manifest` takes care of things for you, you might just have
1016             to re-run your release script.
1017              
1018             =cut
1019              
1020             # _check_output_lines - for command output with one message per line.
1021             # The message hash identifies the first part of the line and serves
1022             # as a category for the message. If a line doesn't matter, don't put
1023             # it's pattern in the message hash.
1024             #
1025             # Prints a summary of what it found. The message is the hash value
1026             # for that output type.
1027             #
1028             # returns the number of interesting things it found, but that's it.
1029             sub _check_output_lines {
1030 2     2   5 my $self = shift;
1031 2         7 my( $message_hash, $message ) = @_;
1032              
1033 2         6 my %state;
1034 2         9 foreach my $state ( keys %$message_hash ) {
1035 4         254 $state{$state} = [ $message =~ /^\Q$state\E\s+(.+)/gm ];
1036             }
1037              
1038 2         6 my $rule = "-" x 50;
1039 2         5 my $count = 0;
1040              
1041 2         10 foreach my $key ( sort keys %state ) {
1042 4         8 my $list = $state{$key};
1043 4 100       14 next unless @$list;
1044              
1045 2         4 $count += @$list;
1046              
1047 2         5 local $" = "\n\t";
1048 2         11 $self->_print( "\n\t$message_hash->{$key}\n\t$rule\n\t@$list\n" );
1049             }
1050              
1051 2         9 return $count;
1052             }
1053              
1054             sub check_manifest {
1055 2     2 1 10058 my $self = shift;
1056              
1057 2         18 $self->_print( "Checking state of MANIFEST... " );
1058              
1059 2         17 my $manifest = $self->run( "$self->{make} manifest 2>&1" );
1060              
1061 2         22 my %message = (
1062             "Removed from MANIFEST:" => 'These files were removed from MANIFEST',
1063             "Added to MANIFEST:" => 'These files were added to MANIFEST',
1064             );
1065              
1066 2         12 my $count = $self->_check_output_lines( \%message, $manifest );
1067              
1068 2 100       11 $self->_die( "\nERROR: Manifest was not up-to-date ($count files).\n" )
1069             if $count;
1070              
1071 1         4 $self->_print( "MANIFEST up-to-date\n" );
1072             }
1073              
1074             =item manifest_name
1075              
1076             Return the name of the manifest file, probably F.
1077              
1078             =item manifest
1079              
1080             This is the old name for manifest_name. It still works but is
1081             deprecated.
1082              
1083             =cut
1084              
1085 7     7 1 4600 sub manifest_name { 'MANIFEST' }
1086              
1087             sub manifest {
1088 1     1 1 2661 $_[0]->_warn( "manifest is deprecated. Use manifest_name" );
1089 1         7 &manifest_name
1090             }
1091              
1092             =item files_in_manifest
1093              
1094             Return the filenames in the manifest file as a list.
1095              
1096             =cut
1097              
1098             sub files_in_manifest {
1099 3     3 1 770 my $self = shift;
1100              
1101 3         928 require ExtUtils::Manifest;
1102              
1103             # I want to use ExtUtils::Manifest so it automatically
1104             # follows the right MANIFEST rules, but I have to adapt
1105             # it's output to my output. Annoying, for sure.
1106 3         13065 my $hash = do {
1107             local $SIG{'__WARN__'} = sub {
1108 1     1   78 my $message = shift;
1109 1 50       9 if( $message =~ m/Debug: (.*)/ ) {
1110 0         0 $self->_debug( $1 );
1111             }
1112             else {
1113 1         12 $self->_die( "files_in_manifest: could not open file\n" );
1114             }
1115 3         37 };
1116              
1117 3         17 ExtUtils::Manifest::maniread( $self->manifest_name );
1118             };
1119              
1120 2         431 sort keys %$hash;
1121             }
1122              
1123             =item check_vcs
1124              
1125             =item vcs_tag
1126              
1127             =item make_vcs_tag
1128              
1129             Note: these methods were formerly "cvs", but are now "vcs" for
1130             Version Control System.
1131              
1132             This is a placeholder method which should be implemented in a mixin
1133             module. Try installing Module::Release::CVS, Module::Release::SVN,
1134             or Module::Release::Git and then loading them in your script. The
1135             default C script does this for you by checking for the
1136             special directories for those source systems.
1137              
1138             Previous to version 1.24, these methods were implemented in this
1139             module to support CVS. They are now in Module::Release::CVS as a
1140             separate module.
1141              
1142             =cut
1143              
1144             sub check_vcs {
1145 1     1 1 3349 $_[0]->_die( "check_vcs must be implemented in a mixin class" );
1146             }
1147              
1148              
1149             sub vcs_tag {
1150 1     1 1 347 $_[0]->_die( "vcs_tag must be implemented in a mixin class" );
1151             }
1152              
1153             sub make_vcs_tag {
1154 1     1 1 322 $_[0]->_die( "make_vcs_tag must be implemented in a mixin class" );
1155             }
1156              
1157             =item touch( FILES )
1158              
1159             Set the modification times of each file in FILES to the current time. It
1160             tries to open the file for writing and immediately closing it, as well as
1161             using utime. It checks that the access and modification times were
1162             updated.
1163              
1164             Returns the number of files which it successfully touched.
1165              
1166             =cut
1167              
1168             sub touch {
1169 4     4 1 40234 my( $self, @files ) = @_;
1170              
1171 4         13 my $time = time;
1172              
1173 4         11 my $count = 0;
1174 4         27 foreach my $file ( @files ) {
1175 11 100       186 unless( -f $file ) {
1176 1         13 $self->_warn( "$file is not a plain file" );
1177 1         5 next;
1178             }
1179              
1180 10 50       323 open my( $fh ), ">>", $file
1181             or $self->_warn( "Could not open file [$file] for writing: $!" );
1182 10         111 close $file;
1183              
1184 10         114 utime( $time, $time, $file );
1185              
1186             # check that it actually worked
1187 10 100       106 unless( 2 == grep { $_ == $time } (stat $file)[8,9] ) {
  20         105  
1188 3         23 $self->_warn( "$file did not set utimes." );
1189 3         60 next;
1190             }
1191              
1192 7         79 $count++;
1193             }
1194              
1195 4         24 $count;
1196             }
1197              
1198             =item touch_all_in_manifest
1199              
1200             Runs touch on all of the files in MANIFEST.
1201              
1202             =cut
1203              
1204 1     1 1 23988 sub touch_all_in_manifest { $_[0]->touch( $_[0]->files_in_manifest ) }
1205              
1206             =back
1207              
1208             =head2 Methods for uploading
1209              
1210             =over 4
1211              
1212             =item should_upload_to_pause
1213              
1214             Should I upload to PAUSE? If C and C are set,
1215             go for it.
1216              
1217             =cut
1218              
1219             sub should_upload_to_pause {
1220 0     0 1 0 $_[0]->_debug( "Checking if I should upload\n" );
1221 0   0     0 my $answer = !!( $_[0]->config->cpan_user && $_[0]->config->cpan_pass );
1222 0         0 $_[0]->_debug( "The answer is [$answer]\n" );
1223 0         0 $answer;
1224             }
1225              
1226             =item check_for_passwords
1227              
1228             Get passwords for CPAN.
1229              
1230             =cut
1231              
1232             sub check_for_passwords {
1233 4     4 1 12592 my $id = $_[0]->config->cpan_user;
1234 4 100       90 unless( defined $id ) {
1235 2         15 $_[0]->_debug( "cpan_user is not set. Not looking for password\n" );
1236 2         15 return;
1237             }
1238              
1239 2         8 my $per_user_env_key = uc( "CPAN_PASS_$id" );
1240 2 100 66     27 if( exists $ENV{$per_user_env_key} and length $ENV{$per_user_env_key} ) {
    50 33        
1241 1         7 $_[0]->_debug( "Found env $per_user_env_key\n" );
1242 1         13 $_[0]->config->set( 'cpan_pass', $ENV{$per_user_env_key} );
1243             }
1244             elsif( my $pass = $_[0]->config->cpan_user && $_[0]->get_env_var( "CPAN_PASS" ) ) {
1245 1         9 $_[0]->_debug( "Used get_env_var to get CPAN_PASS\n" );
1246 1         12 $_[0]->config->set( 'cpan_pass', $pass );
1247             }
1248              
1249 2         32 $_[0]->_debug( "CPAN pass is " . $_[0]->config->cpan_pass . "\n" );
1250             }
1251              
1252             =item get_changes()
1253              
1254             Read and parse the F file. This is pretty specific, so
1255             you may well want to overload it.
1256              
1257             =cut
1258              
1259             sub get_changes {
1260 3 100   3 1 396 open my $fh, '<:encoding(UTF-8)', 'Changes' or return '';
1261              
1262 2         361 my $data = <$fh>; # get first line
1263              
1264 2         26 while( <$fh> ) {
1265 4 100       21 last if /^\S/;
1266 2         7 $data .= $_;
1267             }
1268              
1269 2         84 return $data;
1270             }
1271              
1272             =item show_recent_contributors()
1273              
1274             Show recent contributors before creating/extending Changes.
1275              
1276             This output relies upon the method C having been
1277             implemented in the relevant mixin for your version control system.
1278              
1279             =cut
1280              
1281             sub show_recent_contributors {
1282 2     2 1 6161 my $self = shift;
1283 2 100       22 return unless $self->can( 'get_recent_contributors' );
1284 1         17 my @contributors = $self->get_recent_contributors;
1285 1 50       29 $self->_print("Contributors since last release:\n") if @contributors;
1286 1         10 $self->_print( "\t", $_, "\n" ) for @contributors;
1287             }
1288              
1289             =item get_release_date()
1290              
1291             Return a string representing the current date and time (in UTC) in the
1292             L format so that it can be added directly to the
1293             Changes file.
1294              
1295             =cut
1296              
1297             sub get_release_date {
1298 2     2 1 2898 state $rc = require Time::Piece;
1299 2         14743 return Time::Piece->gmtime->datetime . 'Z';
1300             }
1301              
1302             =item run
1303              
1304             Run a command in the shell.
1305              
1306             =item run_error
1307              
1308             Returns true if the command ran successfully, and false otherwise. Use
1309             this function in any other method that calls run to figure out what to
1310             do when a command doesn't work. You may want to handle that yourself.
1311              
1312             =cut
1313              
1314 6     6   20 sub _run_error_reset { $_[0]->{_run_error} = 0 }
1315 2     2   24 sub _run_error_set { $_[0]->{_run_error} = 1 }
1316 3     3 1 6881 sub run_error { $_[0]->{_run_error} }
1317              
1318             sub run {
1319 5     5 1 53334 my( $self, @command ) = @_;
1320 5         15 @command = grep { defined } @command;
  4         17  
1321              
1322 5         21 $self->_run_error_reset;
1323              
1324 5         37 $self->_debug( "@command\n" );
1325 5 100       28 $self->_die( "Didn't get a command!" ) unless @command;
1326              
1327 3         36 my $pid = IPC::Open3::open3(
1328             my $child_in, my $child_out, my $child_err = gensym,
1329             @command
1330             );
1331 2         16801 close $child_in;
1332              
1333 2         51 $child_out->autoflush;
1334              
1335             #open my($fh), "-|", "$command" or $self->_die( "Could not open command [$command]: $!" );
1336             #$fh->autoflush;
1337              
1338 2         317 my $output = '';
1339 2         9 my $error = '';
1340 2         8 my $buffer = '';
1341 2         29 local $| = 1;
1342              
1343 2 50       29 my $readlen = $self->debug ? 1 : 256;
1344              
1345 2         930005 while( read $child_out, $buffer, $readlen ) {
1346 1         47 $self->_debug( $_, $buffer );
1347 1         10 $output .= $buffer;
1348             }
1349              
1350 2         115 while( read $child_err, $buffer, $readlen ) {
1351 0         0 $self->_debug( $_, $buffer );
1352 0         0 $error .= $buffer;
1353             }
1354              
1355 2 50       16 if( $error =~ m/exec of .*? failed| Windows/x ) {
1356 0         0 $self->_warn( "Could not run <$command[0]>: $error" );
1357             }
1358 2         23 $self->_debug( $self->_dashes, "\n" );
1359              
1360 2         88 waitpid( $pid, 0 );
1361 2         26 my $child_exit_status = $? >> 8;
1362              
1363 2 50       15 $self->_warn( $error ) if length $error;
1364              
1365 2 100       23 if( $child_exit_status ) {
1366 1         10 $self->_run_error_set;
1367 1         14 $self->_warn( "Command [$command[0]] didn't close cleanly: $child_exit_status" );
1368             }
1369              
1370 2         155 return $output;
1371             }
1372              
1373             =item get_env_var
1374              
1375             Get an environment variable or prompt for it
1376              
1377             =cut
1378              
1379             sub get_env_var {
1380 6     6 1 12840 my ($self, $field) = @_;
1381              
1382             # Check for an explicit argument passed
1383 6 100       70 return $self->{lc $field} if defined $self->{lc $field};
1384              
1385 5         18 my $pass = $ENV{$field};
1386              
1387 5 100 100     44 return $pass if defined( $pass ) && length( $pass );
1388 3 50       13 return if $field =~ m/\ACPAN_PASS_/;
1389              
1390 3         16 $self->_print( "$field is not set. Enter it now: " );
1391 3 100       32 if ($field eq 'CPAN_PASS') {
1392             # don't echo passwords to the screen
1393 1         804 require Term::ReadKey;
1394 1         4371 local $| = 1;
1395 1         5 Term::ReadKey::ReadMode('noecho');
1396 1         39 $pass = $self->_slurp;
1397 1         6 Term::ReadKey::ReadMode('restore');
1398             }
1399             else {
1400 2         14 $pass = $self->_slurp;
1401             }
1402 3         37 chomp $pass;
1403              
1404 3 100 66     28 return $pass if defined( $pass ) && length( $pass );
1405              
1406 1         12 $self->_debug( "$field not supplied. Aborting...\n" );
1407             }
1408              
1409             =back
1410              
1411             =head2 Methods for developers
1412              
1413             =over
1414              
1415             =item _print( LIST )
1416              
1417             Send the LIST to whatever is in output_fh, or to STDOUT. If you set
1418             output_fh to a null filehandle, output goes nowhere.
1419              
1420             =cut
1421              
1422 74     74   3364 sub _print { print { $_[0]->output_fh } @_[1..$#_] }
  74         288  
1423              
1424             =item _slurp
1425              
1426             Read a line from whatever is in input_fh and return it.
1427              
1428             =cut
1429              
1430             sub _slurp {
1431 1     1   6741 my $fh = $_[0]->input_fh;
1432 1         25 return <$fh>;
1433             }
1434              
1435             =item _dashes()
1436              
1437             Use this for a string representing a line in the output. Since it's a
1438             method you can override it if you like.
1439              
1440             =cut
1441              
1442 3     3   1148 sub _dashes { "-" x 73 }
1443              
1444             =item _debug( LIST )
1445              
1446             Send the LIST to whatever is in debug_fh, or to STDERR. If you aren't
1447             debugging, debug_fh should return a null filehandle.
1448              
1449             =cut
1450 23     23   254 use Carp qw(carp);
  23         42  
  23         4702  
1451             sub _debug {
1452 90     90   4182 my $self = shift;
1453              
1454 90         332 my $debug_fh = $self->debug_fh;
1455              
1456 90         268 print { $debug_fh } @_;
  90         640  
1457             }
1458              
1459              
1460             #eval { print { shift->debug_fh } @_; 1 } or carp "Failure in _debug: [@_] [$@]" }
1461              
1462             =item _die( LIST )
1463              
1464             =cut
1465              
1466 22     22   16074 sub _die { croak @_[1..$#_] }
1467              
1468             =item _warn( LIST )
1469              
1470             =cut
1471              
1472 11 100   11   2025 sub _warn { carp @_[1..$#_] unless $_[0]->quiet }
1473              
1474             =back
1475              
1476             =head1 TO DO
1477              
1478             * What happened to my Changes munging?
1479              
1480             =head1 CREDITS
1481              
1482             Ken Williams turned my initial release(1) script into the present
1483             module form.
1484              
1485             Andy Lester handled the maintenance while I was on my Big Camping
1486             Trip. He applied patches from many authors.
1487              
1488             Andreas König suggested changes to make it work better with PAUSE.
1489              
1490             Chris Nandor helped with figuring out the broken SourceForge stuff.
1491              
1492             H.Merijn Brand has contributed many patches and features.
1493              
1494             =head1 SOURCE AVAILABILITY
1495              
1496             This source is in GitHub
1497              
1498             https://github.com/briandfoy/module-release
1499              
1500             =head1 AUTHOR
1501              
1502             brian d foy, C<< >>
1503              
1504             =head1 COPYRIGHT AND LICENSE
1505              
1506             Copyright © 2007-2025, brian d foy C<< >>. All rights reserved.
1507              
1508             This program is free software; you can redistribute it and/or modify
1509             it under the Artistic License 2.0.
1510              
1511             =cut
1512              
1513             1;