forked from onflow/flow-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ps1
85 lines (68 loc) · 2.83 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
<#
.Synopsis
Install the Flow CLI on Windows.
.DESCRIPTION
By default, the latest release will be installed.
If '-Version' is specified, then the given version is installed.
.Parameter Directory
The destination path to install to.
.Parameter Version
The version to install.
.Parameter AddToPath
Add the absolute destination path to the 'User' scope environment variable 'Path'.
.Parameter GitHubToken
Optional GitHub token to use to prevent rate limiting.
.EXAMPLE
Install the current version
.\install.ps1
.EXAMPLE
Install version v0.5.2
.\install.ps1 -Version v0.5.2
.EXAMPLE
Invoke-Expression "& { $(Invoke-RestMethod 'https://storage.googleapis.com/flow-cli/install.ps1') }"
#>
param (
[string] $version="",
[string] $directory = "$env:APPDATA\Flow",
[bool] $addToPath = $true,
[string] $githubToken = ""
)
Set-StrictMode -Version 3.0
# Enable support for ANSI escape sequences
Set-ItemProperty HKCU:\Console VirtualTerminalLevel -Type DWORD 1
$ErrorActionPreference = "Stop"
$repo = "onflow/flow-cli"
$versionURL = "https://api.github.com/repos/$repo/releases/latest"
$assetsURL = "https://github.com/$repo/releases/download"
# Add the GitHub token to the web request headers if it was provided
$webRequestOptions = if ($githubToken) {
@{ 'Headers' = @{ 'Authorization' = "Bearer $githubToken" } }
} else {
@{}
}
if (!$version) {
$q = (Invoke-WebRequest -Uri "$versionURL" -UseBasicParsing @webRequestOptions) | ConvertFrom-Json
$version = $q.tag_name
}
Write-Output("Installing version {0} ..." -f $version)
New-Item -ItemType Directory -Force -Path $directory | Out-Null
$progressPreference = 'silentlyContinue'
Invoke-WebRequest -Uri "$assetsURL/$version/flow-cli-$version-windows-amd64.zip" -UseBasicParsing -OutFile "$directory\flow.zip" @webRequestOptions
Expand-Archive -Path "$directory\flow.zip" -DestinationPath "$directory" -Force
try {
Stop-Process -Name flow -Force
Start-Sleep -Seconds 1
}
catch {}
Move-Item -Path "$directory\flow-cli.exe" -Destination "$directory\flow.exe" -Force
# Check if the directory is already in the PATH
$existingPaths = [Environment]::GetEnvironmentVariable("PATH", [System.EnvironmentVariableTarget]::User).Split(';')
if ($addToPath -and $existingPaths -notcontains $directory) {
Write-Output "Adding to PATH ..."
$processPath = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::Process) + ";$directory"
$userPath = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::User) + ";$directory"
[System.Environment]::SetEnvironmentVariable("PATH", $processPath, [System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable("PATH", $userPath, [System.EnvironmentVariableTarget]::User)
}
Write-Output "Done."
Start-Sleep -Seconds 1