-
Notifications
You must be signed in to change notification settings - Fork 79
/
version-gen
executable file
·109 lines (94 loc) · 3.06 KB
/
version-gen
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
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use autodie;
use FindBin qw($Bin);
use JSON::PP;
use Tie::IxHash;
# Write various version things for ProceduralParts.
# Paul Fenwick, January 2015.
# This code is CC-0 licensed. You can use it for anything, without attribution.
# This requires JSON::PP to run, which comes with Perl since v5.14, or can be
# installed with `cpanm JSON::PP`.
my $VERSION_FILE = "GameData/ProceduralParts/ProceduralParts.version";
my $ASSEMBLY_TEMPLATE = "Source/Properties/AssemblyInfo.in";
my $ASSEMBLY_FILE = "Source/Properties/AssemblyInfo.cs";
# Fix the interface of Tie::IxHash to preserve key order
sub tied_hash_ref {
tie(my %hash, 'Tie::IxHash', @_);
return \%hash;
}
# Here's our base metadata. We'll add VERSION info to this before outputting.
my $metadata = tied_hash_ref(
NAME => "Procedural Parts",
URL => "https://raw.githubusercontent.com/KSP-RO/ProceduralParts/master/GameData/ProceduralParts/ProceduralParts.version",
DOWNLOAD => "https://github.com/KSP-RO/ProceduralParts/releases",
GITHUB => tied_hash_ref(
USERNAME => "KSP-RO",
REPOSITORY => "ProceduralParts",
ALLOW_PRE_RELEASE => JSON::PP::false,
),
VERSION => tied_hash_ref(),
KSP_VERSION => tied_hash_ref(
MAJOR => 1,
MINOR => 8,
PATCH => 1
),
KSP_VERSION_MIN => tied_hash_ref(
MAJOR => 1,
MINOR => 8,
PATCH => 1
),
KSP_VERSION_MAX => tied_hash_ref(
MAJOR => 1,
MINOR => 12,
PATCH => 99
),
);
chdir($Bin);
my $version = `git describe --tags --long --match "v*"`;
chomp($version);
if ($version !~ m{
# Parse a version in the form v1.2.3-4
^v
(?<major>\d+)\.
(?<minor>\d+)\.
(?<patch>\d+)
(-pre)?-
(?<build>\d+)
}x) {
die "Cannot parse $version as a version!";
}
my $assembly_version = "$+{major}.$+{minor}.$+{patch}.$+{build}";
my $assembly_version_major = "$+{major}";
my $assembly_version_minor = "$+{minor}";
my $assembly_version_major_first = "$+{major}.0.0.0";
say "Writing metadata for version $assembly_version";
# Using `int` below forces Perl to internally represent these as numbers,
# even though they were the result of a string operation. This means no
# unsightly quotes in the output JSON.
$metadata->{VERSION} = tied_hash_ref(
MAJOR => int $+{major},
MINOR => int $+{minor},
PATCH => int $+{patch},
# If used, AVC could try to get the user to
# install git builds, which we don't want.
BUILD => 0
);
# Write our AVC metadata
open(my $version_fh, '>', $VERSION_FILE);
print {$version_fh} JSON::PP->new->indent->indent_length(4)->space_after->encode( $metadata );
close $version_fh;
# Write our AssemblyInfo
open(my $template_fh, '<', $ASSEMBLY_TEMPLATE);
open(my $assembly_fh, '>', $ASSEMBLY_FILE);
while (<$template_fh>) {
s{\@VERSION\@}{$assembly_version};
s{\@VERSION_MAJOR_FIRST\@}{$assembly_version_major_first};
s{\@VERSION_MAJOR\@}{$assembly_version_major};
s{\@VERSION_MINOR\@}{$assembly_version_minor};
print {$assembly_fh} $_;
}
close($template_fh);
close($assembly_fh);