-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.ps1
More file actions
394 lines (360 loc) · 9.99 KB
/
code.ps1
File metadata and controls
394 lines (360 loc) · 9.99 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env pwsh
# Author: Cheatoid ~ https://github.com/Cheatoid
# License: MIT
<#
.SYNOPSIS
Launch VS Code or VS Code Insiders with an isolated profile and no extensions (cross-platform).
.PARAMETER TempBase
Optional path to use as the base folder for temp user-data and extensions directories.
If provided, two subfolders will be created: <TempBase>/user-data and <TempBase>/extensions.
.PARAMETER KeepTemp
Keep the temporary directories after the editor exits. Mutually exclusive with -TempBase.
.PARAMETER Insiders
Force using VS Code Insiders. If Insiders is not available the script will error.
.PARAMETER NoInsiders
Force using stable VS Code (do not use Insiders even if present).
.EXAMPLE
.\code.ps1 -Insiders
Force open VS Code Insiders with a clean profile.
.EXAMPLE
.\code.ps1 -NoInsiders -TempBase C:\tmp\vscode-clean
Force open stable VS Code and use the provided TempBase for user-data and extensions.
#>
param(
[string]$TempBase,
[switch]$KeepTemp,
[switch]$Insiders,
[switch]$NoInsiders
)
# Validate mutual exclusivity
if ($TempBase -and $KeepTemp) {
Write-Error "Parameters -TempBase and -KeepTemp are mutually exclusive. Choose one."
exit 2
}
if ($Insiders -and $NoInsiders) {
Write-Error "Parameters -Insiders and -NoInsiders are mutually exclusive. Choose one."
exit 2
}
function New-UniqueTempDir {
param([string]$prefix = "vscode-clean")
$base = [System.IO.Path]::GetTempPath()
$name = "$prefix-$([guid]::NewGuid().ToString('N') )"
$path = Join-Path $base $name
New-Item -ItemType Directory -Path $path -Force | Out-Null
return $path
}
function Find-CodeBinary {
param(
[switch]$preferInsiders, # if set, prefer Insiders
[switch]$forceInsiders, # if set, require Insiders
[switch]$forceStable # if set, require stable (skip Insiders)
)
# Helper to return structured result
function _result($path, $flavor) {
return @{ Path = $path; Mode = "cli"; Flavor = $flavor }
}
# If CLI on PATH, prefer according to flags
$insidersCmd = Get-Command "code-insiders" -ErrorAction SilentlyContinue
$stableCmd = Get-Command "code" -ErrorAction SilentlyContinue
if ($forceInsiders) {
if ($insidersCmd) {
return _result $insidersCmd.Source "insiders"
}
# try common user-local/system locations for Insiders before failing
}
if ($forceStable) {
if ($stableCmd) {
return _result $stableCmd.Source "stable"
}
# try common user-local/system locations for stable before failing
}
# If neither forced, respect preferInsiders flag or default prefer Insiders
$preferIns = $preferInsiders -or (-not $forceStable -and -not $forceInsiders)
if ($preferIns) {
if ($insidersCmd) {
return _result $insidersCmd.Source "insiders"
}
if ($stableCmd) {
return _result $stableCmd.Source "stable"
}
}
else {
if ($stableCmd) {
return _result $stableCmd.Source "stable"
}
if ($insidersCmd) {
return _result $insidersCmd.Source "insiders"
}
}
# Platform-specific candidate lists, preferring user-local installs and Insiders first (or stable first if forced)
if ($IsWindows) {
$candidatesInsiders = @(
"$env:LOCALAPPDATA\Programs\Microsoft VS Code Insiders\bin\code-insiders.cmd",
"$env:ProgramFiles\Microsoft VS Code Insiders\bin\code-insiders.cmd",
"$env:ProgramFiles(x86)\Microsoft VS Code Insiders\bin\code-insiders.cmd"
)
$candidatesStable = @(
"$env:LOCALAPPDATA\Programs\Microsoft VS Code\bin\code.cmd",
"$env:ProgramFiles\Microsoft VS Code\bin\code.cmd",
"$env:ProgramFiles(x86)\Microsoft VS Code\bin\code.cmd"
)
if ($forceInsiders) {
foreach ($p in $candidatesInsiders) {
if ($p -and (Test-Path $p)) {
return _result $p "insiders"
}
}
return $null
}
if ($forceStable) {
foreach ($p in $candidatesStable) {
if ($p -and (Test-Path $p)) {
return _result $p "stable"
}
}
return $null
}
# Default: prefer Insiders
foreach ($p in $candidatesInsiders) {
if ($p -and (Test-Path $p)) {
return _result $p "insiders"
}
}
foreach ($p in $candidatesStable) {
if ($p -and (Test-Path $p)) {
return _result $p "stable"
}
}
}
elseif ($IsMacOS) {
$candidatesInsiders = @(
"$HOME/.local/bin/code-insiders",
"/usr/local/bin/code-insiders",
"/opt/homebrew/bin/code-insiders",
"/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin/code",
"$HOME/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin/code"
)
$candidatesStable = @(
"$HOME/.local/bin/code",
"/usr/local/bin/code",
"/opt/homebrew/bin/code",
"/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code",
"$HOME/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"
)
if ($forceInsiders) {
foreach ($p in $candidatesInsiders) {
if ($p -and (Test-Path $p)) {
return _result $p "insiders"
}
}
return $null
}
if ($forceStable) {
foreach ($p in $candidatesStable) {
if ($p -and (Test-Path $p)) {
return _result $p "stable"
}
}
return $null
}
if ($preferIns) {
foreach ($p in $candidatesInsiders) {
if ($p -and (Test-Path $p)) {
return _result $p "insiders"
}
}
foreach ($p in $candidatesStable) {
if ($p -and (Test-Path $p)) {
return _result $p "stable"
}
}
}
else {
foreach ($p in $candidatesStable) {
if ($p -and (Test-Path $p)) {
return _result $p "stable"
}
}
foreach ($p in $candidatesInsiders) {
if ($p -and (Test-Path $p)) {
return _result $p "insiders"
}
}
}
return $null
}
else {
# Linux
$candidatesInsiders = @(
"$HOME/.local/bin/code-insiders",
"/usr/bin/code-insiders",
"/usr/local/bin/code-insiders",
"/snap/bin/code-insiders"
)
$candidatesStable = @(
"$HOME/.local/bin/code",
"/usr/bin/code",
"/usr/local/bin/code",
"/snap/bin/code"
)
if ($forceInsiders) {
foreach ($p in $candidatesInsiders) {
if ($p -and (Test-Path $p)) {
return _result $p "insiders"
}
}
return $null
}
if ($forceStable) {
foreach ($p in $candidatesStable) {
if ($p -and (Test-Path $p)) {
return _result $p "stable"
}
}
return $null
}
if ($preferIns) {
foreach ($p in $candidatesInsiders) {
if ($p -and (Test-Path $p)) {
return _result $p "insiders"
}
}
foreach ($p in $candidatesStable) {
if ($p -and (Test-Path $p)) {
return _result $p "stable"
}
}
}
else {
foreach ($p in $candidatesStable) {
if ($p -and (Test-Path $p)) {
return _result $p "stable"
}
}
foreach ($p in $candidatesInsiders) {
if ($p -and (Test-Path $p)) {
return _result $p "insiders"
}
}
}
}
return $null
}
# Prepare temp dirs
$createdTempBase = $false
if ($TempBase) {
try {
if (-not (Test-Path -LiteralPath $TempBase)) {
New-Item -ItemType Directory -Path $TempBase -Force | Out-Null
$createdTempBase = $true
}
$userDataDir = Join-Path $TempBase "user-data"
$extensionsDir = Join-Path $TempBase "extensions"
New-Item -ItemType Directory -Path $userDataDir -Force | Out-Null
New-Item -ItemType Directory -Path $extensionsDir -Force | Out-Null
}
catch {
Write-Error "Failed to create TempBase directories: $_"
exit 3
}
}
else {
$userDataDir = New-UniqueTempDir "vscode-user"
$extensionsDir = New-UniqueTempDir "vscode-ext"
}
Write-Host "User data dir: $userDataDir"
Write-Host "Extensions dir: $extensionsDir"
# Determine find options from switches
$findParams = @{ }
if ($Insiders) {
$findParams["forceInsiders"] = $true
}
elseif ($NoInsiders) {
$findParams["forceStable"] = $true
}
else {
$findParams["preferInsiders"] = $true
}
$found = Find-CodeBinary @findParams
try {
if ($found) {
$exe = $found.Path
$args = @(
"--user-data-dir", $userDataDir,
"--extensions-dir", $extensionsDir,
"--disable-extensions"
)
Write-Host "Launching: $exe $( $args -join ' ' )"
$proc = Start-Process -FilePath $exe -ArgumentList $args -PassThru
Wait-Process -Id $proc.Id
}
else {
if ($IsMacOS) {
# macOS fallback: prefer Insiders app if Insiders forced or available, else stable
$insidersApp = "Visual Studio Code - Insiders"
$stableApp = "Visual Studio Code"
$args = @("--user-data-dir", $userDataDir, "--extensions-dir", $extensionsDir, "--disable-extensions")
if ($Insiders) {
if (Test-Path "/Applications/Visual Studio Code - Insiders.app") {
$appToOpen = $insidersApp
}
else {
Write-Error "Insiders requested but not found on this system."
exit 6
}
}
elseif ($NoInsiders) {
if (Test-Path "/Applications/Visual Studio Code.app") {
$appToOpen = $stableApp
}
else {
Write-Error "Stable VS Code requested but not found on this system."
exit 7
}
}
else {
if (Test-Path "/Applications/Visual Studio Code - Insiders.app") {
$appToOpen = $insidersApp
}
elseif (Test-Path "/Applications/Visual Studio Code.app") {
$appToOpen = $stableApp
}
else {
Write-Error "Neither VS Code Insiders nor stable app found."
exit 4
}
}
Write-Host "CLI not found. Using 'open -a' fallback for macOS: $appToOpen"
$proc = Start-Process -FilePath "open" -ArgumentList "-a", $appToOpen, "--args", $args -PassThru
Wait-Process -Id $proc.Id
}
else {
Write-Error "'code-insiders' and 'code' not found on PATH and no known fallback available for this OS."
Write-Host "Install the CLI or run the editor manually with:"
Write-Host " --user-data-dir $userDataDir --extensions-dir $extensionsDir --disable-extensions"
exit 5
}
}
}
finally {
# Cleanup logic
if ($KeepTemp) {
Write-Host "Kept temp dirs (requested):"
Write-Host " $userDataDir"
Write-Host " $extensionsDir"
}
elseif ($TempBase) {
Write-Host "TempBase was provided; leaving directories in place:"
Write-Host " $userDataDir"
Write-Host " $extensionsDir"
}
else {
try {
Remove-Item -LiteralPath $userDataDir -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $extensionsDir -Recurse -Force -ErrorAction SilentlyContinue
}
catch {
Write-Warning "Failed to remove temp dirs: $_"
}
}
}