This repository was archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrustup.ps1
More file actions
125 lines (97 loc) · 4.56 KB
/
rustup.ps1
File metadata and controls
125 lines (97 loc) · 4.56 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
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.
function Expand-ZIPFile($file, $destination) {
$shell = new-object -com shell.application
$zip = $shell.namespace($file)
if (-Not (Test-Path "$destination")) {
New-Item $destination -ItemType Directory -Force
}
$dst = $shell.namespace($destination)
$dst.Copyhere($zip.items())
}
function Acquire-7z() {
$7z_path = ""
# Check for user installation, then machine installation.
if (Test-Path -Path HKCU:\Software\7-Zip) {
$7z_path = (Get-ItemProperty -Path HKCU:\Software\7-Zip).Path
} elseif (Test-Path -Path HKLM:\Software\7-Zip) {
$7z_path = (Get-ItemProperty -Path HKLM:\Software\7-Zip).Path
}
# Ensure the path from the registry is valid.
if ($7z_path -and (Test-Path (Join-Path $7z_path "7z.exe"))) {
$7z_path = Join-Path $7z_path "7z.exe"
Write-Host "Using local 7-Zip at $7z_path"
} else {
$7z_url = "http://downloads.sourceforge.net/project/sevenzip/7-Zip/9.20/7za920.zip"
$7z_tmp = "$TMP_DIR\7za920.zip"
$7z_ex = "$TMP_DIR\7za.exe"
# Did we already download and extract it?
if (!(Test-Path $7z_ex)) {
# Download 7zip
Start-BitsTransfer $7z_url $7z_tmp -DisplayName "Downloading 7-Zip" -Description $7z_url
Expand-ZIPFile -File $7z_tmp -Destination "$TMP_DIR"
}
$7z_path = $7z_ex
Write-Host "Using downloaded 7-Zip at $7z_path"
}
$7z_path
}
function which($name) {
Get-Command $name | Select-Object -ExpandProperty Definition
}
Import-Module BitsTransfer
$TMP_DIR = "$env:temp\rustup-tmp-install"
New-Item $TMP_DIR -ItemType Directory -Force | Out-Null
Set-Location $TMP_DIR
# Detect 32 or 64 bit
switch ([IntPtr]::Size) {
4 {
$arch = 32
$rust_dl = "https://static.rust-lang.org/dist/rust-nightly-i686-pc-windows-gnu.exe"
$cargo_dl = "https://static.rust-lang.org/cargo-dist/cargo-nightly-i686-pc-windows-gnu.tar.gz"
}
8 {
$arch = 64
$rust_dl = "https://static.rust-lang.org/dist/rust-nightly-x86_64-pc-windows-gnu.exe"
$cargo_dl = "https://static.rust-lang.org/cargo-dist/cargo-nightly-x86_64-pc-windows-gnu.tar.gz"
}
default { echo "ERROR: The processor architecture could not be determined."; exit 1 }
}
$7z = Acquire-7z # Check/Download 7-Zip
# Download the latest rust and cargo binaries
$rust_installer = "$TMP_DIR\rust_install.exe"
$cargo_binary = "$TMP_DIR\cargo_install.tar.gz"
Start-BitsTransfer $rust_dl $rust_installer -DisplayName "Downloading the lastest Rust nightly - this may take a while" -Description $rust_dl
Start-BitsTransfer $cargo_dl $cargo_binary -DisplayName "Downloading the latest Cargo nightly - this may take a while" -Description $cargo_dl
echo "Downloads complete."
# Install the rust binaries
Start-Process $rust_installer -Wait
# Refresh path for this process after installation
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "User")
$env:Path += ";" + [System.Environment]::GetEnvironmentVariable("Path", "Machine")
# Looking for the dir which has rustc in it, which may fail if the user doesn't add rust\bin to
# their path or for multiple rust versions
$rust_bin = which "rustc.exe" | Split-Path
rustc -v
echo "Rust is Ready!"
# Extract the Cargo binary with 7-Zip
Start-Process $7z -ArgumentList "e $cargo_binary -y" -NoNewWindow -Wait
Start-Process $7z -ArgumentList "e .\cargo_install.tar *.exe -r -y" -NoNewWindow -Wait
try {
# Attempt to copy Cargo next to rustc. If this fails, it's because the user installed Rust to a privileged path.
Copy-Item "$TMP_DIR\cargo.exe" $rust_bin -ErrorAction Stop
} catch {
# Unprivileged copy failed, so do a privileged copy instead. This will perform a UAC prompt.
# This is unfortunately the cleanest way of achieving this.
# Gotcha #1: '-Verb RunAs' and '-NoNewWindow' are incompatible, so instead we use '-WindowStyle Hidden'
$proc = Start-Process powershell -ArgumentList "-Command &{Copy-Item \`"$TMP_DIR\cargo.exe\`" \`"$rust_bin\`"}" -Verb RunAs -WindowStyle Hidden -PassThru
# Gotcha #2: '-Verb RunAs' is ALSO incompatible with "-Wait"... yuck.
# Spin for a bit so we don't try to cargo -V until it has actually been copied.
do { Start-Sleep -m 50 } until ($proc.HasExited)
}
cargo -V
echo "Cargo is Ready!"
cmd /c pause