forked from ducaale/xh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ps1
69 lines (44 loc) · 2.3 KB
/
install.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
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ProgressPreference = 'SilentlyContinue'
$release = Invoke-RestMethod -Method Get -Uri "https://api.github.com/repos/ducaale/xh/releases/latest"
$asset = $release.assets | Where-Object name -like *x86_64-pc-windows*.zip
$destdir = "$home\bin\"
$zipfile = "$env:TEMP\$($asset.name)"
$zipfilename = [System.IO.Path]::GetFileNameWithoutExtension("$zipfile")
Write-Output "Downloading: $($asset.name)"
Invoke-RestMethod -Method Get -Uri $asset.browser_download_url -OutFile $zipfile
# Checks if an older version of xh.exe (includes xhs.exe) exists in '$destdir', if yes, then delete it, if not, then download latest zip to extract from.
$xhPath = "${destdir}xh.exe"
$xhsPath = "${destdir}xhs.exe"
if (Test-Path -Path $xhPath -PathType Leaf) {
"Removing previous installation of xh from $($destdir)"
Remove-Item -r -fo $xhPath
Remove-Item -r -fo $xhsPath
}
# xh.exe extraction start.
Add-Type -Assembly System.IO.Compression.FileSystem
$zip = [IO.Compression.ZipFile]::OpenRead($zipfile)
$entries = $zip.Entries | Where-Object { $_.FullName -like '*.exe' }
# Create dir for result of extraction.
New-Item -ItemType Directory -Path $destdir -Force | Out-Null
# Extraction.
$entries | ForEach-Object { [IO.Compression.ZipFileExtensions]::ExtractToFile( $_, $destdir + $_.Name) }
# Free the zipfile.
$zip.Dispose()
Remove-Item -Path $zipfile
# Copy xh.exe as xhs.exe into bin.
Copy-Item $xhPath $xhsPath
# Add to environment variables.
$p = [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::User)
if (!$p.ToLower().Contains($destdir.ToLower())) {
# Path to "user"/bin.
Write-Output "Adding $destdir to your Path"
$p += "$destdir"
[System.Environment]::SetEnvironmentVariable('Path', $p, [System.EnvironmentVariableTarget]::User)
$Env:Path = [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine) + ";" + $p
# Path to xhs.exe.
Write-Host "PATH environment variable changed (restart your applications that use command line)." -foreground yellow
}
# Get version from zip file name.
$xhVersion = $($zipfilename.trim("xh-v -x86_64-pc-windows-msvc.zip"))
Write-Output "xh v$($xhVersion) has been installed to:`n - $xhPath`n - $xhsPath"