Skip to content

Commit

Permalink
new: Support shorthand moon run. (#1065)
Browse files Browse the repository at this point in the history
* Update deps.

* Update args.

* Update blog post.

* Fix syntax.
  • Loading branch information
milesj committed Sep 25, 2023
1 parent 7b41756 commit 3e8695a
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 19 deletions.
33 changes: 24 additions & 9 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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",
Expand Down
51 changes: 46 additions & 5 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<OsString> {
let mut args: Vec<OsString> = vec![];
let mut leading_args: Vec<OsString> = 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);
Expand All @@ -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());
Expand Down
24 changes: 24 additions & 0 deletions crates/cli/tests/run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion nextgen/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
19 changes: 17 additions & 2 deletions website/blog/2023-09-25_moon-v1.14.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!--truncate-->

## 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
Expand Down
1 change: 1 addition & 0 deletions website/docs/commands/run.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions website/docs/run-task.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 3e8695a

Please sign in to comment.