Files
douyi_guaji_main/packaging/pyinstaller/build_windows.ps1
T

164 lines
5.5 KiB
PowerShell

param(
[string]$Version = "",
[switch]$SkipInstall,
[switch]$Obfuscate,
[switch]$PackExe,
[switch]$PackExeForce,
[string]$UpxDir = ""
)
$ErrorActionPreference = "Stop"
$Root = Resolve-Path (Join-Path $PSScriptRoot "..\..")
Set-Location $Root
if (-not $Version) {
$Version = (python -c "from douyin.core.version import APP_VERSION; print(str(APP_VERSION).lstrip('Vv'))").Trim()
}
if (-not $SkipInstall) {
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python -m pip install pyinstaller
if ($Obfuscate) {
python -m pip install pyarmor
}
}
$DistRoot = Join-Path $Root "dist\DouyinGuaji-$Version"
if (Test-Path $DistRoot) {
Remove-Item -LiteralPath $DistRoot -Recurse -Force
}
New-Item -ItemType Directory -Force $DistRoot | Out-Null
$BuildSourceRoot = $Root
$HiddenImportsFile = Join-Path $Root "build\obfuscation-hidden-imports.json"
if ($Obfuscate) {
$BuildSourceRoot = Join-Path $Root "build\obfuscated-src"
python -m tools.obfuscation_staging --source-root $Root --staging-root $BuildSourceRoot --hidden-imports-output $HiddenImportsFile
if ($LASTEXITCODE -ne 0) {
throw "PyArmor obfuscation failed with exit code $LASTEXITCODE"
}
}
$CommonArgs = @(
"--clean",
"--noconfirm",
"--distpath", $DistRoot,
"--workpath", (Join-Path $Root "build\pyinstaller"),
"--paths", $BuildSourceRoot,
"--add-data", "assets;assets",
"--add-data", "webui;webui",
"--add-data", "config\README.md;config",
"--hidden-import", "PIL._tkinter_finder",
"--hidden-import", "ttkbootstrap",
"--hidden-import", "logging.handlers",
"--hidden-import", "cryptography.hazmat.primitives.serialization",
"--collect-submodules", "douyin",
"--collect-submodules", "gui",
"--collect-submodules", "operations"
)
$ExcludedOptionalModules = @(
"easyocr",
"matplotlib",
"modelscope",
"pandas",
"pytest",
"scipy",
"skimage",
"tensorflow",
"torch",
"torchvision"
)
foreach ($name in $ExcludedOptionalModules) {
$CommonArgs += @("--exclude-module", $name)
}
if ($Obfuscate -and (Test-Path $HiddenImportsFile)) {
$HiddenImports = Get-Content -LiteralPath $HiddenImportsFile -Raw | ConvertFrom-Json
$CliOnlyHiddenImportRoots = @("cv2", "numpy", "paddleocr", "paddle")
$RunCliArgs = @()
foreach ($name in $HiddenImports) {
$nameText = [string]$name
$rootName = ($nameText -split "\.")[0]
if ($CliOnlyHiddenImportRoots -contains $rootName) {
$RunCliArgs += @("--hidden-import", $nameText)
} else {
$CommonArgs += @("--hidden-import", $nameText)
}
}
} else {
$RunCliArgs = @()
}
python -m PyInstaller @CommonArgs --name DouyinGuajiGUI --windowed (Join-Path $BuildSourceRoot "gui\gui_app.py")
python -m PyInstaller @CommonArgs --name web_gui --console (Join-Path $BuildSourceRoot "gui\web_gui.py")
python -m PyInstaller @CommonArgs @RunCliArgs --name run_cli --console (Join-Path $BuildSourceRoot "bin\run_cli.py")
foreach ($name in @("DouyinGuajiGUI", "web_gui", "run_cli")) {
$srcDir = Join-Path $DistRoot $name
Copy-Item -LiteralPath (Join-Path $srcDir "$name.exe") -Destination (Join-Path $DistRoot "$name.exe") -Force
$srcInternal = Join-Path $srcDir "_internal"
$dstInternal = Join-Path $DistRoot "_internal"
if (Test-Path $srcInternal) {
New-Item -ItemType Directory -Force $dstInternal | Out-Null
Get-ChildItem -LiteralPath $srcInternal -Force | Copy-Item -Destination $dstInternal -Recurse -Force
}
Remove-Item -LiteralPath $srcDir -Recurse -Force
}
foreach ($dir in @("assets", "webui")) {
$target = Join-Path $DistRoot $dir
if (Test-Path $target) {
Remove-Item -LiteralPath $target -Recurse -Force
}
Copy-Item -LiteralPath (Join-Path $Root $dir) -Destination $target -Recurse -Force
}
New-Item -ItemType Directory -Force (Join-Path $DistRoot "config") | Out-Null
Copy-Item -LiteralPath (Join-Path $Root "config\README.md") -Destination (Join-Path $DistRoot "config\README.md") -Force
foreach ($dir in @("logs", "pic", "state")) {
$runtimeDir = Join-Path $DistRoot $dir
New-Item -ItemType Directory -Force $runtimeDir | Out-Null
New-Item -ItemType File -Force (Join-Path $runtimeDir ".keep") | Out-Null
}
$licensePath = Join-Path $Root "license.dat"
if (Test-Path $licensePath) {
Copy-Item -LiteralPath $licensePath -Destination (Join-Path $DistRoot "license.dat") -Force
}
Copy-Item -LiteralPath (Join-Path $Root "README.md") -Destination (Join-Path $DistRoot "README.md") -Force
if ($PackExe) {
$PackArgs = @($DistRoot)
if ($UpxDir) {
$PackArgs += @("--upx-dir", $UpxDir)
}
if ($PackExeForce) {
$PackArgs += "--force"
}
python -m tools.exe_packing @PackArgs
if ($LASTEXITCODE -ne 0) {
throw "UPX packing failed with exit code $LASTEXITCODE"
}
}
python -m tools.distribution_manifest $DistRoot
if ($LASTEXITCODE -ne 0) {
throw "Distribution manifest check failed with exit code $LASTEXITCODE"
}
python -m tools.exe_packing $DistRoot --smoke-only
if ($LASTEXITCODE -ne 0) {
throw "Executable smoke check failed with exit code $LASTEXITCODE"
}
python -m tools.distribution_manifest $DistRoot
if ($LASTEXITCODE -ne 0) {
throw "Post-smoke distribution manifest check failed with exit code $LASTEXITCODE"
}
Write-Host "[OK] Portable package: $DistRoot"
Write-Host "[NEXT] If Inno Setup is installed: iscc packaging\inno\douyin_guaji.iss /DMyAppVersion=$Version"