diff --git a/Cargo.lock b/Cargo.lock index 0c88b49..df218c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2429,6 +2429,15 @@ dependencies = [ "wayland-server", ] +[[package]] +name = "weft-runtime" +version = "0.1.0" +dependencies = [ + "anyhow", + "tracing", + "tracing-subscriber", +] + [[package]] name = "weft-servo-shell" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 534e7d6..b0755ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ members = [ "crates/weft-appd", "crates/weft-build-meta", "crates/weft-compositor", + "crates/weft-runtime", "crates/weft-servo-shell", ] resolver = "2" diff --git a/crates/weft-runtime/Cargo.toml b/crates/weft-runtime/Cargo.toml new file mode 100644 index 0000000..263b810 --- /dev/null +++ b/crates/weft-runtime/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "weft-runtime" +version.workspace = true +edition.workspace = true +rust-version.workspace = true + +[[bin]] +name = "weft-runtime" +path = "src/main.rs" + +[dependencies] +anyhow = "1.0" +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter"] } diff --git a/crates/weft-runtime/src/main.rs b/crates/weft-runtime/src/main.rs new file mode 100644 index 0000000..bf2065d --- /dev/null +++ b/crates/weft-runtime/src/main.rs @@ -0,0 +1,98 @@ +use std::path::PathBuf; + +use anyhow::Context; + +fn main() -> anyhow::Result<()> { + tracing_subscriber::fmt() + .with_env_filter( + tracing_subscriber::EnvFilter::try_from_default_env() + .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")), + ) + .init(); + + let args: Vec = std::env::args().collect(); + if args.len() < 3 { + anyhow::bail!("usage: weft-runtime "); + } + let app_id = &args[1]; + let session_id: u64 = args[2] + .parse() + .with_context(|| format!("invalid session_id: {}", args[2]))?; + + tracing::info!(session_id, %app_id, "weft-runtime starting"); + + let pkg_dir = resolve_package(app_id)?; + tracing::info!(path = %pkg_dir.display(), "package resolved"); + + let wasm_path = pkg_dir.join("app.wasm"); + if !wasm_path.exists() { + anyhow::bail!("app.wasm not found at {}", wasm_path.display()); + } + + // TODO: Load wasm_path into a Wasmtime Engine and run the module. + // Until Wasmtime is integrated, print READY and exit cleanly so that + // weft-appd can complete the session lifecycle in tests and development. + tracing::info!(session_id, %app_id, wasm = %wasm_path.display(), "Wasmtime integration pending"); + + println!("READY"); + + tracing::info!(session_id, %app_id, "exiting"); + Ok(()) +} + +fn resolve_package(app_id: &str) -> anyhow::Result { + for store_root in package_store_roots() { + let pkg_dir = store_root.join(app_id); + let manifest = pkg_dir.join("wapp.toml"); + if manifest.exists() { + return Ok(pkg_dir); + } + } + anyhow::bail!("package '{}' not found in any package store", app_id) +} + +fn package_store_roots() -> Vec { + if let Ok(explicit) = std::env::var("WEFT_APP_STORE") { + return vec![PathBuf::from(explicit)]; + } + + let mut roots = Vec::new(); + + if let Ok(home) = std::env::var("HOME") { + roots.push( + PathBuf::from(home) + .join(".local") + .join("share") + .join("weft") + .join("apps"), + ); + } + + roots.push(PathBuf::from("/usr/share/weft/apps")); + + roots +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn package_store_roots_includes_system_path() { + let roots = package_store_roots(); + assert!( + roots + .iter() + .any(|p| p == &PathBuf::from("/usr/share/weft/apps")) + ); + } + + #[test] + fn package_store_roots_uses_weft_app_store_when_set() { + // SAFETY: test binary is single-threaded at this point. + unsafe { std::env::set_var("WEFT_APP_STORE", "/custom/store") }; + let roots = package_store_roots(); + assert_eq!(roots, vec![PathBuf::from("/custom/store")]); + unsafe { std::env::remove_var("WEFT_APP_STORE") }; + } +} diff --git a/scripts/wsl-test.sh b/scripts/wsl-test.sh index 67772d4..0935536 100644 --- a/scripts/wsl-test.sh +++ b/scripts/wsl-test.sh @@ -30,5 +30,9 @@ echo "" echo "==> cargo test -p weft-appd" cargo test -p weft-appd 2>&1 +echo "" +echo "==> cargo test -p weft-runtime" +cargo test -p weft-runtime 2>&1 + echo "" echo "ALL DONE"