From 1989a3b93b986f6b13fe90308e0bdaca45a28d2e Mon Sep 17 00:00:00 2001 From: Drew Tada Date: Tue, 13 Feb 2024 13:32:38 -0500 Subject: [PATCH] bootstrap sequence uses new metadata --- kinode/build.rs | 7 +- .../app_store/{pkg => }/metadata.json | 0 kinode/packages/chess/{pkg => }/metadata.json | 0 .../packages/homepage/{pkg => }/metadata.json | 0 .../kns_indexer/{pkg => }/metadata.json | 0 .../packages/terminal/{pkg => }/metadata.json | 0 .../packages/tester/{pkg => }/metadata.json | 0 kinode/src/state.rs | 74 ++++--------------- lib/src/core.rs | 2 +- 9 files changed, 21 insertions(+), 62 deletions(-) rename kinode/packages/app_store/{pkg => }/metadata.json (100%) rename kinode/packages/chess/{pkg => }/metadata.json (100%) rename kinode/packages/homepage/{pkg => }/metadata.json (100%) rename kinode/packages/kns_indexer/{pkg => }/metadata.json (100%) rename kinode/packages/terminal/{pkg => }/metadata.json (100%) rename kinode/packages/tester/{pkg => }/metadata.json (100%) diff --git a/kinode/build.rs b/kinode/build.rs index 0a051b6f..e85ab2fe 100644 --- a/kinode/build.rs +++ b/kinode/build.rs @@ -17,13 +17,14 @@ async fn main() -> anyhow::Result<()> { let mut bootstrapped_processes = Vec::new(); writeln!( bootstrapped_processes, - "pub static BOOTSTRAPPED_PROCESSES: &[(&str, &[u8])] = &[", + "pub static BOOTSTRAPPED_PROCESSES: &[(&str, &[u8], &[u8])] = &[", ) .unwrap(); let packages_dir = format!("{}/packages", pwd.display()); eprintln!("{packages_dir:?}"); for entry in std::fs::read_dir(packages_dir).unwrap() { let entry_path = entry.unwrap().path(); + let metadata_path = format!("{}/metadata.json", entry_path.display()); let parent_pkg_path = format!("{}/pkg", entry_path.display()); kit::build::execute(&entry_path, false, false, false, true).await?; @@ -72,8 +73,8 @@ async fn main() -> anyhow::Result<()> { // Add zip bytes to bootstrapped_processes.rs writeln!( bootstrapped_processes, - " (\"{}\", include_bytes!(\"{}\")),", - zip_filename, zip_path, + " (\"{}\", include_bytes!(\"{}\"), include_bytes!(\"{}\")),", + zip_filename, metadata_path, zip_path, ) .unwrap(); } diff --git a/kinode/packages/app_store/pkg/metadata.json b/kinode/packages/app_store/metadata.json similarity index 100% rename from kinode/packages/app_store/pkg/metadata.json rename to kinode/packages/app_store/metadata.json diff --git a/kinode/packages/chess/pkg/metadata.json b/kinode/packages/chess/metadata.json similarity index 100% rename from kinode/packages/chess/pkg/metadata.json rename to kinode/packages/chess/metadata.json diff --git a/kinode/packages/homepage/pkg/metadata.json b/kinode/packages/homepage/metadata.json similarity index 100% rename from kinode/packages/homepage/pkg/metadata.json rename to kinode/packages/homepage/metadata.json diff --git a/kinode/packages/kns_indexer/pkg/metadata.json b/kinode/packages/kns_indexer/metadata.json similarity index 100% rename from kinode/packages/kns_indexer/pkg/metadata.json rename to kinode/packages/kns_indexer/metadata.json diff --git a/kinode/packages/terminal/pkg/metadata.json b/kinode/packages/terminal/metadata.json similarity index 100% rename from kinode/packages/terminal/pkg/metadata.json rename to kinode/packages/terminal/metadata.json diff --git a/kinode/packages/tester/pkg/metadata.json b/kinode/packages/tester/metadata.json similarity index 100% rename from kinode/packages/tester/pkg/metadata.json rename to kinode/packages/tester/metadata.json diff --git a/kinode/src/state.rs b/kinode/src/state.rs index b5364fdb..3459b916 100644 --- a/kinode/src/state.rs +++ b/kinode/src/state.rs @@ -389,7 +389,8 @@ async fn bootstrap( let packages = get_zipped_packages().await; - for (package_name, mut package) in packages.clone() { + for (package_metadata, mut package) in packages.clone() { + let package_name = package_metadata.properties.package_name.as_str(); // special case tester: only load it in if in simulation mode if package_name == "tester" { #[cfg(not(feature = "simulation-mode"))] @@ -397,32 +398,7 @@ async fn bootstrap( } println!("fs: handling package {package_name}...\r"); - // get and read metadata.json - let Ok(mut package_metadata_zip) = package.by_name("metadata.json") else { - println!( - "fs: missing metadata for package {}, skipping", - package_name - ); - continue; - }; - let mut metadata_content = Vec::new(); - package_metadata_zip - .read_to_end(&mut metadata_content) - .unwrap(); - drop(package_metadata_zip); - // TODO this should just deserialize to an ERC721Metadata struct - let package_metadata: serde_json::Value = - serde_json::from_slice(&metadata_content).expect("fs: metadata parse error"); - - // println!("fs: found package metadata: {:?}\r", package_metadata); - - let package_name = package_metadata["properties"]["package_name"] - .as_str() - .expect("fs: metadata parse error: bad package name"); - - let package_publisher = package_metadata["properties"]["publisher"] - .as_str() - .expect("fs: metadata parse error: bad publisher name"); + let package_publisher = package_metadata.properties.publisher.as_str(); // create a new package in VFS let our_drive_name = [package_name, package_publisher].join(":"); @@ -631,7 +607,8 @@ async fn bootstrap( } // second loop: go and grant_capabilities to processes // can't do this in first loop because we need to have all processes in the map first - for (package_name, mut package) in packages { + for (package_metadata, mut package) in packages { + let package_name = package_metadata.properties.package_name.as_str(); // special case tester: only load it in if in simulation mode if package_name == "tester" { #[cfg(not(feature = "simulation-mode"))] @@ -655,31 +632,7 @@ async fn bootstrap( let package_manifest = serde_json::from_str::>(&package_manifest) .expect("fs: manifest parse error"); - // get and read metadata.json - let Ok(mut package_metadata_zip) = package.by_name("metadata.json") else { - println!( - "fs: missing metadata for package {}, skipping", - package_name - ); - continue; - }; - let mut metadata_content = Vec::new(); - package_metadata_zip - .read_to_end(&mut metadata_content) - .unwrap(); - drop(package_metadata_zip); - // TODO deserialize to an ERC721Metadata struct - let package_metadata: serde_json::Value = - serde_json::from_slice(&metadata_content).expect("fs: metadata parse error"); - - // println!("fs: found package metadata: {:?}\r", package_metadata); - let package_name = package_metadata["properties"]["package_name"] - .as_str() - .expect("fs: metadata parse error: bad package name"); - - let package_publisher = package_metadata["properties"]["publisher"] - .as_str() - .expect("fs: metadata parse error: bad publisher name"); + let package_publisher = package_metadata.properties.publisher.as_str(); // for each process-entry in manifest.json: for entry in package_manifest { @@ -760,16 +713,21 @@ fn sign_cap(cap: Capability, keypair: Arc) -> Vec } /// read in `include!()`ed .zip package files -async fn get_zipped_packages() -> Vec<(String, zip::ZipArchive>)> { +async fn get_zipped_packages() -> Vec<(Erc721Metadata, zip::ZipArchive>)> { // println!("fs: reading distro packages...\r"); let mut packages = Vec::new(); - for (package_name, bytes) in BOOTSTRAPPED_PROCESSES.iter() { + for (package_name, metadata_bytes, bytes) in BOOTSTRAPPED_PROCESSES.iter() { if let Ok(zip) = zip::ZipArchive::new(std::io::Cursor::new(*bytes)) { - // add to list of packages - // println!("fs: found package: {}\r", package_name); - packages.push((package_name.to_string(), zip)); + if let Ok(metadata) = serde_json::from_slice::(metadata_bytes) { + packages.push(( + metadata, + zip + )); + } else { + println!("fs: metadata for package {} is not valid Erc721Metadata", package_name); + } } } diff --git a/lib/src/core.rs b/lib/src/core.rs index 29f827b3..2a997e77 100644 --- a/lib/src/core.rs +++ b/lib/src/core.rs @@ -1050,7 +1050,7 @@ pub struct Erc721Metadata { pub image: Option, pub external_url: Option, pub animation_url: Option, - pub properties: Option, + pub properties: Erc721Properties, } #[derive(Clone, Debug, Serialize, Deserialize)]