-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Add package build/download/unpack commands
Adds a new CLI command for working with packages: * package download: download a package from a registry * build: build a package from a wasmer.toml manifest * unpack: extract package contents
- Loading branch information
Showing
12 changed files
with
468 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use std::path::PathBuf; | ||
|
||
use anyhow::Context; | ||
|
||
/// Build a container from a package manifest. | ||
#[derive(clap::Parser, Debug)] | ||
pub struct CmdPackageBuild { | ||
/// Path of the package or wasmer.toml manifest. | ||
/// | ||
/// Defaults to current directory. | ||
#[clap(short = 'p', long)] | ||
package: Option<PathBuf>, | ||
|
||
/// Output path for the package file. | ||
/// Defaults to current directory + [name]-[version].webc. | ||
#[clap(short = 'o', long)] | ||
out: Option<PathBuf>, | ||
} | ||
|
||
impl CmdPackageBuild { | ||
pub(crate) fn execute(&self) -> Result<(), anyhow::Error> { | ||
let manifest_path = if let Some(p) = &self.package { | ||
if p.is_dir() { | ||
let manifest_path = p.join("wasmer.toml"); | ||
if !manifest_path.is_file() { | ||
anyhow::bail!( | ||
"Specified directory '{}' does not contain a wasmer.toml manifest", | ||
p.display() | ||
); | ||
} | ||
manifest_path | ||
} else if p.is_file() { | ||
p.clone() | ||
} else { | ||
anyhow::bail!( | ||
"Specified path '{}' is not a file or directory", | ||
p.display() | ||
); | ||
} | ||
} else { | ||
let dir = std::env::current_dir().context("could not get current directory")?; | ||
let manifest_path = dir.join("wasmer.toml"); | ||
if !manifest_path.is_file() { | ||
anyhow::bail!( | ||
"Current directory '{}' does not contain a wasmer.toml manifest - specify a path with --package-dir", | ||
dir.display() | ||
); | ||
} | ||
manifest_path | ||
}; | ||
|
||
let pkg = webc::wasmer_package::Package::from_manifest(manifest_path)?; | ||
|
||
let manifest = pkg | ||
.manifest() | ||
.package_annotation::<webc::metadata::annotations::Wapm>("wapm") | ||
.context("could not load package metadata")? | ||
.context("could not find package metadata")?; | ||
|
||
let pkgname = manifest.name.replace('/', "-"); | ||
let name = format!("{}-{}.webc", pkgname, manifest.version,); | ||
let out_path = if let Some(p) = &self.out { | ||
if p.is_dir() { | ||
p.join(name) | ||
} else { | ||
if let Some(parent) = p.parent() { | ||
std::fs::create_dir_all(parent).context("could not create output directory")?; | ||
} | ||
|
||
p.to_owned() | ||
} | ||
} else { | ||
std::env::current_dir() | ||
.context("could not determine current directory")? | ||
.join(name) | ||
}; | ||
|
||
if out_path.exists() { | ||
anyhow::bail!( | ||
"Output path '{}' already exists - specify a different path with -o/--out", | ||
out_path.display() | ||
); | ||
} | ||
|
||
let data = pkg.serialize()?; | ||
std::fs::write(&out_path, &data) | ||
.with_context(|| format!("could not write contents to '{}'", out_path.display()))?; | ||
|
||
eprintln!("Package written to '{}'", out_path.display()); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
/// Download a package from the dev registry. | ||
#[test] | ||
fn test_cmd_package_build() { | ||
let dir = tempfile::tempdir().unwrap(); | ||
let path = dir.path(); | ||
|
||
std::fs::write( | ||
path.join("wasmer.toml"), | ||
r#" | ||
[package] | ||
name = "wasmer/hello" | ||
version = "0.1.0" | ||
description = "hello" | ||
[fs] | ||
"data" = "data" | ||
"#, | ||
) | ||
.unwrap(); | ||
|
||
std::fs::create_dir(path.join("data")).unwrap(); | ||
std::fs::write(path.join("data").join("hello.txt"), "Hello, world!").unwrap(); | ||
|
||
let cmd = CmdPackageBuild { | ||
package: Some(path.to_owned()), | ||
out: Some(path.to_owned()), | ||
}; | ||
|
||
cmd.execute().unwrap(); | ||
|
||
webc::Container::from_disk(path.join("wasmer-hello-0.1.0.webc")).unwrap(); | ||
} | ||
} |
Oops, something went wrong.