-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.ps1
309 lines (256 loc) · 14 KB
/
publish.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#
# Terraria Plugin Publishing Script, CoderCow 2017
#
#
# Actions Performed by this Script
#
# * Uses GitVersion to increment a component of the version number in the project's AssemblyInfo.cs.
# The component of the SemVer version number to increase (major, minor or patch) is determined by
# the commits done since the last tag (BREAKING, feature or fix commits).
# * Uses clog to generate a changelog in markdown format. The entries are also determined by the
# commits since the last tag.
# * Bundles release files in a zip file.
# * Creates a commit for the changed AssemblyInfo.cs and tags this commit with the new SemVer,
# also pushes this commit and the tag.
# * Uses GitReleaseManager to create a GitHub release and attaches the zip file to it, using the
# generated changelog as the description text.
# * Performs a login on https://tshock.co and edits the related plugin resource to update its
# version information.
# * Converts the repository's README.md to html and uses it as the description text for the tshock
# resource, so that descriptions remain consistent.
# * Adds a resource update to tshock.co using using the converted changelog.md as description.
#
# Usage Notes
#
# The conventional commit message format of angularjs is expected when scanning recent commits.
# It's documented here: https://github.com/conventional-changelog/conventional-changelog/blob/a5505865ff3dd710cf757f50530e73ef0ca641da/conventions/angular.md
#
# Do not run this script when you're currently on a tagged commit.
# Do not commit AssemblyInfo.cs before invoking this script.
#
# To create pre-releases, make a new branch and call it "pre". When running this script while being on
# a commit in this branch, versions will looks like this "X.X.X-pre.Y" where X.X.X is your next release
# version and Y your current pre-release candidate. The version should become "X.X.X" (without the suffix)
# once you merge "pre" (fast forward should work fine) back into master and run this script again.
#
# Installing Dependencies
#
# If you have chocolatey, most of the required dependencies can be installed by:
# choco install GitReleaseManager.Portable GitVersion.Portable 7zip rust -y & cargo install clog-cli
#
# Note that cargo is the bundled package manager of rust.
# You may have to register some of the binaries in your PATH.
#
# To install the markdown powershell module, see this:
# https://social.technet.microsoft.com/wiki/contents/articles/30591.convert-markdown-to-html-using-powershell.aspx
#
# Further Notes
#
# In case you prefer GitHub issues for the changelog generation instead of commits you might rewrite
# this script to use GitReleaseManager for this, as it supports changelogs based on issues and milestones.
#
# clog repo: https://github.com/clog-tool/clog-cli
# GitVersion docs: http://gitversion.readthedocs.io
# GitReleaseManager docs: http://gitreleasemanager.readthedocs.io
$ErrorActionPreference = "Stop"
Import-Module PowershellMarkdown
$outDir = "$PSScriptRoot\bin\Release"
$assemblyInfoPath = "$PSScriptRoot\Properties\AssemblyInfo.cs"
# the *.cs file which contains the main plugin class annontated with the ApiVersion attribute
$pluginSourceFilePath = "$PSScriptRoot\Implementation\ProtectorPlugin.cs"
# the tshock binary is used to determine the tshock version this plugin was built against
$tshockBinaryPath = "$outDir\TShockAPI.dll"
# the OTAPI binary is used to determine the Terraria version this plugin was built against
$otapiBinaryPath = "$outDir\OTAPI.dll"
$targetName = "Protector"
$projectFile = "$PSScriptRoot\$targetName.csproj"
$commitMessageFormat = "chore(version): tick plugin version {0}"
$tagNameFormat = "release {0} for Terraria {1} (API {2})"
$outZipFileNameFormat = "Protector_{0}_API_{2}.zip"
$gitHubUser = "CoderCow"
$gitHubRepoOwner = "CoderCow"
$gitHubRepoName = "Protector-Plugin"
# information used to update the ressource on tshock.co
$tshockUser = "[email protected]"
$tshockResourceUri = "https://tshock.co/xf/index.php?resources/protector.190/"
# for the XenForo description text
$readmeFile = "$PSScriptRoot\README.md"
$binariesToPublish = @(
"$outDir\$targetName.dll",
"$outDir\$targetName.pdb",
"$outDir\Plugin Common Lib *.dll",
"$outDir\Plugin Common Lib *.pdb"
)
function Main {
$pluginApiVersion = Get-ApiVersion
Write-Host "The plugin's API Version is $pluginApiVersion" -ForegroundColor Cyan
$tshockVersion = Get-TshockVersion
Write-Host "This plugin was built against TShock $tshockVersion" -ForegroundColor Cyan
$terrariaVersion = Get-OtapiVersion
Write-Host "This plugin was built against Terraria $terrariaVersion" -ForegroundColor Cyan
$versionInfo = Update-AssemblyVersion
$releaseVersion = $versionInfo.SemVer
$isPrerelease = $versionInfo.PreReleaseTag -ne ""
Write-Host "Release version will be $releaseVersion"
$outChangelogFile = "$outDir\changelog.md"
$gitHubUrl = "https://github.com/$gitHubUser/$gitHubRepoName"
Generate-Changelog $pluginApiVersion $tshockVersion $terrariaVersion $outChangelogFile $gitHubUrl
$outZipFile = "$outDir\" + ($outZipFileNameFormat -f $releaseVersion,$terrariaVersion,$pluginApiVersion)
Package-Files $outZipFile
Create-Commit $releaseVersion $terrariaVersion $pluginApiVersion
Write-Host "Publishing to GitHub..."
Create-GitHubRelease $releaseVersion $outChangelogFile $outZipFile
Start-Process "$gitHubUrl/releases"
Write-Host "Updating TShock resource..."
Update-TShockResource $releaseVersion $terrariaVersion $pluginApiVersion $outChangelogFile $gitHubUrl
Start-Process "$tshockResourceUri/updates"
}
function Get-ApiVersion {
# Try to parse the ApiVersion from the c-sharp file
$regExResult = Get-Content $pluginSourceFilePath | Select-String "\[\s*ApiVersion\s*\(\s*(\d+)\s*,\s*(\d+)\s*\)"
if ($regExResult -eq $null) {
Write-Host "Failed parsing API Version from file $pluginSourceFilePath"
exit 1
}
$apiMajor = $regExResult.Matches[0].Groups[1].Value
$apiMinor = $regExResult.Matches[0].Groups[2].Value
"$apiMajor.$apiMinor"
}
function Get-TShockVersion {
# Get the file version of the TShock binary
[System.Diagnostics.FileVersionInfo]::GetVersionInfo($tshockBinaryPath).FileVersion.ToString()
}
function Get-OtapiVersion {
# Get the file version of the OTAPI binary
[System.Diagnostics.FileVersionInfo]::GetVersionInfo($otapiBinaryPath).FileVersion.ToString()
}
function Update-AssemblyVersion {
# GitVersion will increment the assembly version and return some information about it in json format
# Actually, this should have happened already be the pre-build event configured for the project, so this
# additional call to GitVersion will just return the current version number.
GitVersion.exe /updateassemblyinfo $assemblyInfoPath | ConvertFrom-Json
}
function Generate-Changelog($pluginApiVersion, $tshockVersion, $terrariaVersion, $outChangelogFile, $gitHubUrl) {
if (Test-Path $outChangelogFile) {
Remove-Item -Force $outChangelogFile
}
# clog builds a markdown changelog from all commits since the last tag
clog.exe --from-latest-tag --setversion $releaseVersion --outfile $outChangelogFile --repository $gitHubUrl
# add some custom lines to the changelog
if ($isPrerelease) {
Add-Content "$outChangelogFile" "**NOTE: This is a pre-release currently under test.**`n"
}
Add-Content "$outChangelogFile" "Plugin API Version: **$pluginApiVersion**. It was built against Terraria **$terrariaVersion** and TShock **$tshockVersion**."
# print the changelog for validation
Write-Host "---- Content of $outChangelogFile ----" -ForegroundColor Green
Get-Content $outChangelogFile | Write-Host -ForegroundColor Cyan
Write-Host "------------- EOF -------------" -ForegroundColor Green
$wantToEdit = Read-Host "Do you want to edit the changelog? [y/n]"
if ($wantToEdit -eq "y") {
Start-Process $outChangelogFile
Read-Host "Press any key to continue"
}
}
function Package-Files($outZipFile) {
$outPluginDir = "$outDir\ServerPlugins"
$outTShockDir = "$outDir\tshock"
if (Test-Path $outZipFile) {
Remove-Item -Force $outZipFile
}
New-Item -ItemType directory -Force $outPluginDir > $null
Move-Item $binariesToPublish -Force "$outPluginDir\"
7z.exe a -y -r -bd -tzip -mx9 $outZipFile $outPluginDir $outTShockDir > $null
}
function Create-Commit($releaseVersion, $terrariaVersion, $pluginApiVersion) {
$tagName = $tagNameFormat -f $releaseVersion,$terrariaVersion,$pluginApiVersion
$commitMessage = $commitMessageFormat -f $releaseVersion,$terrariaVersion,$pluginApiVersion
git add $assemblyInfoPath
git commit --message $commitMessage
git tag --annotate $releaseVersion --message $tagName
}
function Create-GitHubRelease($releaseVersion, $outChangelogFile, $outZipFile) {
$gitHubPassword = Read-Host "Enter password for GitHub user $gitHubUser"
# This ensures that errors can be seen if they happen
$ErrorActionPreference = "Continue"
git push origin --follow-tags
GitReleaseManager.exe create -u $gitHubUser -p $gitHubPassword -o $gitHubRepoOwner -r $gitHubRepoName -n $releaseVersion -i $outChangelogFile -a $outZipFile
GitReleaseManager.exe publish -u $gitHubUser -p $gitHubPassword -o $gitHubRepoOwner -r $gitHubRepoName -t $releaseVersion
}
function Update-TShockResource($releaseVersion, $terrariaVersion, $pluginApiVersion, $changelogFile, $gitHubUrl) {
$tshockPassword = Read-Host "Enter password for TShock XenForo user $tshockUser"
# Invoke-WebRequest: https://msdn.microsoft.com/powershell/reference/5.1/microsoft.powershell.utility/Invoke-WebRequest
# FormObject: https://msdn.microsoft.com/en-us/library/microsoft.powershell.commands.formobject(v=vs.85).aspx
# authenticate
$response = Invoke-WebRequest -Uri "https://tshock.co/xf/index.php?login" -SessionVariable session
$formObject = $response.Forms["pageLogin"]
$formObject.Fields["login"] = $tshockUser
$formObject.Fields["password"] = $tshockPassword
$response = Invoke-WebRequest -Uri "https://tshock.co/xf/index.php?login/login" -Method Post -Body $formObject -WebSession $session
# if this causes a 403 then the login has probably failed
$response = Invoke-WebRequest -Uri "$tshockResourceUri/edit" -WebSession $session
# form in that page has no id, so finding it is a bit of a hassle
$formHtmlElement = $response.ParsedHtml.IHTMLDocument3_getElementsByTagName("form") | Where { $_.action.EndsWith("/save") }
$fields = Construct-FormFields $response $formHtmlElement
# can't set the version prefix directly, need a prefix id instead
# <optgroup label="Version">
$prefixOptionsList = $response.ParsedHtml.IHTMLDocument3_getElementsByTagName("optgroup") | Where { $_.label -eq "Version" }
# <option value="6" data-css="prefix prefixRed" >1.18 (obsolete)</option>
$prefixOption = $prefixOptionsList.getElementsByTagName("option") | Where { $_.innerText -eq $pluginApiVersion }
if ($prefixOption) {
$prefixId = $prefixOption.value
$fields["prefix_id"] = $prefixId
} else {
Write-Host "Looks like there's yet no prefix for API $pluginApiVersion available." -ForegroundColor Cyan
Write-Host "Keeping the current prefix for now." -ForegroundColor Green
}
# this is actually "API Version"
$fields["custom_fields[tshockver]"] = $pluginApiVersion
$fields["custom_fields[tshockversion]"] = $tshockVersion
$fields["version_string"] = $releaseVersion
$readmeMarkdown = Get-Content $readmeFile
$fields["message_html"] = Convert-MarkdownToHtml $readmeMarkdown
# save the resource
$response = Invoke-WebRequest -Uri "$tshockResourceUri/save" -Method Post -Body $fields -WebSession $session
# open "Post Resource Update" page
$response = Invoke-WebRequest -Uri "$tshockResourceUri/add-version" -WebSession $session
$formHtmlElement = $response.ParsedHtml.IHTMLDocument3_getElementsByTagName("form") | Where { $_.action.EndsWith("/save-version") }
$fields = Construct-FormFields $response $formHtmlElement
$fields["download-url"] = "$gitHubUrl/releases/tag/$releaseVersion"
$fields["version-string"] = $releaseVersion
$fields["title"] = "$releaseVersion Update"
$changelogMarkdown = Get-Content $changelogFile
# remove commit hashes
$fields["message_html"] = Convert-MarkdownToHtml $changelogMarkdown
# post update
$response = Invoke-WebRequest -Uri "$tshockResourceUri/save-version" -Method Post -Body $fields -WebSession $session
}
function Convert-MarkdownToHtml($markdown) {
$html = ConvertFrom-Markdown -MarkdownContent ($markdown -join "`n")
# make things a bit prettier because XenForo does some weird transformations to the html
$html = $html -replace '<p><a name=".*"></a></p>[\s\n\r]*',""
$html = $html -replace "</p>[\s\n\r]*<p>","<br><br>"
$html = $html -replace "<h3>","<br><h3>"
$html = $html -replace '<p><a name=".*"></a></p>[\s\n\r]*',""
$html = $html -replace "<h4>","<br><h4>"
$html = $html -replace "<h2>(.*)</h2>",'<span style="font-size: 18px"><b>$1</b></span><br>'
$html = $html -replace "<h3>(.*)</h3>",'<span style="font-size: 15px"><b>$1</b></span><br>'
$html = $html -replace "<h4>(.*)</h4>",'<span style="font-size: 14px"><b>$1</b></span><br>'
$html = $html -replace "</h2>[\s\n\r]*<br>","</h2><br>"
$html = $html -replace "</span><br>[\s\n\r]*<p>","</span><br><br><p>"
return $html
}
function Construct-FormFields($request, $formHtmlElement) {
$fields = @{}
$inputFields = $formHtmlElement.getElementsByTagName("input") | Where { $_.name -and $_.type -ne "button" }
foreach ($newField in $inputFields) {
$fields[$newField.name] = $newField.Value
}
foreach ($newField in $formHtmlElement.getElementsByTagName("textarea")) {
$fields.Add($newField.name, $newField.Value)
}
foreach ($newField in $formHtmlElement.getElementsByTagName("select")) {
$fields.Add($newField.name, $newField.Value)
}
return $fields
}
Main