Skip to content

Commit f0de6c4

Browse files
committed
Add basic windows install script.
Adds a powershell install script for windows users that are at least running powershell 5. It is almost a direct port of install.sh. This would be used to automatically download the script much like curl | bash, but with: `Invoke-WebRequest https://urltoscript.com | Select-Object -ExpandProperty Content | Out-File $env:TMP/install_rustlings.ps1; Unblock-File $env:TMP/install_rustlings.ps1; Invoke-Expression $env:TMP/install_rustlings.ps1`
1 parent e6161a6 commit f0de6c4

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

install.ps1

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env pwsh
2+
3+
#Requires -Version 5
4+
param($path = "$pwd/rustlings")
5+
6+
if((Get-ExecutionPolicy) -gt 'RemoteSigned' -or (Get-ExecutionPolicy) -eq 'ByPass') {
7+
Write-Output "PowerShell requires an execution policy of 'RemoteSigned' to run this script."
8+
Write-Output "To make this change please run:"
9+
Write-Output "'Set-ExecutionPolicy RemoteSigned -scope CurrentUser'"
10+
exit 1
11+
}
12+
13+
Write-Host "Let's get you set up with Rustlings!"
14+
15+
Write-Host "Checking requirements..."
16+
if (Get-Command git -ErrorAction SilentlyContinue) {
17+
Write-Host "SUCCESS: Git is installed"
18+
} else {
19+
Write-Host "WARNING: Git does not seem to be installed."
20+
Write-Host "Please download Git using your package manager or over https://git-scm.com/!"
21+
exit 1
22+
}
23+
24+
if (Get-Command rustc -ErrorAction SilentlyContinue) {
25+
Write-Host "SUCCESS: Rust is installed"
26+
} else {
27+
Write-Host "WARNING: Rust does not seem to be installed."
28+
Write-Host "Please download Rust using https://rustup.rs!"
29+
exit 1
30+
}
31+
32+
if (Get-Command cargo -ErrorAction SilentlyContinue) {
33+
Write-Host "SUCCESS: Cargo is installed"
34+
} else {
35+
Write-Host "WARNING: Cargo does not seem to be installed."
36+
Write-Host "Please download Rust and Cargo using https://rustup.rs!"
37+
exit 1
38+
}
39+
40+
# Function that compares two versions strings v1 and v2 given in arguments (e.g 1.31 and 1.33.0).
41+
# Returns 1 if v1 > v2, 0 if v1 == v2, 2 if v1 < v2.
42+
function vercomp($v1, $v2) {
43+
if ($v1 -eq $v2) {
44+
return 0
45+
}
46+
47+
$v1 = $v1.Replace(".", "0")
48+
$v2 = $v2.Replace(".", "0")
49+
if ($v1.Length -gt $v2.Length) {
50+
$v2 = $v2.PadRight($v1.Length, "0")
51+
} else {
52+
$v1 = $v1.PadRight($v2.Length, "0")
53+
}
54+
55+
if ($v1 -gt $v2) {
56+
return 1
57+
} else {
58+
return 2
59+
}
60+
}
61+
62+
$rustVersion = $(rustc --version).Split(" ")[1]
63+
$minRustVersion = "1.31"
64+
if ((vercomp $rustVersion $minRustVersion) -eq 2) {
65+
Write-Host "WARNING: Rust version is too old: $rustVersion - needs at least $minRustVersion"
66+
Write-Host "Please update Rust with 'rustup update'"
67+
exit 1
68+
} else {
69+
Write-Host "SUCCESS: Rust is up to date"
70+
}
71+
72+
Write-Host "Cloning Rustlings at $path"
73+
git clone -q https://github.com/rust-lang/rustlings $path
74+
if (!($LASTEXITCODE -eq 0)) {
75+
exit 1
76+
}
77+
78+
# UseBasicParsing is deprecated, pwsh 6 or above will automatically use it,
79+
# but anyone running pwsh 5 will have to pass the argument.
80+
$version = Invoke-WebRequest -UseBasicParsing https://api.github.com/repos/rust-lang/rustlings/releases/latest `
81+
| ConvertFrom-Json | Select-Object -ExpandProperty tag_name
82+
Write-Host "Checking out version $version..."
83+
Set-Location $path
84+
git checkout -q tags/$version
85+
86+
Write-Host "Installing the 'rustlings' executable..."
87+
cargo install --force --path .
88+
if (!(Get-Command rustlings -ErrorAction SilentlyContinue)) {
89+
Write-Host "WARNING: Please check that you have '~/.cargo/bin' in your PATH environment variable!"
90+
}
91+
92+
Write-Host "All done! Run 'rustlings' to get started."

0 commit comments

Comments
 (0)