Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
226 lines (214 loc) · 10.3 KB
/
Copy pathinstall.ps1
File metadata and controls
226 lines (214 loc) · 10.3 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<#
.SYNOPSIS
Installs or updates ADO Lens for Azure DevOps: work items, boards, wiki, Git and TFVC served to Claude over MCP.
.DESCRIPTION
Run it from an unpacked package, in a regular (non-elevated) PowerShell window outside Claude.
1. checks whether the Claude registration has to change,
2. copies the program to the installation folder (no administrator rights needed),
3. sets up a connection when there is none: Azure DevOps address, PAT (hidden input, stored in
Windows Credential Manager, never in a file) and default project,
4. registers the 'ado-lens' MCP server in Claude Desktop (and Claude Code, when installed).
Claude Desktop writes its configuration back while it runs, so when the registration has to change the
script asks you to quit Claude, waits for it and starts it again afterwards. Settings, PATs and metadata
stay in place on an update.
.EXAMPLE
.\install.ps1
.EXAMPLE
.\install.ps1 -Organization https://dev.azure.com/contoso/Demo -Mode dev
#>
[CmdletBinding()]
param(
[string] $InstallDir = (Join-Path $env:LOCALAPPDATA 'Programs\ADO Lens'),
[string] $Organization,
[ValidateSet('po', 'dev', 'full')]
[string] $Mode,
[switch] $NoRegister,
[switch] $DesktopOnly,
[switch] $CodeOnly,
[switch] $NoRestart
)
$ErrorActionPreference = 'Stop'
function Say($m) { Write-Host " $m" }
function Head($m) { Write-Host "`n$m" -ForegroundColor Cyan }
# Claude Desktop of this Windows session only: on a server with remote desktop sessions other people's Claude runs too
# (and an elevated window sees their paths). The Claude Code CLI is also claude.exe, also under the Claude folders
# (...\claude-code\...), but it is not the desktop app and does not need to be closed.
$mySession = (Get-Process -Id $PID).SessionId
function Test-DesktopPath([string] $path) {
$path -and $path -notlike '*\claude-code\*' -and ($path -like '*\WindowsApps\Claude_*' -or $path -like '*\AnthropicClaude\*')
}
function Get-ClaudeDesktop {
Get-Process -Name claude -ErrorAction SilentlyContinue | Where-Object { $_.SessionId -eq $mySession -and (Test-DesktopPath $_.Path) }
}
# Run from a terminal inside Claude (e.g. its Code tab): Claude cannot be closed without closing this script too.
function Test-InsideClaude {
$id = $PID
for ($i = 0; $i -lt 15 -and $id; $i++) {
$p = Get-CimInstance Win32_Process -Filter "ProcessId=$id" -ErrorAction SilentlyContinue
if (-not $p) { break }
if ($p.Name -ieq 'claude.exe' -and (Test-DesktopPath $p.ExecutablePath)) { return $true }
$id = $p.ParentProcessId
}
return $false
}
function Start-ClaudeDesktop {
$pkg = Get-AppxPackage -Name Claude -ErrorAction SilentlyContinue | Select-Object -First 1
if ($pkg) { Start-Process "shell:AppsFolder\$($pkg.PackageFamilyName)!Claude"; return $true }
$exe = Join-Path $env:LOCALAPPDATA 'AnthropicClaude\claude.exe'
if (Test-Path $exe) { Start-Process $exe; return $true }
return $false
}
$src = $PSScriptRoot
$pkgExe = Join-Path $src 'bin\adolens.exe'
if (-not (Test-Path $pkgExe)) { throw "$pkgExe not found - run install.ps1 from an unpacked ADO Lens package." }
$exe = Join-Path ([IO.Path]::GetFullPath($InstallDir)) 'bin\adolens.exe'
$scope = @()
if ($DesktopOnly) { $scope += '--desktop' }
if ($CodeOnly) { $scope += '--code' }
Head '1/6 Claude registration'
$needsRegister = $false
if ($NoRegister) {
Say 'skipped (-NoRegister)'
}
else {
& $pkgExe register --check --exe $exe @scope
$needsRegister = ($LASTEXITCODE -eq 3)
}
$closedClaude = $false
$skippedRegister = $false
if ($needsRegister -and (Get-ClaudeDesktop)) {
Write-Host ' The Claude registration has to change and Claude Desktop is running.' -ForegroundColor Yellow
if (Test-InsideClaude) {
Write-Host ' This window runs inside Claude, which cannot be closed while it runs this script.' -ForegroundColor Yellow
Write-Host ' The program is installed now; to register it, quit Claude and run in a regular PowerShell window:' -ForegroundColor Yellow
Write-Host " & `"$exe`" register"
$needsRegister = $false
$skippedRegister = $true
}
else {
Write-Host ' Quit Claude now: tray icon (next to the clock) -> right click -> Quit.' -ForegroundColor Yellow
Write-Host ' Waiting... (S = skip the registration now and register later with: adolens register)' -ForegroundColor Yellow
$waited = 0
while (Get-ClaudeDesktop) {
# Every 20 seconds: what is still running, so it can be found in Task Manager (Details tab, by PID).
if ($waited % 20 -eq 0) {
foreach ($p in Get-ClaudeDesktop) { Say "still running: PID $($p.Id) $($p.Path)" }
}
$key = $null
try { if ([Console]::KeyAvailable) { $key = [Console]::ReadKey($true).Key } } catch { }
if ($key -eq 'S' -or $waited -ge 600) {
if ($waited -ge 600) { Say 'Claude is still running after 10 minutes - skipping the registration' }
$needsRegister = $false
$skippedRegister = $true
break
}
Start-Sleep -Seconds 1
$waited++
}
if (-not $skippedRegister) {
Start-Sleep -Seconds 2
$closedClaude = $true
Say 'Claude is closed'
}
}
}
Head "2/6 Copying files to $InstallDir"
$binDst = Join-Path $InstallDir 'bin'
New-Item -ItemType Directory -Force -Path $binDst | Out-Null
$target = [IO.Path]::GetFullPath($InstallDir).TrimEnd('\')
if ((Resolve-Path $src).Path.TrimEnd('\') -ieq $target) {
Say 'the package is already in the target folder - skipping copy'
}
else {
# A running adolens.exe (an open Claude session) cannot be overwritten, but it can be renamed: that session keeps
# running from the *.old file, new sessions start the new version, a later install deletes the leftover.
Get-ChildItem $binDst -Filter '*.old' | ForEach-Object { Remove-Item $_.FullName -Force -ErrorAction SilentlyContinue }
foreach ($f in Get-ChildItem (Join-Path $src 'bin') -File) {
$dst = Join-Path $binDst $f.Name
if (Test-Path $dst) {
try { Remove-Item $dst -Force -ErrorAction Stop }
catch {
$oldName = "$($f.Name).$((Get-Date).ToString('yyyyMMddHHmmss')).old"
Rename-Item $dst $oldName
Say "$($f.Name) is in use by an open Claude session - renamed to $oldName"
}
}
Copy-Item $f.FullName $dst -Force
}
foreach ($f in 'install.ps1', 'uninstall.ps1', 'README.md', 'README.pl.md', 'CHANGELOG.md', 'SECURITY.md', 'LICENSE', 'THIRD-PARTY-NOTICES.md') {
if (Test-Path (Join-Path $src $f)) { Copy-Item (Join-Path $src $f) $InstallDir -Force }
}
if (Test-Path (Join-Path $src 'skills')) { Copy-Item (Join-Path $src 'skills') $InstallDir -Recurse -Force }
}
Say "version: $(& $exe version)"
# "adolens" in any new terminal: the program folder on the user's PATH, as the setup EXE does. Read and written raw
# (REG_EXPAND_SZ) so entries like %USERPROFILE%\... stay as they are.
$binFull = [IO.Path]::GetFullPath($binDst).TrimEnd('\')
$envKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment', $true)
$userPath = [string]$envKey.GetValue('Path', '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
$entries = @($userPath -split ';' | Where-Object { $_ })
if (-not ($entries | Where-Object { [Environment]::ExpandEnvironmentVariables($_).TrimEnd('\') -ieq $binFull })) {
$envKey.SetValue('Path', (($entries + $binFull) -join ';'), [Microsoft.Win32.RegistryValueKind]::ExpandString)
# Tells Windows (Explorer, new terminals) that the environment changed.
[Environment]::SetEnvironmentVariable('ADOLENS_PATH_REFRESH', '1', 'User')
[Environment]::SetEnvironmentVariable('ADOLENS_PATH_REFRESH', $null, 'User')
Say "added to PATH: $binFull ('adolens' works in terminals opened from now on)"
}
$envKey.Close()
Head '3/6 Azure DevOps connection'
& $exe status --quiet --offline
$state = $LASTEXITCODE
if ($state -eq 4 -or $Organization) {
$loginArgs = @('login')
if ($Organization) { $loginArgs += @('--org', $Organization) }
& $exe @loginArgs
if ($LASTEXITCODE -ne 0) { throw "Login failed - run the installer again or later: `"$exe`" login" }
}
else {
Say 'already configured'
}
Head '4/6 Who uses it and where code goes'
# Interactive questions with defaults (Enter accepts); skipped in a non-interactive run.
$canAsk = [Environment]::UserInteractive -and $Host.Name -ne 'Default Host' -and
-not ([Environment]::GetCommandLineArgs() | Where-Object { $_ -like '-NonI*' })
if ($canAsk) {
$setupArgs = @('setup', '--wizard')
if ($Mode) { $setupArgs += @('--mode', $Mode) }
& $exe @setupArgs
}
elseif ($Mode) { & $exe config --mode $Mode | Out-Null; Say "mode: $Mode" }
else { Say 'skipped (not interactive) - later: "adolens setup", or Claude asks when needed' }
Head '5/6 Skills for Claude'
# Claude Code reads skills from %USERPROFILE%\.claude\skills; Claude Desktop takes each skill as a ZIP uploaded in
# Settings -> Capabilities -> Skills, so the ZIPs are prepared next to the program.
$skillSrc = Join-Path $InstallDir 'skills'
if (Test-Path $skillSrc) {
# Claude Code loads every installed skill in each session: only the skills of the mode go there. The ZIPs for
# Claude Desktop are written next to the program; both with descriptions in the language of the installation.
Say (& $exe skills | Out-String).Trim()
}
else { Say 'no skills in this package' }
Head '6/6 Registering in Claude'
if ($needsRegister) {
& $exe register @scope --force
if ($LASTEXITCODE -ne 0) { throw 'Registration failed.' }
}
elseif ($NoRegister) {
Say "skipped (-NoRegister). Later: `"$exe`" register"
}
elseif ($skippedRegister) {
Write-Host " skipped - quit Claude Desktop, then run: & `"$exe`" register" -ForegroundColor Yellow
}
else {
Say 'already registered'
}
Head 'Done'
if ($closedClaude -and -not $NoRestart) {
if (Start-ClaudeDesktop) { Say 'Claude Desktop started again' } else { Say 'start Claude Desktop again' }
}
else {
Say 'Claude Desktop: restart it (tray icon -> Quit, then start it). Claude Code: start a new session or /mcp -> reconnect.'
}
Say "Try in Claude: what work item types and fields does our project have?"
Say "Status: `"$exe`" status"
Say "Uninstall: $InstallDir\uninstall.ps1"