forked from alibaba/open-code-review
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
142 lines (126 loc) · 4.98 KB
/
Copy pathinstall.ps1
File metadata and controls
142 lines (126 loc) · 4.98 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
138
139
140
141
142
# Install the ocr (Open Code Review) CLI from GitHub releases on Windows.
# irm https://raw.githubusercontent.com/alibaba/open-code-review/main/install.ps1 | iex
# Prefer to inspect first:
# irm https://raw.githubusercontent.com/alibaba/open-code-review/main/install.ps1 -OutFile install.ps1
# notepad install.ps1 # review, then: .\install.ps1
# Env: OCR_INSTALL_DIR (default $env:LOCALAPPDATA\Programs\ocr), OCR_VERSION (default latest).
# Requires PowerShell 5.1+ or PowerShell 7+.
$ErrorActionPreference = 'Stop'
function Err([string]$Message) {
[Console]::Error.WriteLine("error: $Message")
exit 1
}
function Get-OcrArch {
$arch = $env:PROCESSOR_ARCHITECTURE
if ([string]::IsNullOrEmpty($arch)) {
Err 'unable to detect architecture (PROCESSOR_ARCHITECTURE is empty); please set it manually'
}
switch -Regex ($arch) {
'^(AMD64|X64|x86_64)$' { return 'amd64' }
'^(ARM64|aarch64)$' { return 'arm64' }
default { Err "unsupported architecture: $arch (only amd64 and arm64 are supported)" }
}
}
function Resolve-OcrVersion([string]$Repo) {
$version = $env:OCR_VERSION
if (-not [string]::IsNullOrWhiteSpace($version)) {
return $version.Trim()
}
try {
$release = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repo/releases/latest" -UseBasicParsing
} catch {
Err "failed to fetch latest release info from github api"
}
if (-not $release.tag_name) {
Err 'could not resolve latest release tag'
}
return [string]$release.tag_name
}
function Get-ChecksumFromFile([string]$ChecksumFile, [string]$AssetName) {
foreach ($line in Get-Content -LiteralPath $ChecksumFile) {
if ([string]::IsNullOrWhiteSpace($line)) { continue }
$parts = $line.Trim() -split '\s+', 2
if ($parts.Count -eq 2 -and $parts[1] -eq $AssetName) {
return $parts[0].ToLowerInvariant()
}
}
return $null
}
function Install-OcrBinary([string]$Source, [string]$InstallDir, [string]$BinName) {
try {
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
$dest = Join-Path $InstallDir $BinName
Copy-Item -LiteralPath $Source -Destination $dest -Force
} catch {
Err "$InstallDir is not writable; set OCR_INSTALL_DIR to a writable path"
}
}
function Show-PostInstallPathNotice([string]$BinName, [string]$InstallDir) {
$pathEntries = $env:PATH -split ';' | ForEach-Object { $_.TrimEnd('\') }
$normalizedInstall = $InstallDir.TrimEnd('\')
$onPath = $false
foreach ($entry in $pathEntries) {
if ($entry -and [string]::Equals($entry, $normalizedInstall, [System.StringComparison]::OrdinalIgnoreCase)) {
$onPath = $true
break
}
}
if (-not $onPath) {
Write-Host "note: $InstallDir is not on your PATH; add it or run $InstallDir\$BinName directly"
return
}
if (-not (Get-Command $BinName -ErrorAction SilentlyContinue)) {
Write-Host "note: open a new shell so $BinName resolves on PATH"
}
}
# Ensure TLS 1.2 for Windows PowerShell 5.1 (Invoke-WebRequest / Invoke-RestMethod).
try {
[Net.ServicePointManager]::SecurityProtocol = `
[Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
} catch {
# Ignore if the runtime already negotiates modern TLS.
}
$Repo = 'alibaba/open-code-review'
$Bin = 'ocr.exe'
$AssetPrefix = 'opencodereview'
$DefaultInstallDir = Join-Path $env:LOCALAPPDATA 'Programs\ocr'
$InstallDir = if (-not [string]::IsNullOrWhiteSpace($env:OCR_INSTALL_DIR)) {
$env:OCR_INSTALL_DIR.Trim()
} else {
$DefaultInstallDir
}
$arch = Get-OcrArch
$os = 'windows'
$Version = Resolve-OcrVersion $Repo
$asset = "$AssetPrefix-$os-$arch.exe"
$base = "https://github.com/$Repo/releases/download/$Version"
$tmp = Join-Path ([System.IO.Path]::GetTempPath()) ("ocr-install-" + [guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Force -Path $tmp | Out-Null
try {
$assetPath = Join-Path $tmp $asset
$sumPath = Join-Path $tmp 'sha256sum.txt'
Write-Host "downloading $Bin $Version ($os/$arch)..."
try {
Invoke-WebRequest -Uri "$base/$asset" -OutFile $assetPath -UseBasicParsing
} catch {
Err "download failed: $base/$asset"
}
try {
Invoke-WebRequest -Uri "$base/sha256sum.txt" -OutFile $sumPath -UseBasicParsing
} catch {
Err 'sha256sum.txt download failed'
}
$want = Get-ChecksumFromFile $sumPath $asset
if ([string]::IsNullOrEmpty($want)) {
Err "no checksum entry for $asset in sha256sum.txt"
}
$got = (Get-FileHash -LiteralPath $assetPath -Algorithm SHA256).Hash.ToLowerInvariant()
if ($got -ne $want) {
Err "checksum mismatch for $asset (got $got, want $want)"
}
Install-OcrBinary $assetPath $InstallDir $Bin
Write-Host "installed $Bin $Version -> $InstallDir\$Bin"
Show-PostInstallPathNotice $Bin $InstallDir
} finally {
Remove-Item -LiteralPath $tmp -Recurse -Force -ErrorAction SilentlyContinue
}