-
Notifications
You must be signed in to change notification settings - Fork 15
/
fix_versions
executable file
·96 lines (75 loc) · 1.94 KB
/
fix_versions
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
#!/opt/perl/bin/perl
use 5.010;
use strict;
use warnings;
no warnings 'syntax';
use autodie;
use POSIX 'strftime';
use Fcntl 'SEEK_CUR';
sub find;
sub fix;
my $CHANGES = "Changes";
my $README = "README";
my @MODULES = `find lib -type f -name "*.pm"`;
chomp @MODULES;
my $VERSION = shift;
die "Illegal version" unless
$VERSION =~ /^(?<year>20[0-9]{2})(?<month>[01][0-9])
(?<date>[0-3][0-9])(?<seq>[0-9][0-9])$/x;
my ($year, $month, $date, $seq) = @+ {qw [year month date seq]};
my $TODAY = strftime "%Y%m%d" => localtime;
die "Will only set versions matching today's date [$TODAY/$year$month$date]"
unless $TODAY eq "$year$month$date";
die "Will not set a sequence number to 00" if $seq eq "00";
unless (find $VERSION, $README, 2) {
say "Going to change $README";
fix $VERSION, $README, 2;
}
unless (find $VERSION, $CHANGES, 1) {
say "Going to change $CHANGES";
fix $VERSION, $CHANGES, 1, 1;
}
foreach my $module (@MODULES) {
unless (find $VERSION, $module, "VERSION =") {
say "Going to change $module";
fix $VERSION, $module, "VERSION =";
}
}
sub find {
my ($VERSION, $FILE, $TARGET) = @_;
open my $fh, "<", $FILE;
my $line = "";
if ($TARGET =~ /^[0-9]+$/) {
$line = <$fh> for 1 .. $TARGET;
}
else {{
$line = <$fh>;
redo unless $line =~ /$TARGET/;
}}
$line =~ /\b$VERSION\b/;
}
sub fix {
my ($VERSION, $FILE, $TARGET, $date) = @_;
open my $fh, "+<", $FILE;
my $line = "";
if ($TARGET =~ /^[0-9]+$/) {
$line = <$fh> for 1 .. $TARGET;
}
else {{
$line = <$fh>;
redo unless $line =~ /$TARGET/;
}}
my $pat = '\b[0-9]{10}\b';
my $repl = $VERSION;
if ($date) {
$pat .= " .*";
$repl .= " " . `date`;
chomp $repl;
}
if ($line =~ s/$pat/$repl/) {
seek $fh, - length ($line), SEEK_CUR;
print $fh $line;
}
close $fh;
}
__END__