forked from PrincessRTFM/TinyCommands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbump
75 lines (67 loc) · 1.65 KB
/
bump
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
#!perl
use strict;
use warnings;
use IO::File;
use File::Glob qw/:bsd_glob/;
use Fcntl qw/:seek/;
my $path = (bsd_glob("*.csproj", GLOB_NOCASE))[0] || '';
unless ($path) {
print STDERR "Can't find a .csproj file to operate on!\n";
exit 2;
}
my $userVer = lc(shift @ARGV || 'none');
my $numVer;
if ($userVer !~ m/^(?:\d+(?:\.\d+)*|major|minor|patch|none)$/) {
print STDERR qq{Invalid version, must be dot-delimited numeric or keyword "major", "minor", "patch", "none"\n};
exit 3;
}
my $file = IO::File->new($path, O_RDWR | O_APPEND);
$file->seek(0, SEEK_SET);
unless ($file) {
printf STDERR qq{Failed to open "%s" (%s)\n}, $path, $! || "unknown error";
exit 255;
}
my @lines = map { s/\r?\n$//r } $file->getlines;
for (my $i = 0; $i < @lines; ++$i) {
my $line = $lines[$i];
if ($line =~ m{^(\s*)<Version>(\d+(?:\.\d+)*)</Version>}i) {
my ($lead, $old) = ($1, $2);
my @parts = split /\./, $old;
if ($userVer eq "major") {
++$parts[0];
$parts[1] = $parts[2] = 0;
}
elsif ($userVer eq "minor") {
++$parts[1];
$parts[2] = 0;
}
elsif ($userVer eq "patch") {
++$parts[2];
}
elsif ($userVer eq "none") {
# nop
}
else {
@parts = split /\./, $userVer;
}
$parts[$_] //= 0 for 0..2;
$numVer = join '.', @parts;
if ($userVer eq "none") {
print "Current version is $numVer\n";
}
else {
print "Updating from version $old to $numVer...\n";
}
$lines[$i] = "$lead<Version>$numVer</Version>";
}
}
$file->seek(0, SEEK_SET);
$file->truncate(0);
$file->print(join "\n", @lines);
$file->print("\n");
$file->close;
system qw/git add/, $path;
if (@ARGV && $numVer) {
system qw/git commit/, @ARGV;
system qw/git tag -a/, "v$numVer";
}