diff --git a/Cargo.lock b/Cargo.lock index e8dedf9cf50..8b2cc1927fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -823,6 +823,18 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.8.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578a7433b776b56a35785ed5ce9a7e777ac0598aac5a6dd1b4b18a307c7fc71b" +dependencies = [ + "indexmap", + "ryu", + "serde", + "yaml-rust", +] + [[package]] name = "sha1" version = "0.10.5" @@ -1196,6 +1208,23 @@ dependencies = [ "webc", ] +[[package]] +name = "wapm-toml" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f792aec62b1881c96858549abf402693bd27130e293abb2cddcc849361251571" +dependencies = [ + "anyhow", + "semver", + "serde", + "serde_cbor", + "serde_derive", + "serde_json", + "serde_yaml", + "thiserror", + "toml", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -1289,6 +1318,7 @@ dependencies = [ "ureq", "url", "wapm-targz-to-pirita", + "wapm-toml", "wasmer-pack", "webc", ] diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index f66c13236bf..06e30587ea3 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -30,6 +30,7 @@ serde_json = "1.0.85" webc = "3.0.1" wasmer-pack = { version = "0.5.0", path = "../wasmer-pack" } wapm-targz-to-pirita = { git = "ssh://git@github.com/wasmerio/pirita.git", version = "0.1.0" } +wapm-toml = "0.3.0" [dev-dependencies] insta = "1.18.2" diff --git a/crates/cli/src/codegen.rs b/crates/cli/src/codegen.rs index 23f97917838..2b67d00c829 100644 --- a/crates/cli/src/codegen.rs +++ b/crates/cli/src/codegen.rs @@ -17,7 +17,7 @@ impl Codegen { pub fn run(self, language: Language) -> Result<(), Error> { let Codegen { out_dir, input } = self; - let pkg = crate::load_pirita_file(&input)?; + let pkg = crate::pirita::load(&input)?; let files = match language { Language::JavaScript => wasmer_pack::generate_javascript(&pkg)?, diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index 0af0171b248..ba3045fb281 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -6,5 +6,3 @@ pub use crate::{ codegen::{Codegen, Language}, show::{Format, Show}, }; - -pub(crate) use crate::pirita::load_pirita_file; diff --git a/crates/cli/src/pirita.rs b/crates/cli/src/pirita.rs index 33e6e110137..2f3170095ab 100644 --- a/crates/cli/src/pirita.rs +++ b/crates/cli/src/pirita.rs @@ -1,15 +1,21 @@ -use std::path::Path; +use std::path::{Path, PathBuf}; use anyhow::{Context, Error}; +use wapm_targz_to_pirita::TransformManifestFunctions; use wasmer_pack::{Command, Interface, Library, Metadata, Module, Package}; use webc::{Manifest, ParseOptions, WebC, WebCOwned}; -pub(crate) fn load_pirita_file(path: &Path) -> Result { - let options = ParseOptions::default(); +pub(crate) fn load(path: &Path) -> Result { + let raw_webc: Vec = if path.is_dir() { + webc_from_dir(path)? + } else if path.extension() == Some("webc".as_ref()) { + std::fs::read(path).with_context(|| format!("Unable to read \"{}\"", path.display()))? + } else { + webc_from_tarball(path)? + }; - let raw = - std::fs::read(path).with_context(|| format!("Unable to read \"{}\"", path.display()))?; - let webc = WebCOwned::parse(raw, &options) + let options = ParseOptions::default(); + let webc = WebCOwned::parse(raw_webc, &options) .with_context(|| format!("Unable to parse \"{}\" as a WEBC file", path.display()))?; let fully_qualified_package_name = webc.get_package_name(); @@ -20,6 +26,41 @@ pub(crate) fn load_pirita_file(path: &Path) -> Result { Ok(Package::new(metadata, libraries, commands)) } +fn webc_from_dir(path: &Path) -> Result, Error> { + if !path.join("wapm.toml").exists() { + anyhow::bail!( + "The \"{}\" directory doesn't contain a \"wapm.tom\" file", + path.display() + ); + } + + todo!("Read all files into a FileMap and call generate_webc_file()"); +} + +fn webc_from_tarball(path: &Path) -> Result, Error> { + let tarball = + std::fs::read(path).with_context(|| format!("Unable to read \"{}\"", path.display()))?; + let files = + wapm_targz_to_pirita::unpack_tar_gz(tarball).context("Unable to unpack the tarball")?; + + wapm_targz_to_pirita::generate_webc_file( + files, + &PathBuf::new(), + None, + &TransformManifestFunctions { + get_atoms_wapm_toml: wapm_toml::get_wapm_atom_file_paths, + get_dependencies: wapm_toml::get_dependencies, + get_package_annotations: wapm_toml::get_package_annotations, + get_modules: wapm_toml::get_modules, + get_commands: wapm_toml::get_commands, + get_manifest_file_names: wapm_toml::get_manifest_file_names, + get_metadata_paths: wapm_toml::get_metadata_paths, + get_bindings: wapm_toml::get_bindings, + get_wapm_manifest_file_name: wapm_toml::get_wapm_manifest_file_name, + }, + ) +} + fn commands(webc: &WebC<'_>, fully_qualified_package_name: &str) -> Result, Error> { let mut commands = Vec::new(); diff --git a/crates/cli/src/show.rs b/crates/cli/src/show.rs index 2a76ebced95..d69630a1bed 100644 --- a/crates/cli/src/show.rs +++ b/crates/cli/src/show.rs @@ -22,7 +22,7 @@ pub struct Show { impl Show { pub fn run(self) -> Result<(), Error> { - let pkg = crate::load_pirita_file(&self.input).context("Unable to load the package")?; + let pkg = crate::pirita::load(&self.input).context("Unable to load the package")?; let summary: Summary = summarize(&pkg);