-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.ps1
More file actions
88 lines (74 loc) · 3.13 KB
/
Copy pathbootstrap.ps1
File metadata and controls
88 lines (74 loc) · 3.13 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
[CmdletBinding()]
param(
[string]$SourceDir = $PSScriptRoot
)
$ErrorActionPreference = "Stop"
function Add-PathEntry {
param([Parameter(Mandatory = $true)][string]$Path)
if (-not (Test-Path -LiteralPath $Path -PathType Container)) {
return
}
$processParts = @($env:PATH -split ";" | Where-Object { $_ })
if ($processParts -notcontains $Path) {
$env:PATH = "$Path;$env:PATH"
}
if ($env:GITHUB_PATH) {
$githubPathEntries = @()
if (Test-Path -LiteralPath $env:GITHUB_PATH) {
$githubPathEntries = @(Get-Content -LiteralPath $env:GITHUB_PATH -ErrorAction SilentlyContinue)
}
if ($githubPathEntries -notcontains $Path) {
Add-Content -LiteralPath $env:GITHUB_PATH -Value $Path
}
}
}
function Install-ChezmoiFromGitHub {
$archMap = @{
X64 = "amd64"
Arm64 = "arm64"
}
$arch = $archMap[[System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString()]
if (-not $arch) {
throw "Unsupported Windows architecture for chezmoi install: $([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)"
}
$release = Invoke-RestMethod -Uri "https://api.github.com/repos/twpayne/chezmoi/releases/latest"
$asset = $release.assets |
Where-Object { $_.name -match "windows" -and $_.name -match $arch -and $_.name -match "\.zip$" } |
Select-Object -First 1
if (-not $asset) {
throw "Could not find a chezmoi Windows $arch zip asset in the latest GitHub release."
}
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ("chezmoi-install-" + [guid]::NewGuid().ToString("N"))
$zipPath = Join-Path $tempDir $asset.name
$binDir = Join-Path $HOME ".local\bin"
New-Item -ItemType Directory -Force -Path $tempDir, $binDir | Out-Null
try {
Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $zipPath
Expand-Archive -LiteralPath $zipPath -DestinationPath $tempDir -Force
$exe = Get-ChildItem -LiteralPath $tempDir -Filter "chezmoi.exe" -Recurse -File | Select-Object -First 1
if (-not $exe) {
throw "Downloaded chezmoi archive did not contain chezmoi.exe."
}
Copy-Item -LiteralPath $exe.FullName -Destination (Join-Path $binDir "chezmoi.exe") -Force
Add-PathEntry -Path $binDir
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
$pathParts = @($userPath -split ";" | Where-Object { $_ })
if ($pathParts -notcontains $binDir) {
$newUserPath = if ($userPath) { "$binDir;$userPath" } else { $binDir }
[Environment]::SetEnvironmentVariable("Path", $newUserPath, "User")
}
} finally {
Remove-Item -LiteralPath $tempDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
if (-not (Get-Command chezmoi -ErrorAction SilentlyContinue)) {
if (Get-Command winget -ErrorAction SilentlyContinue) {
winget install --id twpayne.chezmoi -e --source winget --accept-package-agreements --accept-source-agreements
Add-PathEntry -Path (Join-Path $env:LOCALAPPDATA "Microsoft\WinGet\Links")
Add-PathEntry -Path (Join-Path $env:LOCALAPPDATA "Microsoft\WindowsApps")
}
if (-not (Get-Command chezmoi -ErrorAction SilentlyContinue)) {
Install-ChezmoiFromGitHub
}
}
chezmoi apply --source $SourceDir