forked from hdp/module-build
/
bump_version.pl
executable file
·128 lines (108 loc) · 3.38 KB
/
bump_version.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env perl
# NOTE: we run this immediately *after* a release so that any reports
# against the repo are obvious
use strict;
use warnings;
use lib 'lib';
use inc::ModuleBuildBuilder;
use Tie::File;
eval { require File::Find::Rule } or
die "$0 requires File::Find::Rule. Please install and try again.\n";
my $current = ModuleBuildBuilder->new_from_context(quiet => 1)->dist_version;
# Get version from command line or prompt
my $version = shift;
unless($version) {
my $default = $current;
# try to construct a reasonable default automatically
$default =~ s/(\d+)$// or
die "Usage: $0 VERSION\ncurrently: $current\n";
my $end = $1;
$default .= sprintf('%0'.length($end).'d', $end+1);
local $| = 1;
print "enter new version [$default]: ";
chomp(my $ans = <STDIN>);
$version = $ans ? $ans : $default;
# TODO check for garbage in?
}
die "must bump forward! ($version < $current)\n"
unless(eval $version >= eval $current);
# NEVER BUMP THESE $VERSION numbers
my @excluded = qw(
lib/Module/Build/YAML.pm
lib/Module/Build/Version.pm
);
# Get list of .pm files
my @pmfiles = File::Find::Rule->new->or(
File::Find::Rule->name('*.pm'),
)->in( 'lib' );
my @scripts = File::Find::Rule->new()->or(
File::Find::Rule->name('*'),
)->in( './scripts' );
# first start the new Changes entry
sub {
my $file = 'Changes';
open(my $fh, '<', $file) or die "cannot read '$file' $!";
my @lines = <$fh>;
my @head;
while(@lines) {
my $line = shift(@lines);
if($line =~ m/^$current/ ) {
# unreleased case -- re-bumping
if($line =~ m/^$current(?: *- *)?$/) {
print "Error parsing $file - found unreleased '$current'\n";
local $| = 1;
print "Are you sure you want to change the version number (y/n)? [n]:";
chomp(my $ans = <STDIN>);
if ( $ans !~ /^y/i ) {
print "Aborting!\n";
exit 1;
}
warn "Updating '$file'\n";
open(my $ofh, '>', $file) or die "cannot write '$file' $!";
print $ofh @head, "$version - \n", @lines;
close($ofh) or die "cannot write '$file' $!";
return;
}
if($line =~ m/^$current - \w/) {
warn "Updating '$file'\n";
open(my $ofh, '>', $file) or die "cannot write '$file' $!";
print $ofh @head, "$version - \n", "\n", $line, @lines;
close($ofh) or die "cannot write '$file' $!";
return;
}
elsif($line =~ m/^$version(?: *- *)?$/) {
# TODO should just be checking for a general number+eol case?
die "$file probably needs to be reverted!";
}
}
else {
push(@head, $line);
}
}
die "cannot find changes entry for current version ($current)!";
}->();
for my $file ( @pmfiles, @scripts ) {
next if grep { $file eq $_ } @excluded;
bump_version( $file, $version );
}
exit;
sub bump_version {
my ( $file, $version ) = @_;
my $o = tie my @lines, 'Tie::File', $file
or die "Couldn't tie '$file' for editing\n";
$o->flock;
# find line to change just like EU::MM::parse_version
my $inpod = 0;
for ( @lines ) {
$inpod = /^=(?!cut)/ ? 1 : /^=cut/ ? 0 : $inpod;
next if $inpod || /^\s*#/;
next unless /(?<!\\)([\$*])(([\w\:\']*)\bVERSION)\b.*\=/;
# TODO check that what we found matches $current?
$_ = "\$VERSION = '$version';";
warn "Updated $file\n";
last;
}
undef $o; untie @lines;
return;
}
# vi:ts=2:sw=2:et:sta