You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

install.ps1 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. param (
  2. [string]$repo = "dora-rs",
  3. [string]$bin = "dora",
  4. [string]$tag,
  5. [string]$target,
  6. [string]$to = "$HOME\.dora\bin",
  7. [switch]$force
  8. )
  9. $ErrorActionPreference = "Stop"
  10. if ($env:GITHUB_ACTIONS) {
  11. $VerbosePreference = "Continue"
  12. Write-Verbose "Verbose mode enabled because we're running in GitHub Actions"
  13. }
  14. try {
  15. # Simulate pipefail-like behavior using ErrorAction and traps
  16. $ErrorActionPreference = "Stop"
  17. $null = (Get-Command "NonExistentCommand" -ErrorAction Stop | Out-Null)
  18. } catch {
  19. Write-Verbose "Pipefail-like support enabled"
  20. }
  21. function Show-Help {
  22. @"
  23. Install a binary released on GitHub
  24. USAGE:
  25. install.ps1 [options]
  26. FLAGS:
  27. -h, --help Display this message
  28. -f, --force Force overwriting an existing binary
  29. OPTIONS:
  30. --repo REPO Github Repository to install the binary from [default: dora-rs]
  31. --bin BIN Name of the binary to install [default: dora]
  32. --tag TAG Tag (version) of the bin to install, defaults to latest release
  33. --to LOCATION Where to install the binary [default: $HOME\.dora\bin]
  34. --target TARGET
  35. "@
  36. }
  37. function Handle-Error {
  38. param (
  39. [string]$Message
  40. )
  41. Write-Error "install: $Message"
  42. exit 1
  43. }
  44. function Need-Command {
  45. param (
  46. [string]$command
  47. )
  48. if (-not (Get-Command $command -ErrorAction SilentlyContinue)) {
  49. Handle-Error "$command (command not found)"
  50. }
  51. }
  52. function New-Temp-Dir() {
  53. [CmdletBinding(SupportsShouldProcess)]
  54. param()
  55. $parent = [System.IO.Path]::GetTempPath()
  56. [string] $name = [System.Guid]::NewGuid()
  57. New-Item -ItemType Directory -Path (Join-Path $parent $name)
  58. }
  59. function Download-File {
  60. param (
  61. [string]$url,
  62. [string]$output
  63. )
  64. try {
  65. $outputDir = Split-Path -Path $output -Parent
  66. if (-not (Test-Path -Path $outputDir)) {
  67. New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
  68. }
  69. Invoke-WebRequest -Uri $url -OutFile $output
  70. } catch {
  71. Write-Error "Failed to download the file. Error: $_"
  72. }
  73. }
  74. Need-Command mkdir
  75. Need-Command Expand-Archive
  76. $url="https://api.github.com/repos/$repo/$bin"
  77. $releases="$url/releases"
  78. $tmp = New-Temp-Dir
  79. if (-not $tag) {
  80. Download-File "$releases/latest" "$tmp/$bin-version.txt"
  81. $json = (Get-Content -Path "$tmp/$bin-version.txt")
  82. $tag = $json | ConvertFrom-Json | Select-Object -ExpandProperty tag_name
  83. }
  84. $target = "x86_64-pc-windows-msvc"
  85. $archive = "https://github.com/$repo/$bin/releases/download/$tag/$bin-$tag-$target.zip"
  86. Write-Host "Repository https://github.com/$repo"
  87. Write-Host "Binary $bin"
  88. Write-Host "Tag $tag"
  89. Write-Host "Target $target"
  90. Write-Host "Destination $to"
  91. Write-Host "Archive $archive"
  92. $zip = "$tmp\$bin-$tag-$target.zip"
  93. Download-File $archive $zip
  94. Write-Host "Placing dora-rs cli in ", $to
  95. Expand-Archive -Path $zip -DestinationPath $to -Force
  96. Remove-Item -Path $tmp -Recurse -Force
  97. function Add-PathToEnv {
  98. param (
  99. [string]$newPath
  100. )
  101. $currentPath = [System.Environment]::GetEnvironmentVariable("PATH", [System.EnvironmentVariableTarget]::User)
  102. if ($currentPath -like "*$newPath*") {
  103. Write-Host $newPath, " is already in the PATH variable."
  104. } else {
  105. $updatedPath = "$currentPath;$newPath"
  106. [System.Environment]::SetEnvironmentVariable("PATH", $updatedPath, [System.EnvironmentVariableTarget]::User)
  107. Write-Host "Path added to PATH variable and reloaded."
  108. }
  109. }
  110. function Confirm-AddToPath {
  111. param (
  112. [string]$newPath
  113. )
  114. $response = Read-Host "Do you want to add ", $newPath, " to your PATH automatically? (y/n): "
  115. if ($response -eq "Y" -or $response -eq "y" -or $response -eq "") {
  116. Add-PathToEnv -newPath $newPath
  117. } else {
  118. Write-Host "You chose not to add", $newPath, " to your PATH."
  119. Write-Host "To run dora CLI without adding to PATH, use: '~/.dora/bin/dora'"
  120. }
  121. }
  122. Confirm-AddToPath -newPath $to