build.pl untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 23 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 191 CC 4 SCAR 15.5
CC
4
Cov
0%
version_current
12.0
version_next
6.0
version_bump
2.0
CRAP 4.7
1 #!/usr/bin/env perl
2  
3 use Data::Dumper;
4 use File::Slurp;
5 use strict;
6  
7 ### Handle version tasks if asked
8  
9 for my $arg (@ARGV) {
10 version_bump() and last if $arg eq '--bump';
11 if ( $arg eq '--retag' ) { version_retag(); exit; }
12 if ( $arg eq '--version' ) { print version_current(), "\n"; exit; }
13 }
14  
15 ### Validate we have what we need to build
16  
17 # These will be necessary for Make Maker to make the module with proper files.
18 require CPAN::Meta;
19 require CPAN::Meta::Converter;
20 require CPAN::Meta::YAML;
21 require ExtUtils::MakeMaker;
22  
23 print "Using ExtUtils::MakeMaker ($ExtUtils::MakeMaker::VERSION)\n";
24 print "Using CPAN::Meta ($CPAN::Meta::VERSION)\n";
25  
26 die "You need a modern version of ExtUtils::MakeMaker." unless $ExtUtils::MakeMaker::VERSION > 6;
27  
28 ### Read the config.txt and try to process it
29  
30 die "No config.txt present." unless -f 'config.txt';
31  
32 my ($module, $author, $license, $abstract, $description, $perl_ver, %requires);
33  
34 my $text = read_file('config.txt') ;
35 1 while chomp $text;
36 die "No config data." unless length $text;
37  
38 eval $text;
39 die $@ if $@;
40  
41 die "Bad config." unless $module && $author && $license &&
42 $abstract && $description && $perl_ver && %requires;
43  
44 ### Post config
45  
46 my $path_chunk = $module;
47 $path_chunk =~ s/::/-/g;
48  
49 my $bug = 'https://rt.cpan.org/Dist/Display.html?Name='.$path_chunk;
50 my $repo = 'http://github.com/bennie/perl-' . $path_chunk;
51 my $git = 'git://github.com/bennie/perl-'.$path_chunk.'.git';
52  
53 my $sourcefile = 'lib/' . $module . '.pm';
54 $sourcefile =~ s/::/\//g;
55  
56 my $require_text = Dumper(\%requires);
57 $require_text =~ s/\$VAR1 = //;
58 $require_text =~ s/;$//;
59 1 while chomp($require_text);
60  
61 ### External data
62  
63 my $version = version_current();
64 my $date = `date '+%Y/%m/%d'`;
65 my $year = `date '+%Y'`;
66 my $distdir = $path_chunk .'-' . $version;
67  
68 chomp $date;
69 chomp $year;
70 chomp $distdir;
71  
72 print "
73 Version : $version
74 Date : $date
75 Year : $year
76 Dist : $distdir
77  
78 ";
79  
80 ### Figure out the provides
81  
82 my %provides; my $provides;
83  
84 for my $file ( `find lib -type f -name "*.pm"` ) {
85 chomp $file;
86 die "Can't figure out what is provided." unless $file =~ /^lib\/(.+).pm$/;
87 my $name = $1;
88 $name =~ s/\//::/g;
89 $provides{$name} = { file => $file, version => $version };
90 }
91  
92 $provides = Dumper(\%provides);
93 $provides =~ s/\$VAR1 = //;
94 $provides =~ s/;$//;
95 1 while chomp($provides);
96  
97  
98 ### Write Makefile.PL
99  
100 open MAKEFILE, '>', 'Makefile.PL';
101  
102 print MAKEFILE "use ExtUtils::MakeMaker;
103  
104 WriteMakefile(
105 ABSTRACT => \"$abstract\",
106 AUTHOR => '$author',
107 LICENSE => '$license',
108 NAME => '$module',
109 VERSION => '$version',
110  
111 PREREQ_PM => $require_text,
112  
113 ( \$ExtUtils::MakeMaker::VERSION < 6.46
114 ? ()
115 : ( META_MERGE => {
116 'meta-spec' => { version => 2 },
117 no_index => {directory => [qw/t/]},
118 provides =>
119  
120 $provides,
121  
122 release_status => 'stable',
123 resources => {
124 repository => {
125 type => 'git',
126 url => '$git',
127 web => '$repo',
128 },
129 bugtracker => {
130 web => '$bug',
131 },
132  
133 },
134 },
135 )
136 ),
137  
138 ( \$ExtUtils::MakeMaker::VERSION < 6.48
139 ? ()
140 : ( MIN_PERL_VERSION => '$perl_ver' )
141 )
142  
143 );";
144  
145 close MAKEFILE;
146  
147 ### Build the distribution directory
148  
149 print `perl Makefile.PL`;
150 print `make distmeta`;
151  
152 ### Updating the tags
153  
154 print "\nUpdating 2016/06/30 -> $date\n";
155 system "find $distdir -type f | xargs perl -p -i -e 's|2016/06/30|$date|g'";
156 print "Updating 0.01 -> $version\n";
157 system "find $distdir -type f | xargs perl -p -i -e 's|0.01|$version|g'";
158 print "Updating 2016 -> $year\n";
159 system "find $distdir -type f | xargs perl -p -i -e 's|2016|$year|g'";
160 print "\n";
161  
162 ### Build the tarball
163  
164 unlink($distdir.'.tar') if -f $distdir.'.tar';
165 unlink($distdir.'.tar.gz') if -f $distdir.'.tar.gz';
166  
167 system "tar cvf $distdir.tar $distdir && gzip --best $distdir.tar";
168  
169 ### META.json check
170  
171 warn "\nSomething is odd! We didn't build a META.json\n\n"
172 unless -f $distdir.'/META.json';
173  
174 ### Cleanup
175  
176 unlink('Makefile');
177 unlink('Makefile.old');
178 unlink('Makefile.PL');
179 unlink('MYMETA.json');
180 unlink('MYMETA.yml');
181  
182 print "\nDONE!\n";
183  
184 ### Subroutines
185  
186 sub version_bump {
187 my $version = version_current();
188 my $next_version = version_next($version);
189 print "Bumping version from $version to $next_version\n";
190 system("git tag -a $next_version -m 'Version $next_version'");
191 system("git push --tags origin master");
192 system("git push --tags github master");
193 our $version = $next_version;
194 }
195  
196 sub version_current {
197 our $version;
198 return $version if defined $version and length $version;
199 $version = `git describe --long | tr '-' ' ' | awk '{ print \$1 }'`;
200 chomp $version;
201 $version = '0.01' unless length $version;
202 return $version;
203 }
204  
205 sub version_next {
206 my $old = shift @_;
207 $old =~ /^(.+)\.(.+?)$/;
208 my ($main,$sub) = ($1,$2);
209 my $length = length($sub);
210 $sub++;
211 if ( length($sub) > $length ) {
212 warn "WARNING: Version length now larger! (From $length digits to ", length($sub), ")\n";
213 $length = length($sub);
214 }
215 return sprintf('%s.%0'.$length.'d',$main,$sub);
216 }
217  
218 sub version_retag {
219 my $tag = version_current();
220 system("git tag -d $tag");
221 system("git push origin :refs/tags/$tag");
222 system("git push github :refs/tags/$tag");
223 print "\nTagging current as $tag\n";
224 system("git tag -a $tag -m 'Version $tag'");
225 system("git push --tags origin master");
226 system("git push --tags github master");
227 }