Skip to content

Commit

Permalink
fix: add --json and --app flags
Browse files Browse the repository at this point in the history
- add new --json and --app flag to the build subcommand
- the main --json flag is left to avoir compatibility issues but does nothing

fix #5
  • Loading branch information
chevdor committed Jun 11, 2021
1 parent 23dba7b commit fc406b8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target
.env
.secrets
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "srtool-cli"
version = "0.3.0"
version = "0.4.0"
authors = ["chevdor <chevdor@gmail.com>"]
edition = "2018"

Expand Down
47 changes: 36 additions & 11 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use clap::{crate_version, Clap};
use log::{debug, info};
use opts::*;
use srtool_lib::*;
use std::path::PathBuf;
use std::process::Command;
use std::{env, fs};

Expand Down Expand Up @@ -33,33 +34,57 @@ fn main() {
debug!("Checking what is the latest available tag...");
const ONE_HOUR: u64 = 60 * 60;

// let dev_mode = env::var("DEV").is_ok();
// debug!("Dev mode: {:?}", dev_mode);

// let tag = match dev_mode {
// false => get_image_tag(Some(ONE_HOUR)).expect("Issue getting the image tag"),
// true => String::from("nightly-2021-02-25-dev"),
// };

let tag = get_image_tag(Some(ONE_HOUR)).expect("Issue getting the image tag");

info!("Using {}:{}", image, tag);

let command = match opts.subcmd {
SubCommand::Build(build_opts) => {
println!("Found {tag}, we will be using chevdor/srtool:{tag} for the build", tag = tag);

let app = if build_opts.app { " --app" } else { "" };
let json = if opts.json || build_opts.json { " --json" } else { "" };
let chain = build_opts.package.replace("-runtime", "");
let default_runtime_dir = format!("runtime/{}", chain);
let runtime_dir = build_opts.runtime_dir.unwrap_or_else(|| PathBuf::from(&default_runtime_dir));

debug!("app: '{}'", &app);
debug!("json: '{}'", &json);
debug!("chain: '{}'", &chain);
debug!("default_runtime_dir: '{}'", &default_runtime_dir);
debug!("runtime_dir: '{}'", &runtime_dir.display());

let path = fs::canonicalize(&build_opts.path).unwrap();

format!(
"docker run --name srtool --rm \
-e PACKAGE={package} \
-e RUNTIME_DIR={runtime_dir} \
-e BUILD_OPTS={c_build_opts} \
-e DEFAULT_FEATURES={default_features} \
-e PROFILE={profile} \
-v {dir}:/build \
-v {tmpdir}cargo:/cargo-home \
{image}:{tag} build",
-e PACKAGE={package} \
-e RUNTIME_DIR={runtime_dir} \
-e BUILD_OPTS={c_build_opts} \
-e DEFAULT_FEATURES={default_features} \
-e PROFILE={profile} \
-v {dir}:/build \
-v {tmpdir}cargo:/cargo-home \
{image}:{tag} build{app}{json}",
package = build_opts.package,
dir = path.display(),
tmpdir = env::temp_dir().display(),
image = image,
tag = tag,
runtime_dir = build_opts.runtime_dir.display(),
runtime_dir = runtime_dir.display(),
c_build_opts = build_opts.build_opts.unwrap_or_else(|| String::from("")),
default_features = build_opts.default_features.unwrap_or_else(|| String::from("")),
profile = build_opts.profile,
json = json,
app = app,
)
}
SubCommand::Info(info_opts) => {
Expand All @@ -83,9 +108,9 @@ fn main() {
if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", command.as_str()]).output().expect("failed to execute process");
} else {
let _res =
let _ =
Command::new("sh").arg("-c").arg(command).spawn().expect("failed to execute process").wait_with_output();
};
}
}

#[cfg(test)]
Expand Down
14 changes: 12 additions & 2 deletions cli/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Opts {
#[clap(short, long, default_value = "chevdor/srtool")]
pub image: String,

/// Whether we output json or something for humans
/// This option is DEPRECATED and has no effect
#[clap(short, long)]
pub json: bool,

Expand Down Expand Up @@ -53,6 +53,16 @@ pub struct BuildOpts {
#[clap(long, short, env = "PACKAGE")]
pub package: String,

/// Enable json output, same than the global --json option
#[clap(long, short)]
pub json: bool,

/// Enable the "app" mode which is a mix of json output and
/// outputing progress during the build. This flag is recommended for CI.
/// the json output will be provided as a single line at the end in compact mode.
#[clap(long, short)]
pub app: bool,

/// By default, srtool will work in the current folder.
/// If your project is located in another location, you can pass it here.
#[clap(index = 1, default_value = ".")]
Expand All @@ -61,7 +71,7 @@ pub struct BuildOpts {
/// If your runtime is not in the standard location runtime/<chain_name>
/// you can pass this args to help srtool find it.
#[clap(short, long, env = "RUNTIME_DIR")]
pub runtime_dir: PathBuf,
pub runtime_dir: Option<PathBuf>,

/// You may pass options to cargo directly here. WARNING, if you pass
/// this value, the automatic build options for Kusama and Polkadot will
Expand Down

0 comments on commit fc406b8

Please sign in to comment.