add refresh command to update distar
[p5sagit/Distar.git] / helpers / bump-version
CommitLineData
8ab5ea70 1#!/usr/bin/env perl
2
3use strict;
4use warnings FATAL => 'all';
5use File::Find;
6
7sub version_parts {
576656c4 8 my $version = shift;
9 my $dotted = $version =~ s/^v//;
10 my @parts = split /\./, $version;
11 if (!$dotted && @parts == 2) {
8ab5ea70 12 my $dec = pop @parts;
576656c4 13 $dec =~ s/_//g;
8ab5ea70 14 push @parts, $dec =~ /(\d{1,3})/g;
15 }
16 $_ += 0 for @parts;
17 push @parts, 0
18 until @parts >= 3;
19 return @parts;
20}
21
576656c4 22my $old_version = shift;
8ab5ea70 23
24my %bump_part = (major => 0, minor => 1, bugfix => 2);
8ab5ea70 25my $bump_this = $bump_part{$ARGV[0]||'bugfix'};
26
576656c4 27my $new_vstring;
28my $new_decimal;
29
8ab5ea70 30if (defined $bump_this) {
576656c4 31 my @new_parts = version_parts($old_version);
8ab5ea70 32 $new_parts[$bump_this]++;
33 $new_parts[$_] = 0 for ($bump_this+1 .. $#new_parts);
576656c4 34 $new_vstring = join('.', @new_parts);
35 my $alpha_pos = index($old_version, '_');
36 my $format = '%i.' . ( '%03i' x (@new_parts - 1) );
37 $new_decimal = sprintf($format, @new_parts);
38 substr $new_decimal, $alpha_pos, 0, '_'
39 if $alpha_pos != -1;
8ab5ea70 40}
576656c4 41elsif ($ARGV[0] =~ /^v?[0-9]+(?:[._][0-9]+)*$/) {
42 $new_decimal = $ARGV[0];
43 $new_vstring = join('.', version_parts($new_decimal));
8ab5ea70 44}
45else {
46 die "no idea which part to bump - $ARGV[0] means nothing to me"
47}
48
576656c4 49warn "Bumping $old_version -> $new_decimal\n";
8ab5ea70 50
51my %files;
52find({
53 no_chdir => 1,
54 wanted => sub {
55 return
56 unless -f && /\.pod$|\.pm$/;
57 my $file = $_;
58 open my $fh, '<', $file
59 or die "can't open $file: $!";
60 my $content = do { local $/; <$fh> };
61 close $fh;
62
63 $content =~ s{
64 ( \$VERSION \s* = \s* )
576656c4 65 (['"]?) v?([0-9]+(?:[._][0-9]+)*) \2
66 ( \s*; )
67 (?:
68 (\s*\#\s*)
69 v?[.0-9]+
70 )?
8ab5ea70 71 }{
576656c4 72 die "unable to bump version number in $file from $old_version, found $3\n"
73 if $3 ne $old_version;
74 $1 . "'" . $new_decimal . "'" . $4 . ($5 ? $5 . $new_vstring : '')
8ab5ea70 75 }xe
76 or return;
77
78 $files{$file} = $content;
79 },
80}, 'lib');
81
a4c19845 82MAKEFILE_PL: {
83 my $file = 'Makefile.PL';
84 open my $fh, '<', $file
85 or die "can't open $file: $!";
86 my $content = do { local $/; <$fh> };
87 close $fh;
88
89 $content =~ s{
90 ( version \s* => \s* )
91 (['"]?) v?([0-9]+(?:[._][0-9]+)*) \2
92 ( \s*, )
93 (?:
94 (\s*\#\s*)
95 v?[.0-9]+
96 )?
97 }{
98 die "unable to bump version number in $file from $old_version, found $3\n"
99 if $3 ne $old_version;
100 $1 . "'" . $new_decimal . "'" . $4 . ($5 ? $5 . $new_vstring : '')
101 }xe
102 or last MAKEFILE_PL;
103
104 $files{$file} = $content;
105}
106
8ab5ea70 107for my $file (sort keys %files) {
108 warn " updating $file\n";
109 open my $fh, '>', $file
110 or die "can't open $file: $!";
111 print { $fh } $files{$file};
112 close $fh;
113}