1# Checks if cargo is in the user's path or in default install path
 2# If not, download with rustup-installer (which respects CARGO_HOME / RUSTUP_HOME)
 3
 4# Like 'set -e' in bash
 5$ErrorActionPreference = "Stop"
 6
 7$cargoHome = if ($env:CARGO_HOME) { $env:CARGO_HOME } else { "$env:USERPROFILE\.cargo" }
 8$rustupPath = "$cargoHome\bin\rustup.exe"
 9$cargoPath = "$cargoHome\bin\cargo.exe"
10
11# Check if cargo is already available in path
12if (Get-Command cargo -ErrorAction SilentlyContinue)
13{
14    cargo --version
15    exit
16}
17# Check if rustup and cargo are available in CARGO_HOME
18elseif (-not ((Test-Path $rustupPath) -and (Test-Path $cargoPath))) {
19    Write-Output "Rustup or Cargo not found in $cargoHome, installing..."
20
21    $tempDir = [System.IO.Path]::GetTempPath()
22
23    # Download and install rustup
24    $RustupInitPath = "$tempDir\rustup-init.exe"
25    Write-Output "Downloading rustup installer..."
26    Invoke-WebRequest `
27        -OutFile $RustupInitPath `
28        -Uri https://static.rust-lang.org/rustup/dist/i686-pc-windows-gnu/rustup-init.exe
29
30    Write-Output "Installing rustup..."
31    & $RustupInitPath -y --default-toolchain none
32    Remove-Item -Force $RustupInitPath
33
34    Write-Output "Rust installation complete."
35    # This is necessary
36}
37
38& $rustupPath --version
39& $cargoPath --version