#!/usr/bin/env perl use strict; use warnings FATAL => 'all'; use File::Find; sub version_parts { my $version = shift; my $dotted = $version =~ s/^v//; my @parts = split /\./, $version; if (!$dotted && @parts == 2) { my $dec = pop @parts; $dec =~ s/_//g; push @parts, $dec =~ /(\d{1,3})/g; } $_ += 0 for @parts; push @parts, 0 until @parts >= 3; return @parts; } my $old_version = shift; my %bump_part = (major => 0, minor => 1, bugfix => 2); my $bump_this = $bump_part{$ARGV[0]||'bugfix'}; my $new_vstring; my $new_decimal; if (defined $bump_this) { my @new_parts = version_parts($old_version); $new_parts[$bump_this]++; $new_parts[$_] = 0 for ($bump_this+1 .. $#new_parts); $new_vstring = join('.', @new_parts); my $alpha_pos = index($old_version, '_'); my $format = '%i.' . ( '%03i' x (@new_parts - 1) ); $new_decimal = sprintf($format, @new_parts); substr $new_decimal, $alpha_pos, 0, '_' if $alpha_pos != -1; } elsif ($ARGV[0] =~ /^v?[0-9]+(?:[._][0-9]+)*$/) { $new_decimal = $ARGV[0]; $new_vstring = join('.', version_parts($new_decimal)); } else { die "no idea which part to bump - $ARGV[0] means nothing to me" } warn "Bumping $old_version -> $new_decimal\n"; my %files; find({ no_chdir => 1, wanted => sub { return unless -f && /\.pod$|\.pm$/; my $file = $_; open my $fh, '<', $file or die "can't open $file: $!"; my $content = do { local $/; <$fh> }; close $fh; $content =~ s{ ( \$VERSION \s* = \s* ) (['"]?) v?([0-9]+(?:[._][0-9]+)*) \2 ( \s*; ) (?: (\s*\#\s*) v?[.0-9]+ )? }{ die "unable to bump version number in $file from $old_version, found $3\n" if $3 ne $old_version; $1 . "'" . $new_decimal . "'" . $4 . ($5 ? $5 . $new_vstring : '') }xe or return; $files{$file} = $content; }, }, 'lib'); MAKEFILE_PL: { my $file = 'Makefile.PL'; open my $fh, '<', $file or die "can't open $file: $!"; my $content = do { local $/; <$fh> }; close $fh; $content =~ s{ ( version \s* => \s* ) (['"]?) v?([0-9]+(?:[._][0-9]+)*) \2 ( \s*, ) (?: (\s*\#\s*) v?[.0-9]+ )? }{ die "unable to bump version number in $file from $old_version, found $3\n" if $3 ne $old_version; $1 . "'" . $new_decimal . "'" . $4 . ($5 ? $5 . $new_vstring : '') }xe or last MAKEFILE_PL; $files{$file} = $content; } for my $file (sort keys %files) { warn " updating $file\n"; open my $fh, '>', $file or die "can't open $file: $!"; print { $fh } $files{$file}; close $fh; }