Skip to content

Commit

Permalink
refactor build::run_command()
Browse files Browse the repository at this point in the history
  • Loading branch information
nick1udwig committed May 24, 2024
1 parent 1429cb9 commit 695666a
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kit"
version = "0.5.1"
version = "0.5.2"
edition = "2021"

[build-dependencies]
Expand Down
3 changes: 2 additions & 1 deletion src/boot_fake_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ pub fn compile_runtime(path: &Path, release: bool) -> Result<()> {

build::run_command(Command::new("cargo")
.args(&args)
.current_dir(path)
.current_dir(path),
false,
)?;

info!("Done compiling Kinode runtime.");
Expand Down
44 changes: 31 additions & 13 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ struct CargoPackage {
}

#[instrument(level = "trace", skip_all)]
pub fn run_command(cmd: &mut Command) -> Result<(String, String)> {
pub fn run_command(cmd: &mut Command, verbose: bool) -> Result<Option<(String, String)>> {
if verbose {
let mut child = cmd.spawn()?;
child.wait()?;
return Ok(None);
}
let output = cmd.output()?;
if output.status.success() {
Ok((
Ok(Some((
String::from_utf8_lossy(&output.stdout).to_string(),
String::from_utf8_lossy(&output.stderr).to_string(),
))
)))
} else {
Err(eyre!(
"Command `{} {:?}` failed with exit code {}\nstdout: {}\nstderr: {}",
Expand Down Expand Up @@ -127,12 +132,14 @@ async fn compile_javascript_wasm_process(
Command::new("bash")
.args(&["-c", &install])
.current_dir(process_dir),
false,
)?;

run_command(
Command::new("bash")
.args(&["-c", &componentize])
.current_dir(process_dir),
false,
)?;

info!(
Expand All @@ -154,13 +161,15 @@ async fn compile_python_wasm_process(process_dir: &Path, python: &str) -> Result
Command::new(python)
.args(&["-m", "venv", PY_VENV_NAME])
.current_dir(process_dir),
false,
)?;
run_command(Command::new("bash")
.args(&[
"-c",
&format!("source ../{PY_VENV_NAME}/bin/activate && pip install {REQUIRED_PY_PACKAGE} && componentize-py -d ../wit/ -w process componentize lib -o ../../pkg/{wasm_file_name}.wasm"),
])
.current_dir(process_dir.join("src"))
.current_dir(process_dir.join("src")),
false,
)?;

info!("Done compiling Python Kinode process in {:?}.", process_dir);
Expand Down Expand Up @@ -194,14 +203,17 @@ async fn compile_rust_wasm_process(
download_file(&wasi_snapshot_url, &wasi_snapshot_file).await?;

// Create target.wasm (compiled .wit) & world
run_command(Command::new("wasm-tools").args(&[
"component",
"wit",
wit_dir.to_str().unwrap(),
"-o",
&bindings_dir.join("target.wasm").to_str().unwrap(),
"--wasm",
]))?;
run_command(Command::new("wasm-tools")
.args(&[
"component",
"wit",
wit_dir.to_str().unwrap(),
"-o",
&bindings_dir.join("target.wasm").to_str().unwrap(),
"--wasm",
]),
false,
)?;

// Copy wit directory to bindings
fs::create_dir_all(&bindings_dir.join("wit"))?;
Expand Down Expand Up @@ -236,7 +248,8 @@ async fn compile_rust_wasm_process(
Command::new("cargo")
.args(&args)
.current_dir(process_dir),
)?;
false,
)?.unwrap();
if stdout.contains("warning") {
warn!("{}", stdout);
}
Expand Down Expand Up @@ -267,6 +280,7 @@ async fn compile_rust_wasm_process(
wasi_snapshot_file.to_str().unwrap(),
])
.current_dir(process_dir),
false,
)?;

let wasm_path = format!("../pkg/{}.wasm", wasm_file_name);
Expand All @@ -286,6 +300,7 @@ async fn compile_rust_wasm_process(
wasm_path.to_str().unwrap(),
])
.current_dir(process_dir),
false,
)?;

info!("Done compiling Rust Kinode process in {:?}.", process_dir);
Expand Down Expand Up @@ -319,6 +334,7 @@ async fn compile_and_copy_ui(package_dir: &Path, valid_node: Option<String>) ->
Command::new("bash")
.args(&["-c", &install])
.current_dir(&ui_path),
false,
)?;

info!("Running npm run build:copy...");
Expand All @@ -327,6 +343,7 @@ async fn compile_and_copy_ui(package_dir: &Path, valid_node: Option<String>) ->
Command::new("bash")
.args(&["-c", &run])
.current_dir(&ui_path),
false,
)?;
} else {
let pkg_ui_path = package_dir.join("pkg/ui");
Expand All @@ -337,6 +354,7 @@ async fn compile_and_copy_ui(package_dir: &Path, valid_node: Option<String>) ->
Command::new("cp")
.args(["-r", "ui", "pkg/ui"])
.current_dir(&package_dir),
false,
)?;
}
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/dev_ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub fn execute(
Command::new("bash")
.args(&["-c", &install_command])
.current_dir(&ui_path),
false,
)?;

info!("Running {}", dev);
Expand All @@ -52,6 +53,7 @@ pub fn execute(
.args(&["-c", &dev_command])
.env("VITE_NODE_URL", url)
.current_dir(&ui_path),
false,
)?;
} else {
return Err(eyre!("'ui' directory not found or 'ui/package.json' does not exist"));
Expand Down
31 changes: 20 additions & 11 deletions src/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub const REQUIRED_PY_PACKAGE: &str = "componentize-py==0.11.0";
#[derive(Clone)]
pub enum Dependency {
Anvil,
Forge,
Forge,
Nvm,
Npm,
Node,
Expand Down Expand Up @@ -74,7 +74,8 @@ fn install_nvm() -> Result<()> {
FETCH_NVM_VERSION,
);
run_command(Command::new("bash")
.args(&["-c", &install_nvm])
.args(&["-c", &install_nvm]),
false,
)?;

info!("Done getting nvm.");
Expand All @@ -86,7 +87,8 @@ fn install_rust() -> Result<()> {
info!("Getting rust...");
let install_rust = "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh";
run_command(Command::new("bash")
.args(&["-c", install_rust])
.args(&["-c", install_rust]),
false,
)?;

info!("Done getting rust.");
Expand All @@ -98,7 +100,8 @@ fn check_python_venv(python: &str) -> Result<()> {
info!("Checking for python venv...");
let venv_result = run_command(Command::new(python)
.args(&["-m", "venv", "kinode-test-venv"])
.current_dir("/tmp")
.current_dir("/tmp"),
false,
);
let venv_dir = PathBuf::from("/tmp/kinode-test-venv");
if venv_dir.exists() {
Expand Down Expand Up @@ -220,15 +223,17 @@ fn call_with_nvm_output(arg: &str) -> Result<String> {
#[instrument(level = "trace", skip_all)]
fn call_with_nvm(arg: &str) -> Result<()> {
run_command(Command::new("bash")
.args(&["-c", &format!("source ~/.nvm/nvm.sh && {}", arg)])
.args(&["-c", &format!("source ~/.nvm/nvm.sh && {}", arg)]),
false,
)?;
Ok(())
}

#[instrument(level = "trace", skip_all)]
fn call_rustup(arg: &str) -> Result<()> {
run_command(Command::new("bash")
.args(&["-c", &format!("rustup {}", arg)])
.args(&["-c", &format!("rustup {}", arg)]),
false,
)?;
Ok(())
}
Expand All @@ -241,7 +246,8 @@ fn call_cargo(arg: &str) -> Result<()> {
format!("cargo --color=always {}", arg)
};
run_command(Command::new("bash")
.args(&["-c", &command])
.args(&["-c", &command]),
false,
)?;
Ok(())
}
Expand Down Expand Up @@ -293,7 +299,8 @@ fn check_rust_toolchains_targets() -> Result<Vec<Dependency>> {
run_command(Command::new("rustup")
.args(&["default", "stable"])
.stdout(Stdio::null())
.stderr(Stdio::null())
.stderr(Stdio::null()),
false,
)?;
let output = Command::new("rustup")
.arg("show")
Expand Down Expand Up @@ -322,7 +329,8 @@ fn check_rust_toolchains_targets() -> Result<Vec<Dependency>> {
run_command(Command::new("rustup")
.args(&["default", "nightly"])
.stdout(Stdio::null())
.stderr(Stdio::null())
.stderr(Stdio::null()),
false,
)?;
let output = Command::new("rustup")
.arg("show")
Expand All @@ -341,7 +349,8 @@ fn check_rust_toolchains_targets() -> Result<Vec<Dependency>> {
run_command(Command::new("rustup")
.args(&["default", original_default])
.stdout(Stdio::null())
.stderr(Stdio::null())
.stderr(Stdio::null()),
false,
)?;
Ok(missing_deps)
}
Expand Down Expand Up @@ -434,7 +443,7 @@ pub fn check_foundry_deps() -> Result<Vec<Dependency>> {
// install forge+anvil+others, could be separated into binary extractions from github releases.
pub fn install_foundry() -> Result<()> {
let install_cmd = "curl -L https://foundry.paradigm.xyz | bash";
run_command(Command::new("bash").args(&["-c", install_cmd]))?;
run_command(Command::new("bash").args(&["-c", install_cmd]), false)?;

Ok(())
}
Expand Down
8 changes: 3 additions & 5 deletions src/update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use fs_err as fs;
use tracing::instrument;

use crate::KIT_CACHE;
//use crate::build;
use crate::build::run_command;

#[instrument(level = "trace", skip_all)]
pub fn execute(mut user_args: Vec<String>, branch: &str) -> Result<()> {
Expand All @@ -19,10 +19,8 @@ pub fn execute(mut user_args: Vec<String>, branch: &str) -> Result<()> {
.map(|v| v.to_string())
.collect();
args.append(&mut user_args);
let mut update_process = Command::new("cargo")
.args(&args[..])
.spawn()?;
update_process.wait()?;

run_command(Command::new("cargo").args(&args[..]), true)?;

let cache_path = format!("{}/kinode-dao-kit-commits", KIT_CACHE);
let cache_path = std::path::Path::new(&cache_path);
Expand Down

0 comments on commit 695666a

Please sign in to comment.