Repository navigation
Expand file tree
/
Copy pathbuild.ps1
More file actions
164 lines (147 loc) · 5.62 KB
/
Copy pathbuild.ps1
File metadata and controls
164 lines (147 loc) · 5.62 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
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
[CmdletBinding()]
param(
[int]$BuildJobs,
[string]$GitCodeGuiRepository = 'lzt404/EasyCLIProxyAPI',
[string]$GitCodeCoreRepository = 'lzt404/CLIProxyAPI',
[switch]$SkipCopy
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$RootDir = $PSScriptRoot
$TargetDir = Join-Path $RootDir 'src-tauri\target'
$BuildRootMarker = Join-Path $TargetDir '.build-workspace-root'
$AppBin = Join-Path $TargetDir 'release\cpa-gui.exe'
$CopyScript = Join-Path $RootDir 'copy.ps1'
Set-Location -LiteralPath $RootDir
function Test-StaleTauriBuildCache {
param([string]$BuildDir, [string]$TargetDir)
if (-not (Test-Path -LiteralPath $BuildDir -PathType Container)) {
return $false
}
$TargetPrefix = $TargetDir.TrimEnd('\', '/') + [IO.Path]::DirectorySeparatorChar
foreach ($directory in Get-ChildItem -LiteralPath $BuildDir -Directory -Filter 'tauri-*') {
$output = Join-Path $directory.FullName 'output'
if (-not (Test-Path -LiteralPath $output -PathType Leaf)) {
continue
}
foreach ($line in [IO.File]::ReadLines($output)) {
$marker = 'PERMISSION_FILES_PATH='
$position = $line.IndexOf($marker, [StringComparison]::Ordinal)
if ($position -lt 0) {
continue
}
$permissionPath = $line.Substring($position + $marker.Length).Trim()
if (-not $permissionPath.StartsWith($TargetPrefix, [StringComparison]::OrdinalIgnoreCase) -or
-not (Test-Path -LiteralPath $permissionPath)) {
return $true
}
}
}
return $false
}
if (-not $PSBoundParameters.ContainsKey('BuildJobs')) {
$BuildJobs = if ($env:CARGO_BUILD_JOBS) {
[int]$env:CARGO_BUILD_JOBS
}
else {
16
}
}
if ($BuildJobs -lt 1 -or $BuildJobs -gt 256) {
throw 'BuildJobs must be between 1 and 256.'
}
if ($GitCodeGuiRepository -notmatch '^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$') {
throw 'GitCodeGuiRepository must use the owner/repository format.'
}
if ($GitCodeCoreRepository -notmatch '^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$') {
throw 'GitCodeCoreRepository must use the owner/repository format.'
}
if (-not (Get-Command bun -ErrorAction SilentlyContinue)) {
throw 'bun is not installed or not in PATH.'
}
Write-Host "Cargo build jobs: $BuildJobs"
Write-Host "GitCode GUI fallback repository: $GitCodeGuiRepository"
Write-Host "GitCode core fallback repository: $GitCodeCoreRepository"
$CachedRoot = if (Test-Path -LiteralPath $BuildRootMarker -PathType Leaf) {
(Get-Content -LiteralPath $BuildRootMarker -Raw).Trim()
}
else {
''
}
if (-not $CachedRoot.Equals($RootDir, [StringComparison]::OrdinalIgnoreCase) -and
(Test-StaleTauriBuildCache -BuildDir (Join-Path $TargetDir 'release\build') -TargetDir $TargetDir)) {
$ExpectedTargetDir = [IO.Path]::GetFullPath((Join-Path $RootDir 'src-tauri\target'))
$ResolvedTargetDir = (Resolve-Path -LiteralPath $TargetDir).Path
if (-not $ResolvedTargetDir.Equals($ExpectedTargetDir, [StringComparison]::OrdinalIgnoreCase)) {
throw "Refusing to clean an unexpected Cargo target directory: $ResolvedTargetDir"
}
if (-not (Get-Command cargo -ErrorAction SilentlyContinue)) {
throw 'cargo is required to refresh the stale Tauri build cache.'
}
Write-Host 'Tauri release cache references a previous workspace path; rebuilding its generated permissions.'
for ($attempt = 1; $attempt -le 3; $attempt++) {
& cargo clean --manifest-path (Join-Path $RootDir 'src-tauri\Cargo.toml') --target-dir $TargetDir -p tauri --release
if ($LASTEXITCODE -eq 0) {
break
}
if ($attempt -eq 3) {
throw "Failed to refresh the Tauri release cache (exit code $LASTEXITCODE)."
}
Start-Sleep -Seconds $attempt
}
}
& bun install
if ($LASTEXITCODE -ne 0) {
throw "bun install failed with exit code $LASTEXITCODE."
}
$PreviousBuildJobs = $env:CARGO_BUILD_JOBS
$PreviousCargoTargetDir = $env:CARGO_TARGET_DIR
$PreviousGitCodeGuiRepository = $env:GITCODE_GUI_REPOSITORY
$PreviousGitCodeCoreRepository = $env:GITCODE_CORE_REPOSITORY
try {
$env:CARGO_BUILD_JOBS = [string]$BuildJobs
$env:CARGO_TARGET_DIR = $TargetDir
$env:GITCODE_GUI_REPOSITORY = $GitCodeGuiRepository
$env:GITCODE_CORE_REPOSITORY = $GitCodeCoreRepository
& bun tauri build --no-bundle
if ($LASTEXITCODE -ne 0) {
throw "Tauri build failed with exit code $LASTEXITCODE."
}
[IO.File]::WriteAllText($BuildRootMarker, $RootDir, [Text.UTF8Encoding]::new($false))
}
finally {
if ($null -eq $PreviousCargoTargetDir) {
Remove-Item Env:CARGO_TARGET_DIR -ErrorAction SilentlyContinue
}
else {
$env:CARGO_TARGET_DIR = $PreviousCargoTargetDir
}
if ($null -eq $PreviousBuildJobs) {
Remove-Item Env:CARGO_BUILD_JOBS -ErrorAction SilentlyContinue
}
else {
$env:CARGO_BUILD_JOBS = $PreviousBuildJobs
}
if ($null -eq $PreviousGitCodeGuiRepository) {
Remove-Item Env:GITCODE_GUI_REPOSITORY -ErrorAction SilentlyContinue
}
else {
$env:GITCODE_GUI_REPOSITORY = $PreviousGitCodeGuiRepository
}
if ($null -eq $PreviousGitCodeCoreRepository) {
Remove-Item Env:GITCODE_CORE_REPOSITORY -ErrorAction SilentlyContinue
}
else {
$env:GITCODE_CORE_REPOSITORY = $PreviousGitCodeCoreRepository
}
}
if (-not (Test-Path -LiteralPath $AppBin -PathType Leaf)) {
throw "Build finished, but executable not found: $AppBin"
}
Write-Host "Built: $AppBin"
if ($SkipCopy) {
Write-Host 'Portable copy skipped; run .\copy.ps1 after closing the running app.'
}
else {
& $CopyScript
}