-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-version.ps1
97 lines (78 loc) · 3.15 KB
/
set-version.ps1
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
param (
[Parameter()]
[string]$RefName = "",
[Parameter()]
[bool]$IsTag = $False,
[Parameter()]
[string]$BuildNumber = "0",
[Parameter()]
[string]$CommitHash = ""
)
# Load Toolkit
. ".build\Output.ps1";
function Read-VersionFromRef([string]$MajorMinorPatch, [string]$RefName = "", [bool]$IsTag = $False, [string]$BuildNumber = "0") {
function preReleaseVersion ([string] $name)
{
$name = $name.Replace("/","-").ToLower();
return "$MajorMinorPatch-$name.$BuildNumber";
}
$ref = "";
if ($RefName -ne "") {
$ref = $RefName; # The branch or tag name for which project is built
if ($IsTag) { # The commit tag name. Present only when building tags.
if ($RefName -like "v*") {
# Its a version tag
$version = $ref.substring(1) ;
}
else {
# Just a tag
$version = preReleaseVersion($ref);
}
}
else {
$version = preReleaseVersion($ref);
}
}
else { # Local build
Write-Host "Reading version from 'local'";
$version = preReleaseVersion("local");
}
return $version;
}
function Set-Version ([string]$version, [string]$GitCommitHash) {
$semVer2Regex = "^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$";
Write-Host "Setting environment version to $version";
# Match semVer2 regex
$regexMatch = [regex]::Match($version, $semVer2Regex);
if (-not $regexMatch.Success) {
Write-Host "Could not parse version: $version";
Invoke-ExitCodeCheck 1;
}
# Extract groups
$matchgroups = $regexMatch.captures.groups;
$majorGroup = $matchgroups[3];
$minorGroup = $matchgroups[4];
$patchGroup = $matchgroups[5];
$preReleaseGroup = $matchgroups[6];
$buildNumber = $matchgroups[2];
# Compose Major.Minor.Patch
$mmp = $majorGroup.Value + "." + $minorGroup.Value + "." + $patchGroup.Value;
# Check if it is a pre release
$env:MORYX_ASSEMBLY_VERSION = $majorGroup.Value + ".0.0.0" # 3.0.0.0
if ($preReleaseGroup.Success) {
$env:MORYX_FILE_VERSION = $mmp + "." + $buildNumber; # 3.1.2.42
$env:MORYX_INFORMATIONAL_VERSION = $mmp + "-" + $preReleaseGroup.Value + "+" + $GitCommitHash; # 3.1.2-beta.1+d95a996ed5ba14a1421dafeb844a56ab08211ead
$env:MORYX_PACKAGE_VERSION = $mmp + "-" + $preReleaseGroup.Value;
} else {
$env:MORYX_INFORMATIONAL_VERSION = $mmp + "+" + $GitCommitHash; # 3.1.2+d95a996ed5ba14a1421dafeb844a56ab08211ead
$env:MORYX_PACKAGE_VERSION = $mmp;
$env:MORYX_FILE_VERSION = $mmp + ".0";
}
}
$Version = Read-VersionFromRef -MajorMinorPatch (Get-Content "VERSION") -RefName $RefName -IsTag $IsTag -BuildNumber $BuildNumber
Set-Version $Version $CommitHash;
Write-Step "Set versions to ..."
Write-Variable "MORYX_ASSEMBLY_VERSION" $env:MORYX_ASSEMBLY_VERSION;
Write-Variable "MORYX_FILE_VERSION" $env:MORYX_FILE_VERSION;
Write-Variable "MORYX_INFORMATIONAL_VERSION" $env:MORYX_INFORMATIONAL_VERSION;
Write-Variable "MORYX_PACKAGE_VERSION" $env:MORYX_PACKAGE_VERSION;