diff --git a/.github/workflows/Compile AHK 2.0 and Release.yml b/.github/workflows/Compile AHK 2.0 and Release.yml index 9b59c83..a3ee5ff 100644 --- a/.github/workflows/Compile AHK 2.0 and Release.yml +++ b/.github/workflows/Compile AHK 2.0 and Release.yml @@ -3,335 +3,72 @@ name: Compile AHK 2.0 and Release on: push: tags: - - "v**" - workflow_dispatch: + - "v**" # 当有以 'v' 开头的标签推送到仓库时触发 + workflow_dispatch: # 允许手动触发 jobs: build-and-release: - runs-on: windows-latest + runs-on: windows-latest # 运行环境是 Windows 最新版本 + steps: - name: Checkout code - uses: actions/checkout@v4 + uses: actions/checkout@v4 # 签出仓库代码 - - name: List all files in the repository for debugging - shell: pwsh - run: | - Write-Host "--- Listing all files in the repository root ($(Get-Location)) after checkout ---" - Get-ChildItem -Path (Get-Location) -Recurse | Select-Object FullName, Name, DirectoryName, Mode | Sort-Object FullName - Write-Host "--- End of repository file listing ---" - - - name: Download and Setup AutoHotkey v2 Core (v2.0.19) - shell: pwsh - run: | - $ahkTag = "v2.0.19" # GitHub release tag name, usually starts with 'v' - $ahkFileNameVersion = "2.0.19" # File name part, usually without 'v' - - # 正确的文件名应该是 AutoHotkey_2.0.19.zip - $ahkZipFileName = "AutoHotkey_${ahkFileNameVersion}.zip" - $ahkZipUrl = "https://github.com/AutoHotkey/AutoHotkey/releases/download/$ahkTag/$ahkZipFileName" - $ahkExtractPath = "AutoHotkey_v2_Core_Files" - - Write-Host "Downloading AutoHotkey v2 Core from ${ahkZipUrl}" - # 仍然保留 -UseBasicParsing 以防万一 - Invoke-WebRequest -Uri $ahkZipUrl -OutFile "ahk-v2-core.zip" -ErrorAction Stop -UseBasicParsing - Write-Host "Extracting AutoHotkey v2 Core to ${ahkExtractPath}" - Expand-Archive -Path "ahk-v2-core.zip" -DestinationPath $ahkExtractPath -Force - - Write-Host "Listing contents of ${ahkExtractPath} after extraction:" - Get-ChildItem -Path $ahkExtractPath -Recurse | Select-Object FullName, Name, DirectoryName | Sort-Object FullName - Write-Host "--- End of AutoHotkey v2 Core file listing ---" - - $ahkMainPath = Join-Path $PWD $ahkExtractPath - $ahkUXPath = Join-Path $PWD $ahkExtractPath "UX" - - if ((Test-Path "$ahkMainPath\AutoHotkey.exe") -or ` - (Test-Path "$ahkMainPath\AutoHotkey64.exe") -or ` - (Test-Path "$ahkUXPath\AutoHotkey.exe") -or ` - (Test-Path "$ahkUXPath\AutoHotkey64.exe")) { - - Write-Host "AutoHotkey interpreter found in main or UX directory." - Write-Host "Adding ${ahkMainPath} to PATH (for AutoHotkey interpreter)" - Add-Content $env:GITHUB_PATH "$ahkMainPath" - if (Test-Path $ahkUXPath) { - Write-Host "Adding ${ahkUXPath} to PATH (for AutoHotkey interpreter in UX folder)" - Add-Content $env:GITHUB_PATH "$ahkUXPath" - } - } else { - Write-Error "AutoHotkey interpreter (e.g., AutoHotkey.exe, AutoHotkey64.exe) not found in expected paths. Check the file listing above." - exit 1 - } - - - name: Download and Setup Ahk2Exe (using GitHub API) - shell: pwsh - run: | - $repoOwner = "AutoHotkey" - $repoName = "Ahk2Exe" - $ahk2ExeExtractPath = "AutoHotkey_Ahk2Exe_Files" - $maxRetries = 5 - $retryDelaySeconds = 10 # 每次重试等待10秒 - $attempt = 0 - $success = $false - $ahk2ExeZipUrl = "" # 动态获取的下载链接 - - while ($attempt -lt $maxRetries -and -not $success) { - $attempt++ - Write-Host "Attempt ${attempt} of ${maxRetries}: Fetching latest Ahk2Exe release information..." - try { - # 使用 Invoke-RestMethod 获取最新 release 信息 - $releaseInfo = Invoke-RestMethod -Uri "https://api.github.com/repos/${repoOwner}/${repoName}/releases/latest" -Headers @{Authorization = "token ${{ secrets.GITHUB_TOKEN }}"} -ErrorAction Stop - - # 从 assets 中找到 .zip 文件 - $zipAsset = $releaseInfo.assets | Where-Object { $_.name -like "*.zip" } - - if ($zipAsset) { - $ahk2ExeZipUrl = $zipAsset.browser_download_url - Write-Host "Found Ahk2Exe download URL: ${ahk2ExeZipUrl}" - - # 下载文件 - Write-Host "Downloading Ahk2Exe from ${ahk2ExeZipUrl}" - Invoke-WebRequest -Uri $ahk2ExeZipUrl -OutFile "ahk2exe.zip" -ErrorAction Stop -UseBasicParsing # 确保这里有 -UseBasicParsing - $success = $true - Write-Host "Ahk2Exe downloaded successfully." - } else { - Write-Warning "Could not find a .zip asset in the latest Ahk2Exe release." - if ($attempt -lt $maxRetries) { - Write-Host "Retrying in ${retryDelaySeconds} seconds..." - Start-Sleep -Seconds $retryDelaySeconds - } - } - } catch { - Write-Warning "Failed to fetch release info or download Ahk2Exe: $($_.Exception.Message)" - if ($attempt -lt $maxRetries) { - Write-Host "Retrying in ${retryDelaySeconds} seconds..." - Start-Sleep -Seconds $retryDelaySeconds - } - } - } - - if (-not $success) { - Write-Error "Failed to download Ahk2Exe after ${maxRetries} attempts." - exit 1 - } - - Write-Host "Extracting Ahk2Exe to ${ahk2ExeExtractPath}" - Expand-Archive -Path "ahk2exe.zip" -DestinationPath $ahk2ExeExtractPath -Force - - Write-Host "Listing contents of ${ahk2ExeExtractPath} after extraction:" - Get-ChildItem -Path $ahk2ExeExtractPath -Recurse | Select-Object FullName, Name, DirectoryName | Sort-Object FullName - Write-Host "--- End of Ahk2Exe file listing ---" - - $ahk2ExeBinPath = Join-Path $PWD $ahk2ExeExtractPath - - if (Test-Path "$ahk2ExeBinPath\Ahk2Exe.exe") { - Write-Host "Found Ahk2Exe.exe in: ${ahk2ExeBinPath}" - Write-Host "Adding ${ahk2ExeBinPath} to PATH (for Ahk2Exe.exe)" - Add-Content $env:GITHUB_PATH "$ahk2ExeBinPath" - } else { - Write-Error "Ahk2Exe.exe not found in '${ahk2ExeBinPath}'. Check the Ahk2Exe file listing above." - exit 1 - } - - - name: Verify Ahk2Exe in PATH - shell: pwsh - run: | - Write-Host "Verifying Ahk2Exe command in new PowerShell session..." - if (-not (Get-Command Ahk2Exe -ErrorAction SilentlyContinue)) { - Write-Error "Ahk2Exe.exe not found in PATH. This is unexpected after previous setup." - Write-Host "Current PATH: ${env:PATH}" - exit 1 - } - Write-Host "Ahk2Exe found in PATH at: ${((Get-Command Ahk2Exe).Path)}" - - - name: Test AutoHotkey Interpreter (Optional but good for debugging) - shell: pwsh - run: | - $scriptToTest = "test_compile.ahk" # 或者 DoroHelper.ahk - $ahkExeName = "AutoHotkey64.exe" - $ahkCoreExtractPath = "AutoHotkey_v2_Core_Files" - - $ahkInterpreterPath = Join-Path $PWD $ahkCoreExtractPath $ahkExeName - if (-not (Test-Path $ahkInterpreterPath)) { - $ahkInterpreterPath = Join-Path $PWD $ahkCoreExtractPath "UX" $ahkExeName - } - if (-not (Test-Path $ahkInterpreterPath)) { - Write-Warning "64-bit interpreter (${ahkExeName}) not found for testing. Falling back to AutoHotkey32.exe." - $ahkExeName = "AutoHotkey32.exe" - $ahkInterpreterPath = Join-Path $PWD $ahkCoreExtractPath $ahkExeName - if (-not (Test-Path $ahkInterpreterPath)) { - $ahkInterpreterPath = Join-Path $PWD $ahkCoreExtractPath "UX" $ahkExeName - } - } - - if (-not (Test-Path $ahkInterpreterPath)) { - Write-Error "Could not find any AutoHotkey interpreter for testing in '${ahkCoreExtractPath}'." - Get-ChildItem -Path (Join-Path $PWD $ahkCoreExtractPath) -Recurse | Select-Object FullName, Name - exit 1 - } - - Write-Host "Attempting to run ${scriptToTest} with ${ahkInterpreterPath}..." - $testOutput = (& "$ahkInterpreterPath" "$scriptToTest" 2>&1) - $testExitCode = $LASTEXITCODE - - Write-Host "--- AutoHotkey Interpreter Test Output ---" - Write-Host $testOutput - Write-Host "Interpreter Exit Code: ${testExitCode}" - Write-Host "--- End of Interpreter Test Output ---" - if ($testExitCode -ne 0) { - Write-Warning "Interpreter test failed with exit code ${testExitCode}. This might indicate an issue with the script or interpreter setup." - } else { - Write-Host "Interpreter test successful." - } - - - name: Compile AutoHotkey Script (Detailed Error Logging) - id: compile_script - shell: pwsh - run: | - $ahkCoreExtractPath = "AutoHotkey_v2_Core_Files" - $scriptFile = "DoroHelper.ahk" # 使用您的实际脚本名称 - $iconFile = "doro.ico" - $outputExe = "DoroHelper.exe" # 输出 EXE 的名称 - - Write-Host "--- Diagnosing Ahk2Exe and Dependencies ---" - Write-Host "Attempting to run Ahk2Exe.exe without arguments to get usage info..." - # 运行 Ahk2Exe.exe 并捕获其所有输出 - try { - $ahk2ExeHelpOutput = (& Ahk2Exe.exe 2>&1) - Write-Host "Ahk2Exe.exe no-arg output:" - Write-Host $ahk2ExeHelpOutput - } catch { - Write-Error "Failed to run Ahk2Exe.exe without arguments. Is it in PATH correctly? Error: $($_.Exception.Message)" - exit 1 - } - - if (-not (Test-Path $scriptFile)) { - Write-Error "AHK script file '${scriptFile}' not found at $(Resolve-Path $scriptFile -ErrorAction SilentlyContinue). Current working directory: $(Get-Location)." - exit 1 - } else { - Write-Host "AHK script file found: $(Resolve-Path $scriptFile)" - } - - if ($iconFile -and (Test-Path $iconFile)) { - Write-Host "Icon file found: $(Resolve-Path $iconFile)" - } else { - Write-Warning "Icon file '${iconFile}' not found. Proceeding without custom icon." - $iconFile = "" - } - - Write-Host "Listing contents of current working directory ($(Get-Location)):" - Get-ChildItem -Path (Get-Location) -Recurse -Depth 1 | Select-Object FullName, Name, DirectoryName, Mode | Sort-Object FullName - Write-Host "--- End of current working directory listing ---" - - # 优先尝试使用 64 位基础二进制文件进行编译 - $baseBinaryName = "AutoHotkey64.exe" - $baseBinaryPath = Join-Path $PWD $ahkCoreExtractPath $baseBinaryName - if (-not (Test-Path $baseBinaryPath)) { - $baseBinaryPath = Join-Path $PWD $ahkCoreExtractPath "UX" $baseBinaryName - } - # 如果 64 位不存在,回退到 32 位 - if (-not (Test-Path $baseBinaryPath)) { - Write-Warning "64-bit base binary (${baseBinaryName}) not found. Falling back to AutoHotkey32.exe." - $baseBinaryName = "AutoHotkey32.exe" - $baseBinaryPath = Join-Path $PWD $ahkCoreExtractPath $baseBinaryName - if (-not (Test-Path $baseBinaryPath)) { - $baseBinaryPath = Join-Path $PWD $ahkCoreExtractPath "UX" $baseBinaryName - } - } - - if (-not (Test-Path $baseBinaryPath)) { - Write-Error "Could not find a suitable 64-bit or 32-bit base binary (${baseBinaryName}) in '${ahkCoreExtractPath}' or '$(${ahkCoreExtractPath})\UX'." - Write-Host "Listing contents of ${ahkCoreExtractPath}:" - Get-ChildItem -Path (Join-Path $PWD $ahkCoreExtractPath) -Recurse | Select-Object FullName - exit 1 - } - - Write-Host "Using Ahk2Exe from PATH." - Write-Host "Script: ${scriptFile}" - Write-Host "Output: ${outputExe}" - Write-Host "Icon: ${iconFile}" - Write-Host "Base Binary for compilation: ${baseBinaryPath}" - - # 构建编译参数 - $compileArgs = @("/in", "`"$scriptFile`"", "/out", "`"$outputExe`"", "/bin", "`"$baseBinaryPath`"", "/cp", "65001", "/debug") - if ($iconFile -and (Test-Path $iconFile)) { - $compileArgs += @("/icon", "`"$iconFile`"") - } - - Write-Host "Attempting to compile with Ahk2Exe.exe..." - Write-Host "Compilation Arguments: $($compileArgs -join ' ')" - - # !!! 关键改进:直接执行 Ahk2Exe 并捕获其所有输出和退出代码 !!! - $ahk2ExeOutput = "" - try { - $ahk2ExeOutput = (& Ahk2Exe.exe $compileArgs 2>&1 | Out-String) # 捕获所有输出 - $ahk2ExeExitCode = $LASTEXITCODE - } catch { - Write-Error "Executing Ahk2Exe.exe failed. Error: $($_.Exception.Message)" - exit 1 - } - - Write-Host "--- Ahk2Exe Output Start (Actual Compilation) ---" - Write-Host $ahk2ExeOutput - Write-Host "--- Ahk2Exe Output End (Actual Compilation) ---" - Write-Host "Ahk2Exe Exit Code: ${ahk2ExeExitCode}" - - if ($ahk2ExeExitCode -ne 0) { - Write-Error "Compilation failed with exit code ${ahk2ExeExitCode}. ${outputExe} not found. See Ahk2Exe output above for details." - exit 1 # 编译失败,终止工作流 - } - - # 再次验证文件是否存在,以防 Ahk2Exe 退出代码为0但文件未生成 - if (-not (Test-Path $outputExe)) { - Write-Error "Ahk2Exe reported success (exit code 0), but '${outputExe}' was not found. This indicates an issue with Ahk2Exe itself." - exit 1 - } - - Write-Host "${outputExe} compiled successfully." - echo "EXE_NAME=$outputExe" >> $env:GITHUB_OUTPUT + - name: Compile AutoHotkey Script with Action + id: compile_script # 为此步骤设置 ID,以便后续引用其输出或工作目录 + uses: benmusson/ahk2exe-action@v1 # 使用 benmusson 提供的 AHK 编译 Action + with: + in: .\DoroHelper.ahk # 你的 AHK 脚本路径 + out: .\DoroHelper.exe # 输出的 EXE 文件名 + icon: .\doro.ico # 你的图标文件路径 + target: x64 # 目标架构为 x64 + ahk-tag: v2.0.19 # 指定 AutoHotkey 2.0.19 版本 + github-token: ${{ secrets.GITHUB_TOKEN }} # 用于避免 GitHub API 限速 - name: Extract Version from AHK script - id: get_version - shell: pwsh + id: get_version # 为此步骤设置 ID,以便后续引用其输出 + shell: pwsh # 使用 PowerShell Core run: | - $scriptPath = "DoroHelper.ahk" # 确保这里是您的主脚本文件路径 + $scriptPath = "DoroHelper.ahk" # 你的主脚本文件路径 if (-not (Test-Path $scriptPath)) { Write-Warning "Script file '${scriptPath}' not found for version extraction. Current directory: $(Get-Location)" - echo "VERSION_TAG=0.0.0" >> $env:GITHUB_OUTPUT + echo "VERSION_TAG=0.0.0" >> $env:GITHUB_OUTPUT # 如果找不到脚本,使用默认版本号 exit 0 } - $content = Get-Content -Path $scriptPath -Raw + $content = Get-Content -Path $scriptPath -Raw # 读取脚本内容 + # 使用正则表达式匹配 currentVersion := "..." $match = $content | Select-String -Pattern 'currentVersion\s*:=\s*"([^"]+)"' if ($match) { - $version = $match.Matches[0].Groups[1].Value + $version = $match.Matches[0].Groups[1].Value # 提取版本号 Write-Host "Version found: ${version}" - echo "VERSION_TAG=$version" >> $env:GITHUB_OUTPUT + echo "VERSION_TAG=$version" >> $env:GITHUB_OUTPUT # 设置输出变量 VERSION_TAG } else { Write-Warning "Version pattern (currentVersion := \"...\") not found in ${scriptPath}. Using default '0.0.0'." - echo "VERSION_TAG=0.0.0" >> $env:GITHUB_OUTPUT + echo "VERSION_TAG=0.0.0" >> $env:GITHUB_OUTPUT # 如果没有匹配到,使用默认版本号 } - name: Determine if Pre-release - id: prerelease_check - shell: bash + id: prerelease_check # 为此步骤设置 ID,以便后续引用其输出 + shell: bash # 使用 Bash shell run: | - VERSION_TAG="${{ steps.get_version.outputs.VERSION_TAG }}" + VERSION_TAG="${{ steps.get_version.outputs.VERSION_TAG }}" # 获取上一步输出的版本号 IS_PRERELEASE="false" - if [[ "$VERSION_TAG" == *beta* ]]; then + if [[ "$VERSION_TAG" == *beta* || "$VERSION_TAG" == *alpha* || "$VERSION_TAG" == *rc* ]]; then # 判断是否包含预发布标识符 IS_PRERELEASE="true" fi - echo "IS_PRERELEASE=$IS_PRERELEASE" >> $GITHUB_OUTPUT + echo "IS_PRERELEASE=$IS_PRERELEASE" >> $GITHUB_OUTPUT # 设置输出变量 IS_PRERELEASE echo "Release version: ${VERSION_TAG}, Is Prerelease: ${IS_PRERELEASE}" - name: Create GitHub Release - uses: softprops/action-gh-release@v2 + uses: softprops/action-gh-release@v2 # 使用 GitHub Release Action with: - tag_name: ${{ steps.get_version.outputs.VERSION_TAG }} - name: ${{ steps.get_version.outputs.VERSION_TAG }} + tag_name: ${{ steps.get_version.outputs.VERSION_TAG }} # 使用从脚本提取的版本号作为标签名 + name: ${{ steps.get_version.outputs.VERSION_TAG }} # Release 的标题 body: | Automated release for DoroHelper ${{ steps.get_version.outputs.VERSION_TAG }} Compiled from commit ${{ github.sha }} - draft: false - prerelease: ${{ steps.prerelease_check.outputs.IS_PRERELEASE }} - files: | - DoroHelper.exe # 确保这里与 Compile AutoHotkey Script 步骤的 'outputExe' 变量一致 + draft: false # 不是草稿 Release + prerelease: ${{ steps.prerelease_check.outputs.IS_PRERELEASE }} # 根据上一步的判断设置是否为预发布 + files: DoroHelper.exe # <-- **此处已修正,仅保留文件名** env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # GitHub 令牌