-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
102 lines (90 loc) · 3.74 KB
/
Copy pathinstall.ps1
File metadata and controls
102 lines (90 loc) · 3.74 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
<#
.SYNOPSIS
Install the latest NFI Backtest Engine release on 64-bit Windows.
.DESCRIPTION
The installer selects the Windows wheel from the latest GitHub release, verifies the
SHA-256 digest published by GitHub, and installs the CLI into an isolated uv tool
environment. If uv is missing, its official standalone installer is used first.
#>
[CmdletBinding()]
param(
[string]$Version = "latest",
[switch]$DryRun
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$Repository = "vntrevx/NFI_BackTestEngine"
$Headers = @{
Accept = "application/vnd.github+json"
"User-Agent" = "nfi-backtest-engine-installer"
"X-GitHub-Api-Version" = "2022-11-28"
}
$GitHubToken = [Environment]::GetEnvironmentVariable("GITHUB_TOKEN")
if ([string]::IsNullOrWhiteSpace($GitHubToken)) {
$GitHubToken = [Environment]::GetEnvironmentVariable("GH_TOKEN")
}
if (-not [string]::IsNullOrWhiteSpace($GitHubToken)) {
$Headers.Authorization = "Bearer $GitHubToken"
}
if (-not [Environment]::Is64BitOperatingSystem) {
throw "NFI Backtest Engine currently requires 64-bit Windows."
}
$ReleaseEndpoint = if ($Version -eq "latest") {
"https://api.github.com/repos/$Repository/releases/latest"
} else {
$EncodedVersion = [Uri]::EscapeDataString($Version)
"https://api.github.com/repos/$Repository/releases/tags/$EncodedVersion"
}
$Release = Invoke-RestMethod -Uri $ReleaseEndpoint -Headers $Headers
$Assets = @($Release.assets | Where-Object { $_.name -like "*-win_amd64.whl" })
if ($Assets.Count -ne 1) {
throw "Expected one Windows x64 wheel in release $($Release.tag_name); found $($Assets.Count)."
}
$Asset = $Assets[0]
if (-not $Asset.digest -or -not $Asset.digest.StartsWith("sha256:")) {
throw "Release asset $($Asset.name) does not have a published SHA-256 digest."
}
if ($DryRun) {
Write-Output "release=$($Release.tag_name)"
Write-Output "asset=$($Asset.name)"
Write-Output "digest=$($Asset.digest)"
return
}
$UvCommand = Get-Command uv -ErrorAction SilentlyContinue
if ($null -eq $UvCommand) {
Write-Host "uv was not found; installing it from the official Astral installer..."
$UvInstaller = Invoke-RestMethod -Uri "https://astral.sh/uv/install.ps1"
& ([scriptblock]::Create($UvInstaller))
$UvCandidate = Join-Path $HOME ".local\bin\uv.exe"
if (-not (Test-Path -LiteralPath $UvCandidate)) {
throw "uv installation completed but uv.exe was not found at $UvCandidate."
}
$UvPath = $UvCandidate
} else {
$UvPath = $UvCommand.Source
}
$TemporaryDirectory = Join-Path ([IO.Path]::GetTempPath()) (
"nfi-bte-install-" + [Guid]::NewGuid().ToString("N")
)
New-Item -ItemType Directory -Path $TemporaryDirectory | Out-Null
try {
$WheelPath = Join-Path $TemporaryDirectory $Asset.name
Write-Host "Downloading $($Asset.name)..."
Invoke-WebRequest -Uri $Asset.browser_download_url -OutFile $WheelPath -Headers $Headers
$ExpectedDigest = $Asset.digest.Substring("sha256:".Length).ToLowerInvariant()
$ActualDigest = (Get-FileHash -LiteralPath $WheelPath -Algorithm SHA256).Hash.ToLowerInvariant()
if ($ActualDigest -ne $ExpectedDigest) {
throw "Downloaded wheel SHA-256 differs from the GitHub release digest."
}
& $UvPath tool install --force --python 3.12 $WheelPath
if ($LASTEXITCODE -ne 0) {
throw "uv tool install failed with exit code $LASTEXITCODE."
}
# Updating the shell path is idempotent. uv prints whether a terminal restart is
# required, so the installer does not guess which shell the user will open next.
& $UvPath tool update-shell
Write-Host "Installed NFI Backtest Engine $($Release.tag_name)."
Write-Host "Run: nfi-bte --version"
} finally {
Remove-Item -LiteralPath $TemporaryDirectory -Recurse -Force -ErrorAction SilentlyContinue
}