Skip to content

Commit

Permalink
feat: render task help via usage (#2760)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored Oct 14, 2024
1 parent 1c1c21c commit b124761
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 30 deletions.
4 changes: 2 additions & 2 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ toml_edit = { version = "0.22", features = ["parse"] }
ubi = { version = "0.2", default-features = false }
url = "2.5.0"
# usage-lib = { path = "../usage/lib", features = ["clap", "docs"] }
usage-lib = { version = "0.10", features = ["clap", "docs"] }
usage-lib = { version = "0.11", features = ["clap", "docs"] }
versions = { version = "6.2.0", features = ["serde"] }
vfox = "0.1"
# vfox = {path="../vfox.rs"}
Expand Down
1 change: 1 addition & 0 deletions e2e/tasks/test_task_run
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ cat <<'EOF' >.mise/tasks/filetask
echo "user=$usage_user"
EOF
assert "mise run filetask --user=jdx" "user=jdx"
assert_contains "mise run filetask --help 2>&1 || true" "-u --user <user>"
2 changes: 1 addition & 1 deletion registry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ unison = ["asdf:susurri/asdf-unison"]
updatecli = ["asdf:updatecli/asdf-updatecli"]
upt = ["asdf:ORCID/asdf-upt"]
upx = ["asdf:jimmidyson/asdf-upx"]
usage = ["asdf:jdx/mise-usage"]
usage = [{full = "asdf:jdx/mise-usage", trust = true}]
usql = ["asdf:itspngu/asdf-usql"]
uv = ["asdf:asdf-community/asdf-uv"]
v = ["asdf:jthegedus/asdf-v"]
Expand Down
68 changes: 45 additions & 23 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::Mutex;
use std::time::SystemTime;

use super::args::ToolArg;
use crate::cli::Cli;
use crate::cmd::CmdLineRunner;
use crate::config::{CONFIG, SETTINGS};
use crate::errors::Error;
Expand Down Expand Up @@ -52,12 +53,16 @@ use nix::sys::signal::SIGTERM;
/// EOF
/// $ mise run build
#[derive(Debug, clap::Args)]
#[clap(visible_alias = "r", verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
#[clap(visible_alias = "r", verbatim_doc_comment, disable_help_flag = true, after_long_help = AFTER_LONG_HELP)]
pub struct Run {
/// Tasks to run
/// Can specify multiple tasks by separating with `:::`
/// e.g.: mise run task1 arg1 arg2 ::: task2 arg1 arg2
#[clap(verbatim_doc_comment, default_value = "default")]
#[clap(
allow_hyphen_values = true,
verbatim_doc_comment,
default_value = "default"
)]
pub task: String,

/// Arguments to pass to the tasks. Use ":::" to separate tasks.
Expand Down Expand Up @@ -117,6 +122,14 @@ pub struct Run {

impl Run {
pub fn run(self) -> Result<()> {
if self.task == "-h" {
self.get_clap_command().print_help()?;
return Ok(());
}
if self.task == "--help" {
self.get_clap_command().print_long_help()?;
return Ok(());
}
SETTINGS.ensure_experimental("`mise run`")?;
time!("run init");
let task_list = self.get_task_lists()?;
Expand All @@ -126,6 +139,14 @@ impl Run {
Ok(())
}

fn get_clap_command(&self) -> clap::Command {
Cli::command()
.get_subcommands()
.find(|s| s.get_name() == "run")
.unwrap()
.clone()
}

fn get_task_lists(&self) -> Result<Vec<Task>> {
once(&self.task)
.chain(self.args.iter())
Expand All @@ -149,7 +170,7 @@ impl Run {
.collect_vec();
if tasks.is_empty() {
if t != "default" {
err_no_task(&t)?;
self.err_no_task(&t)?;
}

Ok(vec![self.prompt_for_task()?])
Expand Down Expand Up @@ -546,30 +567,31 @@ impl Run {
// TODO
Ok(())
}
}

fn err_no_task(name: &str) -> Result<()> {
if let Some(cwd) = &*dirs::CWD {
let includes = CONFIG.task_includes_for_dir(cwd);
let path = includes
.iter()
.map(|d| d.join(name))
.find(|d| d.is_file() && !file::is_executable(d));
if let Some(path) = path {
warn!(
"no task {} found, but a non-executable file exists at {}",
style::ered(name),
display_path(&path)
);
let yn =
prompt::confirm("Mark this file as executable to allow it to be run as a task?")?;
if yn {
file::make_executable(&path)?;
info!("marked as executable, try running this task again");
fn err_no_task(&self, name: &str) -> Result<()> {
if let Some(cwd) = &*dirs::CWD {
let includes = CONFIG.task_includes_for_dir(cwd);
let path = includes
.iter()
.map(|d| d.join(name))
.find(|d| d.is_file() && !file::is_executable(d));
if let Some(path) = path {
warn!(
"no task {} found, but a non-executable file exists at {}",
style::ered(name),
display_path(&path)
);
let yn = prompt::confirm(
"Mark this file as executable to allow it to be run as a task?",
)?;
if yn {
file::make_executable(&path)?;
info!("marked as executable, try running this task again");
}
}
}
bail!("no task {} found", style::ered(name));
}
bail!("no task {} found", style::ered(name));
}

fn is_glob_pattern(path: &str) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ expression: output
"config": {
"props": {}
},
"disable_help": null,
"name": "test",
"usage": "",
"version": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ expression: output
"config": {
"props": {}
},
"disable_help": null,
"name": "filetask",
"usage": "",
"version": null
Expand Down
10 changes: 8 additions & 2 deletions tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ run a command inside of development docker container

## `filetask`

- Depends: lint, build

**Usage**: `filetask [-f --force] [-u --user <user>] <file> <arg_with_default>`

**Aliases**: `ft`
Expand All @@ -82,10 +80,18 @@ This is a test build script

The file to write

##### Default

`file.txt`

#### `<arg_with_default>`

An arg with a default

##### Default

`mydefault`

### Flags

#### `-f --force`
Expand Down
1 change: 0 additions & 1 deletion tasks/filetask
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#USAGE arg "<arg_with_default>" help="An arg with a default" default="mydefault"

#MISE description="This is a test build script"
#MISE depends=["lint", "build"]
#MISE sources=[".test-tool-versions"]
#MISE outputs=["$MISE_PROJECT_ROOT/test/test-build-output.txt"]
#MISE env={TEST_BUILDSCRIPT_ENV_VAR = "VALID"}
Expand Down

0 comments on commit b124761

Please sign in to comment.