Skip to content

Commit 0c57746

Browse files
authored
feat(install): change powershell installation script (#1636)
* feat(install): change powershell installation script * fix: declare variables before usage * fix: change order * refa: reorder variables
1 parent 22c3695 commit 0c57746

File tree

1 file changed

+61
-54
lines changed

1 file changed

+61
-54
lines changed

install.ps1

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
function Install-Cargo-Shuttle {
2-
$RepoUrl = "https://github.com/shuttle-hq/shuttle"
3-
4-
Write-Host @"
2+
Write-Host @"
53
_ _ _ _
64
___| |__ _ _| |_| |_| | ___
75
/ __| '_ \| | | | __| __| |/ _ \
@@ -15,57 +13,11 @@ Please file an issue if you encounter any problems!
1513
===================================================
1614
"@
1715

18-
if (Get-Command -CommandType Application -ErrorAction SilentlyContinue cargo-binstall.exe) {
19-
Write-Host "Installing cargo-shuttle using cargo binstall"
20-
cargo-binstall.exe cargo-shuttle --no-confirm
21-
if ($?) {
22-
Write-Host "Installed cargo-shuttle, try running ``cargo shuttle --help``" -ForegroundColor Green
23-
return
24-
}
25-
else {
26-
Write-Host "Could not install from release using cargo binstall, trying manual binary download" -ForegroundColor Red
27-
}
28-
}
29-
else {
30-
Write-Host "cargo binstall not found, trying manual binary download" -ForegroundColor Red
31-
}
16+
$Arch = [Environment]::GetEnvironmentVariable("PROCESSOR_ARCHITECTURE", [EnvironmentVariableTarget]::Machine)
17+
$TempDir = $Env:TEMP
3218

33-
$CargoHome = if ($null -ne $Env:CARGO_HOME) { $Env:CARGO_HOME } else { "$HOME\.cargo" }
34-
$TempDir = $Env:TEMP
35-
$Arch = [Environment]::GetEnvironmentVariable("PROCESSOR_ARCHITECTURE", [EnvironmentVariableTarget]::Machine)
36-
if (($Arch -eq "AMD64") -and (Get-Command -CommandType Application -ErrorAction SilentlyContinue tar.exe)) {
37-
(Invoke-WebRequest "$RepoUrl/releases/latest" -Headers @{ "Accept" = "application/json" }).Content -match '"tag_name":"([^"]*)"' | Out-Null
38-
$LatestRelease = $Matches.1
39-
$BinaryUrl = "$RepoUrl/releases/download/$LatestRelease/cargo-shuttle-$LatestRelease-x86_64-pc-windows-msvc.tar.gz"
40-
Invoke-WebRequest $BinaryUrl -OutFile "$TempDir\cargo-shuttle.tar.gz"
41-
New-Item -ItemType Directory -Force "$TempDir\cargo-shuttle"
42-
tar.exe -xzf "$TempDir\cargo-shuttle.tar.gz" -C "$TempDir\cargo-shuttle"
43-
Move-Item -Force "$TempDir\cargo-shuttle\cargo-shuttle-x86_64-pc-windows-msvc-$LatestRelease\cargo-shuttle.exe" "$CargoHome\bin\cargo-shuttle.exe"
44-
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue "$TempDir\cargo-shuttle.tar.gz", "$TempDir\cargo-shuttle"
45-
Write-Host "Installed cargo-shuttle, try running ``cargo shuttle --help``" -ForegroundColor Green
46-
return
47-
}
48-
elseif ($Arch -ne "AMD64") {
49-
Write-Host "Unsupported Architecture: Binaries are not currently built for $Arch, skipping manual binary download" -ForegroundColor Red
50-
}
51-
else {
52-
Write-Host "Could not find tar.exe, skipping manual binary download (required to extract the release asset)" -ForegroundColor Red
53-
}
54-
55-
if (Get-Command -CommandType Application -ErrorAction SilentlyContinue cargo.exe) {
56-
Write-Host "Installing cargo-shuttle using cargo install (from source)"
57-
cargo.exe install cargo-shuttle --locked
58-
if ($?) {
59-
Write-Host "Installed cargo-shuttle, try running ``cargo shuttle --help``" -ForegroundColor Green
60-
return
61-
}
62-
else {
63-
Write-Host "Could not install cargo-shuttle using cargo" -ForegroundColor Red
64-
return
65-
}
66-
}
67-
else {
68-
if ($Arch -in "AMD64", "x86") {
19+
if (!(Get-Command -CommandType Application -ErrorAction SilentlyContinue cargo.exe)) {
20+
if ($Arch -in "AMD64", "x86") {
6921
Write-Host "Could not find cargo.exe, Rust may not be installed" -ForegroundColor Red
7022
$Confirm = Read-Host -Prompt "Would you like to install Rust via Rustup? [y/N]"
7123
if ($Confirm -inotin "y", "yes") {
@@ -92,9 +44,64 @@ Please file an issue if you encounter any problems!
9244
Write-Host "Please install Rust manually, more info at: https://rust-lang.github.io/rustup/installation/other.html" -ForegroundColor Red
9345
return
9446
}
95-
}
47+
}
48+
49+
if (Get-Command -CommandType Application -ErrorAction SilentlyContinue cargo-binstall.exe) {
50+
Write-Host "Installing cargo-shuttle using cargo binstall"
51+
cargo-binstall.exe cargo-shuttle --no-confirm
52+
if ($?) {
53+
Write-Host "Installed cargo-shuttle, try running ``cargo shuttle --help``" -ForegroundColor Green
54+
return
55+
}
56+
else {
57+
Write-Host "Could not install from release using cargo binstall, trying manual binary download" -ForegroundColor Red
58+
}
59+
}
60+
else {
61+
Write-Host "Cargo binstall not found, trying manual binary download" -ForegroundColor Red
62+
}
63+
64+
$RepoUrl = "https://github.com/shuttle-hq/shuttle"
65+
$CargoHome = if ($null -ne $Env:CARGO_HOME) { $Env:CARGO_HOME } else { "$HOME\.cargo" }
66+
67+
if (($Arch -eq "AMD64") -and (Get-Command -CommandType Application -ErrorAction SilentlyContinue tar.exe)) {
68+
(Invoke-WebRequest "$RepoUrl/releases/latest" -Headers @{ "Accept" = "application/json" }).Content -match '"tag_name":"([^"]*)"' | Out-Null
69+
$LatestRelease = $Matches.1
70+
$BinaryUrl = "$RepoUrl/releases/download/$LatestRelease/cargo-shuttle-$LatestRelease-x86_64-pc-windows-msvc.tar.gz"
71+
Invoke-WebRequest $BinaryUrl -OutFile "$TempDir\cargo-shuttle.tar.gz"
72+
New-Item -ItemType Directory -Force "$TempDir\cargo-shuttle"
73+
tar.exe -xzf "$TempDir\cargo-shuttle.tar.gz" -C "$TempDir\cargo-shuttle"
74+
Move-Item -Force "$TempDir\cargo-shuttle\cargo-shuttle-x86_64-pc-windows-msvc-$LatestRelease\cargo-shuttle.exe" "$CargoHome\bin\cargo-shuttle.exe"
75+
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue "$TempDir\cargo-shuttle.tar.gz", "$TempDir\cargo-shuttle"
76+
Write-Host "Installed cargo-shuttle, try running ``cargo shuttle --help``" -ForegroundColor Green
77+
return
78+
}
79+
elseif ($Arch -ne "AMD64") {
80+
Write-Host "Unsupported Architecture: Binaries are not currently built for $Arch, skipping manual binary download" -ForegroundColor Red
81+
}
82+
else {
83+
Write-Host "Could not find tar.exe, skipping manual binary download (required to extract the release asset)" -ForegroundColor Red
84+
}
85+
86+
87+
if (Get-Command -CommandType Application -ErrorAction SilentlyContinue cargo.exe) {
88+
Write-Host "Installing cargo-shuttle using cargo install (from source)"
89+
cargo.exe install cargo-shuttle --locked
90+
if ($?) {
91+
Write-Host "Installed cargo-shuttle, try running ``cargo shuttle --help``" -ForegroundColor Green
92+
return
93+
}
94+
else {
95+
Write-Host "Could not install cargo-shuttle using cargo" -ForegroundColor Red
96+
return
97+
}
98+
}
99+
else {
100+
Write-Host "Could not find cargo.exe, Rust may not be installed" -ForegroundColor Red
101+
}
96102
}
97103

104+
98105
$OldErrorAction = $ErrorActionPreference
99106
$ErrorActionPreference = "Stop"
100107
Install-Cargo-Shuttle

0 commit comments

Comments
 (0)