From 3e8695a289c94b96406a4ce17a6483f9a6717ff9 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Thu, 21 Sep 2023 21:03:36 -0700 Subject: [PATCH] new: Support shorthand `moon run`. (#1065) * Update deps. * Update args. * Update blog post. * Fix syntax. --- Cargo.lock | 33 ++++++++++++----- Cargo.toml | 5 ++- crates/cli/src/lib.rs | 51 +++++++++++++++++++++++--- crates/cli/tests/run_test.rs | 24 ++++++++++++ nextgen/config/Cargo.toml | 2 +- packages/cli/CHANGELOG.md | 3 ++ website/blog/2023-09-25_moon-v1.14.mdx | 19 +++++++++- website/docs/commands/run.mdx | 1 + website/docs/run-task.mdx | 3 ++ 9 files changed, 122 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 690abd84baf..36e40a694ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1678,9 +1678,9 @@ checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" [[package]] name = "extism" -version = "0.5.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25944ee40692db74f53a65804c08f23b88c4f2b0903f3098e94bfcf6cbfd3d0f" +checksum = "7a94848d5b49906bd97b83cf5a8bd25082dbc6f8bdfe98f12687910228734552" dependencies = [ "anyhow", "extism-manifest", @@ -1702,9 +1702,9 @@ dependencies = [ [[package]] name = "extism-runtime" -version = "0.5.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b284fa52d9c9eed7c26bfaa348183f1c83f08e080859a392b481c70f3c0f848e" +checksum = "a3b0ba8ef6ecbf59c0f6e47fd2feea575ebc3a09e81603d06a41af92fe61cdfa" dependencies = [ "anyhow", "cbindgen", @@ -3337,6 +3337,7 @@ dependencies = [ "serde_yaml 0.9.25", "starbase_sandbox", "tracing", + "version_spec", ] [[package]] @@ -4688,9 +4689,9 @@ dependencies = [ [[package]] name = "proto_core" -version = "0.18.3" +version = "0.18.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a321436d096a0dd86c95f8a735045ce5f36c9783174be5d193d1004298de54e4" +checksum = "dec55eb026f4887cdf8ae1b6de17c0b96f4c83c50f1cdfea325468f3b3ff2fcc" dependencies = [ "cached", "extism", @@ -4701,7 +4702,6 @@ dependencies = [ "proto_wasm_plugin", "regex", "reqwest", - "schematic", "semver", "serde", "serde_json", @@ -4714,6 +4714,7 @@ dependencies = [ "tinytemplate", "tracing", "url", + "version_spec", "warpgate", ] @@ -5122,9 +5123,9 @@ dependencies = [ [[package]] name = "schematic" -version = "0.11.6" +version = "0.11.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8592df69240651bac26079f142ab7c796ed888f7b5c0c4cddb5efc34149ca79b" +checksum = "fe561cb5c8380d36ef80bf81a7cc16acfde8a0d130dd3394cc0cb48809d065ab" dependencies = [ "garde", "indexmap 2.0.0", @@ -6421,6 +6422,20 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "version_spec" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf76e050d169eab93ec6369ec26d60748d48f2f2edb59ed8f357e7d7242e809c" +dependencies = [ + "human-sort", + "once_cell", + "regex", + "schematic", + "semver", + "serde", +] + [[package]] name = "void" version = "1.0.2" diff --git a/Cargo.toml b/Cargo.toml index be3f1b13d17..6863720b6f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,7 @@ pathdiff = "0.2.1" petgraph = { version = "0.6.4", default-features = false, features = [ "serde-1", ] } -proto_core = { version = "0.18.3", features = ["schematic"] } +proto_core = "0.18.4" relative-path = { version = "1.9.0", features = ["serde"] } regex = "1.9.5" reqwest = { version = "0.11.20", default-features = false, features = [ @@ -47,8 +47,9 @@ reqwest = { version = "0.11.20", default-features = false, features = [ "native-tls-vendored", ] } rustc-hash = "1.1.0" -schematic = { version = "0.11.6", default-features = false, features = [ +schematic = { version = "0.11.7", default-features = false, features = [ "schema", + "url", "yaml", "type_semver", "type_warpgate", diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index 98a97fb436c..dc23eb7d190 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -37,6 +37,7 @@ use starbase::{tracing::TracingOptions, App, AppResult}; use starbase_styles::color; use starbase_utils::string_vec; use std::env; +use std::ffi::OsString; use tracing::debug; pub use app::BIN_NAME; @@ -59,23 +60,63 @@ fn setup_caching(mode: &CacheMode) { } } -fn detect_running_version() { +fn detect_running_version(args: &[OsString]) { let version = env!("CARGO_PKG_VERSION"); if let Ok(exe_with) = env::var("MOON_EXECUTED_WITH") { - debug!("Running moon v{} (with {})", version, color::file(exe_with)); + debug!( + args = ?args, + "Running moon v{} (with {})", + version, + color::file(exe_with) + ); } else { - debug!("Running moon v{}", version); + debug!(args = ?args, "Running moon v{}", version); } env::set_var("MOON_VERSION", version); } +fn gather_args() -> Vec { + let mut args: Vec = vec![]; + let mut leading_args: Vec = vec![]; + let mut check_for_target = true; + + env::args_os().enumerate().for_each(|(index, arg)| { + if let Some(a) = arg.to_str() { + // Script being executed, so persist it + if index == 0 && a.ends_with(BIN_NAME) { + leading_args.push(arg); + return; + } + + // Find first non-option value + if check_for_target && !a.starts_with('-') { + check_for_target = false; + + // Looks like a target, but is not `run`, so prepend! + if a.contains(':') { + leading_args.push(OsString::from("run")); + } + } + } + + args.push(arg); + }); + + // We need a separate args list because options before the + // target cannot be placed before "run" + leading_args.extend(args); + + leading_args +} + pub async fn run_cli() -> AppResult { App::setup_diagnostics(); // Create app and parse arguments - let cli = CLI::parse(); + let args = gather_args(); + let cli = CLI::parse_from(&args); setup_colors(cli.color); setup_logging(&cli.log); @@ -89,7 +130,7 @@ pub async fn run_cli() -> AppResult { ..TracingOptions::default() }); - detect_running_version(); + detect_running_version(&args); let mut app = App::new(); app.set_state(cli.global_args()); diff --git a/crates/cli/tests/run_test.rs b/crates/cli/tests/run_test.rs index 3f8eef4cd3e..7b372eb3106 100644 --- a/crates/cli/tests/run_test.rs +++ b/crates/cli/tests/run_test.rs @@ -111,6 +111,30 @@ fn creates_run_report() { assert!(sandbox.path().join(".moon/cache/runReport.json").exists()); } +#[test] +fn runs_with_shorthand_syntax() { + let sandbox = cases_sandbox(); + sandbox.enable_git(); + + sandbox + .run_moon(|cmd| { + cmd.arg("base:standard"); + }) + .success(); +} + +#[test] +fn runs_with_shorthand_syntax_with_leading_option() { + let sandbox = cases_sandbox(); + sandbox.enable_git(); + + sandbox + .run_moon(|cmd| { + cmd.arg("--force").arg("base:standard"); + }) + .success(); +} + #[test] fn bails_on_failing_task() { let sandbox = cases_sandbox(); diff --git a/nextgen/config/Cargo.toml b/nextgen/config/Cargo.toml index bca3a2bf36c..f297483dda5 100644 --- a/nextgen/config/Cargo.toml +++ b/nextgen/config/Cargo.toml @@ -27,13 +27,13 @@ rustc-hash = { workspace = true } schematic = { workspace = true, features = [ "json_schema", "typescript", - "yaml", "valid_url", ] } serde = { workspace = true } serde_json = { workspace = true } serde_yaml = { workspace = true } tracing = { workspace = true } +version_spec = { version = "0.1.0", features = ["schematic"] } [dev-dependencies] httpmock = "0.6.8" diff --git a/packages/cli/CHANGELOG.md b/packages/cli/CHANGELOG.md index 585708d2595..2a78f6ea3b1 100644 --- a/packages/cli/CHANGELOG.md +++ b/packages/cli/CHANGELOG.md @@ -14,6 +14,9 @@ #### 🚀 Updates +- Added a `moon run` shorthand, where "run" can be omitted. For example, `moon run app:build` can be + written as `moon app:build`. + - This only works for targets that contain a `:`. - Updated `moon ci` to support running an explicit list of targets, instead of running everything. - Updated `node.version`, `npm.version`, `pnpm.version`, `yarn.version`, and `rust.version` to support partial versions and requirements/ranges like `1.2`, `1`, or `^1.2`. diff --git a/website/blog/2023-09-25_moon-v1.14.mdx b/website/blog/2023-09-25_moon-v1.14.mdx index d435a28dc61..572148659b6 100644 --- a/website/blog/2023-09-25_moon-v1.14.mdx +++ b/website/blog/2023-09-25_moon-v1.14.mdx @@ -2,14 +2,29 @@ slug: moon-v1.14 title: moon v1.14 - ??? authors: [milesj] -tags: [] +tags: [run, target, toolchain, version] # image: ./img/moon/v1.13.png --- -??? +In this release, we've focused on loosening restrictions to improve the overall developer +experience. +## Shorthand for `moon run` + +This has been a long requested feature and we're happy to finally deliver it! You can now omit the +"run" keyword for the [`moon run`](/docs/commands/run) command, just so long as the first non-option +argument is a target (must contain a `:`). + +```shell +# v1.13 +$ moon run app:build + +# v1.14+ +$ moon app:build +``` + ## Configure partial toolchain versions Since moon's inception, our integrated toolchain required fully qualified semantic versions diff --git a/website/docs/commands/run.mdx b/website/docs/commands/run.mdx index dc8174de005..fad38a8622a 100644 --- a/website/docs/commands/run.mdx +++ b/website/docs/commands/run.mdx @@ -11,6 +11,7 @@ and development times... over time. ```shell # Run `lint` in project `app` $ moon run app:lint +$ moon app:lint # Run `dev` in project `client` and `server` $ moon run client:dev server:dev diff --git a/website/docs/run-task.mdx b/website/docs/run-task.mdx index 7310c14d8f0..df4cbd5b2fb 100644 --- a/website/docs/run-task.mdx +++ b/website/docs/run-task.mdx @@ -16,6 +16,9 @@ below, our project is `app`, the task is `build`, and the target is `app:build`. ```shell $ moon run app:build + +# In v1.14+, "run" can be omitted +$ moon app:build ``` When this command is ran, it will do the following: