-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
137 lines (117 loc) · 4.11 KB
/
Copy pathinstall.ps1
File metadata and controls
137 lines (117 loc) · 4.11 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
param(
[string]$Version = "latest",
[string]$Prefix = "",
[string]$BinDir = "",
[string]$VimDir = "",
[switch]$NoVim,
[switch]$VimPack,
[string]$Repo = $env:GREF_REPO
)
$ErrorActionPreference = "Stop"
if ([string]::IsNullOrWhiteSpace($Repo)) {
$Repo = "albertize/gref"
}
function Fail($Message) {
Write-Error "gref install: $Message"
exit 1
}
function Get-AssetName {
switch ($env:PROCESSOR_ARCHITECTURE) {
"AMD64" { $arch = "amd64" }
"ARM64" { $arch = "arm64" }
default { Fail "unsupported architecture: $env:PROCESSOR_ARCHITECTURE" }
}
"gref-windows-$arch.zip"
}
function Get-BinDir {
if ($BinDir) {
return $BinDir.TrimEnd([char]'\')
}
if ($Prefix) {
return (Join-Path $Prefix "bin")
}
Join-Path $HOME "bin"
}
function Get-VimDir {
if ($VimDir) {
return $VimDir.TrimEnd([char]'\')
}
if ($Prefix) {
return (Join-Path $Prefix "vimfiles")
}
if ($VimPack) {
return (Join-Path $HOME "vimfiles\pack\gref\start\gref")
}
Join-Path $HOME "vimfiles"
}
function Install-File($Source, $Destination) {
New-Item -ItemType Directory -Force -Path (Split-Path -Parent $Destination) | Out-Null
Copy-Item -Force $Source $Destination
}
function Install-Package($PackageDir) {
$bin = Join-Path $PackageDir "bin\gref.exe"
if (!(Test-Path $bin)) {
Fail "package is missing bin\gref.exe"
}
$targetBinDir = Get-BinDir
$targetBin = Join-Path $targetBinDir "gref.exe"
Install-File $bin $targetBin
Write-Host "installed gref to $targetBin"
if (!$NoVim) {
$plugin = Join-Path $PackageDir "vim\plugin\gref.vim"
$autoload = Join-Path $PackageDir "vim\autoload\gref.vim"
if (!(Test-Path $plugin)) {
Fail "package is missing vim\plugin\gref.vim"
}
if (!(Test-Path $autoload)) {
Fail "package is missing vim\autoload\gref.vim"
}
$targetVimDir = Get-VimDir
Install-File $plugin (Join-Path $targetVimDir "plugin\gref.vim")
Install-File $autoload (Join-Path $targetVimDir "autoload\gref.vim")
Write-Host "installed Vim runtime to $targetVimDir"
}
$pathEntries = ($env:PATH -split ";") | ForEach-Object { $_.TrimEnd([char]'\') }
if ($pathEntries -notcontains $targetBinDir.TrimEnd([char]'\')) {
Write-Host "note: $targetBinDir is not in PATH"
}
}
$scriptDir = if ($PSScriptRoot) { $PSScriptRoot } else { (Get-Location).Path }
if (Test-Path (Join-Path $scriptDir "bin\gref.exe")) {
Install-Package $scriptDir
exit 0
}
$asset = Get-AssetName
$tmp = Join-Path ([System.IO.Path]::GetTempPath()) ("gref-install-" + [System.Guid]::NewGuid())
New-Item -ItemType Directory -Path $tmp | Out-Null
try {
if ($Version -eq "latest") {
$baseUrl = "https://github.com/$Repo/releases/latest/download"
} else {
$baseUrl = "https://github.com/$Repo/releases/download/$Version"
}
$assetPath = Join-Path $tmp $asset
$sumsPath = Join-Path $tmp "SHA256SUMS"
Write-Host "downloading $asset from $Repo ($Version)"
Invoke-WebRequest -UseBasicParsing "$baseUrl/$asset" -OutFile $assetPath
Invoke-WebRequest -UseBasicParsing "$baseUrl/SHA256SUMS" -OutFile $sumsPath
$assetPattern = '(\s|\*)' + [regex]::Escape($asset) + '$'
$checksumLine = Get-Content $sumsPath | Where-Object { $_ -match $assetPattern } | Select-Object -First 1
if (!$checksumLine) {
Fail "checksum for $asset not found"
}
$expected = ($checksumLine -split "\s+")[0].ToUpperInvariant()
$actual = (Get-FileHash -Algorithm SHA256 $assetPath).Hash.ToUpperInvariant()
if ($actual -ne $expected) {
Fail "checksum mismatch for $asset"
}
$extractDir = Join-Path $tmp "package"
Expand-Archive -Path $assetPath -DestinationPath $extractDir
$packageRoot = Get-ChildItem -Path $extractDir -Directory | Select-Object -First 1
if (!$packageRoot) {
Fail "archive did not contain a package directory"
}
Install-Package $packageRoot.FullName
} finally {
Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue
}