|
| 1 | +# Binary install script for the OSS release layout (see packages/core/src/install/cdn.ts). |
| 2 | +# |
| 3 | +# irm https://bailian-wiki.oss-cn-hangzhou.aliyuncs.com/release/install.ps1 | iex |
| 4 | +# $env:BAILIAN_CHANNEL = "sync-release"; irm ... | iex |
| 5 | +# $env:BAILIAN_VERSION = "1.10.1"; irm ... | iex |
| 6 | +# |
| 7 | +# Layout: |
| 8 | +# {CDN}/{channel}.json — rolling manifest (version + assets) |
| 9 | +# {CDN}/v{version}/{file}.zip — per-platform zip |
| 10 | +param() |
| 11 | + |
| 12 | +$ErrorActionPreference = "Stop" |
| 13 | + |
| 14 | +$CdnBase = if ($env:BAILIAN_CLI_CDN) { $env:BAILIAN_CLI_CDN.TrimEnd("/") } else { "https://bailian-wiki.oss-cn-hangzhou.aliyuncs.com/release" } |
| 15 | +$Channel = if ($env:BAILIAN_CHANNEL) { $env:BAILIAN_CHANNEL } else { "latest" } |
| 16 | +$Version = $env:BAILIAN_VERSION |
| 17 | +$InstallRoot = if ($env:BAILIAN_SHARE_DIR) { $env:BAILIAN_SHARE_DIR } else { Join-Path $env:LOCALAPPDATA "bailian-cli" } |
| 18 | +$ConfigDir = if ($env:BAILIAN_CONFIG_DIR) { $env:BAILIAN_CONFIG_DIR } else { Join-Path $env:USERPROFILE ".bailian" } |
| 19 | + |
| 20 | +function Write-Log([string]$Message) { |
| 21 | + [Console]::Error.WriteLine($Message) |
| 22 | +} |
| 23 | + |
| 24 | +function Get-VersionTag([string]$Ver) { |
| 25 | + if ($Ver.StartsWith("v")) { return $Ver } |
| 26 | + return "v$Ver" |
| 27 | +} |
| 28 | + |
| 29 | +$Arch = if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") { "arm64" } else { "x64" } |
| 30 | +if ($Arch -eq "arm64") { |
| 31 | + throw "windows arm64 is not supported for binary install; use: npm install -g bailian-cli" |
| 32 | +} |
| 33 | +$PlatformKey = "windows-$Arch" |
| 34 | + |
| 35 | +$ManifestUrl = "$CdnBase/$Channel.json" |
| 36 | +Write-Log "Fetching $ManifestUrl" |
| 37 | +$Manifest = Invoke-RestMethod -Uri $ManifestUrl |
| 38 | + |
| 39 | +$PinnedVersion = $Version |
| 40 | +if ([string]::IsNullOrWhiteSpace($Version)) { |
| 41 | + $Version = $Manifest.version |
| 42 | +} |
| 43 | +if ([string]::IsNullOrWhiteSpace($Version)) { |
| 44 | + throw "Could not parse version from $Channel channel manifest" |
| 45 | +} |
| 46 | + |
| 47 | +$ZipFile = $null |
| 48 | +$ExpectedSha = $null |
| 49 | +$InnerName = $null |
| 50 | +$AssetMeta = $null |
| 51 | +if ($Manifest.assets -and $Manifest.assets.$PlatformKey) { |
| 52 | + $AssetMeta = $Manifest.assets.$PlatformKey |
| 53 | +} |
| 54 | + |
| 55 | +$UseManifestAsset = $AssetMeta -and ( |
| 56 | + [string]::IsNullOrWhiteSpace($PinnedVersion) -or ($PinnedVersion -eq $Manifest.version) |
| 57 | +) |
| 58 | + |
| 59 | +if ($UseManifestAsset) { |
| 60 | + $ZipFile = $AssetMeta.file |
| 61 | + $ExpectedSha = $AssetMeta.sha256 |
| 62 | + $InnerName = $AssetMeta.inner |
| 63 | +} else { |
| 64 | + $ZipFile = "bl-$Version-$PlatformKey.zip" |
| 65 | + $InnerName = "bl-$Version-$PlatformKey.exe" |
| 66 | +} |
| 67 | + |
| 68 | +if ([string]::IsNullOrWhiteSpace($ZipFile) -or [string]::IsNullOrWhiteSpace($InnerName)) { |
| 69 | + throw "Manifest missing asset metadata for $PlatformKey" |
| 70 | +} |
| 71 | + |
| 72 | +$Tag = Get-VersionTag $Version |
| 73 | +$Url = "$CdnBase/$Tag/$ZipFile" |
| 74 | +$TempDir = Join-Path ([System.IO.Path]::GetTempPath()) ("bailian-cli-install-" + [guid]::NewGuid().ToString("N")) |
| 75 | +New-Item -ItemType Directory -Path $TempDir | Out-Null |
| 76 | + |
| 77 | +try { |
| 78 | + $ZipPath = Join-Path $TempDir $ZipFile |
| 79 | + Write-Log "Downloading $ZipFile…" |
| 80 | + Invoke-WebRequest -Uri $Url -OutFile $ZipPath |
| 81 | + |
| 82 | + $Actual = (Get-FileHash -Algorithm SHA256 -Path $ZipPath).Hash.ToLowerInvariant() |
| 83 | + if (-not [string]::IsNullOrWhiteSpace($ExpectedSha)) { |
| 84 | + if ($ExpectedSha.ToLowerInvariant() -ne $Actual) { |
| 85 | + throw "Checksum mismatch for $ZipFile" |
| 86 | + } |
| 87 | + } else { |
| 88 | + $SumsUrl = "$CdnBase/$Tag/SHA256SUMS" |
| 89 | + $SumsPath = Join-Path $TempDir "SHA256SUMS" |
| 90 | + Invoke-WebRequest -Uri $SumsUrl -OutFile $SumsPath |
| 91 | + $Expected = $null |
| 92 | + Get-Content $SumsPath | ForEach-Object { |
| 93 | + if ($_ -match "^([a-fA-F0-9]+)\s+$([regex]::Escape($ZipFile))\s*$") { |
| 94 | + $Expected = $Matches[1].ToLowerInvariant() |
| 95 | + } |
| 96 | + } |
| 97 | + if (-not $Expected) { throw "Checksum for $ZipFile not found in SHA256SUMS" } |
| 98 | + if ($Expected -ne $Actual) { throw "Checksum mismatch for $ZipFile" } |
| 99 | + } |
| 100 | + |
| 101 | + Write-Log "Extracting $InnerName…" |
| 102 | + Expand-Archive -Path $ZipPath -DestinationPath $TempDir -Force |
| 103 | + $ExtractPath = Join-Path $TempDir $InnerName |
| 104 | + if (-not (Test-Path -LiteralPath $ExtractPath)) { |
| 105 | + # Some Expand-Archive versions nest a single top-level folder. |
| 106 | + $Found = Get-ChildItem -Path $TempDir -Recurse -File | Where-Object { $_.Name -eq $InnerName } | Select-Object -First 1 |
| 107 | + if (-not $Found) { throw "Extracted binary missing: $InnerName" } |
| 108 | + $ExtractPath = $Found.FullName |
| 109 | + } |
| 110 | + |
| 111 | + $VersionDir = Join-Path $InstallRoot "versions\$Version" |
| 112 | + New-Item -ItemType Directory -Force -Path $VersionDir | Out-Null |
| 113 | + New-Item -ItemType Directory -Force -Path $ConfigDir | Out-Null |
| 114 | + $Target = Join-Path $VersionDir "bl.exe" |
| 115 | + Copy-Item -Force $ExtractPath $Target |
| 116 | + |
| 117 | + $ShimDir = Join-Path $InstallRoot "bin" |
| 118 | + New-Item -ItemType Directory -Force -Path $ShimDir | Out-Null |
| 119 | + Copy-Item -Force $Target (Join-Path $ShimDir "bl.exe") |
| 120 | + Copy-Item -Force $Target (Join-Path $ShimDir "bailian.exe") |
| 121 | + Set-Content -Path (Join-Path $ConfigDir "install-method") -Value "binary" -NoNewline |
| 122 | + |
| 123 | + $UserPath = [Environment]::GetEnvironmentVariable("Path", "User") |
| 124 | + if (-not ($UserPath -split ";" | Where-Object { $_ -eq $ShimDir })) { |
| 125 | + [Environment]::SetEnvironmentVariable("Path", "$ShimDir;$UserPath", "User") |
| 126 | + Write-Log "Added $ShimDir to your user PATH. Open a new terminal to use bl." |
| 127 | + } |
| 128 | + |
| 129 | + Write-Log "Installed: $Target (bailian-cli $Version) [$Channel]" |
| 130 | + Write-Log "Done. Run: bl --help" |
| 131 | +} |
| 132 | +finally { |
| 133 | + Remove-Item -Recurse -Force $TempDir -ErrorAction SilentlyContinue |
| 134 | +} |
0 commit comments