-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
170 lines (147 loc) · 6.5 KB
/
Copy pathinstall.ps1
File metadata and controls
170 lines (147 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
param(
[switch]$Help,
[switch]$Quiet,
[switch]$NoRtk
)
$ErrorActionPreference = "Stop"
$repoRaw = "https://raw.githubusercontent.com/Coding-Dev-Tools/rtk-command-code/main"
$rtkApiLatest = "https://api.github.com/repos/rtk-ai/rtk/releases/latest"
if ($Help) {
Write-Output @"
RTK for Command Code — Installer (Windows)
Installs RTK token optimization instructions for Command Code CLI, and (if it
is missing) the RTK binary itself.
Usage:
.\install.ps1 Interactive install (from a cloned repo)
.\install.ps1 -Quiet Silent install (no prompts; auto-installs RTK)
.\install.ps1 -NoRtk Do not install the RTK binary, only the instructions
iwr <raw>/install.ps1 | iex Install directly from the web
What it does:
1. Installs the RTK binary from its latest GitHub release if rtk is missing
2. Installs AGENTS.md + references\ to ~\.commandcode\
(copies sibling files when run from a clone, otherwise downloads them)
3. Confirms Command Code CLI will read the instructions
Requires:
- Command Code CLI (cmd, cmdc, or command-code in PATH)
"@
return
}
# --- Helpers ---
function Write-Step($msg) { Write-Host " => $msg" -ForegroundColor Cyan }
function Write-Ok($msg) { Write-Host " [OK] $msg" -ForegroundColor Green }
function Write-Warn($msg) { Write-Host " [!!] $msg" -ForegroundColor Yellow }
function Write-Fail($msg) { Write-Host " [FAIL] $msg" -ForegroundColor Red; exit 1 }
$agentsDir = "$env:USERPROFILE\.commandcode"
function Install-Rtk {
Write-Step "Installing the RTK binary (latest Windows release)..."
try {
$rel = Invoke-RestMethod -Uri $rtkApiLatest -UseBasicParsing -Headers @{ "User-Agent" = "rtk-command-code-installer" }
$arch = if ($env:PROCESSOR_ARCHITECTURE -match "ARM64") { "aarch64|arm64" } else { "x86_64|amd64|x64" }
$asset = $rel.assets |
Where-Object { $_.name -match "windows" -and $_.name -match "\.zip$" -and $_.name -match $arch } |
Select-Object -First 1
if (-not $asset) {
$asset = $rel.assets |
Where-Object { $_.name -match "windows" -and $_.name -match "\.zip$" } |
Select-Object -First 1
}
if (-not $asset) { throw "No Windows .zip asset found in the latest RTK release." }
$binDir = Join-Path $agentsDir "bin"
New-Item -ItemType Directory -Path $binDir -Force | Out-Null
$zipPath = Join-Path $env:TEMP $asset.name
Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $zipPath -UseBasicParsing
Expand-Archive -Path $zipPath -DestinationPath $binDir -Force
Remove-Item $zipPath -Force -ErrorAction SilentlyContinue
$exe = Get-ChildItem -Path $binDir -Recurse -Filter "rtk.exe" | Select-Object -First 1
if (-not $exe) { throw "rtk.exe was not found after extraction." }
if ($exe.DirectoryName -ne $binDir) {
Copy-Item $exe.FullName (Join-Path $binDir "rtk.exe") -Force
}
# Add to the user PATH (persistent) and the current session.
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if (($userPath -split ';') -notcontains $binDir) {
[Environment]::SetEnvironmentVariable("Path", "$userPath;$binDir", "User")
}
$env:Path = "$env:Path;$binDir"
Write-Ok "RTK installed to $binDir (added to your PATH)"
} catch {
Write-Warn "Could not auto-install RTK: $($_.Exception.Message)"
Write-Warn "Install manually: https://github.com/rtk-ai/rtk/releases"
}
}
# --- Step 1: Check prerequisites ---
Write-Step "Checking prerequisites..."
$hasCmd = $null -ne (Get-Command "cmd", "cmdc", "command-code" -ErrorAction SilentlyContinue)
if ($hasCmd) {
Write-Ok "Command Code CLI found"
} else {
Write-Warn "Command Code CLI not found in PATH."
Write-Warn "Install it first: npm install -g command-code"
}
$hasRtk = $null -ne (Get-Command "rtk" -ErrorAction SilentlyContinue)
if ($hasRtk) {
$v = & rtk --version
Write-Ok "RTK found: $v"
} elseif ($NoRtk) {
Write-Warn "RTK not found (-NoRtk set). Install later: https://github.com/rtk-ai/rtk#installation"
} else {
Write-Warn "RTK not found in PATH."
$doInstall = $true
if (-not $Quiet) {
$reply = Read-Host "Install RTK now? (Y/n)"
if ($reply -eq "n" -or $reply -eq "N") { $doInstall = $false }
}
if ($doInstall) {
Install-Rtk
} else {
Write-Warn "Skipped RTK install. Install later: https://github.com/rtk-ai/rtk#installation"
}
}
# --- Step 2: Determine source paths (null when piped via `iwr | iex`) ---
$scriptDir = $null
if ($MyInvocation.MyCommand.Path) {
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
}
$refDir = Join-Path $agentsDir "references"
New-Item -ItemType Directory -Path $refDir -Force | Out-Null
$agentsDest = Join-Path $agentsDir "AGENTS.md"
$commandsDest = Join-Path $refDir "commands.md"
$analyticsDest = Join-Path $refDir "analytics.md"
function Get-RemoteFile($rel, $dest) {
Invoke-WebRequest -Uri "$repoRaw/$rel" -OutFile $dest -UseBasicParsing
}
# --- Step 3: Install AGENTS.md + references ---
$localAgents = if ($scriptDir) { Join-Path $scriptDir "AGENTS.md" } else { $null }
if ($localAgents -and (Test-Path $localAgents)) {
Write-Step "Installing instructions from local files..."
Copy-Item $localAgents $agentsDest -Force
Copy-Item (Join-Path $scriptDir "references\commands.md") $commandsDest -Force
Copy-Item (Join-Path $scriptDir "references\analytics.md") $analyticsDest -Force
} else {
Write-Step "Downloading instructions from GitHub..."
Get-RemoteFile "AGENTS.md" $agentsDest
Get-RemoteFile "references/commands.md" $commandsDest
Get-RemoteFile "references/analytics.md" $analyticsDest
}
# --- Step 4: Verify ---
Write-Step "Verifying installation..."
foreach ($f in @($agentsDest, $commandsDest, $analyticsDest)) {
if (-not (Test-Path $f) -or (Get-Item $f).Length -eq 0) {
Write-Fail "Missing or empty after install: $f"
}
}
$testContent = Get-Content $agentsDest -Raw
if ($testContent -match "RTK") {
Write-Ok "Installed AGENTS.md + references\ to $agentsDir"
} else {
Write-Fail "AGENTS.md does not contain RTK instructions"
}
# --- Done ---
Write-Host ""
Write-Host "Installation complete!" -ForegroundColor Green
Write-Host ""
Write-Host " Command Code CLI will now inject RTK instructions into every session."
Write-Host " Restart any active Command Code sessions to apply."
Write-Host ""
Write-Host " Verify: rtk gain"
Write-Host ""