-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.ps1
215 lines (173 loc) · 5.29 KB
/
setup.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
if (($PSVersionTable.PSVersion.Major) -lt 5) {
Write-Warning "PowerShell 5 or later is required to run this install script."
Write-Warning "Please upgrade: https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell"
break
}
$allowedExecutionPolicy = @('Unrestricted', 'RemoteSigned', 'Bypass')
if ((Get-ExecutionPolicy).ToString() -notin $allowedExecutionPolicy) {
Write-Warning "PowerShell requires an execution policy in [$($allowedExecutionPolicy -join ", ")] to run this install script."
Write-Warning "For example, to set the execution policy to 'RemoteSigned' please run :"
Write-Warning "'Set-ExecutionPolicy RemoteSigned -scope CurrentUser'"
break
}
$tempdir = (Join-Path ([IO.Path]::GetTempPath()) ([IO.Path]::GetRandomFileName()));
function Fetch-FileFromWeb(
[string]$url,
[string]$path
) {
$cl = New-Object Net.WebClient
$cl.Headers['User-Agent'] = 'System.Net.WebClient'
$cl.DownloadFile($url, $path)
}
function Fetch-JsonFromWeb(
[string]$url
) {
$cl = New-Object Net.WebClient
$cl.Headers['User-Agent'] = 'System.Net.WebClient'
$cl.Headers['Accept'] = 'application/json'
return $cl.DownloadString($url) | ConvertFrom-Json
}
function get_goarch {
$arch = "$([Runtime.InteropServices.RuntimeInformation]::OSArchitecture)";
$wmi_arch = $null;
if ((-not $arch) -and [Environment]::Is64BitOperatingSystem) {
$arch = "X64";
}
# [Environment]::Is64BitOperatingSystem is only available on .net 4 or
# newer, so if we still don't know, try another method
if (-not $arch) {
if (Get-Command "Get-WmiObject" -ErrorAction SilentlyContinue) {
$wmi_arch = (Get-WmiObject -Class Win32_OperatingSystem | Select-Object *).OSArchitecture
if ($wmi_arch.StartsWith("64")) {
$arch = "X64";
}
elseif ($wmi_arch.StartsWith("32")) {
$arch = "X86";
}
}
}
switch ($arch) {
"Arm" {
return "arm"
}
"Arm64" {
return "arm64"
}
"X86" {
return "i386"
}
"X64" {
return "amd64"
}
default {
echo "Unsupported architecture: $arch (wmi: $wmi_arch)"
throw "unsupported architecture"
}
}
}
function get-real-tag {
param(
[string] $tag
)
$release_url = "https://github.com/twpayne/chezmoi/releases/$tag"
$real_tag = (Fetch-JsonFromWeb $release_url).tag_name
return $real_tag
}
function verify-hash {
param(
[string] $target,
[string] $checksums
)
$basename = [IO.Path]::GetFileName($target);
# what checksum are we looking for?
$want = (Get-Content $checksums | ForEach-Object {
$line = $_;
if ($line -match "$($basename)$") {
$hash, $name = ($line -split "\s+");
return $hash;
}
} | Select-Object -First 1).ToLower();
$got = (Get-FileHash -LiteralPath $target -Algorithm SHA256).Hash.ToLower();
if ($want -ne $got) {
Write-Error "Wanted: $want"
Write-Error "Got: $got"
throw "Checksum mismatch!"
}
}
function unpack-file {
param(
[string] $file
)
if ($file.EndsWith(".tar.gz") -or $file.EndsWith(".tgz")) {
tar -xzf $file
}
elseif ($file.EndsWith(".tar")) {
tar -xf $file
}
elseif ($file.EndsWith(".zip")) {
Expand-Archive -LiteralPath $file -DestinationPath .
}
else {
throw "can't unpack unknown format for $file"
}
}
function Install-Chezmoi {
$BinDir = "$PSScriptRoot/target"
$Tag = 'latest'
# some sub-functions (ie, get_goarch, likely others) require fetching of
# non-existent properties to not error
Set-StrictMode -off
# $BinDir = Resolve-Path $BinDir
$arch = get_goarch
$real_tag = get-real-tag $Tag
$version = if ($real_tag.StartsWith("v")) { $real_tag.Substring(1) } else { $real_tag };
echo "found version $version for $Tag/windows/$arch"
$binsuffix = ".exe"
$format = "zip"
$github_download = "https://github.com/twpayne/chezmoi/releases/download"
New-Item -Type Directory -Path $tempdir | Out-Null
# download tarball
$name = "chezmoi_$($version)_windows_$($arch)"
$tarball = "$name.$format"
$tarball_url = "$($github_download)/$real_tag/$tarball"
echo $tarball_url
$tmp_tarball = (Join-Path $tempdir $tarball)
Fetch-FileFromWeb $tarball_url $tmp_tarball
# download checksums
$checksums = "chezmoi_$($version)_checksums.txt"
$checksums_url = "$($github_download)/$($real_tag)/$($checksums)"
$tmp_checksums = (Join-Path $tempdir $checksums)
Fetch-FileFromWeb $checksums_url $tmp_checksums
# verify checksums
verify-hash $tmp_tarball $tmp_checksums
Push-Location $tempdir
unpack-file $tarball
# install the binary
if (-not (Test-Path $BinDir)) {
New-Item -Type Directory -Path $BinDir | Out-Null
}
$binary = "chezmoi$($binsuffix)";
$tmp_binary = (Join-Path $tempdir $binary);
Move-Item -Force -Path $tmp_binary -Destination $BinDir
echo "Installed $($BinDir)/$($binary)"
if ($ExecArgs) {
& "$($BinDir)/$($binary)" $ExecArgs
}
}
try {
Invoke-Expression ("Install-Chezmoi")
}
catch {
Write-Host "An error occurred while installing: $_"
}
finally {
Pop-Location
if (Test-Path $tempdir) {
Remove-Item -LiteralPath $tempdir -Recurse -Force
}
#--apply --dry-run --verbose
Invoke-Expression ("$PSScriptRoot/target/chezmoi init --one-shot --source=$PSScriptRoot")
if (Test-Path $HOME/.config) {
Remove-Item $HOME/.config -Recurse -Force
}
}