forked from PowerShell/vscode-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstall-VSCode.ps1
615 lines (489 loc) · 18.4 KB
/
Install-VSCode.ps1
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<#PSScriptInfo
.VERSION 1.4
.GUID 539e5585-7a02-4dd6-b9a6-5dd288d0a5d0
.AUTHOR Microsoft
.COMPANYNAME Microsoft Corporation
.COPYRIGHT (c) Microsoft Corporation
.TAGS install vscode installer
.LICENSEURI https://github.com/PowerShell/vscode-powershell/blob/develop/LICENSE.txt
.PROJECTURI https://github.com/PowerShell/vscode-powershell/blob/develop/scripts/Install-VSCode.ps1
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
30/08/2019 - added functionality to install the "User Install" variant of Stable Edition.
--
07/11/2018 - added support for PowerShell Core and macOS/Linux platforms.
--
15/08/2018 - added functionality to install the new "User Install" variant of Insiders Edition.
--
21/03/2018 - added functionality to install the VSCode context menus. Also, VSCode is now always added to the search path
--
20/03/2018 - fix OS detection to prevent error
--
28/12/2017 - added functionality to support 64-bit versions of VSCode
& support for installation of VSCode Insiders Edition.
--
Initial release.
#>
<#
.SYNOPSIS
Installs Visual Studio Code, the PowerShell extension, and optionally
a list of additional extensions.
.DESCRIPTION
This script can be used to easily install Visual Studio Code and the
PowerShell extension on your machine. You may also specify additional
extensions to be installed using the -AdditionalExtensions parameter.
The -LaunchWhenDone parameter will cause VS Code to be launched as
soon as installation has completed.
Please contribute improvements to this script on GitHub!
https://github.com/PowerShell/vscode-powershell/blob/develop/scripts/Install-VSCode.ps1
.PARAMETER Architecture
A validated string defining the bit version to download. Values can be either 64-bit or 32-bit.
If 64-bit is chosen and the OS Architecture does not match, then the 32-bit build will be
downloaded instead. If parameter is not used, then 64-bit is used as default.
.PARAMETER BuildEdition
A validated string defining which build edition or "stream" to download:
Stable or Insiders Edition (system install or user profile install).
If the parameter is not used, then stable is downloaded as default.
.PARAMETER AdditionalExtensions
An array of strings that are the fully-qualified names of extensions to be
installed in addition to the PowerShell extension. The fully qualified
name is formatted as "<publisher name>.<extension name>" and can be found
next to the extension's name in the details tab that appears when you
click an extension in the Extensions panel in Visual Studio Code.
.PARAMETER LaunchWhenDone
When present, causes Visual Studio Code to be launched as soon as installation
has finished.
.PARAMETER EnableContextMenus
When present, causes the installer to configure the Explorer context menus
.EXAMPLE
Install-VSCode.ps1 -Architecture 32-bit
Installs Visual Studio Code (32-bit) and the powershell extension.
.EXAMPLE
Install-VSCode.ps1 -LaunchWhenDone
Installs Visual Studio Code (64-bit) and the PowerShell extension and then launches
the editor after installation completes.
.EXAMPLE
Install-VSCode.ps1 -AdditionalExtensions 'eamodio.gitlens', 'vscodevim.vim'
Installs Visual Studio Code (64-bit), the PowerShell extension, and additional
extensions.
.EXAMPLE
Install-VSCode.ps1 -BuildEdition Insider-User -LaunchWhenDone
Installs Visual Studio Code Insiders Edition (64-bit) to the user profile and then launches the editor
after installation completes.
.NOTES
This script is licensed under the MIT License:
Copyright (c) Microsoft Corporation.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#>
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[parameter()]
[ValidateSet('64-bit', '32-bit')]
[string]$Architecture = '64-bit',
[parameter()]
[ValidateSet('Stable-System', 'Stable-User', 'Insider-System', 'Insider-User')]
[string]$BuildEdition = "Stable-System",
[Parameter()]
[ValidateNotNull()]
[string[]]$AdditionalExtensions = @(),
[switch]$LaunchWhenDone,
[switch]$EnableContextMenus
)
# Taken from https://code.visualstudio.com/docs/setup/linux#_installation
$script:VSCodeYumRepoEntry = @"
[code]
name=Visual Studio Code
baseurl=https://packages.microsoft.com/yumrepos/vscode
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc
"@
$script:VSCodeZypperRepoEntry = @"
[code]
name=Visual Studio Code
baseurl=https://packages.microsoft.com/yumrepos/vscode
enabled=1
type=rpm-md
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc
"@
function Test-IsOsArchX64 {
if ($PSVersionTable.PSVersion.Major -lt 6) {
return (Get-CimInstance -ClassName Win32_OperatingSystem).OSArchitecture -eq '64-bit'
}
return [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture -eq [System.Runtime.InteropServices.Architecture]::X64
}
function Get-AvailablePackageManager
{
if (Get-Command 'apt' -ErrorAction SilentlyContinue) {
return 'apt'
}
if (Get-Command 'dnf' -ErrorAction SilentlyContinue) {
return 'dnf'
}
if (Get-Command 'yum' -ErrorAction SilentlyContinue) {
return 'yum'
}
if (Get-Command 'zypper' -ErrorAction SilentlyContinue) {
return 'zypper'
}
}
function Get-CodePlatformInformation {
param(
[Parameter(Mandatory=$true)]
[ValidateSet('32-bit', '64-bit')]
[string]
$Bitness,
[Parameter(Mandatory=$true)]
[ValidateSet('Stable-System', 'Stable-User', 'Insider-System', 'Insider-User')]
[string]
$BuildEdition
)
if ($IsWindows -or $PSVersionTable.PSVersion.Major -lt 6) {
$os = 'Windows'
}
elseif ($IsLinux) {
$os = 'Linux'
}
elseif ($IsMacOS) {
$os = 'MacOS'
}
else {
throw 'Could not identify operating system'
}
if ($Bitness -ne '64-bit' -and $os -ne 'Windows') {
throw "Non-64-bit *nix systems are not supported"
}
if ($BuildEdition.EndsWith('User') -and $os -ne 'Windows') {
throw 'User builds are not available for non-Windows systems'
}
switch ($BuildEdition) {
'Stable-System' {
$appName = "Visual Studio Code ($Bitness)"
break
}
'Stable-User' {
$appName = "Visual Studio Code ($($Architecture) - User)"
break
}
'Insider-System' {
$appName = "Visual Studio Code - Insiders Edition ($Bitness)"
break
}
'Insider-User' {
$appName = "Visual Studio Code - Insiders Edition ($($Architecture) - User)"
break
}
}
switch ($os) {
'Linux' {
$pacMan = Get-AvailablePackageManager
switch ($pacMan) {
'apt' {
$platform = 'linux-deb-x64'
$ext = 'deb'
break
}
{ 'dnf','yum','zypper' -contains $_ } {
$platform = 'linux-rpm-x64'
$ext = 'rpm'
break
}
default {
$platform = 'linux-x64'
$ext = 'tar.gz'
break
}
}
if ($BuildEdition.StartsWith('Insider')) {
$exePath = '/usr/bin/code-insiders'
break
}
$exePath = '/usr/bin/code'
break
}
'MacOS' {
$platform = 'darwin'
$ext = 'zip'
if ($BuildEdition.StartsWith('Insider')) {
$exePath = '/usr/local/bin/code-insiders'
break
}
$exePath = '/usr/local/bin/code'
break
}
'Windows' {
$ext = 'exe'
switch ($Bitness) {
'32-bit' {
$platform = 'win32'
if (Test-IsOsArchX64) {
$installBase = ${env:ProgramFiles(x86)}
break
}
$installBase = ${env:ProgramFiles}
break
}
'64-bit' {
$installBase = ${env:ProgramFiles}
if (Test-IsOsArchX64) {
$platform = 'win32-x64'
break
}
Write-Warning '64-bit install requested on 32-bit system. Installing 32-bit VSCode'
$platform = 'win32'
break
}
}
switch ($BuildEdition) {
'Stable-System' {
$exePath = "$installBase\Microsoft VS Code\bin\code.cmd"
}
'Stable-User' {
$exePath = "${env:LocalAppData}\Programs\Microsoft VS Code\bin\code.cmd"
}
'Insider-System' {
$exePath = "$installBase\Microsoft VS Code Insiders\bin\code-insiders.cmd"
}
'Insider-User' {
$exePath = "${env:LocalAppData}\Programs\Microsoft VS Code Insiders\bin\code-insiders.cmd"
}
}
}
}
switch ($BuildEdition) {
'Stable-System' {
$channel = 'stable'
break
}
'Stable-User' {
$channel = 'stable'
$platform += '-user'
break
}
'Insider-System' {
$channel = 'insider'
break
}
'Insider-User' {
$channel = 'insider'
$platform += '-user'
break
}
}
$info = @{
AppName = $appName
ExePath = $exePath
Platform = $platform
Channel = $channel
FileUri = "https://vscode-update.azurewebsites.net/latest/$platform/$channel"
Extension = $ext
}
if ($pacMan) {
$info['PackageManager'] = $pacMan
}
return $info
}
function Save-WithBitsTransfer {
param(
[Parameter(Mandatory=$true)]
[string]
$FileUri,
[Parameter(Mandatory=$true)]
[string]
$Destination,
[Parameter(Mandatory=$true)]
[string]
$AppName
)
Write-Host "`nDownloading latest $AppName..." -ForegroundColor Yellow
Remove-Item -Force $Destination -ErrorAction SilentlyContinue
$bitsDl = Start-BitsTransfer $FileUri -Destination $Destination -Asynchronous
while (($bitsDL.JobState -eq 'Transferring') -or ($bitsDL.JobState -eq 'Connecting')) {
Write-Progress -Activity "Downloading: $AppName" -Status "$([math]::round($bitsDl.BytesTransferred / 1mb))mb / $([math]::round($bitsDl.BytesTotal / 1mb))mb" -PercentComplete ($($bitsDl.BytesTransferred) / $($bitsDl.BytesTotal) * 100 )
}
switch ($bitsDl.JobState) {
'Transferred' {
Complete-BitsTransfer -BitsJob $bitsDl
break
}
'Error' {
throw 'Error downloading installation media.'
}
}
}
function Install-VSCodeFromTar {
param(
[Parameter(Mandatory=$true)]
[string]
$TarPath,
[Parameter()]
[switch]
$Insiders
)
$tarDir = Join-Path ([System.IO.Path]::GetTempPath()) 'VSCodeTar'
$destDir = '/opt/VSCode-linux-x64'
New-Item -ItemType Directory -Force -Path $tarDir
try {
Push-Location $tarDir
tar xf $TarPath
Move-Item -LiteralPath "$tarDir/VSCode-linux-x64" $destDir
}
finally {
Pop-Location
}
if ($Insiders) {
ln -s "$destDir/code-insiders" /usr/bin/code-insiders
return
}
ln -s "$destDir/code" /usr/bin/code
}
# We need to be running as elevated on *nix
if (($IsLinux -or $IsMacOS) -and (id -u) -ne 0) {
throw "Must be running as root to install VSCode.`nInvoke this script with (for example):`n`tsudo pwsh -f Install-VSCode.ps1 -BuildEdition Stable-System"
}
try {
$prevProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
# Get information required for installation
$codePlatformInfo = Get-CodePlatformInformation -Bitness $Architecture -BuildEdition $BuildEdition
# Download the installer
$tmpdir = [System.IO.Path]::GetTempPath()
$ext = $codePlatformInfo.Extension
$installerName = "vscode-install.$ext"
$installerPath = [System.IO.Path]::Combine($tmpdir, $installerName)
if ($PSVersionTable.PSVersion.Major -le 5) {
Save-WithBitsTransfer -FileUri $codePlatformInfo.FileUri -Destination $installerPath -AppName $codePlatformInfo.AppName
}
# We don't want to use RPM packages -- see the installation step below
elseif ($codePlatformInfo.Extension -ne 'rpm') {
if ($PSCmdlet.ShouldProcess($codePlatformInfo.FileUri, "Invoke-WebRequest -OutFile $installerPath")) {
Invoke-WebRequest -Uri $codePlatformInfo.FileUri -OutFile $installerPath
}
}
# Install VSCode
switch ($codePlatformInfo.Extension) {
# On Debian-like Linux distros
'deb' {
if (-not $PSCmdlet.ShouldProcess($installerPath, 'apt install -y')) {
break
}
# The deb file contains the information to install its own repository,
# so we just need to install it
apt install -y $installerPath
break
}
# On distros using rpm packages, the RPM package doesn't set up the repo.
# To install VSCode properly in way that the package manager tracks it,
# we have to do things the hard way - install the repo and install the package
'rpm' {
$pacMan = $codePlatformInfo.PackageManager
if (-not $PSCmdlet.ShouldProcess($installerPath, "$pacMan install -y")) {
break
}
# Install the VSCode repo with the package manager
rpm --import https://packages.microsoft.com/keys/microsoft.asc
switch ($pacMan) {
'zypper' {
$script:VSCodeZypperRepoEntry > /etc/zypp/repos.d/vscode.repo
zypper refresh -y
}
default {
$script:VSCodeYumRepoEntry > /etc/yum.repos.d/vscode.repo
& $pacMan check-update -y
}
}
switch ($BuildEdition) {
'Stable-System' {
& $pacMan install -y code
}
default {
& $pacMan install -y code-insiders
}
}
break
}
# On Windows
'exe' {
$exeArgs = '/verysilent /tasks=addtopath'
if ($EnableContextMenus) {
$exeArgs = '/verysilent /tasks=addcontextmenufiles,addcontextmenufolders,addtopath'
}
if (-not $PSCmdlet.ShouldProcess("$installerPath $exeArgs", 'Start-Process -Wait')) {
break
}
Start-Process -Wait $installerPath -ArgumentList $exeArgs
break
}
# On Mac
'zip' {
if (-not $PSCmdlet.ShouldProcess($installerPath, "Expand-Archive -DestinationPath $zipDirPath -Force; Move-Item $zipDirPath/*.app /Applications/")) {
break
}
$zipDirPath = [System.IO.Path]::Combine($tmpdir, 'VSCode')
Expand-Archive -LiteralPath $installerPath -DestinationPath $zipDirPath -Force
Move-Item "$zipDirPath/*.app" '/Applications/'
break
}
# Remaining Linux distros using tar - more complicated
'tar.gz' {
if (-not $PSCmdlet.ShouldProcess($installerPath, 'Install-VSCodeFromTar (expand, move to /opt/, symlink)')) {
break
}
Install-VSCodeFromTar -TarPath $installerPath -Insiders:($BuildEdition -ne 'Stable-System')
break
}
default {
throw "Unkown package type: $($codePlatformInfo.Extension)"
}
}
$codeExePath = $codePlatformInfo.ExePath
# Install any extensions
$extensions = @("ms-vscode.PowerShell") + $AdditionalExtensions
if ($PSCmdlet.ShouldProcess(($extensions -join ','), "$codeExePath --install-extension")) {
if ($IsLinux -or $IsMacOS) {
# On *nix we need to install extensions as the user -- VSCode refuses root
$extsSlashes = $extensions -join '/'
sudo -H -u $env:SUDO_USER pwsh -c "`$exts = '$extsSlashes' -split '/'; foreach (`$e in `$exts) { $codeExePath --install-extension `$e }"
}
else {
foreach ($extension in $extensions) {
Write-Host "`nInstalling extension $extension..." -ForegroundColor Yellow
& $codeExePath --install-extension $extension
}
}
}
# Launch if requested
if ($LaunchWhenDone) {
$appName = $codePlatformInfo.AppName
if (-not $PSCmdlet.ShouldProcess($appName, "Launch with $codeExePath")) {
return
}
Write-Host "`nInstallation complete, starting $appName...`n`n" -ForegroundColor Green
& $codeExePath
return
}
if ($PSCmdlet.ShouldProcess('Installation complete!', 'Write-Host')) {
Write-Host "`nInstallation complete!`n`n" -ForegroundColor Green
}
}
finally {
$ProgressPreference = $prevProgressPreference
}