forked from millerscout/Kenshi-Mod-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversioning.ps1
236 lines (184 loc) · 6.87 KB
/
versioning.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#inspiration from https://gist.github.com/kumichou/acefc48476957aad6b0c9abf203c304c
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$bumpKind
)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
function UpdateVersionForUpdater($versionName){
$zipName = $versionName;
if($versionName -eq 'selfcontained'){ $zipName = "FullRelease"}
echo "https://github.com/millerscout/Kenshi-Mod-Manager/releases/download/v$($newVersion)/$($zipName).zip"
if(checkStatus("https://github.com/millerscout/Kenshi-Mod-Manager/releases/download/v$($newVersion)/$($zipName).zip")==1){
$xml = Get-Content -Path "updatelist-$($versionName).xml"
$xml = $xml -replace "<!-- NEXT VERSION -->", "<!-- NEXT VERSION -->`n<item>
<version>$($newVersion).0.0</version>
<url>https://github.com/millerscout/Kenshi-Mod-Manager/releases/download/v$($newVersion)/$($zipName).zip</url>
<changelog>https://github.com/millerscout/Kenshi-Mod-Manager/releases/tag/v$($newVersion)</changelog>
</item>"
Set-Content -Path "updatelist-$($versionName).xml" -Value $xml -Force
}
}
function checkStatus($url) {
$HTTP_Request = [System.Net.WebRequest]::Create($url)
$HTTP_Response = $HTTP_Request.GetResponse()
$HTTP_Status = [int]$HTTP_Response.StatusCode
If ($HTTP_Status -eq 200) {
$result = 1
}
Else {
$result = 0
}
If ($HTTP_Response -eq $null) { }
Else { $HTTP_Response.Close() }
return $result
}
function getVersion() {
Invoke-Expression "git fetch | Write-Verbose"
$tag = Invoke-Expression "git describe --tags --always 2>&1"
$tag = $tag.Split('-')[0]
$tag = $tag -replace 'v', ''
if ($tag -notmatch "\d+\.\d+") {
$tag = '1.0'
}
Write-Verbose "Version found: $tag"
return $tag
}
function bumpVersion($kind, $version) {
$major, $minor = $version.split('.')
switch ($kind) {
"major" {
$major = [int]$major + 1
$minor = 0
}
"minor" {
$minor = [int]$minor + 1
}
}
return [string]::Format("{0}.{1}", $major, $minor)
}
function commitVersion($kind, $version) {
Invoke-Expression "git add . 2>&1 | Write-Verbose"
Invoke-Expression "git commit -m 'New Version: $version' 2>&1 | Write-Verbose"
Invoke-Expression "git tag v$version 2>&1 | Write-Verbose"
}
function SetVersion ($file, $version) {
Write-Verbose "Changing version in $file to $version"
$fileObject = get-item $file
$sr = new-object System.IO.StreamReader( $file, [System.Text.Encoding]::GetEncoding("utf-8") )
$content = $sr.ReadToEnd()
$sr.Close()
$content = [Regex]::Replace($content, "<Version>[\s\S]*?<\/Version>", "<Version>"+$version+"</Version>");
$content = [Regex]::Replace($content, "<FileVersion>[\s\S]*?<\/FileVersion>", "<FileVersion>"+$version+"</FileVersion>");
$content = [Regex]::Replace($content, "<AssemblyVersion>[\s\S]*?<\/AssemblyVersion>", "<AssemblyVersion>"+$version+"</AssemblyVersion>");
$sw = new-object System.IO.StreamWriter( $file, $false, [System.Text.Encoding]::GetEncoding("utf-8") )
$sw.Write( $content )
$sw.Close()
}
function setVersionInDir($dir, $version) {
if ($version -eq "") {
Write-Verbose "version not found"
exit 1
}
# Set the Assembly version
$info_files = Get-ChildItem $dir -Recurse -Include "KenshiModTool.csproj"
foreach($file in $info_files)
{
Setversion $file $version
}
}
$validCommands = @("major", "minor")
Invoke-Expression "git checkout master"
Invoke-Expression "git pull"
if ($bumpKind -eq '')
{
Write-Output "Missing which number to bump up!"
exit 1
}
if (-not $validCommands.Contains($bumpKind))
{
Write-Output "Invalid command!"
exit 1
}
$oldVersion = getVersion
$newVersion = bumpVersion $bumpKind $oldVersion
$dir = "KenshiModTool/."
setVersionInDir $dir $newVersion
commitVersion $bumpKind $newVersion
Write-Output $newVersion
dotnet publish -c selfcontained -p:PublishProfile=KenshiModTool\Properties\PublishProfiles\FullRelease.pubxml
dotnet publish -c standalone -p:PublishProfile=KenshiModTool\Properties\PublishProfiles\Standalone.pubxml
Remove-Item "publish" -Include *.pdb -Recurse -force
cd publish
cd FullRelease
7z a -tzip "..\FullRelease" "*"
cd..
cd Standalone
7z a -tzip "..\Standalone" "*"
cd..
cd..
Invoke-Expression "git push --tags | Write-Verbose"
Invoke-Expression "git push | Write-Verbose"
$secret_key = Get-Content $env:APPDATA"..\..\..\.ssh\Token(Oauth)Kenshideploy"
$secret_key
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $secret_key)
$headers.Add("Content-Type", "application/json")
$body = "{
`n `"tag_name`": `"v$($newVersion)`",
`n `"target_commitish`": `"master`",
`n `"name`": `"v$($newVersion)`",
`n `"body`": `"Requirement if you use Standalone: [.Net Core 3.1.7 x64](https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-desktop-3.1.7-windows-x64-installer)\nText not avaiable Yet.`",
`n `"draft`": false,
`n `"prerelease`": false
`n}"
$response = Invoke-RestMethod 'https://api.github.com/repos/millerscout/Kenshi-Mod-Manager/releases
' -Method 'POST' -Headers $headers -Body $body
$id = $response.id
$dir = "publish"
function UploadFile($name) {
Clear-Variable headers
Clear-Variable body
Clear-Variable response
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$secret_key = Get-Content $env:APPDATA"..\..\..\.ssh\Token(Oauth)Kenshideploy"
$headers.Add("Authorization", $secret_key)
$headers.Add("Content-Type", "application/zip")
$body = ".\publish\$($name)"
$path = "https://uploads.github.com/repos/millerscout/Kenshi-Mod-Manager/releases/$($id)/assets?name=$($name)"
$path
$body
$response = Invoke-RestMethod $path -Method 'POST' -Headers $headers -Infile $body
$response.id
}
$info_files = Get-ChildItem $dir -Filter "*.zip" | Sort-Object -Descending
foreach($file in $info_files)
{
$name = $file | Select-object name | ForEach-Object {$_.Name}
$Stoploop = $false
[int]$Retrycount = "0"
do {
try {
UploadFile $name
Write-Host "Job completed"
$Stoploop = $true
}
catch {
if ($Retrycount -gt 3){
Write-Host "Could not send Package after 3 retrys."
$Stoploop = $true
}
else {
Write-Host "Could not send package retrying in 30 seconds..."
Start-Sleep -Seconds 30
$Retrycount = $Retrycount + 1
}
}
}
While ($Stoploop -eq $false)
}
UpdateVersionForUpdater("standalone")
UpdateVersionForUpdater("selfcontained")
Invoke-Expression "git add . | Write-Verbose"
Invoke-Expression "git commit -m 'updatelist Bump.' | Write-Verbose"
Invoke-Expression "git push | Write-Verbose"