Skip to content

Commit

Permalink
Merge pull request #166 from kinode-dao/hf/kit-build-verbose
Browse files Browse the repository at this point in the history
`kit build --verbose`
  • Loading branch information
nick1udwig authored May 25, 2024
2 parents 1429cb9 + b32bff3 commit a23725e
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 81 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
4 changes: 3 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 Expand Up @@ -431,6 +432,7 @@ pub async fn execute(
fakechain_port,
true,
recv_kill_in_start_chain,
false,
).await?;

if node_home.exists() {
Expand Down
100 changes: 68 additions & 32 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 @@ -96,6 +101,7 @@ pub async fn download_file(url: &str, path: &Path) -> Result<()> {
async fn compile_javascript_wasm_process(
process_dir: &Path,
valid_node: Option<String>,
verbose: bool,
) -> Result<()> {
info!(
"Compiling Javascript Kinode process in {:?}...",
Expand Down Expand Up @@ -127,12 +133,14 @@ async fn compile_javascript_wasm_process(
Command::new("bash")
.args(&["-c", &install])
.current_dir(process_dir),
verbose,
)?;

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

info!(
Expand All @@ -143,7 +151,11 @@ async fn compile_javascript_wasm_process(
}

#[instrument(level = "trace", skip_all)]
async fn compile_python_wasm_process(process_dir: &Path, python: &str) -> Result<()> {
async fn compile_python_wasm_process(
process_dir: &Path,
python: &str,
verbose: bool,
) -> Result<()> {
info!("Compiling Python Kinode process in {:?}...", process_dir);
let wit_dir = process_dir.join("wit");
download_file(KINODE_WIT_URL, &wit_dir.join("kinode.wit")).await?;
Expand All @@ -154,13 +166,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),
verbose,
)?;
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")),
verbose,
)?;

info!("Done compiling Python Kinode process in {:?}.", process_dir);
Expand All @@ -171,6 +185,7 @@ async fn compile_python_wasm_process(process_dir: &Path, python: &str) -> Result
async fn compile_rust_wasm_process(
process_dir: &Path,
features: &str,
verbose: bool,
) -> Result<()> {
info!("Compiling Rust Kinode process in {:?}...", process_dir);

Expand All @@ -194,14 +209,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",
]),
verbose,
)?;

// Copy wit directory to bindings
fs::create_dir_all(&bindings_dir.join("wit"))?;
Expand Down Expand Up @@ -232,16 +250,20 @@ async fn compile_rust_wasm_process(
args.push("--features");
args.push(&features);
}
let (stdout, stderr) = run_command(
let result = run_command(
Command::new("cargo")
.args(&args)
.current_dir(process_dir),
verbose,
)?;
if stdout.contains("warning") {
warn!("{}", stdout);
}
if stderr.contains("warning") {
warn!("{}", stderr);

if let Some((stdout, stderr)) = result {
if stdout.contains("warning") {
warn!("{}", stdout);
}
if stderr.contains("warning") {
warn!("{}", stderr);
}
}

// Adapt the module using wasm-tools
Expand All @@ -267,6 +289,7 @@ async fn compile_rust_wasm_process(
wasi_snapshot_file.to_str().unwrap(),
])
.current_dir(process_dir),
verbose,
)?;

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

info!("Done compiling Rust Kinode process in {:?}.", process_dir);
Ok(())
}

#[instrument(level = "trace", skip_all)]
async fn compile_and_copy_ui(package_dir: &Path, valid_node: Option<String>) -> Result<()> {
async fn compile_and_copy_ui(
package_dir: &Path,
valid_node: Option<String>,
verbose: bool,
) -> Result<()> {
let ui_path = package_dir.join("ui");
info!("Building UI in {:?}...", ui_path);

Expand All @@ -319,6 +347,7 @@ async fn compile_and_copy_ui(package_dir: &Path, valid_node: Option<String>) ->
Command::new("bash")
.args(&["-c", &install])
.current_dir(&ui_path),
verbose,
)?;

info!("Running npm run build:copy...");
Expand All @@ -327,6 +356,7 @@ async fn compile_and_copy_ui(package_dir: &Path, valid_node: Option<String>) ->
Command::new("bash")
.args(&["-c", &run])
.current_dir(&ui_path),
verbose,
)?;
} else {
let pkg_ui_path = package_dir.join("pkg/ui");
Expand All @@ -337,6 +367,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),
verbose,
)?;
}
} else {
Expand All @@ -353,29 +384,31 @@ async fn compile_package_and_ui(
valid_node: Option<String>,
skip_deps_check: bool,
features: &str,
verbose: bool,
) -> Result<()> {
compile_and_copy_ui(package_dir, valid_node).await?;
compile_package(package_dir, skip_deps_check, features).await?;
compile_and_copy_ui(package_dir, valid_node, verbose).await?;
compile_package(package_dir, skip_deps_check, features, verbose).await?;
Ok(())
}

#[instrument(level = "trace", skip_all)]
async fn compile_package_item(
entry: std::io::Result<std::fs::DirEntry>,
features: String,
verbose: bool,
) -> Result<()> {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
if path.join(RUST_SRC_PATH).exists() {
compile_rust_wasm_process(&path, &features).await?;
compile_rust_wasm_process(&path, &features, verbose).await?;
} else if path.join(PYTHON_SRC_PATH).exists() {
let python = get_python_version(None, None)?
.ok_or_else(|| eyre!("kit requires Python 3.10 or newer"))?;
compile_python_wasm_process(&path, &python).await?;
compile_python_wasm_process(&path, &python, verbose).await?;
} else if path.join(JAVASCRIPT_SRC_PATH).exists() {
let valid_node = get_newest_valid_node_version(None, None)?;
compile_javascript_wasm_process(&path, valid_node).await?;
compile_javascript_wasm_process(&path, valid_node, verbose).await?;
}
}
Ok(())
Expand All @@ -386,6 +419,7 @@ async fn compile_package(
package_dir: &Path,
skip_deps_check: bool,
features: &str,
verbose: bool,
) -> Result<()> {
let mut checked_rust = false;
let mut checked_py = false;
Expand All @@ -396,14 +430,14 @@ async fn compile_package(
if path.is_dir() {
if path.join(RUST_SRC_PATH).exists() && !checked_rust && !skip_deps_check {
let deps = check_rust_deps()?;
get_deps(deps)?;
get_deps(deps, verbose)?;
checked_rust = true;
} else if path.join(PYTHON_SRC_PATH).exists() && !checked_py {
check_py_deps()?;
checked_py = true;
} else if path.join(JAVASCRIPT_SRC_PATH).exists() && !checked_js && !skip_deps_check {
let deps = check_js_deps()?;
get_deps(deps)?;
get_deps(deps, verbose)?;
checked_js = true;
}
}
Expand All @@ -412,7 +446,7 @@ async fn compile_package(
let mut tasks = tokio::task::JoinSet::new();
let features = features.to_string();
for entry in package_dir.read_dir()? {
tasks.spawn(compile_package_item(entry, features.clone()));
tasks.spawn(compile_package_item(entry, features.clone(), verbose.clone()));
}
while let Some(res) = tasks.join_next().await {
res??;
Expand All @@ -428,6 +462,7 @@ pub async fn execute(
ui_only: bool,
skip_deps_check: bool,
features: &str,
verbose: bool,
) -> Result<()> {
if !package_dir.join("pkg").exists() {
if Some(".DS_Store") == package_dir.file_name().and_then(|s| s.to_str()) {
Expand All @@ -445,25 +480,26 @@ pub async fn execute(
if ui_only {
return Err(eyre!("kit build: can't build UI: no ui directory exists"));
} else {
compile_package(package_dir, skip_deps_check, features).await
compile_package(package_dir, skip_deps_check, features, verbose).await
}
} else {
if no_ui {
return compile_package(package_dir, skip_deps_check, features).await;
return compile_package(package_dir, skip_deps_check, features, verbose).await;
}

let deps = check_js_deps()?;
get_deps(deps)?;
get_deps(deps, verbose)?;
let valid_node = get_newest_valid_node_version(None, None)?;

if ui_only {
compile_and_copy_ui(package_dir, valid_node).await
compile_and_copy_ui(package_dir, valid_node, verbose).await
} else {
compile_package_and_ui(
package_dir,
valid_node,
skip_deps_check,
features,
verbose,
).await
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/build_start_package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ pub async fn execute(
url: &str,
skip_deps_check: bool,
features: &str,
verbose: bool,
) -> Result<()> {
build::execute(package_dir, no_ui, ui_only, skip_deps_check, features).await?;
build::execute(package_dir, no_ui, ui_only, skip_deps_check, features, verbose).await?;
start_package::execute(package_dir, url).await?;
Ok(())
}
7 changes: 4 additions & 3 deletions src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ pub async fn start_chain(
port: u16,
piped: bool,
recv_kill: BroadcastRecvBool,
verbose: bool,
) -> Result<Option<Child>> {
let deps = check_foundry_deps()?;
get_deps(deps)?;
get_deps(deps, verbose)?;

let state_hash = write_kinostate().await?;
let state_path = format!("./kinostate-{}.json", state_hash);
Expand Down Expand Up @@ -121,15 +122,15 @@ async fn wait_for_anvil(

/// kit chain, alias to anvil
#[instrument(level = "trace", skip_all)]
pub async fn execute(port: u16) -> Result<()> {
pub async fn execute(port: u16, verbose: bool) -> Result<()> {
let (send_to_cleanup, mut recv_in_cleanup) = tokio::sync::mpsc::unbounded_channel();
let (send_to_kill, _recv_kill) = tokio::sync::broadcast::channel(1);
let recv_kill_in_cos = send_to_kill.subscribe();

let handle_signals = tokio::spawn(cleanup_on_signal(send_to_cleanup.clone(), recv_kill_in_cos));

let recv_kill_in_start_chain = send_to_kill.subscribe();
let child = start_chain(port, false, recv_kill_in_start_chain).await?;
let child = start_chain(port, false, recv_kill_in_start_chain, verbose).await?;
let Some(mut child) = child else {
return Err(eyre!("Port {} is already in use by another anvil process", port));
};
Expand Down
4 changes: 3 additions & 1 deletion src/dev_ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn execute(
) -> Result<()> {
if !skip_deps_check {
let deps = check_js_deps()?;
get_deps(deps)?;
get_deps(deps, false)?;
}
let valid_node = get_newest_valid_node_version(None, None)?;

Expand All @@ -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
Loading

0 comments on commit a23725e

Please sign in to comment.