Skip to content

Make the Install-VSCode script work on *nix #1597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Nov 7, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 52 additions & 17 deletions scripts/Install-VSCode.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,41 @@ param(
[switch]$WhatIf
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this is an anti-pattern in PowerShell. Don't declare this parameter but change the [CmdletBinding()] to [CmdletBinding(SupportsShouldProcess)]. Then, below instead of checking $WhatIf, use if ($PSCmdlet.ShouldProcess(<target>, <action>) { ... }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I wondered about the right way for this -- I didn't find anything that discussed WhatIf very well. I'll change it 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok @rkeithhill, I've implemented this with ShouldProcess now. Let me know if I'm using it the right way :)

)

function Test-IsOsX64 {
# 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
"@

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-LinuxReleaseInfo {
if (-not (Test-Path '/etc/*-release')) {
return $null
function Get-AvailablePackageManager
{
if (Get-Command 'apt' -ErrorAction SilentlyContinue) {
return 'apt'
}

return Get-Content -Raw '/etc/*-release' -ErrorAction SilentlyContinue `
| ConvertFrom-Csv -Delimiter '=' -Header 'Key','Value' `
| ForEach-Object { $obj = @{} } { $obj[$_.Key] = $_.Value } { [pscustomobject]$obj }
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 {
Expand Down Expand Up @@ -215,18 +234,18 @@ function Get-CodePlatformInformation {

switch ($os) {
'Linux' {
$releaseInfo = Get-LinuxReleaseInfo
$pacMan = Get-AvailablePackageManager

switch ($releaseInfo.NAME) {
{ 'Ubuntu','Debian' -contains $_ } {
switch ($pacMan) {
'apt' {
$platform = 'linux-deb-x64'
$ext = 'deb'
break
}

{ 'Fedora','CentOS','RedHat' -contains $_ } {
{ 'dnf','yum','zypper' -contains $_ } {
$platform = 'linux-rpm-x64'
$ext = 'deb'
$ext = 'rpm'
break
}

Expand Down Expand Up @@ -265,7 +284,7 @@ function Get-CodePlatformInformation {
'32-bit' {
$platform = 'win32'

if (Test-IsOsX64) {
if (Test-IsOsArchX64) {
$installBase = ${env:ProgramFiles(x86)}
break
}
Expand Down Expand Up @@ -322,14 +341,20 @@ function Get-CodePlatformInformation {
}
}

return @{
$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 {
Expand Down Expand Up @@ -437,17 +462,27 @@ try {
Write-Host "WhatIf: apt install $installerPath"
break
}

# The deb file contains the information to install its own repository,
# so we just need to install it
apt install $installerPath
break
}

# On RedHat-list Linux distros
'rpm' {
$pacMan = $codePlatformInfo.PackageManager
if ($WhatIfPreference) {
Write-Host "WhatIf: rpm -ivh $installerPath"
Write-Host "WhatIf: $pacMan install $installerPath"
break
}
rpm -ivh $installerPath

# Install the VSCode repo with the package manager
rpm --import https://packages.microsoft.com/keys/microsoft.asc
$script:VSCodeYumRepoEntry > /etc/yum.repos.d/vscode.repo

# Use dnf or yum depending on the detected package manager
& $pacMan install $installerPath
break
}

Expand Down Expand Up @@ -529,4 +564,4 @@ try {
finally {
$ProgressPreference = $prevProgressPreference
$WhatIfPreference = $prevWhatIfPreference
}
}