install.ps1
install.ps1Browse 14 files
2,112 tokens
7,165 bytes
Token encoding: o200k_base
Snapshot 24fd22b
← Back to SKILL.md
1#Requires -Version 5.12<#3.SYNOPSIS4 Install the ast-grep binary on Windows.5 6.DESCRIPTION7 Tries package managers in priority order, then falls back to downloading a8 pinned release zip from GitHub into <skill_root>/bin/sg.exe.9 10 Order:11 1. Already installed? -> nothing to do12 2. Scoop (most common Windows dev tool installer)13 3. Winget (Microsoft built-in)14 4. Chocolatey (choco)15 5. npm (@ast-grep/cli)16 6. cargo binstall / cargo install17 7. pip (ast-grep-cli)18 8. GitHub release zip -> <skill_root>/bin/sg.exe19 20.PARAMETER Method21 Force one method: scoop | winget | choco | npm | cargo | pip | github22 23.PARAMETER Version24 Pin a specific version when downloading from GitHub. Default: 0.45.025 26.PARAMETER NoFallback27 Don't fall back to GitHub zip; fail if all package managers miss28 29.PARAMETER Quiet30 Suppress non-error output31 32.EXAMPLE33 .\install.ps134 .\install.ps1 -Method scoop35 .\install.ps1 -Version 0.42.0 -Method github36#>37 38param(39 [string]$Method = "",40 [string]$Version = "0.45.0",41 [switch]$NoFallback,42 [switch]$Quiet43)44 45$ErrorActionPreference = 'Stop'46$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path47$CacheBinDir = if ($env:OMO_AST_GREP_BIN_DIR) { $env:OMO_AST_GREP_BIN_DIR } else { Join-Path $ScriptDir "bin" }48 49function Log([string]$msg) {50 if (-not $Quiet) {51 [Console]::Error.WriteLine("[install.ps1] $msg")52 }53}54 55function Err([string]$msg) {56 [Console]::Error.WriteLine("[install.ps1] error: $msg")57}58 59function Has-Cmd([string]$name) {60 $null -ne (Get-Command $name -ErrorAction SilentlyContinue)61}62 63function Test-AstGrep {64 if (Has-Cmd 'ast-grep') { return $true }65 if (Has-Cmd 'sg') { return $true }66 if (Test-Path (Join-Path $CacheBinDir 'sg.exe')) { return $true }67 if (Test-Path (Join-Path $CacheBinDir 'ast-grep.exe')) { return $true }68 return $false69}70 71if (-not $Method -and (Test-AstGrep)) {72 Log "ast-grep already installed"73 exit 074}75 76function Detect-Arch {77 $a = $env:PROCESSOR_ARCHITECTURE78 switch -Wildcard ($a) {79 'AMD64' { return 'x86_64' }80 'ARM64' { return 'aarch64' }81 default { return 'unknown' }82 }83}84 85$Arch = Detect-Arch86 87function Try-Scoop {88 if (-not (Has-Cmd 'scoop')) { return $false }89 Log "trying: scoop install main/ast-grep"90 try { scoop install main/ast-grep; return $LASTEXITCODE -eq 0 }91 catch { return $false }92}93 94function Try-Winget {95 if (-not (Has-Cmd 'winget')) { return $false }96 Log "trying: winget install --id ast-grep.ast-grep"97 try { winget install --id ast-grep.ast-grep --silent --accept-package-agreements --accept-source-agreements; return $LASTEXITCODE -eq 0 }98 catch { return $false }99}100 101function Try-Choco {102 if (-not (Has-Cmd 'choco')) { return $false }103 Log "trying: choco install ast-grep -y"104 try { choco install ast-grep -y; return $LASTEXITCODE -eq 0 }105 catch { return $false }106}107 108function Try-Npm {109 if (-not (Has-Cmd 'npm')) { return $false }110 Log "trying: npm install -g @ast-grep/cli"111 try { npm install -g '@ast-grep/cli'; return $LASTEXITCODE -eq 0 }112 catch { return $false }113}114 115function Try-Cargo {116 if (Has-Cmd 'cargo-binstall') {117 Log "trying: cargo binstall -y ast-grep"118 try { cargo binstall -y ast-grep; if ($LASTEXITCODE -eq 0) { return $true } } catch {}119 }120 if (-not (Has-Cmd 'cargo')) { return $false }121 Log "trying: cargo install ast-grep --locked"122 try { cargo install ast-grep --locked; return $LASTEXITCODE -eq 0 }123 catch { return $false }124}125 126function Try-Pip {127 $pip = $null128 foreach ($p in 'pip3','pip','py') {129 if (Has-Cmd $p) { $pip = $p; break }130 }131 if (-not $pip) { return $false }132 Log "trying: $pip install --user ast-grep-cli"133 try {134 if ($pip -eq 'py') { py -m pip install --user ast-grep-cli }135 else { & $pip install --user ast-grep-cli }136 return $LASTEXITCODE -eq 0137 } catch { return $false }138}139 140function Triple-For-Windows {141 switch ($Arch) {142 'x86_64' { return 'x86_64-pc-windows-msvc' }143 'aarch64' { return 'aarch64-pc-windows-msvc' }144 default { return '' }145 }146}147 148function Try-Github {149 $triple = Triple-For-Windows150 if (-not $triple) {151 Err "no GitHub release asset for arch $Arch"152 return $false153 }154 155 $asset = "app-$triple.zip"156 $url = "https://github.com/ast-grep/ast-grep/releases/download/$Version/$asset"157 $tmp = Join-Path $env:TEMP ("ast-grep-install-" + [guid]::NewGuid().ToString('N').Substring(0,8))158 New-Item -ItemType Directory -Path $tmp -Force | Out-Null159 try {160 Log "downloading $url"161 Invoke-WebRequest -Uri $url -OutFile (Join-Path $tmp $asset) -UseBasicParsing162 Expand-Archive -Path (Join-Path $tmp $asset) -DestinationPath (Join-Path $tmp 'extract') -Force163 164 New-Item -ItemType Directory -Path $CacheBinDir -Force | Out-Null165 166 $candidates = @(167 (Join-Path $tmp 'extract/ast-grep.exe'),168 (Join-Path $tmp 'extract/sg.exe')169 )170 $src = $null171 foreach ($c in $candidates) {172 if (Test-Path $c) { $src = $c; break }173 }174 if (-not $src) {175 Err "no ast-grep.exe or sg.exe found inside $asset"176 return $false177 }178 179 $dest = Join-Path $CacheBinDir 'sg.exe'180 Copy-Item -Path $src -Destination $dest -Force181 Log "installed cached binary: $dest"182 Log "verify: & '$dest' --version"183 Log ""184 Log "Add to PATH for direct sg use:"185 Log " `$env:Path = '$CacheBinDir;' + `$env:Path"186 return $true187 } finally {188 Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue189 }190}191 192function Run-Method([string]$m) {193 switch ($m) {194 'scoop' { return Try-Scoop }195 'winget' { return Try-Winget }196 'choco' { return Try-Choco }197 'npm' { return Try-Npm }198 'cargo' { return Try-Cargo }199 'pip' { return Try-Pip }200 'github' { return Try-Github }201 default { Err "unknown method: $m"; return $false }202 }203}204 205if ($Method) {206 if (Run-Method $Method) { exit 0 }207 Err "method '$Method' failed"208 exit 2209}210 211$methods = @('scoop', 'winget', 'choco', 'npm', 'cargo', 'pip')212foreach ($m in $methods) {213 if (Run-Method $m) {214 Log "installed via $m"215 exit 0216 }217 Log "$m unavailable or failed; trying next"218}219 220if (-not $NoFallback) {221 Log "all package managers failed; falling back to GitHub release"222 if (Try-Github) { exit 0 }223}224 225Err "all install methods failed."226Err ""227Err "Manual options:"228Err " scoop install main/ast-grep # Scoop"229Err " winget install --id ast-grep.ast-grep # Winget"230Err " choco install ast-grep # Chocolatey"231Err " npm install -g @ast-grep/cli # any OS with Node"232Err " cargo install ast-grep --locked # any OS with Rust"233Err " pip install ast-grep-cli # any OS with Python"234Err " https://github.com/ast-grep/ast-grep/releases # manual binary"235exit 2236 Referenced from SKILL.md
SKILL.mdView in source ↗
Source excerpt starting at line 19.SKILL.mdView in source ↗19This skill ships a Python wrapper at `scripts/ast_grep_helper.py` and platform install scripts at `install.sh` (POSIX) and `install.ps1` (Windows). The helper adds offline pattern validation, the two-pass write trick, and binary auto-resolution. Use it as your default entry point.
Source excerpt starting at line 144.SKILL.mdView in source ↗144python scripts/ast_grep_helper.py doctor # check ast-grep binary availability145python scripts/ast_grep_helper.py install # delegate to install.sh / install.ps1146```
Source excerpt starting at line 275.2756. `references/sgconfig.md` — project-level configuration. Read when you set up `sg scan` for a real project.2767. `references/install.md` — per-OS install methods. Read only if `install.sh` / `install.ps1` fail.