Skip to content

Commit

Permalink
Add preinstall-toolchains binary and run it in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Sep 8, 2024
1 parent b1a0c25 commit f3ecf4c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ jobs:
sudo rm -rf /usr/share/swift
# du -sh /usr/*/* 2>/dev/null | sort -h || true
- name: Preinstall toolchains
run: cargo run -p dylint_internal --bin preinstall-toolchains

- name: Test
run: |
if [[ '${{ matrix.package }}' =~ ^cargo-dylint ]]; then
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ repository = "https://github.com/trailofbits/dylint"

[dependencies]
anyhow = "1.0"
assert_cmd = "2.0"
regex = "1.10"

ansi_term = { version = "0.12", optional = true }
bitflags = { version = "2.6", optional = true }
Expand All @@ -22,7 +24,6 @@ if_chain = { version = "1.0", optional = true }
is-terminal = { version = "0.4", optional = true }
log = { version = "0.4", optional = true }
once_cell = { version = "1.19", optional = true }
regex = { version = "1.10", optional = true }
rust-embed = { version = "8.5", features = [
"include-exclude",
], optional = true }
Expand Down Expand Up @@ -53,7 +54,7 @@ examples = ["cargo", "cargo-util", "rustup", "walkdir"]
git = ["command", "git2", "if_chain"]
packaging = ["cargo", "rust-embed"]
rustup = ["command"]
sed = ["regex"]
sed = []
testing = ["ctor", "env_logger", "packaging"]

[lints]
Expand Down
62 changes: 62 additions & 0 deletions internal/src/bin/preinstall-toolchains.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use anyhow::{Context, Result};
use assert_cmd::output::OutputError;
use regex::Regex;
use std::{
fs::read_to_string,
io::{BufRead, BufReader},
process::{Child, Command, Stdio},
};

fn main() -> Result<()> {
let re = Regex::new(r"\<nightly-[0-9]{4}-[0-9]{2}-[0-9]{2}\>")?;

let mut ls_files_child = Command::new("git")
.arg("ls-files")
.stdout(Stdio::piped())
.spawn()
.with_context(|| "Could not spawn `git ls-files`")?;

let ls_files_stdout = ls_files_child.stdout.take().unwrap();
let mut toolchains = BufReader::new(ls_files_stdout).lines().try_fold(
Vec::new(),
|mut toolchains, result| -> Result<Vec<_>> {
let path = result.with_context(|| "Could not read from `git ls-files`")?;
if let Ok(contents) = read_to_string(&path) {
toolchains.extend(re.find_iter(&contents).map(|m| m.as_str().to_owned()));
} else {
eprintln!("Could not read `{path}`");
}
Ok(toolchains)
},
)?;

ensure_child_success(ls_files_child)?;

toolchains.sort();
toolchains.dedup();

let rustup_children = toolchains
.into_iter()
.map(|toolchain| -> Result<Child> {
Command::new("rustup")
.args(["toolchain", "install", &toolchain, "--no-self-update"])
.spawn()
.map_err(Into::into)
})
.collect::<Result<Vec<_>>>()
.with_context(|| "Could not spawn `rustup`")?;

for rustup_child in rustup_children {
ensure_child_success(rustup_child)?;
}

Ok(())
}

fn ensure_child_success(child: Child) -> Result<()> {
let output = child.wait_with_output()?;
if !output.status.success() {
eprintln!("child failed: {}", OutputError::new(output));
}
Ok(())
}

0 comments on commit f3ecf4c

Please sign in to comment.