Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/build/caller_utils_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn to_pascal_case(s: &str) -> String {

// Find the world name in the world WIT file, prioritizing types-prefixed worlds
fn find_world_names(api_dir: &Path) -> Result<Vec<String>> {
println!("Looking in {api_dir:?} for world names...");
let mut world_names = Vec::new();

// Look for world definition files
Expand Down Expand Up @@ -471,7 +472,7 @@ fn generate_async_function(signature: &SignatureStruct) -> String {
// Create the caller-utils crate with a single lib.rs file
fn create_caller_utils_crate(api_dir: &Path, base_dir: &Path) -> Result<()> {
// Path to the new crate
let caller_utils_dir = base_dir.join("crates").join("caller-utils");
let caller_utils_dir = base_dir.join("target").join("caller-utils");
println!(
"Creating caller-utils crate at {}",
caller_utils_dir.display()
Expand All @@ -495,7 +496,7 @@ process_macros = "0.1.0"
futures-util = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
hyperware_app_common = { git = "https://github.com/hyperware-ai/hyperprocess-macro", rev = "f29952f" }
hyperware_app_common = { git = "https://github.com/hyperware-ai/hyperprocess-macro", rev = "df8f395" }
once_cell = "1.20.2"
futures = "0.3"
uuid = { version = "1.0" }
Expand Down Expand Up @@ -744,11 +745,11 @@ fn update_workspace_cargo_toml(base_dir: &Path) -> Result<()> {
// Check if caller-utils is already in the members list
let caller_utils_exists = members_array
.iter()
.any(|m| m.as_str().map_or(false, |s| s == "crates/caller-utils"));
.any(|m| m.as_str().map_or(false, |s| s == "target/caller-utils"));

if !caller_utils_exists {
println!("Adding caller-utils to workspace members");
members_array.push(Value::String("crates/caller-utils".to_string()));
members_array.push(Value::String("target/caller-utils".to_string()));

// Write back the updated TOML
let updated_content = toml::to_string_pretty(&parsed_toml)
Expand All @@ -773,7 +774,7 @@ fn update_workspace_cargo_toml(base_dir: &Path) -> Result<()> {
}

// Add caller-utils as a dependency to hyperware:process crates
fn add_caller_utils_to_projects(projects: &[PathBuf]) -> Result<()> {
pub fn add_caller_utils_to_projects(projects: &[PathBuf]) -> Result<()> {
for project_path in projects {
let cargo_toml_path = project_path.join("Cargo.toml");
println!(
Expand Down Expand Up @@ -805,7 +806,7 @@ fn add_caller_utils_to_projects(projects: &[PathBuf]) -> Result<()> {
let mut t = toml::map::Map::new();
t.insert(
"path".to_string(),
Value::String("../crates/caller-utils".to_string()),
Value::String("../target/caller-utils".to_string()),
);
t
}),
Expand Down Expand Up @@ -839,15 +840,12 @@ fn add_caller_utils_to_projects(projects: &[PathBuf]) -> Result<()> {
}

// Create caller-utils crate and integrate with the workspace
pub fn create_caller_utils(base_dir: &Path, api_dir: &Path, projects: &[PathBuf]) -> Result<()> {
pub fn create_caller_utils(base_dir: &Path, api_dir: &Path) -> Result<()> {
// Step 1: Create the caller-utils crate
create_caller_utils_crate(api_dir, base_dir)?;

// Step 2: Update workspace Cargo.toml
update_workspace_cargo_toml(base_dir)?;

// Step 3: Add caller-utils dependency to each hyperware:process project
add_caller_utils_to_projects(projects)?;

Ok(())
}
77 changes: 48 additions & 29 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,25 @@ pub fn remove_missing_features(cargo_toml_path: &Path, features: Vec<&str>) -> R
.collect())
}

#[instrument(level = "trace", skip_all)]
pub fn get_process_name(cargo_toml_path: &Path) -> Result<String> {
let cargo_toml_content = fs::read_to_string(cargo_toml_path)?;
let cargo_toml: toml::Value = cargo_toml_content.parse()?;

if let Some(process_name) = cargo_toml
.get("package")
.and_then(|p| p.get("name"))
.and_then(|n| n.as_str())
{
let process_name = process_name.replace("_", "-");
Ok(process_name.to_string())
} else {
Err(eyre!(
"No package.name field in Cargo.toml at {cargo_toml_path:?}"
))
}
}

/// Check if the first element is empty and there are no more elements
#[instrument(level = "trace", skip_all)]
fn is_only_empty_string(splitted: &Vec<&str>) -> bool {
Expand Down Expand Up @@ -901,18 +920,24 @@ async fn compile_rust_wasm_process(
features: &str,
verbose: bool,
) -> Result<()> {
let Some(package_dir) = process_dir.parent() else {
return Err(eyre!(
"Could not derive package dir from process_dir ({process_dir:?}) parent"
));
};
let process_name = get_process_name(&process_dir.join("Cargo.toml"))?;
info!("Compiling Rust Hyperware process in {:?}...", process_dir);

// Paths
let wit_dir = process_dir.join("target").join("wit");
let bindings_dir = process_dir
let wit_dir = package_dir.join("target").join("wit");
let bindings_dir = package_dir
.join("target")
.join("bindings")
.join(process_dir.file_name().unwrap());
.join(package_dir.file_name().unwrap());
fs::create_dir_all(&bindings_dir)?;

// Check and download wasi_snapshot_preview1.wasm if it does not exist
let wasi_snapshot_file = process_dir
let wasi_snapshot_file = package_dir
.join("target")
.join("wasi_snapshot_preview1.wasm");
let wasi_snapshot_url = format!(
Expand All @@ -935,6 +960,8 @@ async fn compile_rust_wasm_process(
let mut args = vec![
"+stable",
"build",
"-p",
&process_name,
"--release",
"--no-default-features",
"--target",
Expand All @@ -950,7 +977,7 @@ async fn compile_rust_wasm_process(
} else {
features.len()
};
let features = remove_missing_features(&process_dir.join("Cargo.toml"), features)?;
let features = remove_missing_features(&package_dir.join("Cargo.toml"), features)?;
if !test_only && original_length != features.len() {
info!(
"process {:?} missing features; using {:?}",
Expand All @@ -963,7 +990,7 @@ async fn compile_rust_wasm_process(
args.push(&features);
}
let result = run_command(
Command::new("cargo").args(&args).current_dir(process_dir),
Command::new("cargo").args(&args).current_dir(package_dir),
verbose,
)?;

Expand All @@ -981,9 +1008,9 @@ async fn compile_rust_wasm_process(
// For use inside of process_dir
// Run `wasm-tools component new`, putting output in pkg/
// and rewriting all `_`s to `-`s
// cargo hates `-`s and so outputs with `_`s; Kimap hates
// cargo hates `-`s and so outputs with `_`s; Hypermap hates
// `_`s and so we convert to and enforce all `-`s
let wasm_file_name_cab = process_dir
let wasm_file_name_cab = package_dir
.file_name()
.and_then(|s| s.to_str())
.unwrap()
Expand All @@ -993,7 +1020,7 @@ async fn compile_rust_wasm_process(
let wasm_file_prefix = Path::new("target/wasm32-wasip1/release");
let wasm_file_cab = wasm_file_prefix.join(&format!("{wasm_file_name_cab}.wasm"));

let wasm_file_pkg = format!("../pkg/{wasm_file_name_hep}.wasm");
let wasm_file_pkg = format!("pkg/{wasm_file_name_hep}.wasm");
let wasm_file_pkg = Path::new(&wasm_file_pkg);

let wasi_snapshot_file = Path::new("target/wasi_snapshot_preview1.wasm");
Expand All @@ -1009,7 +1036,7 @@ async fn compile_rust_wasm_process(
"--adapt",
wasi_snapshot_file.to_str().unwrap(),
])
.current_dir(process_dir),
.current_dir(package_dir),
verbose,
)?;

Expand Down Expand Up @@ -1070,11 +1097,11 @@ async fn compile_and_copy_ui(

#[instrument(level = "trace", skip_all)]
async fn build_wit_dir(
process_dir: &Path,
package_dir: &Path,
apis: &HashMap<String, Vec<u8>>,
wit_version: Option<u32>,
) -> Result<()> {
let wit_dir = process_dir.join("target").join("wit");
let wit_dir = package_dir.join("target").join("wit");
if wit_dir.exists() {
fs::remove_dir_all(&wit_dir)?;
}
Expand Down Expand Up @@ -1569,6 +1596,8 @@ async fn compile_package(
})
.to_string();

build_wit_dir(&package_dir, &apis, metadata.properties.wit_version).await?;

let mut tasks = tokio::task::JoinSet::new();
let features = features.to_string();
let mut to_compile = HashSet::new();
Expand All @@ -1588,26 +1617,16 @@ async fn compile_package(
let is_py_process = path.join(PYTHON_SRC_PATH).exists();
let is_js_process = path.join(JAVASCRIPT_SRC_PATH).exists();
if is_rust_process || is_py_process || is_js_process {
build_wit_dir(&path, &apis, metadata.properties.wit_version).await?;
} else {
continue;
to_compile.insert((path, is_rust_process, is_py_process, is_js_process));
}
to_compile.insert((path, is_rust_process, is_py_process, is_js_process));
}

// TODO: move process/target/wit -> target/wit
if !ignore_deps && !dependencies.is_empty() {
info!("{hyperapp_processed_projects:?}");
if let Some(ref processed_projects) = hyperapp_processed_projects {
for processed_project in processed_projects {
let api_dir = processed_project.join("target").join("wit");
info!("{processed_project:?} {api_dir:?}");
caller_utils_generator::create_caller_utils(
package_dir,
&api_dir,
&[processed_project.clone()],
)?;
}
let api_dir = package_dir.join("target").join("wit");
//info!("{processed_project:?} {api_dir:?}");
if let Some(ref processed_projects) = hyperapp_processed_projects {
caller_utils_generator::create_caller_utils(package_dir, &api_dir)?;
for processed_project in processed_projects {
caller_utils_generator::add_caller_utils_to_projects(&[processed_project.clone()])?;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/new/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ pub fn execute(
if !is_hypermap_safe(&package_name, false) {
let error = if !is_from_dir {
eyre!(
"`package_name` '{}' must be Kimap safe (a-z, 0-9, - allowed).",
"`package_name` '{}' must be Hypermap safe (a-z, 0-9, - allowed).",
package_name
)
} else {
eyre!(
"`package_name` (derived from given directory {:?}) '{}' must be Kimap safe (a-z, 0-9, - allowed).",
"`package_name` (derived from given directory {:?}) '{}' must be Hypermap safe (a-z, 0-9, - allowed).",
new_dir,
package_name,
)
Expand All @@ -297,7 +297,7 @@ pub fn execute(
}
if !is_hypermap_safe(&publisher, true) {
return Err(eyre!(
"`publisher` '{}' must be Kimap safe (a-z, 0-9, -, . allowed).",
"`publisher` '{}' must be Hypermap safe (a-z, 0-9, -, . allowed).",
publisher
));
}
Expand Down
2 changes: 1 addition & 1 deletion src/new/templates/rust/no-ui/blank/blank/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use hyperware_process_lib::{await_message, call_init, println, Address};

wit_bindgen::generate!({
path: "target/wit",
path: "../target/wit",
world: "process-v1",
});

Expand Down
6 changes: 4 additions & 2 deletions src/new/templates/rust/no-ui/chat/chat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use crate::hyperware::process::chat::{
ChatMessage, Request as ChatRequest, Response as ChatResponse, SendRequest,
};
use hyperware_process_lib::logging::{error, info, init_logging, Level};
use hyperware_process_lib::{await_message, call_init, println, Address, Message, Request, Response};
use hyperware_process_lib::{
await_message, call_init, println, Address, Message, Request, Response,
};

wit_bindgen::generate!({
path: "target/wit",
path: "../target/wit",
world: "chat-template-dot-os-v0",
generate_unused_types: true,
additional_derives: [serde::Deserialize, serde::Serialize, process_macros::SerdeJsonInto],
Expand Down
10 changes: 7 additions & 3 deletions src/new/templates/rust/no-ui/chat/send/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::hyperware::process::chat::{Request as ChatRequest, Response as ChatResponse, SendRequest};
use hyperware_process_lib::{await_next_message_body, call_init, println, Address, Message, Request};
use crate::hyperware::process::chat::{
Request as ChatRequest, Response as ChatResponse, SendRequest,
};
use hyperware_process_lib::{
await_next_message_body, call_init, println, Address, Message, Request,
};

wit_bindgen::generate!({
path: "target/wit",
path: "../target/wit",
world: "chat-template-dot-os-v0",
generate_unused_types: true,
additional_derives: [serde::Deserialize, serde::Serialize],
Expand Down
2 changes: 1 addition & 1 deletion src/new/templates/rust/no-ui/echo/echo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use hyperware_process_lib::logging::{error, info, init_logging, Level};
use hyperware_process_lib::{await_message, call_init, println, Address, Message, Response};

wit_bindgen::generate!({
path: "target/wit",
path: "../target/wit",
world: "process-v1",
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use hyperware_process_lib::logging::{error, info, init_logging, Level};
use hyperware_process_lib::{await_message, call_init, Address, Message, Response};

wit_bindgen::generate!({
path: "target/wit",
path: "../target/wit",
world: "fibonacci-template-dot-os-v0",
generate_unused_types: true,
additional_derives: [serde::Deserialize, serde::Serialize, process_macros::SerdeJsonInto],
Expand Down
6 changes: 4 additions & 2 deletions src/new/templates/rust/no-ui/fibonacci/number/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::hyperware::process::fibonacci::{Request as FibonacciRequest, Response as FibonacciResponse};
use crate::hyperware::process::fibonacci::{
Request as FibonacciRequest, Response as FibonacciResponse,
};
use hyperware_process_lib::{
await_next_message_body, call_init, println, Address, Message, Request,
};

wit_bindgen::generate!({
path: "target/wit",
path: "../target/wit",
world: "fibonacci-template-dot-os-v0",
generate_unused_types: true,
additional_derives: [serde::Deserialize, serde::Serialize],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use hyperware_process_lib::{
};

wit_bindgen::generate!({
path: "target/wit",
path: "../target/wit",
world: "file-transfer-template-dot-os-v0",
generate_unused_types: true,
additional_derives: [serde::Deserialize, serde::Serialize, process_macros::SerdeJsonInto],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::hyperware::process::standard::Address as WitAddress;
use hyperware_process_lib::{our_capabilities, spawn, Address, OnExit, Request, Response};

wit_bindgen::generate!({
path: "target/wit",
path: "../target/wit",
world: "file-transfer-worker-api-v0",
generate_unused_types: true,
additional_derives: [serde::Deserialize, serde::Serialize, process_macros::SerdeJsonInto],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use hyperware_process_lib::{
};

wit_bindgen::generate!({
path: "target/wit",
path: "../target/wit",
world: "file-transfer-template-dot-os-v0",
generate_unused_types: true,
additional_derives: [serde::Deserialize, serde::Serialize, process_macros::SerdeJsonInto],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use hyperware_process_lib::{
};

wit_bindgen::generate!({
path: "target/wit",
path: "../target/wit",
world: "file-transfer-template-dot-os-v0",
generate_unused_types: true,
additional_derives: [serde::Deserialize, serde::Serialize, process_macros::SerdeJsonInto],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::hyperware::process::file_transfer::{
Request as TransferRequest, Response as TransferResponse,
};
use hyperware_process_lib::{await_next_message_body, call_init, println, Address, Message, Request};
use hyperware_process_lib::{
await_next_message_body, call_init, println, Address, Message, Request,
};

wit_bindgen::generate!({
path: "target/wit",
path: "../target/wit",
world: "file-transfer-template-dot-os-v0",
generate_unused_types: true,
additional_derives: [serde::Deserialize, serde::Serialize, process_macros::SerdeJsonInto],
Expand Down
Loading