install.ps1 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #Requires -Version 5.1
  2. <#
  3. .SYNOPSIS
  4. Install the ast-grep binary on Windows.
  5. .DESCRIPTION
  6. Tries package managers in priority order, then falls back to downloading a
  7. pinned release zip from GitHub into <skill_root>/bin/sg.exe.
  8. Order:
  9. 1. Already installed? -> nothing to do
  10. 2. Scoop (most common Windows dev tool installer)
  11. 3. Winget (Microsoft built-in)
  12. 4. Chocolatey (choco)
  13. 5. npm (@ast-grep/cli)
  14. 6. cargo binstall / cargo install
  15. 7. pip (ast-grep-cli)
  16. 8. GitHub release zip -> <skill_root>/bin/sg.exe
  17. .PARAMETER Method
  18. Force one method: scoop | winget | choco | npm | cargo | pip | github
  19. .PARAMETER Version
  20. Pin a specific version when downloading from GitHub. Default: 0.45.0
  21. .PARAMETER NoFallback
  22. Don't fall back to GitHub zip; fail if all package managers miss
  23. .PARAMETER Quiet
  24. Suppress non-error output
  25. .EXAMPLE
  26. .\install.ps1
  27. .\install.ps1 -Method scoop
  28. .\install.ps1 -Version 0.42.0 -Method github
  29. #>
  30. param(
  31. [string]$Method = "",
  32. [string]$Version = "0.45.0",
  33. [switch]$NoFallback,
  34. [switch]$Quiet
  35. )
  36. $ErrorActionPreference = 'Stop'
  37. $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  38. $CacheBinDir = if ($env:OMO_AST_GREP_BIN_DIR) { $env:OMO_AST_GREP_BIN_DIR } else { Join-Path $ScriptDir "bin" }
  39. function Log([string]$msg) {
  40. if (-not $Quiet) {
  41. [Console]::Error.WriteLine("[install.ps1] $msg")
  42. }
  43. }
  44. function Err([string]$msg) {
  45. [Console]::Error.WriteLine("[install.ps1] error: $msg")
  46. }
  47. function Has-Cmd([string]$name) {
  48. $null -ne (Get-Command $name -ErrorAction SilentlyContinue)
  49. }
  50. function Test-AstGrep {
  51. if (Has-Cmd 'ast-grep') { return $true }
  52. if (Has-Cmd 'sg') { return $true }
  53. if (Test-Path (Join-Path $CacheBinDir 'sg.exe')) { return $true }
  54. if (Test-Path (Join-Path $CacheBinDir 'ast-grep.exe')) { return $true }
  55. return $false
  56. }
  57. if (-not $Method -and (Test-AstGrep)) {
  58. Log "ast-grep already installed"
  59. exit 0
  60. }
  61. function Detect-Arch {
  62. $a = $env:PROCESSOR_ARCHITECTURE
  63. switch -Wildcard ($a) {
  64. 'AMD64' { return 'x86_64' }
  65. 'ARM64' { return 'aarch64' }
  66. default { return 'unknown' }
  67. }
  68. }
  69. $Arch = Detect-Arch
  70. function Try-Scoop {
  71. if (-not (Has-Cmd 'scoop')) { return $false }
  72. Log "trying: scoop install main/ast-grep"
  73. try { scoop install main/ast-grep; return $LASTEXITCODE -eq 0 }
  74. catch { return $false }
  75. }
  76. function Try-Winget {
  77. if (-not (Has-Cmd 'winget')) { return $false }
  78. Log "trying: winget install --id ast-grep.ast-grep"
  79. try { winget install --id ast-grep.ast-grep --silent --accept-package-agreements --accept-source-agreements; return $LASTEXITCODE -eq 0 }
  80. catch { return $false }
  81. }
  82. function Try-Choco {
  83. if (-not (Has-Cmd 'choco')) { return $false }
  84. Log "trying: choco install ast-grep -y"
  85. try { choco install ast-grep -y; return $LASTEXITCODE -eq 0 }
  86. catch { return $false }
  87. }
  88. function Try-Npm {
  89. if (-not (Has-Cmd 'npm')) { return $false }
  90. Log "trying: npm install -g @ast-grep/cli"
  91. try { npm install -g '@ast-grep/cli'; return $LASTEXITCODE -eq 0 }
  92. catch { return $false }
  93. }
  94. function Try-Cargo {
  95. if (Has-Cmd 'cargo-binstall') {
  96. Log "trying: cargo binstall -y ast-grep"
  97. try { cargo binstall -y ast-grep; if ($LASTEXITCODE -eq 0) { return $true } } catch {}
  98. }
  99. if (-not (Has-Cmd 'cargo')) { return $false }
  100. Log "trying: cargo install ast-grep --locked"
  101. try { cargo install ast-grep --locked; return $LASTEXITCODE -eq 0 }
  102. catch { return $false }
  103. }
  104. function Try-Pip {
  105. $pip = $null
  106. foreach ($p in 'pip3','pip','py') {
  107. if (Has-Cmd $p) { $pip = $p; break }
  108. }
  109. if (-not $pip) { return $false }
  110. Log "trying: $pip install --user ast-grep-cli"
  111. try {
  112. if ($pip -eq 'py') { py -m pip install --user ast-grep-cli }
  113. else { & $pip install --user ast-grep-cli }
  114. return $LASTEXITCODE -eq 0
  115. } catch { return $false }
  116. }
  117. function Triple-For-Windows {
  118. switch ($Arch) {
  119. 'x86_64' { return 'x86_64-pc-windows-msvc' }
  120. 'aarch64' { return 'aarch64-pc-windows-msvc' }
  121. default { return '' }
  122. }
  123. }
  124. function Try-Github {
  125. $triple = Triple-For-Windows
  126. if (-not $triple) {
  127. Err "no GitHub release asset for arch $Arch"
  128. return $false
  129. }
  130. $asset = "app-$triple.zip"
  131. $url = "https://github.com/ast-grep/ast-grep/releases/download/$Version/$asset"
  132. $tmp = Join-Path $env:TEMP ("ast-grep-install-" + [guid]::NewGuid().ToString('N').Substring(0,8))
  133. New-Item -ItemType Directory -Path $tmp -Force | Out-Null
  134. try {
  135. Log "downloading $url"
  136. Invoke-WebRequest -Uri $url -OutFile (Join-Path $tmp $asset) -UseBasicParsing
  137. Expand-Archive -Path (Join-Path $tmp $asset) -DestinationPath (Join-Path $tmp 'extract') -Force
  138. New-Item -ItemType Directory -Path $CacheBinDir -Force | Out-Null
  139. $candidates = @(
  140. (Join-Path $tmp 'extract/ast-grep.exe'),
  141. (Join-Path $tmp 'extract/sg.exe')
  142. )
  143. $src = $null
  144. foreach ($c in $candidates) {
  145. if (Test-Path $c) { $src = $c; break }
  146. }
  147. if (-not $src) {
  148. Err "no ast-grep.exe or sg.exe found inside $asset"
  149. return $false
  150. }
  151. $dest = Join-Path $CacheBinDir 'sg.exe'
  152. Copy-Item -Path $src -Destination $dest -Force
  153. Log "installed cached binary: $dest"
  154. Log "verify: & '$dest' --version"
  155. Log ""
  156. Log "Add to PATH for direct sg use:"
  157. Log " `$env:Path = '$CacheBinDir;' + `$env:Path"
  158. return $true
  159. } finally {
  160. Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue
  161. }
  162. }
  163. function Run-Method([string]$m) {
  164. switch ($m) {
  165. 'scoop' { return Try-Scoop }
  166. 'winget' { return Try-Winget }
  167. 'choco' { return Try-Choco }
  168. 'npm' { return Try-Npm }
  169. 'cargo' { return Try-Cargo }
  170. 'pip' { return Try-Pip }
  171. 'github' { return Try-Github }
  172. default { Err "unknown method: $m"; return $false }
  173. }
  174. }
  175. if ($Method) {
  176. if (Run-Method $Method) { exit 0 }
  177. Err "method '$Method' failed"
  178. exit 2
  179. }
  180. $methods = @('scoop', 'winget', 'choco', 'npm', 'cargo', 'pip')
  181. foreach ($m in $methods) {
  182. if (Run-Method $m) {
  183. Log "installed via $m"
  184. exit 0
  185. }
  186. Log "$m unavailable or failed; trying next"
  187. }
  188. if (-not $NoFallback) {
  189. Log "all package managers failed; falling back to GitHub release"
  190. if (Try-Github) { exit 0 }
  191. }
  192. Err "all install methods failed."
  193. Err ""
  194. Err "Manual options:"
  195. Err " scoop install main/ast-grep # Scoop"
  196. Err " winget install --id ast-grep.ast-grep # Winget"
  197. Err " choco install ast-grep # Chocolatey"
  198. Err " npm install -g @ast-grep/cli # any OS with Node"
  199. Err " cargo install ast-grep --locked # any OS with Rust"
  200. Err " pip install ast-grep-cli # any OS with Python"
  201. Err " https://github.com/ast-grep/ast-grep/releases # manual binary"
  202. exit 2