install.ps1 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # CodeGraph standalone installer for Windows (PowerShell).
  2. #
  3. # Downloads a self-contained bundle (a vendored Node runtime + the app) from
  4. # GitHub Releases. No Node.js, no build tools required.
  5. #
  6. # irm https://raw.githubusercontent.com/colbymchenry/codegraph/main/install.ps1 | iex
  7. #
  8. # Upgrade with `codegraph upgrade` (or just re-run this). To uninstall: remove
  9. # $env:LOCALAPPDATA\codegraph and drop its \current\bin entry from your user PATH.
  10. #
  11. # Environment:
  12. # CODEGRAPH_VERSION release tag to install (default: latest)
  13. # CODEGRAPH_INSTALL_DIR install location (default: %LOCALAPPDATA%\codegraph)
  14. $ErrorActionPreference = 'Stop'
  15. $repo = 'colbymchenry/codegraph'
  16. $installDir = if ($env:CODEGRAPH_INSTALL_DIR) { $env:CODEGRAPH_INSTALL_DIR } else { Join-Path $env:LOCALAPPDATA 'codegraph' }
  17. # 1. Detect architecture -> target matching the release archives.
  18. $arch = if ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture -eq 'Arm64') { 'arm64' } else { 'x64' }
  19. $target = "win32-$arch"
  20. # 2. Resolve the version (latest release unless pinned).
  21. $version = $env:CODEGRAPH_VERSION
  22. if (-not $version) {
  23. $version = (Invoke-RestMethod "https://api.github.com/repos/$repo/releases/latest").tag_name
  24. }
  25. if (-not $version) { throw "codegraph: could not resolve latest version; set CODEGRAPH_VERSION." }
  26. # 3. Download + extract the bundle into a stable 'current' dir (overwritten on upgrade).
  27. $url = "https://github.com/$repo/releases/download/$version/codegraph-$target.zip"
  28. Write-Host "Installing CodeGraph $version ($target)..."
  29. $tmp = Join-Path $env:TEMP ("cg-" + [guid]::NewGuid().ToString())
  30. New-Item -ItemType Directory -Force -Path $tmp | Out-Null
  31. $zip = Join-Path $tmp 'cg.zip'
  32. Invoke-WebRequest -Uri $url -OutFile $zip
  33. $dest = Join-Path $installDir 'current'
  34. if (Test-Path $dest) { Remove-Item -Recurse -Force $dest }
  35. New-Item -ItemType Directory -Force -Path $dest | Out-Null
  36. Expand-Archive -Path $zip -DestinationPath $dest -Force
  37. # Archives contain a top-level codegraph-<target>\ dir; flatten it.
  38. $inner = Join-Path $dest "codegraph-$target"
  39. if (Test-Path $inner) {
  40. Get-ChildItem -Force $inner | Move-Item -Destination $dest -Force
  41. Remove-Item -Recurse -Force $inner
  42. }
  43. Remove-Item -Recurse -Force $tmp
  44. # 4. Put the launcher dir on the user's PATH.
  45. $binDir = Join-Path $dest 'bin'
  46. $userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
  47. if (($userPath -split ';') -notcontains $binDir) {
  48. [Environment]::SetEnvironmentVariable('Path', "$binDir;$userPath", 'User')
  49. Write-Host "Added $binDir to your PATH (restart your terminal to pick it up)."
  50. }
  51. Write-Host "Installed to $dest"
  52. # 5. Warn if a different codegraph earlier on PATH will shadow this install.
  53. # Most often a stale `npm i -g @colbymchenry/codegraph`, whose launcher keeps
  54. # running its own version-pinned bundle — so `codegraph --version` disagrees
  55. # with what we just installed (issue #1071). Check both the persisted PATH a
  56. # fresh shell sees (Machine + User) and this session's PATH (catches dirs a
  57. # shell profile injects, e.g. conda / npm).
  58. $expected = Join-Path $binDir 'codegraph.cmd'
  59. function Find-FirstCodegraph([string]$pathStr) {
  60. foreach ($dir in ($pathStr -split ';')) {
  61. if (-not $dir) { continue }
  62. foreach ($leaf in @('codegraph.cmd', 'codegraph.exe', 'codegraph.bat', 'codegraph.ps1')) {
  63. $cand = Join-Path $dir $leaf
  64. if (Test-Path -LiteralPath $cand) { return $cand }
  65. }
  66. }
  67. return $null
  68. }
  69. $machinePath = [Environment]::GetEnvironmentVariable('Path', 'Machine')
  70. $freshPath = ((@($machinePath, [Environment]::GetEnvironmentVariable('Path', 'User')) | Where-Object { $_ }) -join ';')
  71. $shadow = $null
  72. foreach ($winner in @((Find-FirstCodegraph $env:Path), (Find-FirstCodegraph $freshPath))) {
  73. if ($winner -and ($winner -ne $expected)) { $shadow = $winner; break }
  74. }
  75. if ($shadow) {
  76. Write-Warning "Another codegraph is earlier on your PATH and will run instead of this install:"
  77. Write-Warning " $shadow"
  78. Write-Warning " (this install: $expected)"
  79. Write-Warning "If 'codegraph --version' shows an unexpected version, remove the other copy"
  80. Write-Warning "(e.g. 'npm rm -g @colbymchenry/codegraph') or put '$binDir' first on your PATH."
  81. }
  82. Write-Host "Run: codegraph --help"