forked from Unity-Technologies/Git-for-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet-Version.ps1
121 lines (105 loc) · 3.45 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
[CmdletBinding()]
Param(
[string]
$NewVersion = ''
,
[switch]
$BumpMajor = $false
,
[switch]
$BumpMinor = $false
,
[switch]
$BumpPatch = $false
,
[switch]
$BumpBuild = $false
,
[switch]
$Trace = $false
)
Set-StrictMode -Version Latest
if ($Trace) { Set-PSDebug -Trace 1 }
. $PSScriptRoot\helpers.ps1 | out-null
Write-Verbose "Set-Version: NewVersion: $NewVersion BumpMajor: $BumpMajor BumpMinor: $BumpMinor BumpPatch: $BumpPatch BumpBuild: $BumpBuild"
if ($NewVersion -eq '') {
if (!$BumpMajor -and !$BumpMinor -and !$BumpPatch -and !$BumpBuild){
Die -1 "You need to indicate which part of the version to update via -BumpMajor/-BumpMinor/-BumpPatch/-BumpBuild flags or a custom version via -NewVersion"
}
}
$source = Get-Content -Raw $PSScriptRoot\TheVersion.cs
Add-Type -TypeDefinition "$source"
function Read-Version([string]$versionFile) {
$versionjson = Get-Content $versionFile | ConvertFrom-Json
$version = $versionjson.version
$parsed = [TheVersion]::Parse("$version")
$parsed
}
function Write-Version([string]$versionFile, [TheVersion]$version) {
Write-Verbose "Writing version $version to $versionFile "
$versionjson = Get-Content $versionFile | ConvertFrom-Json
$versionjson.version = $version.Version
ConvertTo-Json $versionjson | Set-Content $versionFile
}
function Set-Version([string]$versionFile, [string]$newValue) {
$parsed = [TheVersion]::Parse("$newValue")
Write-Version $versionFile $parsed
}
function Bump-Version([string]$versionFile,
[bool]$bumpMajor, [bool] $bumpMinor,
[bool]$bumpPatch, [bool] $bumpBuild,
[string]$newValue)
{
Write-Verbose "Reading $versionFile"
$versionjson = Get-Content $versionFile | ConvertFrom-Json
$version = $versionjson.version
$parsed = [TheVersion]::Parse("$version")
Write-Verbose "Read version $parsed"
if ($bumpMajor) {
if ($newValue -ne '') {
Write-Verbose "Setting major part $newValue"
$newVersion = $parsed.SetMajor($newValue)
} else {
Write-Verbose "Bumping major part"
$newVersion = $parsed.bumpMajor()
}
} elseif ($bumpMinor) {
if ($newValue -ne '') {
Write-Verbose "Setting minor part $newValue"
$newVersion = $parsed.SetMinor($newValue)
} else {
Write-Verbose "Bumping minor part"
$newVersion = $parsed.BumpMinor()
}
} elseif ($bumpPatch) {
if ($newValue -ne '') {
Write-Verbose "Setting patch part $newValue"
$newVersion = $parsed.SetPatch($newValue)
} else {
Write-Verbose "Bumping patch part"
$newVersion = $parsed.BumpPatch()
}
} elseif ($bumpBuild) {
if ($newValue -ne '') {
Write-Verbose "Setting build part $newValue"
$newVersion = $parsed.SetBuild($newValue)
} else {
Write-Verbose "Bumping build part"
$newVersion = $parsed.BumpBuild()
}
}
Write-Verbose "Writing version $($newVersion.Version) to $versionFile "
$versionjson.version = $newVersion.Version
ConvertTo-Json $versionjson | Set-Content $versionFile
}
if ($NewVersion -ne '' -and !($BumpMajor -or $BumpMinor -or $BumpPatch -or $BumpBuild)) {
$versionFile = "$rootDirectory\src\com.unity.git.ui\version.json"
Set-Version $versionFile $NewVersion
$versionFile = "$rootDirectory\src\com.unity.git.api\version.json"
Set-Version $versionFile $NewVersion
} else {
$versionFile = "$rootDirectory\src\com.unity.git.ui\version.json"
Bump-Version $versionFile $BumpMajor $BumpMinor $BumpPatch $BumpBuild $NewVersion
$versionFile = "$rootDirectory\src\com.unity.git.api\version.json"
Bump-Version $versionFile $BumpMajor $BumpMinor $BumpPatch $BumpBuild $NewVersion
}