| 1 |
#!/usr/bin/env perl |
| 2 |
# bump-version [options] [VERSION] file ... |
| 3 |
# |
| 4 |
# read one or more files and increment the given version |
| 5 |
|
| 6 |
$VERSION = '0.1'; |
| 7 |
|
| 8 |
=pod |
| 9 |
|
| 10 |
=head1 USAGE |
| 11 |
|
| 12 |
bump-version [options] [VERSION] file ... |
| 13 |
|
| 14 |
=head1 DESCRIPTION |
| 15 |
|
| 16 |
The C<bump-version> script increments the version number in the |
| 17 |
given file. |
| 18 |
|
| 19 |
If VERSION is given, it must be in the format of "C<num.num>". |
| 20 |
|
| 21 |
Use the C<--list> option to see which files have a VERSION string. |
| 22 |
|
| 23 |
=head1 OPTIONS |
| 24 |
|
| 25 |
=over |
| 26 |
|
| 27 |
=item C<--list, -l> |
| 28 |
|
| 29 |
List the files that reference version numbers. |
| 30 |
|
| 31 |
=item C<--man> |
| 32 |
|
| 33 |
Show the entire man page. |
| 34 |
|
| 35 |
=item C<--major, -M> |
| 36 |
|
| 37 |
Bump the major version |
| 38 |
|
| 39 |
=item C<--minor, -m> |
| 40 |
|
| 41 |
Bump the minor version |
| 42 |
|
| 43 |
=item C<--verbose, -v> |
| 44 |
|
| 45 |
Be verbose. |
| 46 |
|
| 47 |
=item C<--version, -V> |
| 48 |
|
| 49 |
Show the current version and exit. |
| 50 |
|
| 51 |
=item C<--norun, -n> |
| 52 |
|
| 53 |
Show the comands but do not actually make any changes. |
| 54 |
|
| 55 |
=back |
| 56 |
|
| 57 |
|
| 58 |
=head1 AUTHOR |
| 59 |
|
| 60 |
Alan K. Stebbens <[email protected]> |
| 61 |
|
| 62 |
=cut |
| 63 |
|
| 64 |
use Getopt::Long qw(VersionMessage :config bundling no_ignore_case auto_version ); |
| 65 |
use Pod::Usage; |
| 66 |
|
| 67 |
my $verbose = ''; |
| 68 |
my $norun = ''; |
| 69 |
my $help = ''; |
| 70 |
my $version = ''; |
| 71 |
my $man = ''; |
| 72 |
my $bump_major = ''; |
| 73 |
my $bump_minor = 1; |
| 74 |
my $major = ''; |
| 75 |
my $minor = ''; |
| 76 |
|
| 77 |
|
| 78 |
sub chatf($@) { |
| 79 |
&talkf if $verbose; |
| 80 |
} |
| 81 |
|
| 82 |
sub chat($) { |
| 83 |
chatf '', shift; |
| 84 |
} |
| 85 |
|
| 86 |
sub talkf($@) { |
| 87 |
my $fmt = shift; |
| 88 |
$fmt = "%s\n" unless $fmt ne ''; |
| 89 |
printf STDERR $fmt, @_; |
| 90 |
} |
| 91 |
|
| 92 |
sub talk($) { |
| 93 |
talkf '', shift; |
| 94 |
} |
| 95 |
|
| 96 |
sub nvtalkf($@) { |
| 97 |
&talkf unless $verbose; |
| 98 |
} |
| 99 |
|
| 100 |
# runcmd COMMAND |
| 101 |
# |
| 102 |
# In $norun mode, print COMMAND prefixed with "(norun) ", and do nothing |
| 103 |
# else. |
| 104 |
# |
| 105 |
# In $verbose mode, print COMMAND prefixed with "--> " before invoking the |
| 106 |
# command. |
| 107 |
# |
| 108 |
# runcmd COMMANDTEXT { BLOCK ; } |
| 109 |
# |
| 110 |
# COMMANDTEXT is displayed for either $norun and $verbose modes, but |
| 111 |
# BLOCK is executed. |
| 112 |
# |
| 113 |
|
| 114 |
sub runcmd($;&) { |
| 115 |
my $cmd = shift; |
| 116 |
my $code = shift; |
| 117 |
if ($norun) { |
| 118 |
talkf "(norun) %s\n", $cmd; |
| 119 |
} else { |
| 120 |
chatf "--> %s\n", $cmd; |
| 121 |
if ($code) { |
| 122 |
unless (&$code) { |
| 123 |
nvtalkf "--> %s\n", $cmd; |
| 124 |
die "Command block failed: $!\n"; |
| 125 |
} |
| 126 |
} else { |
| 127 |
STDERR->flush; |
| 128 |
unless (system($cmd)) { |
| 129 |
nvtalkf "--> %s\n", $cmd; |
| 130 |
die "Command failed: $!\n"; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
sub update_version($) { |
| 137 |
my $file = shift; |
| 138 |
$new_file = "$file.new"; |
| 139 |
chatf "Scanning %s for \$VERSION..\n", $file; |
| 140 |
my($in, $out); |
| 141 |
if (open($in, "<", $file)) { |
| 142 |
if (!$norun) { |
| 143 |
chatf "Opening %s for output..\n", $new_file; |
| 144 |
open($out, ">", $new_file) or die("Cannot open $out for output: $!\n"); |
| 145 |
} |
| 146 |
my $done = ''; |
| 147 |
my $changes = ''; |
| 148 |
while (<$in>) { |
| 149 |
talkf "%03d: %s", $., $_ if $verbose > 1; |
| 150 |
if (/^(\s*)\$VERSION\s*=\s*['"]?(\d+)\.(\d+)['"]?[\s;]/) { |
| 151 |
my ($prefix, $cur_major, $cur_minor) = ($1, $2, $3); |
| 152 |
talkf "%20s: ", $file if $many_files && !$verbose; |
| 153 |
talkf "Found version: %s.%s\n", $cur_major, $cur_minor; |
| 154 |
($new_major, $new_minor) = bump_version($cur_major, $cur_minor); |
| 155 |
if ($new_major ne $cur_major or $new_minor ne $cur_minor) { |
| 156 |
talkf "%20s: ", $file if $many_files && !$verbose; |
| 157 |
talkf " New version: %s.%s\n", $new_major, $new_minor; |
| 158 |
$_ = sprintf("%s\$VERSION = '%s.%s';\n", $prefix, $new_major, $new_minor); |
| 159 |
$changes = 1; |
| 160 |
} |
| 161 |
print $out $_ if $out ne ''; |
| 162 |
last; |
| 163 |
} |
| 164 |
print $out $_ if $out ne ''; |
| 165 |
} |
| 166 |
# finish up reading & writing |
| 167 |
if ($out ne '') { |
| 168 |
while (<$in>) { print $out $_; } # write the rest of the file |
| 169 |
close($out) or die "Close $out failed: $!\n"; |
| 170 |
} |
| 171 |
close($in); |
| 172 |
if ($changes && !$norun) { |
| 173 |
talk "Saving changes.."; |
| 174 |
runcmd "mv $file $file.old", sub { |
| 175 |
rename($file, $file.'.old') or die "mv $file $file.old failed: $!"; |
| 176 |
}; |
| 177 |
runcmd "mv $new_file $file", sub { |
| 178 |
rename($new_file, $file) or die "mv $new_file $file: $!"; |
| 179 |
}; |
| 180 |
talkf "%s updated.\n", $file; |
| 181 |
} elsif (!$changes) { |
| 182 |
talk "No changes."; |
| 183 |
} |
| 184 |
|
| 185 |
} else { |
| 186 |
warn "Cannot open and read $file: $!"; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
# bump_version($input_major_version, $input_minor_version) |
| 191 |
# |
| 192 |
# Increment the version numbers, according to $bump_major or $bump_minor. |
| 193 |
# |
| 194 |
# if $major and $minor are already set (by option), they override. |
| 195 |
|
| 196 |
sub bump_version($$) { |
| 197 |
if ($major eq '' || $minor eq '') { |
| 198 |
($major, $minor) = (shift, shift); |
| 199 |
if ($bump_major) { |
| 200 |
$major += 1; |
| 201 |
$minor = 0; |
| 202 |
} elsif ($bump_minor) { |
| 203 |
$minor += 1; |
| 204 |
} |
| 205 |
} |
| 206 |
return ($major, $minor); |
| 207 |
} |
| 208 |
|
| 209 |
############## |
| 210 |
# main program |
| 211 |
|
| 212 |
GetOptions('verbose|v+' => \$verbose, |
| 213 |
'help|h' => sub { pod2usage(1); }, |
| 214 |
'list|l' => \$list, |
| 215 |
'man' => sub { pod2usage(-exitval => 0, -verbose => 2); }, |
| 216 |
'major|M' => \$bump_major, |
| 217 |
'minor|m' => \$bump_minor, |
| 218 |
'norun|n' => \$norun, |
| 219 |
'version|V' => sub { VersionMessage(); } |
| 220 |
) or pod2usage(2); |
| 221 |
|
| 222 |
$bump_minor = '' if $bump_major; |
| 223 |
|
| 224 |
chomp($out = `echo Makefile.PL README* *.pm`); |
| 225 |
@files = split(' ', $out); |
| 226 |
|
| 227 |
# list? |
| 228 |
|
| 229 |
if ($list) { |
| 230 |
unshift(@ARGV, @files) unless $#ARGV >= 0; |
| 231 |
my $cmd = "egrep '\\<\\d+\\.\\d+\\>' " . join(' ', @ARGV); |
| 232 |
talkf "--> %s\n", $cmd; |
| 233 |
system $cmd; |
| 234 |
exit; |
| 235 |
} |
| 236 |
|
| 237 |
# process optional version argument |
| 238 |
|
| 239 |
if ($#ARGV >= 0) { |
| 240 |
if ($ARGV[0] =~ /^(\d+)\.(\d+)$/) { |
| 241 |
($major, $minor) = ($1, $2); |
| 242 |
shift(@ARGV); |
| 243 |
talkf "Using version: %s.%s\n", $major, $minor; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
# if there are no file arguments, and only a version given, show the syntax |
| 248 |
|
| 249 |
$#ARGV >=0 or pod2usage(1); |
| 250 |
|
| 251 |
# process each file argument |
| 252 |
|
| 253 |
$many_files = 1 if $#ARGV > 0; |
| 254 |
|
| 255 |
foreach my $file (@ARGV) { |
| 256 |
update_version($file); |
| 257 |
} |
| 258 |
|
| 259 |
exit; |
| 260 |
|