-
Notifications
You must be signed in to change notification settings - Fork 2
/
install-ch.ps1
72 lines (55 loc) · 2.45 KB
/
install-ch.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
$program = "ch"
$repository = "camerondurham/ch"
$zip_filename = "ch-windows-amd64.zip"
function Get-LatestReleaseVersion($repository) {
# call with Get-LatestReleaseVersion("camerondurham/ch")
$release_endpoint = "https://api.github.com/repos/$repository/releases/latest"
$resp = Invoke-WebRequest -Uri $release_endpoint -UseBasicParsing
$content = ConvertFrom-Json $resp.content
return $content.tag_name
}
function Get-ReleasePackage($Url, $DownloadPath, $ExtractPath) {
Invoke-WebRequest $Url -OutFile $DownloadPath
Write-Output "Unpacking file"
Expand-Archive -LiteralPath $DownloadPath -DestinationPath $ExtractPath -Force
}
function Get-ReleaseUrl($Repository, $Version, $Filename) {
# repository: username/reponame format
# example: camerondurham/ch
# version: version tag to download
# example: v0.0.6-beta
# zip_filename: asset to download
# example: ch-windows-amd64.zip
"https://github.com/{0}/releases/download/{1}/{2}" -f $Repository.Trim(" "), $Version.Trim(" "), $Filename.Trim(" ")
}
function Add-ToPath($directory) {
if (Test-Path $directory) {
# add program binary location to path
$path = [Environment]::GetEnvironmentVariable('Path')
$newpath = "$path" + ";$directory"
# set path for future sessions
[Environment]::SetEnvironmentVariable('Path', $newpath, [EnvironmentVariableTarget]::User)
# set path for current session
$env:Path = $newpath
}
else {
Write-Output "Invalid path [$directory] , ignoring..."
}
}
$latest_version = Get-LatestReleaseVersion($repository)
$download_url = Get-ReleaseUrl -Repository $repository -Version $latest_version -Filename $zip_filename
$home_directory = [Environment]::GetFolderPath("USERPROFILE")
Write-Output "Downloading $program version: $latest_version to $home_directory"
$download_path = Join-Path -Path $home_directory -ChildPath $zip_filename
$unpack_path = $home_directory
Get-ReleasePackage -Url $download_url -DownloadPath $download_path -ExtractPath $unpack_path
# remove zip file
Remove-Item (Join-Path -Path $unpack_path -ChildPath $zip_filename)
$unpacked_folder = $zip_filename.TrimEnd(".zip")
$binary_location = Join-Path -Path $unpack_path -ChildPath $unpacked_folder
# add directory to path if not already already added
$found=([Environment]::GetEnvironmentVariable('Path') | Select-String -Pattern $unpacked_folder)
if ([string]::IsNullOrEmpty($found)) {
Add-ToPath $binary_location
}
Write-Output "Done! Try using ch with: ch --help"