-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
122 lines (95 loc) · 3.12 KB
/
Copy pathbuild.ps1
File metadata and controls
122 lines (95 loc) · 3.12 KB
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
param(
[ValidatePattern('^\d+\.\d+\.\d+$')]
[string]$Version,
[switch]$NoIncrement,
[switch]$Upx,
[switch]$NoUpx
)
$ErrorActionPreference = "Stop"
$packageName = "resizerrust"
$cargoToml = Join-Path $PSScriptRoot "Cargo.toml"
$cargoLock = Join-Path $PSScriptRoot "Cargo.lock"
function Get-CargoVersion {
$content = Get-Content $cargoToml -Raw
if ($content -notmatch '(?ms)^\[package\]\s+.*?^version\s*=\s*"([^"]+)"') {
throw "version not found in $cargoToml"
}
$matches[1]
}
function Set-CargoLockVersion {
param([string]$NewVersion)
if (!(Test-Path $cargoLock)) {
return
}
$lines = Get-Content $cargoLock
$inPackage = $false
$isAppPackage = $false
$updated = $false
for ($i = 0; $i -lt $lines.Count; $i++) {
if ($lines[$i] -eq "[[package]]") {
$inPackage = $true
$isAppPackage = $false
continue
}
if ($inPackage -and $lines[$i] -eq "name = `"$packageName`"") {
$isAppPackage = $true
continue
}
if ($isAppPackage -and $lines[$i] -match '^version = ') {
$lines[$i] = "version = `"$NewVersion`""
$updated = $true
break
}
}
if (!$updated) {
throw "$packageName version not found in $cargoLock"
}
Set-Content $cargoLock $lines
}
function Set-CargoVersion {
param([string]$NewVersion)
$content = Get-Content $cargoToml -Raw
$updated = [regex]::Replace(
$content,
'(?ms)^(\[package\]\s+.*?^version\s*=\s*)"[^"]+"',
('${1}"' + $NewVersion + '"'),
1
)
if ($updated -eq $content -and (Get-CargoVersion) -ne $NewVersion) {
throw "could not update version in $cargoToml"
}
Set-Content $cargoToml $updated -NoNewline
Set-CargoLockVersion $NewVersion
}
function Run {
param([string]$Command, [string[]]$Arguments)
& $Command @Arguments
if ($LASTEXITCODE -ne 0) {
throw "$Command $($Arguments -join ' ') failed"
}
}
$nativeCmake = "$env:ProgramFiles\CMake\bin\cmake.exe"
if (Test-Path $nativeCmake) {
$env:CMAKE = $nativeCmake
}
if (![string]::IsNullOrWhiteSpace($Version)) {
Set-CargoVersion $Version
}
Run "cargo" @("build", "--release", "--locked")
$artifactDir = Join-Path $PSScriptRoot "artifacts"
$targetExe = Join-Path $PSScriptRoot "target\release\$packageName.exe"
$artifactExe = Join-Path $artifactDir "$packageName.exe"
$artifactSha = Join-Path $artifactDir "$packageName.exe.sha256"
$rootExe = Join-Path $PSScriptRoot "$packageName.exe"
if (!(Test-Path $targetExe)) {
throw "Build did not create $targetExe"
}
New-Item -ItemType Directory -Path $artifactDir -Force | Out-Null
Copy-Item $targetExe $artifactExe -Force
if ($Upx -and !$NoUpx -and (Test-Path (Join-Path $PSScriptRoot "upx.exe"))) {
Run (Join-Path $PSScriptRoot "upx.exe") @("-3", $artifactExe)
}
Copy-Item $artifactExe $rootExe -Force
$hash = (Get-FileHash $artifactExe -Algorithm SHA256).Hash.ToLowerInvariant()
"$hash $packageName.exe" | Set-Content $artifactSha
Write-Host "Built $artifactExe ($(Get-CargoVersion))" -ForegroundColor Green