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
cargo fmt --all -- --check --color always
cargo wasi build
cargo clippy -v
cargo clippy -v --target wasm32-wasi
cd ../ipfs_rpc
cargo fmt --all -- --check --color always
cargo wasi build
cargo clippy -v
cargo clippy -v --target wasm32-wasi
# cd ../../../../tools/wit_embedder
# 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> {
SubCommand::with_name("build")
.about("build provided Rust project to Wasm")
.args(&[Arg::with_name(IN_WASM_PATH)
.takes_value(true)
.short("i")
.help("path to a Cargo.toml file")])
.setting(clap::AppSettings::TrailingVarArg)
.setting(clap::AppSettings::AllowLeadingHyphen)
.arg(Arg::from_usage("[optional]... 'cargo build arguments'").multiple(true))
}
pub fn show_wit<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("show")
.about("show WIT in provided Wasm file")
.setting(clap::AppSettings::ArgRequiredElseHelp)
.args(&[Arg::with_name(IN_WASM_PATH)
.required(true)
.takes_value(true)

View File

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

View File

@ -29,27 +29,22 @@ mod args;
mod build;
mod errors;
use clap::App;
use clap::AppSettings;
pub(crate) type Result<T> = std::result::Result<T, crate::errors::CLIError>;
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)
.author(args::AUTHORS)
.about(args::DESCRIPTION)
.setting(AppSettings::ArgRequiredElseHelp)
.subcommand(args::build())
.subcommand(args::show_wit());
let arg_matches = app.get_matches();
match app.get_matches().subcommand() {
("build", Some(arg)) => {
let manifest_path = arg
.value_of(args::IN_WASM_PATH)
.map(std::path::PathBuf::from);
match arg_matches.subcommand() {
("build", Some(args)) => {
let trailing_args: Vec<&str> = args.values_of("optional").unwrap_or_default().collect();
crate::build::build(manifest_path)
crate::build::build(trailing_args)
}
("show", Some(arg)) => {
let wasm_path = arg.value_of(args::IN_WASM_PATH).unwrap();