Skip to content

Commit

Permalink
Fix the install.ps1 script to better support Powershell v5
Browse files Browse the repository at this point in the history
  * Fix architecture detection
  * Use built-in Expand-Archive to unzip packages.

Document some examples of how to run the install script
  • Loading branch information
zb140 committed Jan 24, 2021
1 parent c784a95 commit 2a80bcb
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
55 changes: 42 additions & 13 deletions assets/scripts/install.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#Requires -Version 5
enum LogLevel {
Debug = 3
Info = 2
Expand Down Expand Up @@ -107,7 +106,8 @@ function log-critical {

function get_goos {
$ri = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform;
if ($ri.Invoke([Runtime.InteropServices.OSPlatform]::Windows)) {
# if $ri isn't defined, then we must be running in Powershell 5.1, which only works on Windows.
if (-not $ri -or $ri.Invoke([Runtime.InteropServices.OSPlatform]::Windows)) {
return "windows"
} elseif ($ri.Invoke([Runtime.InteropServices.OSPlatform]::Linux)) {
return "linux"
Expand All @@ -121,22 +121,37 @@ function get_goos {
}

function get_goarch {
# convert these to strings because the enums don't apparently work in switch TODO: is this fixable?
switch ("$([Runtime.InteropServices.RuntimeInformation]::OSArchitecture)") {
"$([Runtime.InteropServices.Architecture]::Arm)" {
$arch = "$([Runtime.InteropServices.RuntimeInformation]::OSArchitecture)";
$wmi_arch = $null;

if (-not $arch) {
if (Get-Command "Get-WmiObject" -ErrorAction SilentlyContinue) {
$wmi_arch = (Get-WmiObject -Class Win32_OperatingSystem | Select-Object *).OSArchitecture
if ($wmi_arch -eq "64-bit") {
$arch = "X64";
} elseif ($wmi_arch -eq "32-bit") {
$arch = "X86";
}
}
}

switch ($arch) {
"Arm" {
return "arm"
}
"$([Runtime.InteropServices.Architecture]::Arm64)" {
"Arm64" {
return "arm64"
}
"$([Runtime.InteropServices.Architecture]::X86)" {
"X86" {
return "i386"
}
"$([Runtime.InteropServices.Architecture]::X64)" {
"X64" {
return "amd64"
}

default {
throw "unsupported architecture: $_"
log-debug "Unsupported architecture: $arch (wmi: $wmi_arch)"
throw "unsupported architecture"
}
}
}
Expand Down Expand Up @@ -191,17 +206,31 @@ function unpack-file {
} elseif ($file.EndsWith(".tar")) {
tar -xf $file
} elseif ($file.EndsWith(".zip")) {
unzip $file
Expand-Archive -LiteralPath $file -DestinationPath .
} else {
throw "can't unpack unknown format for $file"
}
}

function main {
<#
.SYNOPSIS
Install the Chezmoi dotfile manager
.DESCRIPTION
Installs Chezmoi to the given directory, defaulting to ./bin
You can specify a particular git tag using the -Tag option.
Examples:
'$params = "-BinDir ~/bindir"', (iwr https://git.io/chezmoi.ps1).Content | powershell -c -
'$params = "-Tag v1.8.10"', (iwr https://git.io/chezmoi.ps1).Content | powershell -c -
#>
function Install-Chezmoi {
[CmdletBinding(PositionalBinding=$false)]
param(
[Parameter(Mandatory = $false)]
[string] $BinDir = (Join-Path (Resolve-Path '.') 'bin'),
[string]
$BinDir = (Join-Path (Resolve-Path '.') 'bin'),

[Parameter(Mandatory = $false)]
[string] $Tag = 'latest',
Expand Down Expand Up @@ -272,7 +301,7 @@ function main {
}

try {
Invoke-Expression ("main " + $params)
Invoke-Expression ("Install-Chezmoi " + $params)
} catch {
Write-Host "An error occurred while installing: $_"
} finally {
Expand Down
7 changes: 7 additions & 0 deletions chezmoi2/cmd/docs.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions cmd/docs.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions docs/INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ with a single command.

Or on systems with Powershell, you can use this command:

# To install in ./bin
(iwr https://git.io/chezmoi.ps1).Content | powershell -c -

# To install in another location
'$params = "-BinDir ~/other"', (iwr https://git.io/chezmoi.ps1).Content | powershell -c -

# For information about other options, try this:
'$params = "-?"', (iwr https://git.io/chezmoi.ps1).Content | powershell -c -

## One-line package install

Install chezmoi with a single command.
Expand Down

0 comments on commit 2a80bcb

Please sign in to comment.