param( [String]$Version = "", [Boolean]$Proxy = $False, [String]$InstallDir = "", [String]$ProxyUrl = "" ) $ErrorActionPreference = "Stop" # Enable TLS 1.2 for Windows PowerShell 5.1 (desktop CLR) if ($PSVersionTable.PSVersion.Major -lt 6) { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 } $Owner = "beautifulrem" $Repo = "tdl-next" $BinName = "tdln" $ChecksumFile = "tdln_checksums.txt" $DefaultProxy = "https://mirror.ghproxy.com/" $DownloadBaseUrl = if ($env:TDLN_DOWNLOAD_BASE_URL) { $env:TDLN_DOWNLOAD_BASE_URL.TrimEnd('/') } else { "https://github.com" } $ApiBaseUrl = if ($env:TDLN_API_BASE_URL) { $env:TDLN_API_BASE_URL.TrimEnd('/') } else { "https://api.github.com" } $Principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) $IsAdmin = $Principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) # Resolve proxy prefix $ProxyPrefix = "" if ($ProxyUrl) { $ProxyPrefix = $ProxyUrl } elseif ($Proxy) { if ($env:TDLN_GITHUB_PROXY) { $ProxyPrefix = $env:TDLN_GITHUB_PROXY } else { $ProxyPrefix = $DefaultProxy } } elseif ($env:TDLN_GITHUB_PROXY) { $ProxyPrefix = $env:TDLN_GITHUB_PROXY } if ($ProxyPrefix -and -not $ProxyPrefix.EndsWith("/")) { $ProxyPrefix = "$ProxyPrefix/" } if ($ProxyPrefix) { Write-Host "Using GitHub proxy: $ProxyPrefix" -ForegroundColor Blue } # Resolve target install directory without forcing Administrator $Location = if ($InstallDir) { $InstallDir } elseif ($env:TDLN_INSTALL_DIR) { $env:TDLN_INSTALL_DIR } elseif ($IsAdmin) { Join-Path $Env:ProgramFiles $BinName } else { Join-Path $Env:LOCALAPPDATA "Programs\$BinName" } if (-not (Test-Path -LiteralPath $Location)) { New-Item -ItemType Directory -Path $Location -Force | Out-Null } # Verify writability $ProbeFile = Join-Path $Location (".tdln-write-probe-" + [Guid]::NewGuid().ToString("N")) try { [IO.File]::WriteAllText($ProbeFile, "probe") Remove-Item -LiteralPath $ProbeFile -Force } catch { Write-Host "Target directory '$Location' is not writable: $($_.Exception.Message)" -ForegroundColor Red Write-Host "Pass -InstallDir to install into a user-writable directory." -ForegroundColor Red exit 1 } # Map architecture matching crates/tdl-engine/src/services/update (prefer native OS arch under WoW64) $RawArch = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE } $Arch = "" switch ($RawArch) { "AMD64" { $Arch = "64bit" } default { Write-Host "Unsupported system architecture: $RawArch" -ForegroundColor Red exit 1 } } $ApiHeaders = @{ "User-Agent" = "tdl-next-installer" "Accept" = "application/vnd.github+json" } if (-not $Version) { $ReleaseUri = "$ApiBaseUrl/repos/$Owner/$Repo/releases/latest" try { $ReleaseInfo = Invoke-RestMethod -Uri $ReleaseUri -Headers $ApiHeaders $Version = $ReleaseInfo.tag_name } catch { $StatusCode = $null if ($_.Exception.Response) { $StatusCode = [int]$_.Exception.Response.StatusCode } if ($StatusCode -ne 404) { throw } # /releases/latest skips pre-releases and 404s until a stable release exists. # Fall back to the newest published release; anonymous API calls never list drafts. Write-Host "No stable release found; falling back to the newest pre-release." -ForegroundColor Yellow $ListUri = "$ApiBaseUrl/repos/$Owner/$Repo/releases?per_page=1" $ReleaseList = @(Invoke-RestMethod -Uri $ListUri -Headers $ApiHeaders) if ($ReleaseList.Count -gt 0) { $Version = $ReleaseList[0].tag_name } } } if (-not $Version) { Write-Host "Failed to resolve target release version from $Owner/$Repo." -ForegroundColor Red exit 1 } Write-Host "Target version: $Version" -ForegroundColor Blue $ArchiveName = "${BinName}_Windows_${Arch}.zip" $ArchiveUrl = "${ProxyPrefix}${DownloadBaseUrl}/${Owner}/${Repo}/releases/download/${Version}/${ArchiveName}" $ChecksumUrl = "${ProxyPrefix}${DownloadBaseUrl}/${Owner}/${Repo}/releases/download/${Version}/${ChecksumFile}" $TempDir = Join-Path ([System.IO.Path]::GetTempPath()) ("tdln-install-" + [Guid]::NewGuid().ToString("N")) New-Item -ItemType Directory -Path $TempDir -Force | Out-Null try { $ChecksumPath = Join-Path $TempDir $ChecksumFile $ArchivePath = Join-Path $TempDir $ArchiveName $ExtractDir = Join-Path $TempDir "extract" Write-Host "Downloading $ChecksumFile from $ChecksumUrl" -ForegroundColor Blue Invoke-WebRequest -UseBasicParsing -Uri $ChecksumUrl -OutFile $ChecksumPath if (-not (Test-Path -LiteralPath $ChecksumPath)) { Write-Host "Download of $ChecksumFile failed." -ForegroundColor Red exit 1 } $ExpectedSha256 = $null foreach ($Line in (Get-Content -LiteralPath $ChecksumPath)) { $Trimmed = $Line.Trim() if (-not $Trimmed -or $Trimmed.StartsWith("#")) { continue } if ($Trimmed -match '^([0-9a-fA-F]{64})\s+\*?(.+)$') { $CandidateDigest = $Matches[1].ToLowerInvariant() $CandidateName = $Matches[2].Trim() if ($CandidateName.StartsWith("./")) { $CandidateName = $CandidateName.Substring(2) } if ($CandidateName -eq $ArchiveName) { $ExpectedSha256 = $CandidateDigest break } } } if (-not $ExpectedSha256) { Write-Host "Checksum entry for '$ArchiveName' missing or invalid in $ChecksumFile." -ForegroundColor Red exit 1 } Write-Host "Downloading $ArchiveName from $ArchiveUrl" -ForegroundColor Blue Invoke-WebRequest -UseBasicParsing -Uri $ArchiveUrl -OutFile $ArchivePath if (-not (Test-Path -LiteralPath $ArchivePath)) { Write-Host "Download of $ArchiveName failed." -ForegroundColor Red exit 1 } $ActualSha256 = (Get-FileHash -Path $ArchivePath -Algorithm SHA256).Hash.ToLowerInvariant() if ($ActualSha256 -ne $ExpectedSha256) { Write-Host "SHA-256 verification failed for $ArchiveName!" -ForegroundColor Red Write-Host " expected: $ExpectedSha256" -ForegroundColor Red Write-Host " actual: $ActualSha256" -ForegroundColor Red exit 1 } Write-Host "SHA-256 verified ($ActualSha256)" -ForegroundColor Green New-Item -ItemType Directory -Path $ExtractDir -Force | Out-Null Expand-Archive -Path $ArchivePath -DestinationPath $ExtractDir -Force $ExtractedBin = Join-Path $ExtractDir "$BinName.exe" if (-not (Test-Path -LiteralPath $ExtractedBin -PathType Leaf)) { Write-Host "Expected binary '$BinName.exe' not found at archive root." -ForegroundColor Red exit 1 } $TargetBin = Join-Path $Location "$BinName.exe" Copy-Item -LiteralPath $ExtractedBin -Destination $TargetBin -Force # Update User PATH unless installing to machine-wide ProgramFiles as Admin $PathTarget = if ($IsAdmin -and $Location.StartsWith($Env:ProgramFiles, [StringComparison]::OrdinalIgnoreCase)) { [EnvironmentVariableTarget]::Machine } else { [EnvironmentVariableTarget]::User } $CurrentPath = [Environment]::GetEnvironmentVariable("Path", $PathTarget) $NormalizedLocation = $Location.TrimEnd('\') $Entries = @() if ($CurrentPath) { $Entries = $CurrentPath -split ';' | Where-Object { $_ -ne '' } } $AlreadyPresent = $false foreach ($Entry in $Entries) { if ($Entry.TrimEnd('\').Equals($NormalizedLocation, [StringComparison]::OrdinalIgnoreCase)) { $AlreadyPresent = $true break } } if (-not $AlreadyPresent) { Write-Host "Adding $Location to $PathTarget PATH..." -ForegroundColor Blue $NewPath = ($Entries + $Location) -join ';' [Environment]::SetEnvironmentVariable("Path", $NewPath, $PathTarget) $env:Path = "$env:Path;$Location" Write-Host "Note: Restart your terminal for PATH changes to take effect in new sessions." -ForegroundColor Yellow } Write-Host "$BinName installed successfully! Location: $TargetBin" -ForegroundColor Green Write-Host "Run '$BinName --help' to get started" -ForegroundColor Green Write-Host "Documentation: https://github.com/beautifulrem/tdl-next" -ForegroundColor Green } finally { if (Test-Path -LiteralPath $TempDir) { Remove-Item -LiteralPath $TempDir -Recurse -Force -ErrorAction SilentlyContinue } }