Debian Codegraph @

scripts/release-windows-winget.ps1

raw
1 param(
2 [string]$Version = "",
3 [string]$Arch = "x64",
4 [string]$BuildDir = "build\winget-$Arch",
5 [string]$OutputDir = "dist\winget-$Arch",
6 [string]$GitHubRepository = "BYVoid/OpenCC",
7 [string]$PackageIdentifier = "BYVoid.OpenCC",
8 [string]$Publisher = "BYVoid",
9 [switch]$PrepareOnly,
10 [switch]$PackageOnly,
11 [switch]$SkipTests
12 )
13
14 Set-StrictMode -Version Latest
15 $ErrorActionPreference = "Stop"
16
17 function Get-ProjectVersion {
18 $packageJsonFile = Join-Path $PSScriptRoot "..\package.json"
19 if (Test-Path $packageJsonFile) {
20 $packageJson = Get-Content -Path $packageJsonFile -Raw | ConvertFrom-Json
21 if ($packageJson.version) {
22 return [string]$packageJson.version
23 }
24 }
25
26 $gitVersionFile = Join-Path $PSScriptRoot "..\cmake\GitVersion.cmake"
27 $content = Get-Content -Path $gitVersionFile -Raw
28
29 $major = [regex]::Match($content, '_OPENCC_FALLBACK_MAJOR\s+(\d+)').Groups[1].Value
30 $minor = [regex]::Match($content, '_OPENCC_FALLBACK_MINOR\s+(\d+)').Groups[1].Value
31 $revision = [regex]::Match($content, '_OPENCC_FALLBACK_REVISION\s+(\d+)').Groups[1].Value
32
33 if (-not $major -or -not $minor -or -not $revision) {
34 throw "Failed to parse version from package.json or cmake/GitVersion.cmake."
35 }
36
37 return "$major.$minor.$revision"
38 }
39
40 function Write-Utf8File {
41 param(
42 [Parameter(Mandatory = $true)][string]$Path,
43 [Parameter(Mandatory = $true)][string]$Content
44 )
45
46 $directory = Split-Path -Parent $Path
47 if ($directory) {
48 New-Item -ItemType Directory -Force -Path $directory | Out-Null
49 }
50
51 $utf8NoBom = New-Object System.Text.UTF8Encoding($false)
52 [System.IO.File]::WriteAllText($Path, $Content, $utf8NoBom)
53 }
54
55 function Invoke-Native {
56 param(
57 [Parameter(Mandatory = $true)][string]$FilePath,
58 [string[]]$Arguments = @()
59 )
60
61 $previousErrorActionPreference = $ErrorActionPreference
62 $ErrorActionPreference = "Continue"
63 try {
64 & $FilePath @Arguments
65 $exitCode = $LASTEXITCODE
66 }
67 finally {
68 $ErrorActionPreference = $previousErrorActionPreference
69 }
70
71 if ($exitCode -ne 0) {
72 throw "$FilePath exited with code $exitCode."
73 }
74 }
75
76 function Normalize-ReleaseVersion {
77 param(
78 [Parameter(Mandatory = $true)][string]$RawVersion
79 )
80
81 $value = $RawVersion.Trim()
82 if ($value.StartsWith("ver.")) {
83 $value = $value.Substring(4)
84 } elseif ($value.StartsWith("v")) {
85 $value = $value.Substring(1)
86 }
87
88 if ($value -notmatch '^\d+\.\d+\.\d+(-(alpha|rc|next)\d+)?$') {
89 throw "Invalid version '$RawVersion'. Expected x.y.z, x.y.z-alphaN, x.y.z-rcN, or x.y.z-nextN."
90 }
91
92 return $value
93 }
94
95 function Normalize-GitHubRepository {
96 param(
97 [Parameter(Mandatory = $true)][string]$RawRepository
98 )
99
100 $value = $RawRepository.Trim()
101 if ($value -notmatch '^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$') {
102 throw "Invalid GitHub repository '$RawRepository'. Expected owner/name."
103 }
104 return $value
105 }
106
107 function Normalize-BaseUrl {
108 param(
109 [Parameter(Mandatory = $true)][string]$RawUrl
110 )
111
112 $value = $RawUrl.Trim().TrimEnd('/')
113 if ($value -notmatch '^https://') {
114 throw "Invalid public base URL '$RawUrl'. Expected an https URL."
115 }
116 return $value
117 }
118
119 function New-WinGetManifests {
120 param(
121 [Parameter(Mandatory = $true)][string]$PackageVersion,
122 [Parameter(Mandatory = $true)][string]$PackageUrl,
123 [Parameter(Mandatory = $true)][string]$InstallerSha256,
124 [Parameter(Mandatory = $true)][string]$ManifestRoot,
125 [Parameter(Mandatory = $true)][string]$PackageIdentifier,
126 [Parameter(Mandatory = $true)][string]$Publisher,
127 [Parameter(Mandatory = $true)][string]$PublisherUrl,
128 [Parameter(Mandatory = $true)][string]$PublisherSupportUrl,
129 [Parameter(Mandatory = $true)][string]$PackagePageUrl,
130 [Parameter(Mandatory = $true)][string]$LicenseUrl
131 )
132 $releaseDate = Get-Date -Format "yyyy-MM-dd"
133
134 $versionManifest = @"
135 # yaml-language-server: `$schema=https://aka.ms/winget-manifest.version.1.9.0.schema.json
136 PackageIdentifier: $packageIdentifier
137 PackageVersion: $PackageVersion
138 DefaultLocale: en-US
139 ManifestType: version
140 ManifestVersion: 1.9.0
141 "@
142
143 $installerManifest = @"
144 # yaml-language-server: `$schema=https://aka.ms/winget-manifest.installer.1.9.0.schema.json
145 PackageIdentifier: $packageIdentifier
146 PackageVersion: $PackageVersion
147 InstallerType: zip
148 NestedInstallerType: portable
149 ReleaseDate: $releaseDate
150 Installers:
151 - Architecture: x64
152 InstallerUrl: $PackageUrl
153 InstallerSha256: $InstallerSha256
154 NestedInstallerFiles:
155 - RelativeFilePath: bin/opencc.exe
156 PortableCommandAlias: opencc
157 - RelativeFilePath: bin/opencc_dict.exe
158 PortableCommandAlias: opencc_dict
159 - RelativeFilePath: bin/opencc_phrase_extract.exe
160 PortableCommandAlias: opencc_phrase_extract
161 ManifestType: installer
162 ManifestVersion: 1.9.0
163 "@
164
165 $localeManifest = @"
166 # yaml-language-server: `$schema=https://aka.ms/winget-manifest.defaultLocale.1.9.0.schema.json
167 PackageIdentifier: $packageIdentifier
168 PackageVersion: $PackageVersion
169 PackageLocale: en-US
170 Publisher: $Publisher
171 PublisherUrl: $PublisherUrl
172 PublisherSupportUrl: $PublisherSupportUrl
173 Author: Carbo Kuo and OpenCC contributors
174 PackageName: OpenCC
175 PackageUrl: $PackagePageUrl
176 License: Apache-2.0
177 LicenseUrl: $LicenseUrl
178 ShortDescription: Open Chinese Convert
179 Description: Command-line tools and libraries for Simplified Chinese, Traditional Chinese, regional variant, and Japanese Kanji conversion.
180 Moniker: opencc
181 Tags:
182 - chinese
183 - conversion
184 - opencc
185 - simplified-chinese
186 - traditional-chinese
187 ManifestType: defaultLocale
188 ManifestVersion: 1.9.0
189 "@
190
191 Write-Utf8File -Path (Join-Path $ManifestRoot "BYVoid.OpenCC.yaml") -Content $versionManifest
192 Write-Utf8File -Path (Join-Path $ManifestRoot "BYVoid.OpenCC.installer.yaml") -Content $installerManifest
193 Write-Utf8File -Path (Join-Path $ManifestRoot "BYVoid.OpenCC.locale.en-US.yaml") -Content $localeManifest
194 }
195
196 function New-PortableStaging {
197 param(
198 [Parameter(Mandatory = $true)][string]$BuildDir,
199 [Parameter(Mandatory = $true)][string]$InstallRoot,
200 [Parameter(Mandatory = $true)][string]$StagingRoot,
201 [Parameter(Mandatory = $true)][bool]$SkipTests
202 )
203
204 Remove-Item -Recurse -Force $BuildDir -ErrorAction SilentlyContinue
205 Remove-Item -Recurse -Force $InstallRoot -ErrorAction SilentlyContinue
206 Remove-Item -Recurse -Force $StagingRoot -ErrorAction SilentlyContinue
207 New-Item -ItemType Directory -Force -Path $BuildDir | Out-Null
208 New-Item -ItemType Directory -Force -Path $InstallRoot | Out-Null
209 New-Item -ItemType Directory -Force -Path $StagingRoot | Out-Null
210
211 Invoke-Native cmake @(
212 "-S", ".",
213 "-B", $BuildDir,
214 "-A", "x64",
215 "-DBUILD_SHARED_LIBS:BOOL=OFF",
216 "-DBUILD_OPENCC_JIEBA_PLUGIN:BOOL=ON",
217 "-DCMAKE_INSTALL_PREFIX:PATH=$InstallRoot",
218 "-DENABLE_GTEST:BOOL=OFF",
219 "-DENABLE_BENCHMARK:BOOL=OFF"
220 )
221
222 Invoke-Native cmake @("--build", $BuildDir, "--config", "Release", "--target", "install")
223
224 if (-not $SkipTests) {
225 Invoke-Native ctest @("--test-dir", $BuildDir, "--build-config", "Release", "--output-on-failure")
226 }
227
228 New-Item -ItemType Directory -Force -Path (Join-Path $StagingRoot "bin") | Out-Null
229 New-Item -ItemType Directory -Force -Path (Join-Path $StagingRoot "bin\plugins") | Out-Null
230 New-Item -ItemType Directory -Force -Path (Join-Path $StagingRoot "share\opencc") | Out-Null
231
232 Copy-Item -Path (Join-Path $InstallRoot "bin\opencc.exe") -Destination (Join-Path $StagingRoot "bin\opencc.exe")
233 Copy-Item -Path (Join-Path $InstallRoot "bin\opencc_dict.exe") -Destination (Join-Path $StagingRoot "bin\opencc_dict.exe")
234 Copy-Item -Path (Join-Path $InstallRoot "bin\opencc_phrase_extract.exe") -Destination (Join-Path $StagingRoot "bin\opencc_phrase_extract.exe")
235 Copy-Item -Path (Join-Path $InstallRoot "bin\plugins\*.dll") -Destination (Join-Path $StagingRoot "bin\plugins")
236 Copy-Item -Path (Join-Path $InstallRoot "share\opencc\*") -Destination (Join-Path $StagingRoot "share\opencc") -Recurse
237 Copy-Item -Path LICENSE -Destination (Join-Path $StagingRoot "LICENSE.txt")
238 Copy-Item -Path README.md -Destination (Join-Path $StagingRoot "README.md")
239 }
240
241 function New-PortableArchive {
242 param(
243 [Parameter(Mandatory = $true)][string]$StagingRoot,
244 [Parameter(Mandatory = $true)][string]$AssetPath,
245 [Parameter(Mandatory = $true)][string]$ChecksumPath,
246 [Parameter(Mandatory = $true)][string]$AssetName
247 )
248
249 if (-not (Test-Path $StagingRoot)) {
250 throw "Missing portable staging directory: $StagingRoot"
251 }
252
253 Remove-Item -Force $AssetPath -ErrorAction SilentlyContinue
254 Remove-Item -Force $ChecksumPath -ErrorAction SilentlyContinue
255
256 Compress-Archive -Path (Join-Path $StagingRoot '*') -DestinationPath $AssetPath -CompressionLevel Optimal
257
258 $hash = (Get-FileHash -Path $AssetPath -Algorithm SHA256).Hash.ToUpperInvariant()
259 Write-Utf8File -Path $ChecksumPath -Content "$hash *$AssetName`n"
260 return $hash
261 }
262
263 $repoRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot ".."))
264 Push-Location $repoRoot
265
266 try {
267 if ($PrepareOnly -and $PackageOnly) {
268 throw "Use at most one of -PrepareOnly and -PackageOnly."
269 }
270
271 $publicBaseUrl = "https://opencc.byvoid.com/opencc-winget-release"
272
273 if (-not $Version) {
274 $Version = Get-ProjectVersion
275 }
276
277 $Version = Normalize-ReleaseVersion -RawVersion $Version
278 $GitHubRepository = Normalize-GitHubRepository -RawRepository $GitHubRepository
279
280 if ($Arch -ne "x64") {
281 throw "This script currently supports only -Arch x64 for WinGet releases."
282 }
283
284 $packageRoot = "https://github.com/$GitHubRepository"
285 $releaseUrlBase = Normalize-BaseUrl -RawUrl $publicBaseUrl
286 $licenseUrl = "$packageRoot/blob/master/LICENSE"
287 $publisherSupportUrl = "$packageRoot/issues"
288
289 $resolvedBuildDir = [System.IO.Path]::GetFullPath((Join-Path $repoRoot $BuildDir))
290 $resolvedOutputDir = [System.IO.Path]::GetFullPath((Join-Path $repoRoot $OutputDir))
291 $installRoot = Join-Path $resolvedOutputDir "install"
292 $stagingRoot = Join-Path $resolvedOutputDir "staging"
293 $assetName = "OpenCC-$Version-windows-$Arch-portable.zip"
294 $assetPath = Join-Path $resolvedOutputDir $assetName
295 $checksumPath = "$assetPath.sha256"
296 $wingetPathIdentifier = $PackageIdentifier.Replace('.', '\')
297 $wingetRoot = Join-Path $resolvedOutputDir "winget-manifests\$wingetPathIdentifier\$Version"
298 $releaseUrl = "$releaseUrlBase/$assetName"
299
300 if (-not $PackageOnly) {
301 if (Test-Path $resolvedOutputDir) {
302 Get-ChildItem -Path $resolvedOutputDir -Force |
303 Where-Object { $_.FullName -ne $stagingRoot } |
304 Remove-Item -Recurse -Force
305 } else {
306 New-Item -ItemType Directory -Force -Path $resolvedOutputDir | Out-Null
307 }
308
309 New-PortableStaging `
310 -BuildDir $resolvedBuildDir `
311 -InstallRoot $installRoot `
312 -StagingRoot $stagingRoot `
313 -SkipTests ([bool]$SkipTests)
314
315 Write-Host "Portable staging: $stagingRoot"
316 }
317
318 if ($PrepareOnly) {
319 return
320 }
321
322 $hash = New-PortableArchive `
323 -StagingRoot $stagingRoot `
324 -AssetPath $assetPath `
325 -ChecksumPath $checksumPath `
326 -AssetName $assetName
327
328 New-WinGetManifests `
329 -PackageVersion $Version `
330 -PackageUrl $releaseUrl `
331 -InstallerSha256 $hash `
332 -ManifestRoot $wingetRoot `
333 -PackageIdentifier $PackageIdentifier `
334 -Publisher $Publisher `
335 -PublisherUrl $packageRoot `
336 -PublisherSupportUrl $publisherSupportUrl `
337 -PackagePageUrl $packageRoot `
338 -LicenseUrl $licenseUrl
339
340 Write-Host "Release archive: $assetPath"
341 Write-Host "SHA256: $hash"
342 Write-Host "WinGet manifests: $wingetRoot"
343 }
344 finally {
345 Pop-Location
346 }