|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +#Requires -Version 5.1 |
| 3 | + |
| 4 | +<# |
| 5 | +.SYNOPSIS |
| 6 | + Install Drycc Workflow CLI for Windows. |
| 7 | +
|
| 8 | +.DESCRIPTION |
| 9 | + This script downloads and installs the Drycc Workflow CLI (drycc.exe) |
| 10 | + to the specified directory. |
| 11 | +
|
| 12 | + Supports one-liner installation via: |
| 13 | + irm https://www.drycc.cc/install-cli.ps1 | iex |
| 14 | +
|
| 15 | + Environment variables: |
| 16 | + INSTALL_DRYCC_PATH - Override installation directory |
| 17 | + INSTALL_DRYCC_VERSION - Override version to install |
| 18 | + INSTALL_DRYCC_MIRROR - Set to "cn" for China mirror |
| 19 | +
|
| 20 | +.PARAMETER InstallPath |
| 21 | + The directory to install drycc.exe. Defaults to $env:LOCALAPPDATA\drycc. |
| 22 | +
|
| 23 | +.PARAMETER Version |
| 24 | + The version of drycc to install. Defaults to 'stable'. |
| 25 | +
|
| 26 | +.EXAMPLE |
| 27 | + .\install-cli.ps1 |
| 28 | +
|
| 29 | +.EXAMPLE |
| 30 | + .\install-cli.ps1 -InstallPath "C:\Program Files\drycc" |
| 31 | +
|
| 32 | +.EXAMPLE |
| 33 | + $env:INSTALL_DRYCC_MIRROR="cn"; irm https://www.drycc.cc/install-cli.ps1 | iex |
| 34 | +#> |
| 35 | + |
| 36 | +param( |
| 37 | + [string]$InstallPath = "$env:LOCALAPPDATA\drycc", |
| 38 | + [string]$Version = "stable" |
| 39 | +) |
| 40 | + |
| 41 | +$ErrorActionPreference = "Stop" |
| 42 | + |
| 43 | +# Support environment variables for one-liner installation |
| 44 | +if ($env:INSTALL_DRYCC_PATH) { |
| 45 | + $InstallPath = $env:INSTALL_DRYCC_PATH |
| 46 | +} |
| 47 | +if ($env:INSTALL_DRYCC_VERSION) { |
| 48 | + $Version = $env:INSTALL_DRYCC_VERSION |
| 49 | +} |
| 50 | + |
| 51 | +# Support mirror for China users |
| 52 | +if ($env:INSTALL_DRYCC_MIRROR -eq "cn") { |
| 53 | + $script:DryccBinUrlBase = "https://github.com/drycc/workflow-cli/releases" |
| 54 | +} else { |
| 55 | + $script:DryccBinUrlBase = "https://github.com/drycc/workflow-cli/releases" |
| 56 | +} |
| 57 | + |
| 58 | +function Get-Architecture { |
| 59 | + $arch = $env:PROCESSOR_ARCHITECTURE.ToLower() |
| 60 | + switch ($arch) { |
| 61 | + "amd64" { return "amd64" } |
| 62 | + "x86" { return "386" } |
| 63 | + "arm64" { return "arm64" } |
| 64 | + default { |
| 65 | + Write-Error "Unsupported architecture: $arch" |
| 66 | + exit 1 |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +function Get-LatestVersion { |
| 72 | + $releasesUrl = "$script:DryccBinUrlBase" |
| 73 | + try { |
| 74 | + $response = Invoke-WebRequest -Uri $releasesUrl -UseBasicParsing |
| 75 | + } catch { |
| 76 | + Write-Error "Could not fetch releases page from $releasesUrl" |
| 77 | + exit 1 |
| 78 | + } |
| 79 | + |
| 80 | + # FIX #1: The server may not return a Content-Type header, causing |
| 81 | + # Invoke-WebRequest to return $response.Content as System.Byte[] instead |
| 82 | + # of a decoded string. We must explicitly decode it before regex matching. |
| 83 | + $content = $response.Content |
| 84 | + if ($content -is [System.Byte[]]) { |
| 85 | + $content = [System.Text.Encoding]::UTF8.GetString($content) |
| 86 | + } |
| 87 | + |
| 88 | + $version = $content | |
| 89 | + Select-String -Pattern '/drycc/workflow-cli/releases/tag/(v[0-9\.]+)' | |
| 90 | + ForEach-Object { $_.Matches[0].Groups[1].Value } | |
| 91 | + Select-Object -First 1 |
| 92 | + |
| 93 | + if (-not $version) { |
| 94 | + Write-Error "Could not extract version from $releasesUrl" |
| 95 | + exit 1 |
| 96 | + } |
| 97 | + |
| 98 | + return $version |
| 99 | +} |
| 100 | + |
| 101 | +function Install-DryccCli { |
| 102 | + $platform = "windows" |
| 103 | + $arch = Get-Architecture |
| 104 | + $latestVersion = Get-LatestVersion |
| 105 | + |
| 106 | + if ($Version -eq "stable") { |
| 107 | + $Version = $latestVersion |
| 108 | + } |
| 109 | + |
| 110 | + # FIX #2: The release asset filenames on GitHub do NOT include a .exe |
| 111 | + # extension (e.g. "drycc-v1.10.2-windows-amd64"), but the script was |
| 112 | + # appending .exe to the download URL, causing a 404 Not Found. |
| 113 | + # We use the bare name for the download URL and rename locally. |
| 114 | + $assetName = "drycc-${Version}-${platform}-${arch}" |
| 115 | + $downloadUrl = "$script:DryccBinUrlBase/download/${Version}/${assetName}" |
| 116 | + $tempFile = [System.IO.Path]::GetTempFileName() + ".exe" |
| 117 | + |
| 118 | + Write-Host "Downloading Drycc Workflow CLI ${Version} for ${platform}-${arch}..." |
| 119 | + Write-Host "URL: $downloadUrl" |
| 120 | + |
| 121 | + try { |
| 122 | + Invoke-WebRequest -Uri $downloadUrl -OutFile $tempFile -UseBasicParsing |
| 123 | + } catch { |
| 124 | + Write-Error "Failed to download from $downloadUrl`n$($_.Exception.Message)" |
| 125 | + exit 1 |
| 126 | + } |
| 127 | + |
| 128 | + # Validate the downloaded file is actually a PE executable, not an error page |
| 129 | + $fileMagic = [System.IO.File]::ReadAllBytes($tempFile)[0..1] |
| 130 | + $magicString = [System.Text.Encoding]::ASCII.GetString($fileMagic) |
| 131 | + if ($magicString -ne 'MZ') { |
| 132 | + Write-Error "Downloaded file is not a valid Windows executable (got '$magicString' instead of 'MZ'). The release asset may not exist." |
| 133 | + Remove-Item -Path $tempFile -Force |
| 134 | + exit 1 |
| 135 | + } |
| 136 | + |
| 137 | + # Create install directory if it doesn't exist |
| 138 | + if (-not (Test-Path $InstallPath)) { |
| 139 | + New-Item -ItemType Directory -Path $InstallPath -Force | Out-Null |
| 140 | + } |
| 141 | + |
| 142 | + $installExe = Join-Path $InstallPath "drycc.exe" |
| 143 | + Move-Item -Path $tempFile -Destination $installExe -Force |
| 144 | + |
| 145 | + # Add to PATH if not already present |
| 146 | + $currentPath = [Environment]::GetEnvironmentVariable("Path", "User") |
| 147 | + if ($currentPath -notlike "*$InstallPath*") { |
| 148 | + [Environment]::SetEnvironmentVariable("Path", "$currentPath;$InstallPath", "User") |
| 149 | + Write-Host "Added $InstallPath to your PATH." |
| 150 | + Write-Host "Please restart your terminal or run: refreshenv" |
| 151 | + } |
| 152 | + |
| 153 | + Write-Host "" |
| 154 | + Write-Host "Drycc Workflow CLI (drycc) has been installed to: $installExe" |
| 155 | + Write-Host "" |
| 156 | + Write-Host "To learn more about Drycc Workflow, execute:" |
| 157 | + Write-Host " $installExe --help" |
| 158 | + Write-Host "" |
| 159 | +} |
| 160 | + |
| 161 | +Install-DryccCli |
0 commit comments