Skip to content

Commit

Permalink
[fud2] Unique fud2 directories and version gating ninja --quiet (#2092
Browse files Browse the repository at this point in the history
)

* attempt to create a unique directory for each fud2 execution

* version checking for --quiet

* trigger CI again

* Use a stable directory name when `--keep`

* Remove an unnecessary `into`

Now that we are cloning the working directory anyway.

* Factor out ninja_supports_quiet

---------

Co-authored-by: Adrian Sampson <[email protected]>
  • Loading branch information
nathanielnrn and sampsyo committed Jun 5, 2024
1 parent 898bb56 commit b62942e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
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.

1 change: 1 addition & 0 deletions fud2/fud-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ env_logger.workspace = true
rhai = "1.18.0"
once_cell = "1.19.0"
ariadne = "0.4.1"
rand = "0.8.5"
17 changes: 11 additions & 6 deletions fud2/fud-core/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::exec::{Driver, Request, StateRef};
use crate::run::Run;
use anyhow::{anyhow, bail};
use argh::FromArgs;
use camino::{Utf8Path, Utf8PathBuf};
use camino::Utf8PathBuf;
use std::fmt::Display;
use std::str::FromStr;

Expand Down Expand Up @@ -164,10 +164,15 @@ fn to_state(driver: &Driver, args: &FakeArgs) -> anyhow::Result<StateRef> {

fn get_request(driver: &Driver, args: &FakeArgs) -> anyhow::Result<Request> {
// The default working directory (if not specified) depends on the mode.
let default_workdir = driver.default_workdir();
let workdir = args.dir.as_deref().unwrap_or_else(|| match args.mode {
Mode::Generate | Mode::Run => default_workdir.as_ref(),
_ => Utf8Path::new("."),
let workdir = args.dir.clone().unwrap_or_else(|| match args.mode {
Mode::Generate | Mode::Run => {
if args.keep.unwrap_or(false) {
driver.stable_workdir()
} else {
driver.fresh_workdir()
}
}
_ => ".".into(),
});

// Find all the operations to route through.
Expand All @@ -187,7 +192,7 @@ fn get_request(driver: &Driver, args: &FakeArgs) -> anyhow::Result<Request> {
end_file: args.output.clone(),
end_state: to_state(driver, args)?,
through: through?,
workdir: workdir.into(),
workdir,
})
}

Expand Down
19 changes: 17 additions & 2 deletions fud2/fud-core/src/exec/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{OpRef, Operation, Request, Setup, SetupRef, State, StateRef};
use crate::{config, run, script, utils};
use camino::{Utf8Path, Utf8PathBuf};
use cranelift_entity::{PrimaryMap, SecondaryMap};
use rand::distributions::{Alphanumeric, DistString};
use std::{collections::HashMap, error::Error, fmt::Display};

#[derive(PartialEq)]
Expand Down Expand Up @@ -195,11 +196,25 @@ impl Driver {
.map(|(op, _)| op)
}

/// The working directory to use when running a build.
pub fn default_workdir(&self) -> Utf8PathBuf {
/// The default working directory name when we want the same directory on every run.
pub fn stable_workdir(&self) -> Utf8PathBuf {
format!(".{}", &self.name).into()
}

/// A new working directory that does not yet exist on the filesystem, for when we
/// want to avoid collisions.
pub fn fresh_workdir(&self) -> Utf8PathBuf {
loop {
let rand_suffix =
Alphanumeric.sample_string(&mut rand::thread_rng(), 8);
let path: Utf8PathBuf =
format!(".{}-{}", &self.name, rand_suffix).into();
if !path.exists() {
return path;
}
}
}

/// Print a list of registered states and operations to stdout.
pub fn print_info(&self) {
println!("States:");
Expand Down
21 changes: 20 additions & 1 deletion fud2/fud-core/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ impl<'a> Run<'a> {
cmd.current_dir(&dir.path);

if !self.global_config.verbose {
cmd.arg("--quiet");
if ninja_supports_quiet(&self.global_config.ninja)? {
cmd.arg("--quiet");
}
} else {
cmd.arg("--verbose");
}
Expand Down Expand Up @@ -449,6 +451,23 @@ impl<W: Write> Emitter<W> {
}
}

/// Check whether a Ninja executable supports the `--quiet` flag.
fn ninja_supports_quiet(ninja: &str) -> std::io::Result<bool> {
let version_output = Command::new(ninja).arg("--version").output()?;
if let Ok(version) = String::from_utf8(version_output.stdout) {
let parts: Vec<&str> = version.split('.').collect();
if parts.len() >= 2 {
let major = parts[0].parse::<u32>().unwrap_or(0);
let minor = parts[1].parse::<u32>().unwrap_or(0);
Ok(major > 1 || (major == 1 && minor >= 11))
} else {
Ok(false)
}
} else {
Ok(false)
}
}

/// A directory that can optionally delete itself when we're done with it.
pub struct TempDir {
path: Utf8PathBuf,
Expand Down

0 comments on commit b62942e

Please sign in to comment.