-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ps1
71 lines (54 loc) · 2.01 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
70
71
$release = "1.5.0"
$os = "windows"
$arch = "amd64"
$default_base_dir="$HOME\.g"
$dest_file = "${base_dir}\downloads\g${release}.${os}-${arch}.zip"
$url = "https://github.com/voidint/g/releases/download/v${release}/g${release}.${os}-${arch}.zip"
param([string] $base_dir = "$default_base_dir")
function NewDirs () {
New-Item -Force -Path "$base_dir\downloads", "$base_dir\bin" -ItemType "directory"
}
function CleanDirs() {
Remove-Item -Recurse -Path "$base_dir"
}
function DownloadRelease() {
Invoke-WebRequest -Uri "$url" -OutFile "$dest_file"
}
function InstallG () {
Expand-Archive "$dest_file" "$base_dir\bin\"
}
function setHOME() {
if ($base_dir -ne $default_base_dir) {
[System.Environment]::SetEnvironmentVariable("G_EXPERIMENTAL", "true", [System.EnvironmentVariableTarget]::User)
}
[System.Environment]::SetEnvironmentVariable("G_HOME", $base_dir, [System.EnvironmentVariableTarget]::User)
[System.Environment]::SetEnvironmentVariable("GOROOT", "$base_dir\go", [System.EnvironmentVariableTarget]::User)
}
function setPath() {
$paths = [System.Environment]::GetEnvironmentVariable("PATH", [System.EnvironmentVariableTarget]::User) -split ';'
$newPaths = @("%G_HOME%\bin", "%GOROOT%\bin", "%GOPATH%\bin")
foreach ($p in $newPaths) {
if ($p -in $paths) {
Write-Output "$p already exists"
continue
}
[System.Environment]::SetEnvironmentVariable(
"PATH",
[System.Environment]::GetEnvironmentVariable("PATH", [System.EnvironmentVariableTarget]::User) + "$p;",
[System.EnvironmentVariableTarget]::User
)
Write-Host -ForegroundColor Green "$p appended"
}
}
function SetEnv () {
setHOME
setPath
}
Write-Host -ForegroundColor Blue "[1/3] Downloading ${url}"
NewDirs
DownloadRelease
Write-Host -ForegroundColor Blue "[2/3] Install g to the ${base_dir}\bin"
InstallG
Write-Host -ForegroundColor Blue "[3/3] Set environment variables"
SetEnv
Write-Host -ForegroundColor Green "Done!"