2022-07-22 06:14:37 +02:00
|
|
|
use std::{
|
|
|
|
io::Read,
|
|
|
|
process::{Command, Stdio},
|
2023-03-06 04:04:12 +01:00
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, Ordering::Relaxed},
|
|
|
|
Mutex,
|
|
|
|
},
|
2022-07-22 06:14:37 +02:00
|
|
|
};
|
|
|
|
|
2023-03-06 04:04:12 +01:00
|
|
|
static CARGO_BUILD_LOCK: Mutex<()> = Mutex::new(());
|
|
|
|
static PLUGINS_BUILT: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
|
|
|
// This runs `cargo build --package nu_plugin_*` to ensure that all plugins
|
|
|
|
// have been built before plugin tests run. We use a lock to avoid multiple
|
|
|
|
// simultaneous `cargo build` invocations clobbering each other.
|
|
|
|
pub fn ensure_plugins_built() {
|
|
|
|
let _guard = CARGO_BUILD_LOCK.lock().expect("could not get mutex lock");
|
|
|
|
|
|
|
|
if PLUGINS_BUILT.load(Relaxed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-22 06:14:37 +02:00
|
|
|
let cargo_path = env!("CARGO");
|
2023-03-06 04:04:12 +01:00
|
|
|
let mut arguments = vec!["build", "--package", "nu_plugin_*", "--quiet"];
|
2022-07-22 06:14:37 +02:00
|
|
|
|
|
|
|
let profile = std::env::var("NUSHELL_CARGO_TARGET");
|
|
|
|
if let Ok(profile) = &profile {
|
|
|
|
arguments.push("--profile");
|
|
|
|
arguments.push(profile);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut command = Command::new(cargo_path)
|
|
|
|
.args(arguments)
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.stderr(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.expect("Failed to spawn cargo build command");
|
|
|
|
|
|
|
|
let stderr = command.stderr.take();
|
|
|
|
|
|
|
|
let success = command
|
|
|
|
.wait()
|
|
|
|
.expect("failed to wait cargo build command")
|
|
|
|
.success();
|
|
|
|
|
|
|
|
if let Some(mut stderr) = stderr {
|
|
|
|
let mut buffer = String::new();
|
|
|
|
stderr
|
|
|
|
.read_to_string(&mut buffer)
|
|
|
|
.expect("failed to read cargo build stderr");
|
|
|
|
if !buffer.is_empty() {
|
2023-01-30 02:37:54 +01:00
|
|
|
println!("=== cargo build stderr\n{buffer}");
|
2022-07-22 06:14:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !success {
|
|
|
|
panic!("cargo build failed");
|
|
|
|
}
|
2023-03-06 04:04:12 +01:00
|
|
|
|
|
|
|
PLUGINS_BUILT.store(true, Relaxed);
|
2022-07-22 06:14:37 +02:00
|
|
|
}
|