Skip to content

Commit

Permalink
Add windows installation script
Browse files Browse the repository at this point in the history
  • Loading branch information
Veetaha committed Sep 23, 2023
1 parent d150950 commit 121b7fb
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 2 deletions.
144 changes: 144 additions & 0 deletions scripts/release/install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# This script downloads the `cargo-marker` and the `marker_rustc_driver`
# binaries from the GitHub release assets. It also sets up the required
# Rust toolchain that the `marker_rustc_driver` depends on.
#
# This script must be self-contained! Nothing here should use anything from
# the marker repository, because users are expected to run this script on
# their machines where they don't have the marker repository cloned.

$ErrorActionPreference = "Stop"

# This script isn't meant to be run from `master`, but if it is, then
# it will install the latest version be it a stable version or a pre-release.
# region replace-version unstable
$version = "0.2.1"
# endregion replace-version unstable

$toolchain = "nightly-2023-08-24"

function With-Log {
Write-Output "> $($args -join ' ')"

$cmd = $args[0]
$rest = $args[1..$args.Length]

& $cmd @rest
}

function Extract-TarGz {
param (
[string]$bin,
[string]$dest
)
$file_stem = "${bin}-${host_triple}"


With-Log cd $temp_dir

Check-Sha256Sum $file_stem "tar.gz"

With-Log tar$exe --extract --file (Join-Path $temp_dir "$file_stem.tar.gz") --directory $dest
}

# There isn't mktemp on Windows, so we have to reinvent the wheel.
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}

# There isn't sha256sum on Windows, so we have to reinvent the wheel.
function Check-Sha256Sum {
param (
[string]$file_stem,
[string]$extension
)

$file = "$file_stem.$extension"

$actual = Get-FileHash -Algorithm SHA256 -Path $file
$expected = Get-Content "$file_stem.sha256"

foreach ($line in $expected) {
$line -match '(\S+)\s*\*?(.*)'
$expected_hash = $Matches[1]
$expected_file = $Matches[2]

if ($expected_file -ne $file) {
continue
}

if ($actual.Hash -eq $expected_hash) {
Write-Output "${file}: OK"
return
}

throw "Checksum verification failed for $file. Expected: $expected_hash, actual: $actual"
}

throw "No checksum found for $file"
}

# Set this to `.exe` if on Windows, otherwise leave it empty.
# Yes! This script can run on unix too if you have PowerShell installed there.
#
# Btw. this is written specically so that it can run on PowerShell 5, which is
# installed by default on Windows 10 and 11. In PowerShell 7 there are automatic
# variables `$IsWindows`, `$IsMacOS`, `$IsLinux`.
$exe = if ([System.Environment]::OSVersion.Platform -eq "Win32NT") {
".exe"
} else {
""
}

With-Log rustup install --profile minimal --no-self-update $toolchain

$host_triple = (
rustc +$toolchain --version --verbose `
| Select-String -Pattern "host: (.*)" `
| ForEach-Object { $_.Matches.Groups[1].Value }
)

$current_dir = (Get-Location).Path
$temp_dir = New-TemporaryDirectory

try {
$files = "{cargo-marker,marker_rustc_driver}-$host_triple.{tar.gz,sha256}"

# Curl is available by default on windows, yay!
# https://curl.se/windows/microsoft.html
#
# Download all files using a single TCP connection with HTTP2 multiplexing
With-Log curl$exe `
--location `
--silent `
--fail `
--show-error `
--retry 5 `
--retry-connrefused `
--remote-name `
--output-dir $temp_dir `
"https://github.com/rust-marker/marker/releases/download/v$version/$files"

# There is a null coalescing operator in PowerShell 7, but that version
# seems to be rather too cutting edge. At least on my Windows 11 laptop
# the version of PowerShell is 5.1
$cargo_home = if ($env:CARGO_HOME) {
$env:CARGO_HOME
} else {
Join-Path $HOME ".cargo"
}

Extract-TarGz "cargo-marker" (Join-Path $cargo_home "bin")

$sysroot = (rustc +$toolchain --print sysroot)
Extract-TarGz "marker_rustc_driver" (Join-Path $sysroot "bin")
} finally {
Write-Output "Removing the temp directory $temp_dir"

# Go back to the original directory before removing the temp directory
# otherwise it will fail because the temporary directory is in use.
cd $current_dir

Remove-Item -Force -Recurse $temp_dir
}
2 changes: 1 addition & 1 deletion scripts/release/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ function extract_archive {
with_log tar --extract --file "$temp_dir/$file_stem.tar.gz" --directory "$dest"
}

extract_archive cargo-marker "${CARGO_HOME-$HOME/.cargo/bin}"
extract_archive cargo-marker "${CARGO_HOME-$HOME/.cargo}/bin"

extract_archive marker_rustc_driver "$(rustc +$toolchain --print sysroot)/bin"
1 change: 1 addition & 0 deletions scripts/release/set-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ files=($(\
-o -name "*.md" \
-o -name "*.toml" \
-o -name "*.sh" \
-o -name "*.ps1" \
\)\
))

Expand Down
6 changes: 6 additions & 0 deletions scripts/release/snapshot.diff
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@
+marker_lints = "0.1.0"
```

=== scripts/release/install.ps1 ===
# region replace-version unstable
-$version = "<version>"
+$version = "0.1.0"
# endregion replace-version unstable

=== scripts/release/install.sh ===
# region replace-version unstable
-version=<version>
Expand Down
3 changes: 2 additions & 1 deletion scripts/update-toolchain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ sed -i "s/nightly-2023-08-24/$1/g" \
./scripts/update-toolchain.sh \
./cargo-marker/src/backend/driver.rs \
./cargo-marker/README.md \
./install.sh
./scripts/release/install.sh \
./scripts/release/install.ps1

0 comments on commit 121b7fb

Please sign in to comment.