enable optional arguments with cargo build (#10)

This commit is contained in:
vms 2020-07-17 15:03:46 +03:00 committed by GitHub
parent e03a84551d
commit e46c5e08b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 22 deletions

View File

@ -54,12 +54,12 @@ jobs:
cd examples/ipfs_node/wasm/ipfs_node cd examples/ipfs_node/wasm/ipfs_node
cargo fmt --all -- --check --color always cargo fmt --all -- --check --color always
cargo wasi build cargo wasi build
cargo clippy -v cargo clippy -v --target wasm32-wasi
cd ../ipfs_rpc cd ../ipfs_rpc
cargo fmt --all -- --check --color always cargo fmt --all -- --check --color always
cargo wasi build cargo wasi build
cargo clippy -v cargo clippy -v --target wasm32-wasi
# cd ../../../../tools/wit_embedder # cd ../../../../tools/wit_embedder
# cargo fmt --all -- --check --color always # cargo fmt --all -- --check --color always

View File

@ -25,15 +25,15 @@ pub const IN_WASM_PATH: &str = "in-wasm-path";
pub fn build<'a, 'b>() -> App<'a, 'b> { pub fn build<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("build") SubCommand::with_name("build")
.about("build provided Rust project to Wasm") .about("build provided Rust project to Wasm")
.args(&[Arg::with_name(IN_WASM_PATH) .setting(clap::AppSettings::TrailingVarArg)
.takes_value(true) .setting(clap::AppSettings::AllowLeadingHyphen)
.short("i") .arg(Arg::from_usage("[optional]... 'cargo build arguments'").multiple(true))
.help("path to a Cargo.toml file")])
} }
pub fn show_wit<'a, 'b>() -> App<'a, 'b> { pub fn show_wit<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("show") SubCommand::with_name("show")
.about("show WIT in provided Wasm file") .about("show WIT in provided Wasm file")
.setting(clap::AppSettings::ArgRequiredElseHelp)
.args(&[Arg::with_name(IN_WASM_PATH) .args(&[Arg::with_name(IN_WASM_PATH)
.required(true) .required(true)
.takes_value(true) .takes_value(true)

View File

@ -17,7 +17,6 @@
use crate::Result; use crate::Result;
use crate::errors::CLIError; use crate::errors::CLIError;
use std::path::PathBuf;
use std::process::Command; use std::process::Command;
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
@ -29,15 +28,13 @@ enum DiagnosticMessage {
RunWithArgs, RunWithArgs,
} }
pub(crate) fn build(manifest_path: Option<PathBuf>) -> Result<()> { pub(crate) fn build(trailing_args: Vec<&str>) -> Result<()> {
use std::io::Read; use std::io::Read;
let mut cargo = Command::new("cargo"); let mut cargo = Command::new("cargo");
cargo.arg("build").arg("--target").arg("wasm32-wasi"); cargo.arg("build").arg("--target").arg("wasm32-wasi");
cargo.arg("--message-format").arg("json-render-diagnostics"); cargo.arg("--message-format").arg("json-render-diagnostics");
if let Some(wasm_path) = manifest_path { cargo.args(trailing_args);
cargo.arg("--manifest-path").arg(wasm_path);
}
let mut process = cargo.stdout(std::process::Stdio::piped()).spawn()?; let mut process = cargo.stdout(std::process::Stdio::piped()).spawn()?;

View File

@ -29,27 +29,22 @@ mod args;
mod build; mod build;
mod errors; mod errors;
use clap::App;
use clap::AppSettings;
pub(crate) type Result<T> = std::result::Result<T, crate::errors::CLIError>; pub(crate) type Result<T> = std::result::Result<T, crate::errors::CLIError>;
pub fn main() -> Result<()> { pub fn main() -> Result<()> {
let app = App::new("CLI tool for embedding WIT to provided Wasm file") let app = clap::App::new("CLI tool for embedding WIT to provided Wasm file")
.version(args::VERSION) .version(args::VERSION)
.author(args::AUTHORS) .author(args::AUTHORS)
.about(args::DESCRIPTION) .about(args::DESCRIPTION)
.setting(AppSettings::ArgRequiredElseHelp)
.subcommand(args::build()) .subcommand(args::build())
.subcommand(args::show_wit()); .subcommand(args::show_wit());
let arg_matches = app.get_matches();
match app.get_matches().subcommand() { match arg_matches.subcommand() {
("build", Some(arg)) => { ("build", Some(args)) => {
let manifest_path = arg let trailing_args: Vec<&str> = args.values_of("optional").unwrap_or_default().collect();
.value_of(args::IN_WASM_PATH)
.map(std::path::PathBuf::from);
crate::build::build(manifest_path) crate::build::build(trailing_args)
} }
("show", Some(arg)) => { ("show", Some(arg)) => {
let wasm_path = arg.value_of(args::IN_WASM_PATH).unwrap(); let wasm_path = arg.value_of(args::IN_WASM_PATH).unwrap();