-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_installer.ps1
More file actions
544 lines (459 loc) · 16.5 KB
/
Copy pathbuild_installer.ps1
File metadata and controls
544 lines (459 loc) · 16.5 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
<#
.SYNOPSIS
Build the AngioEye Windows installer.
.DESCRIPTION
Builds a PyInstaller one-dir bundle for the unified GUI/CLI application, copies
editable pipeline/postprocess modules next to the bundle, generates an Inno Setup
script, and compiles the final setup executable into dist\installer.
.EXAMPLE
.\build_installer.ps1 -IncludeAllExtras
.EXAMPLE
.\build_installer.ps1 -InnoSetupCompiler "C:\Program Files (x86)\Inno Setup 6\ISCC.exe"
.EXAMPLE
.\build_installer.ps1 -Console
#>
[CmdletBinding()]
param(
[string]$InnoSetupCompiler = "",
[string]$Python = "",
[switch]$IncludePipelineExtras,
[switch]$IncludePostprocessExtras,
[switch]$IncludeAllExtras,
[switch]$Console,
[switch]$SkipClean
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$RepoRoot = $PSScriptRoot
$PyprojectPath = Join-Path $RepoRoot "pyproject.toml"
$BuildRoot = Join-Path $RepoRoot "build\installer"
$PyInstallerWorkDir = Join-Path $BuildRoot "pyinstaller-work"
$PyInstallerDistDir = Join-Path $BuildRoot "pyinstaller-dist"
$InstallerOutputDir = Join-Path $RepoRoot "dist\installer"
$GeneratedGuiEntryPoint = Join-Path $BuildRoot "angioeye_gui_entry.py"
$GeneratedTclTkRuntimeHook = Join-Path $BuildRoot "angioeye_tcltk_runtime.py"
$GeneratedSpec = Join-Path $BuildRoot "AngioEye.onedir.spec"
$GeneratedInnoScript = Join-Path $BuildRoot "AngioEye.iss"
function Get-FullPath {
param([Parameter(Mandatory = $true)][string]$Path)
return [System.IO.Path]::GetFullPath($Path)
}
function Assert-ChildPath {
param(
[Parameter(Mandatory = $true)][string]$BasePath,
[Parameter(Mandatory = $true)][string]$TargetPath
)
$baseFull = (Get-FullPath $BasePath).TrimEnd(
[System.IO.Path]::DirectorySeparatorChar,
[System.IO.Path]::AltDirectorySeparatorChar
)
$targetFull = Get-FullPath $TargetPath
$prefix = $baseFull + [System.IO.Path]::DirectorySeparatorChar
if (-not $targetFull.StartsWith($prefix, [System.StringComparison]::OrdinalIgnoreCase)) {
throw "Refusing to operate outside the repository: $targetFull"
}
}
function Read-ProjectMetadata {
if (-not (Test-Path -LiteralPath $PyprojectPath -PathType Leaf)) {
throw "pyproject.toml was not found at $PyprojectPath"
}
$content = Get-Content -LiteralPath $PyprojectPath -Raw -Encoding UTF8
$name = "AngioEye"
$version = $null
if ($content -match '(?m)^\s*name\s*=\s*"([^"]+)"\s*$') {
$name = $Matches[1]
}
if ($content -match '(?m)^\s*version\s*=\s*"([^"]+)"\s*$') {
$version = $Matches[1]
}
if (-not $version) {
throw "Could not read [project].version from pyproject.toml"
}
return [pscustomobject]@{
Name = $name
Version = $version
}
}
function ConvertTo-InnoQuotedValue {
param([Parameter(Mandatory = $true)][string]$Value)
return $Value.Replace('"', '""')
}
function ConvertTo-InnoVersionInfo {
param([Parameter(Mandatory = $true)][string]$Version)
if ($Version -notmatch '^\d+(\.\d+){0,3}$') {
return $null
}
$parts = New-Object System.Collections.Generic.List[string]
foreach ($part in $Version.Split(".")) {
$parts.Add($part)
}
while ($parts.Count -lt 4) {
$parts.Add("0")
}
return ($parts -join ".")
}
function Resolve-InnoSetupCompiler {
if ($InnoSetupCompiler) {
if (-not (Test-Path -LiteralPath $InnoSetupCompiler -PathType Leaf)) {
throw "Inno Setup compiler was not found: $InnoSetupCompiler"
}
return (Get-FullPath $InnoSetupCompiler)
}
$pathCommand = Get-Command "ISCC.exe" -ErrorAction SilentlyContinue
if ($pathCommand) {
return $pathCommand.Source
}
$candidates = @(
"$env:LOCALAPPDATA\Programs\Inno Setup 6\ISCC.exe",
"${env:ProgramFiles(x86)}\Inno Setup 6\ISCC.exe",
"${env:ProgramFiles}\Inno Setup 6\ISCC.exe"
)
foreach ($candidate in $candidates) {
if ($candidate -and (Test-Path -LiteralPath $candidate -PathType Leaf)) {
return (Get-FullPath $candidate)
}
}
throw "Inno Setup compiler (ISCC.exe) was not found. Install Inno Setup 6 or pass -InnoSetupCompiler."
}
function Resolve-PythonExe {
if ($Python) {
if (-not (Test-Path -LiteralPath $Python -PathType Leaf)) {
throw "Python executable was not found: $Python"
}
return (Get-FullPath $Python)
}
$venvPython = Join-Path $RepoRoot ".venv\Scripts\python.exe"
if (Test-Path -LiteralPath $venvPython -PathType Leaf) {
return (Get-FullPath $venvPython)
}
$pathCommand = Get-Command "python.exe" -ErrorAction SilentlyContinue
if ($pathCommand) {
return $pathCommand.Source
}
throw "Python was not found. Install Python, run uv sync, or pass -Python."
}
function Invoke-PyInstaller {
param([Parameter(Mandatory = $true)][string[]]$Arguments)
$uv = Get-Command "uv.exe" -ErrorAction SilentlyContinue
if (-not $uv) {
$uv = Get-Command "uv" -ErrorAction SilentlyContinue
}
if ($uv -and -not $Python) {
$uvArgs = @("run")
if ($IncludeAllExtras -or $IncludePipelineExtras) {
$uvArgs += @("--extra", "pipelines")
}
$uvArgs += @("--extra", "postprocess")
$uvArgs += @("--with", "pyinstaller", "python", "-m", "PyInstaller")
$uvArgs += $Arguments
& $uv.Source @uvArgs
if ($LASTEXITCODE -ne 0) {
throw "PyInstaller failed with exit code $LASTEXITCODE"
}
return
}
$pythonExe = Resolve-PythonExe
& $pythonExe -m PyInstaller @Arguments
if ($LASTEXITCODE -ne 0) {
throw "PyInstaller failed with exit code $LASTEXITCODE"
}
}
function Write-GeneratedEntryPoints {
New-Item -ItemType Directory -Force -Path $BuildRoot | Out-Null
@'
import os
import sys
from pathlib import Path
bundle_root = Path(getattr(sys, "_MEIPASS", Path(sys.executable).resolve().parent))
for tcl_dir in (
bundle_root / "_tcl_data",
bundle_root / "_internal" / "_tcl_data",
bundle_root / "tcl",
bundle_root / "tcl8.6",
):
if (tcl_dir / "init.tcl").is_file():
os.environ["TCL_LIBRARY"] = str(tcl_dir)
break
for tk_dir in (
bundle_root / "_tk_data",
bundle_root / "_internal" / "_tk_data",
bundle_root / "tk",
bundle_root / "tk8.6",
):
if (tk_dir / "tk.tcl").is_file():
os.environ["TK_LIBRARY"] = str(tk_dir)
break
from launcher import main
if __name__ == "__main__":
raise SystemExit(main())
'@ | Set-Content -LiteralPath $GeneratedGuiEntryPoint -Encoding UTF8
@'
import os
import sys
from pathlib import Path
bundle_root = Path(getattr(sys, "_MEIPASS", Path(sys.executable).resolve().parent))
for tcl_dir in (
bundle_root / "_tcl_data",
bundle_root / "_internal" / "_tcl_data",
bundle_root / "tcl",
bundle_root / "tcl8.6",
):
if (tcl_dir / "init.tcl").is_file():
os.environ["TCL_LIBRARY"] = str(tcl_dir)
break
for tk_dir in (
bundle_root / "_tk_data",
bundle_root / "_internal" / "_tk_data",
bundle_root / "tk",
bundle_root / "tk8.6",
):
if (tk_dir / "tk.tcl").is_file():
os.environ["TK_LIBRARY"] = str(tk_dir)
break
'@ | Set-Content -LiteralPath $GeneratedTclTkRuntimeHook -Encoding UTF8
}
function ConvertTo-PythonLiteralPath {
param([Parameter(Mandatory = $true)][string]$Path)
return (Get-FullPath $Path).Replace("\", "\\").Replace("'", "\'")
}
function Write-PyInstallerSpec {
$src = ConvertTo-PythonLiteralPath (Join-Path $RepoRoot "src")
$guiEntry = ConvertTo-PythonLiteralPath $GeneratedGuiEntryPoint
$tclTkRuntimeHook = ConvertTo-PythonLiteralPath $GeneratedTclTkRuntimeHook
$icon = ConvertTo-PythonLiteralPath (Join-Path $RepoRoot "AngioEye.ico")
$logo = ConvertTo-PythonLiteralPath (Join-Path $RepoRoot "Angioeye_logo.png")
$settings = ConvertTo-PythonLiteralPath (Join-Path $RepoRoot "default_settings.json")
$pyproject = ConvertTo-PythonLiteralPath $PyprojectPath
$pyInstallerConsole = "False"
if ($Console) {
$pyInstallerConsole = "True"
}
@"
# -*- mode: python ; coding: utf-8 -*-
from PyInstaller.utils.hooks import collect_data_files, collect_submodules
def common_datas():
datas = []
datas += collect_data_files("pipelines")
datas += collect_data_files("postprocess")
datas += collect_data_files("jinja2")
datas += collect_data_files("matplotlib")
datas += collect_data_files("pandas")
datas += collect_data_files("plotly")
datas += collect_data_files("scipy")
datas += collect_data_files("sv_ttk")
datas += collect_data_files("tkinterdnd2")
datas += [(r'$logo', ".")]
datas += [(r'$icon', ".")]
datas += [(r'$settings', ".")]
datas += [(r'$pyproject', ".")]
return datas
def common_hiddenimports():
hiddenimports = []
hiddenimports += ["angio_eye", "cli", "launcher"]
hiddenimports += collect_submodules("pipelines")
hiddenimports += collect_submodules("postprocess")
hiddenimports += collect_submodules("jinja2")
hiddenimports += collect_submodules("matplotlib")
hiddenimports += collect_submodules("pandas")
hiddenimports += collect_submodules("plotly")
hiddenimports += collect_submodules("scipy")
hiddenimports += collect_submodules("tkinterdnd2")
hiddenimports += ["matplotlib.backends.backend_ps"]
return hiddenimports
a = Analysis(
[r'$guiEntry'],
pathex=[r'$src'],
binaries=[],
datas=common_datas(),
hiddenimports=common_hiddenimports(),
hookspath=[r'$(ConvertTo-PythonLiteralPath (Join-Path $RepoRoot "hooks"))'],
hooksconfig={},
runtime_hooks=[r'$tclTkRuntimeHook'],
excludes=[],
noarchive=False,
optimize=0,
)
pyz = PYZ(a.pure)
def script_entry(name):
for item in a.scripts:
if item[0] == name:
return [item]
raise RuntimeError(f"Could not find PyInstaller script entry: {name}")
gui_exe = EXE(
pyz,
script_entry("angioeye_gui_entry"),
[],
exclude_binaries=True,
name="AngioEye",
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=$pyInstallerConsole,
disable_windowed_traceback=False,
icon=r'$icon',
)
coll = COLLECT(
gui_exe,
a.binaries,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name="AngioEye",
)
"@ | Set-Content -LiteralPath $GeneratedSpec -Encoding UTF8
}
function Copy-EditablePackageModules {
param(
[Parameter(Mandatory = $true)][string]$PackageName,
[Parameter(Mandatory = $true)][string]$BundleDir
)
$sourceDir = Join-Path (Join-Path $RepoRoot "src") $PackageName
$destinationDir = Join-Path $BundleDir $PackageName
if (-not (Test-Path -LiteralPath $sourceDir -PathType Container)) {
return
}
if (Test-Path -LiteralPath $destinationDir -PathType Container) {
Remove-Item -LiteralPath $destinationDir -Recurse -Force
}
New-Item -ItemType Directory -Force -Path $destinationDir | Out-Null
Get-ChildItem -LiteralPath $sourceDir -Force |
Where-Object {
$_.Name -ne "__pycache__" -and
$_.Name -ne "__init__.py" -and
$_.Name -notlike "*.pyc"
} |
ForEach-Object {
$destination = Join-Path $destinationDir $_.Name
if ($_.PSIsContainer) {
Copy-Item -LiteralPath $_.FullName -Destination $destination -Recurse -Force
} else {
Copy-Item -LiteralPath $_.FullName -Destination $destination -Force
}
}
}
function Copy-InstallerExtras {
param([Parameter(Mandatory = $true)][string]$BundleDir)
$extraFiles = @(
"LICENSE",
"THIRD_PARTY_NOTICES",
"README.md",
"AngioEye.ico",
"default_settings.json",
"pyproject.toml"
)
foreach ($fileName in $extraFiles) {
$source = Join-Path $RepoRoot $fileName
if (Test-Path -LiteralPath $source -PathType Leaf) {
Copy-Item -LiteralPath $source -Destination (Join-Path $BundleDir $fileName) -Force
}
}
Copy-EditablePackageModules -PackageName "pipelines" -BundleDir $BundleDir
Copy-EditablePackageModules -PackageName "postprocess" -BundleDir $BundleDir
}
function Write-InnoSetupScript {
param(
[Parameter(Mandatory = $true)][string]$AppName,
[Parameter(Mandatory = $true)][string]$AppVersion,
[Parameter(Mandatory = $true)][string]$BundleDir
)
$appNameInno = ConvertTo-InnoQuotedValue $AppName
$appVersionInno = ConvertTo-InnoQuotedValue $AppVersion
$bundleDirInno = ConvertTo-InnoQuotedValue (Get-FullPath $BundleDir)
$outputDirInno = ConvertTo-InnoQuotedValue (Get-FullPath $InstallerOutputDir)
$licensePathInno = ConvertTo-InnoQuotedValue (Get-FullPath (Join-Path $BundleDir "LICENSE"))
$iconPathInno = ConvertTo-InnoQuotedValue (Get-FullPath (Join-Path $BundleDir "AngioEye.ico"))
$setupBaseNameInno = ConvertTo-InnoQuotedValue ("$AppName-setup-v$AppVersion")
$appVersionedNameInno = ConvertTo-InnoQuotedValue ("$AppName v$AppVersion")
$versionInfo = ConvertTo-InnoVersionInfo $AppVersion
$versionInfoLine = ""
if ($versionInfo) {
$versionInfoLine = "VersionInfoVersion=$versionInfo"
}
@"
#define MyAppName "$appNameInno"
#define MyAppVersion "$appVersionInno"
#define MyAppVersionedName "$appVersionedNameInno"
#define MyAppPublisher "AngioEye"
#define MyAppExeName "AngioEye.exe"
#define MyBundleDir "$bundleDirInno"
#define MyOutputDir "$outputDirInno"
#define MySetupBaseName "$setupBaseNameInno"
#define MyLicensePath "$licensePathInno"
#define MyIconPath "$iconPathInno"
[Setup]
AppId={{8A8F3E62-62E9-41B5-A3B4-548D203D5A89}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppVerName={#MyAppName} {#MyAppVersion}
DefaultDirName={localappdata}\Programs\{#MyAppName}\{#MyAppVersion}
DefaultGroupName={#MyAppName} {#MyAppVersion}
UsePreviousAppDir=no
DisableProgramGroupPage=yes
LicenseFile={#MyLicensePath}
OutputDir={#MyOutputDir}
OutputBaseFilename={#MySetupBaseName}
SetupIconFile={#MyIconPath}
UninstallDisplayIcon={app}\{#MyAppExeName}
Compression=lzma2
SolidCompression=yes
WizardStyle=modern
PrivilegesRequired=lowest
ArchitecturesAllowed=x64compatible
$versionInfoLine
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
[Tasks]
Name: "desktopicon"; Description: "Create a desktop shortcut"; GroupDescription: "Additional icons:"
[InstallDelete]
Type: filesandordirs; Name: "{app}\*"
[Files]
Source: "{#MyBundleDir}\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
[Icons]
Name: "{group}\{#MyAppVersionedName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{group}\Uninstall {#MyAppVersionedName}"; Filename: "{uninstallexe}"
Name: "{autodesktop}\{#MyAppVersionedName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "Launch {#MyAppVersionedName}"; Flags: nowait postinstall skipifsilent
"@ | Set-Content -LiteralPath $GeneratedInnoScript -Encoding UTF8
}
$metadata = Read-ProjectMetadata
$appName = $metadata.Name
$appVersion = $metadata.Version
$bundleDir = Join-Path $PyInstallerDistDir "AngioEye"
$guiExe = Join-Path $bundleDir "AngioEye.exe"
Write-Host "Building $appName $appVersion installer..."
if (-not $SkipClean) {
Assert-ChildPath -BasePath $RepoRoot -TargetPath $BuildRoot
Assert-ChildPath -BasePath $RepoRoot -TargetPath $InstallerOutputDir
Remove-Item -LiteralPath $BuildRoot -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $InstallerOutputDir -Recurse -Force -ErrorAction SilentlyContinue
}
New-Item -ItemType Directory -Force -Path $BuildRoot | Out-Null
New-Item -ItemType Directory -Force -Path $InstallerOutputDir | Out-Null
Write-GeneratedEntryPoints
Write-PyInstallerSpec
Invoke-PyInstaller -Arguments @(
"--noconfirm",
"--clean",
"--distpath", $PyInstallerDistDir,
"--workpath", $PyInstallerWorkDir,
$GeneratedSpec
)
if (-not (Test-Path -LiteralPath $guiExe -PathType Leaf)) {
throw "PyInstaller did not produce the expected executable: $guiExe"
}
Copy-InstallerExtras -BundleDir $bundleDir
$iscc = Resolve-InnoSetupCompiler
Write-InnoSetupScript -AppName $appName -AppVersion $appVersion -BundleDir $bundleDir
& $iscc $GeneratedInnoScript
if ($LASTEXITCODE -ne 0) {
throw "Inno Setup failed with exit code $LASTEXITCODE"
}
$installerPath = Join-Path $InstallerOutputDir "$appName-setup-v$appVersion.exe"
if (-not (Test-Path -LiteralPath $installerPath -PathType Leaf)) {
throw "Inno Setup did not produce the expected installer: $installerPath"
}
Write-Host "Installer created: $installerPath"