-
Notifications
You must be signed in to change notification settings - Fork 4
/
install.ps1
118 lines (94 loc) · 2.44 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
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
#!/usr/bin/env pwsh
# inherit from https://deno.land/x/install@v0.1.4/install.ps1
# Copyright 2018 the Deno authors. All rights reserved. MIT license.
# required:
# 1. $repo or $r
# 2. $version or $v
# 2. $exe or $e
$ErrorActionPreference = 'Stop'
$inputRepo = if ($repo) {
"${repo}"
} else {
"${r}"
}
$inputVersion = if ($version) {
"${version}"
} else {
"${v}"
}
$inputExe = if ($exe) {
"${exe}"
} else {
"${e}"
}
$githubUrl = if ($github) {
"${github}"
} elseif ($g) {
"${g}"
}else {
"https://github.com"
}
$arr = $inputRepo.Split('/')
$owner = $arr.Get(0)
$repoName = $arr.Get(1)
$exeName = "${inputExe}"
if ($exeName -eq "") {
$exeName = "${repoName}"
}
if ($inputVersion) {
$version = "${inputVersion}"
}
if ([Environment]::Is64BitProcess) {
$arch = "amd64"
} else {
$arch = "386"
}
$BinDir = "$Home\bin"
$downloadedTagGz = "$BinDir\${exeName}.tar.gz"
$downloadedExe = "$BinDir\${exeName}.exe"
$Target = "windows_$arch"
# GitHub requires TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ResourceUri = if (!$version) {
"${githubUrl}/${owner}/${repoName}/releases/latest/download/${exeName}_${Target}.tar.gz"
} else {
"${githubUrl}/${owner}/${repoName}/releases/download/${Version}/${exeName}_${Target}.tar.gz"
}
if (!(Test-Path $BinDir)) {
New-Item $BinDir -ItemType Directory | Out-Null
}
Invoke-WebRequest $ResourceUri -OutFile $downloadedTagGz -UseBasicParsing -ErrorAction Stop
function Check-Command {
param($Command)
$found = $false
try
{
$Command | Out-Null
$found = $true
}
catch [System.Management.Automation.CommandNotFoundException]
{
$found = $false
}
$found
}
if (Check-Command -Command tar) {
Invoke-Expression "tar -xvzf $downloadedTagGz -C $BinDir"
} else {
function Expand-Tar($tarFile, $dest) {
if (-not (Get-Command Expand-7Zip -ErrorAction Ignore)) {
Install-Package -Scope CurrentUser -Force 7Zip4PowerShell > $null
}
Expand-7Zip $tarFile $dest
}
Expand-Tar $downloadedTagGz $BinDir
}
Remove-Item $downloadedTagGz
$User = [EnvironmentVariableTarget]::User
$Path = [Environment]::GetEnvironmentVariable('Path', $User)
if (!(";$Path;".ToLower() -like "*;$BinDir;*".ToLower())) {
[Environment]::SetEnvironmentVariable('Path', "$Path;$BinDir", $User)
$Env:Path += ";$BinDir"
}
Write-Output "${exeName} was installed successfully to $downloadedExe"
Write-Output "Run '${exeName} --help' to get started"