kiwix-js-pwa/scripts/Rewrite-AppVersion.ps1
Jaifroid 08ec9ce00f Fix version number rewriting to take account of SHA
Former-commit-id: a259d3adae04d642b3cc6d174a29b1b318fe01a6 [formerly de928fe89d2488a1d61c033664ef7787c270fe54] [formerly 88aa47db0d11a7bc16f989fc6e7d54b29521a838] [formerly 6c237f6569c37428a1418cdf11f11018d3103b76 [formerly 09a0ebfe43e4b8b8e504c63e5d5f1488bb055c0b [formerly e45f10629d829e04290c48d313d56bb7917bcfcc]]]
Former-commit-id: fdd3826f3a03647a5c325fddd0a623ce9a19e9d9 [formerly 00357d7248b2c8f6fccec5076142d3d907916ee0 [formerly 7049b52d4f2aa72338be72da7e48cd047d8432d1]]
Former-commit-id: 08ecaaf170c9ce86c554fa3c00a42f0223189146 [formerly 04f6e65096c968920a58ff5967f922a93a64eb94]
Former-commit-id: 0b935f2a9c1bfef616157b1ba9f24426a4f2bcf4
2022-04-03 17:26:50 +01:00

61 lines
2.7 KiB
PowerShell

# Sets the App Version Number if $INPUT_VERSION is provided, or sets up a nightly version if launched by CRON
# Script is intended to be run by a GitHub Action, but an input can be provided for testing
[CmdletBinding()]
param (
[string]$customversion = ''
)
if ($customversion) {
"`nUser set custom input version: $customversion"
$INPUT_VERSION = $customversion
}
if ($INPUT_TARGET -eq "nightly") {
"`nUser manually requested a nighlty build..."
$CRON_LAUNCHED = "1"
}
if ($INPUT_VERSION) {
$VERSION = $INPUT_VERSION
} elseif ($TAG_VERSION) {
$VERSION = $TAG_VERSION
} else {
# No version was provided, so we use one from init.js, and ensure all the others match
$app_params = Select-String 'appVersion' "$PSScriptRoot\..\www\js\init.js" -List
if ($app_params -match 'params\[[''"]appVersion[''"]]\s*=\s*[''"]([^''"]+)') {
$app_tag = $matches[1]
$VERSION = "v$app_tag"
# Add a commit SHA if launched by CRON
if ($CRON_LAUNCHED) {
$COMMIT_ID = $(git rev-parse --short HEAD)
$VERSION = "v$app_tag-$COMMIT_ID"
}
} else {
"`nCould not construct a valid nightly version number."
}
}
if ($VERSION -match '^v?[\d.]') {
$VERSION = $VERSION -replace '^v', ''
"`nSetting App Version to $VERSION in service-worker.js and init.js ..."
(Get-Content ./service-worker.js) -replace '(appVersion\s*=\s*["''])[^"'']+', "`${1}$VERSION" | Set-Content -encoding "utf8BOM" ./service-worker.js
(Get-Content ./www/js/init.js) -replace '(appVersion..\s*=\s*["''])[^"'']+', "`${1}$VERSION" | Set-Content -encoding "utf8BOM" ./www/js/init.js
$PackageJson = Get-Content -Raw ./package.json
$nwVersion = $PackageJson -match '"build":\s\{[^"]+"nwVersion":\s"([^"'']+)'
$CustomVersion = $VERSION -replace '^([^-]+)(-[0-9a-z]{7})?.*', '$1$2-E'
if ($nwVersion) {
$nwVersion = $matches[1]
$CustomVersion = $customversion -creplace '-E', '-N'
$BuildNWJSScript = Get-Content -Raw ./scripts/Build-NWJS.ps1
"Setting App Version to $CustomVersion in Build-NWJS.ps1 ..."
$BuildNWJSScript = $BuildNWJSScript -replace '(appBuild\s*=\s*["''])[^"'']+', ("`${1}$CustomVersion")
"Setting NWJS build to $nwVersion in Build-NWJS.ps1 ..."
$BuildNWJSScript = $BuildNWJSScript -replace '(version10\s*=\s*["''])[^"'']+', "`${1}$nwVersion"
Set-Content -encoding "utf8BOM" ./scripts/Build-NWJS.ps1 $BuildNWJSScript
}
"Setting App Version to $CustomVersion in package.json ...`n"
$PackageJson = $PackageJson -replace '("version":\s+")[^"]+', "`${1}$CustomVersion"
Set-Content ./package.json $PackageJson
} else {
"No valid INPUT_VERSION or TAG_VERSION were provided. File version numbers were unchanged.`n"
}