<# .SYNOPSIS Automated build script using Portable Python (No installation required) .DESCRIPTION This script downloads a temporary Python environment, installs dependencies, builds the executable, and then cleans up the Python environment. Your Windows system remains clean. #> $ErrorActionPreference = "Stop" # Configuration $PythonVer = "3.10.11" $PythonUrl = "https://www.python.org/ftp/python/$PythonVer/python-$PythonVer-embed-amd64.zip" $GetPipUrl = "https://bootstrap.pypa.io/get-pip.py" $WorkDir = Get-Location $TempDir = Join-Path $WorkDir ".temp_python" $PythonExe = Join-Path $TempDir "python.exe" $PipExe = Join-Path $TempDir "Scripts\pip.exe" Write-Host "[Step 1/6] Setting up temporary workspace..." -ForegroundColor Cyan if (Test-Path $TempDir) { Remove-Item $TempDir -Recurse -Force } New-Item -ItemType Directory -Path $TempDir | Out-Null Write-Host "[Step 2/6] Downloading Portable Python $PythonVer..." -ForegroundColor Cyan $ZipPath = Join-Path $WorkDir "python-embed.zip" Invoke-WebRequest -Uri $PythonUrl -OutFile $ZipPath Expand-Archive -Path $ZipPath -DestinationPath $TempDir Remove-Item $ZipPath # Fix python3xx._pth to allow 'import site' (Crucial for pip) $PthFile = Get-ChildItem $TempDir -Filter "python*._pth" | Select-Object -First 1 if ($PthFile) { $Content = Get-Content $PthFile.FullName $Content = $Content -replace "#import site", "import site" $Content | Set-Content $PthFile.FullName Write-Host " -> Enabled 'import site' in $($PthFile.Name)" -ForegroundColor Gray } Write-Host "[Step 3/6] Installing pip..." -ForegroundColor Cyan $GetPipPath = Join-Path $TempDir "get-pip.py" Invoke-WebRequest -Uri $GetPipUrl -OutFile $GetPipPath & $PythonExe $GetPipPath --no-warn-script-location Remove-Item $GetPipPath Write-Host "[Step 4/6] Installing dependencies..." -ForegroundColor Cyan # Install PyInstaller first & $PythonExe -m pip install pyinstaller --no-warn-script-location # Install greenlet binary (Critical Fix) # Playwright depends on greenlet, which needs C++ runtime. # Pre-built wheel usually works, but sometimes fails in embeddable python. # Explicitly installing it helps. & $PythonExe -m pip install greenlet --no-warn-script-location # Install project requirements & $PythonExe -m pip install -r requirements.txt --no-warn-script-location -i https://pypi.tuna.tsinghua.edu.cn/simple Write-Host "[Step 5/6] Building executable..." -ForegroundColor Cyan # Run PyInstaller # --add-data "src;dest": 将文件打包到目录中 (Windows 用 ; 分隔) # 这里我们希望 product_map.txt 和 使用说明.txt 都在输出目录中 # PyInstaller 的 --onedir 模式下,非代码资源如果不指定,不会自动复制到 dist & $PythonExe -m PyInstaller --noconfirm --onedir --console --clean --name "scbank_tool" main.py # 手动复制配置文件到发布目录 (比 --add-data 更直观,因为用户需要编辑) Write-Host "Copying configuration files..." -ForegroundColor Gray Copy-Item "product_map.txt" -Destination "dist\scbank_tool\" -Force Copy-Item "使用说明.txt" -Destination "dist\scbank_tool\" -Force if ($LASTEXITCODE -eq 0) { Write-Host "[Success] Build completed successfully!" -ForegroundColor Green Write-Host "Executable location: dist\scbank_tool\scbank_tool.exe" } else { Write-Host "[Error] Build failed." -ForegroundColor Red } Write-Host "[Step 6/6] Cleaning up temporary Python environment..." -ForegroundColor Cyan Remove-Item $TempDir -Recurse -Force Write-Host "Done." Read-Host "Press Enter to exit..."