Skip to content

Commit

Permalink
feat: added keep-order task output type (jdx#3763)
Browse files Browse the repository at this point in the history
* chore: release 2024.12.16 (jdx#3720)

* feat: add `usage` field to tasks

* feat: added keep-order task output type

Fixes jdx#2347

---------

Co-authored-by: mise-en-dev <release@mise.jdx.dev>
  • Loading branch information
jdx and mise-en-dev authored Dec 21, 2024
1 parent 5d79196 commit d020c05
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 54 deletions.
6 changes: 5 additions & 1 deletion docs/cli/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ Don't show any output except for errors

Change how tasks information is output when running tasks

- `prefix` - Print stdout/stderr by line, prefixed with the task's label - `interleave` - Print directly to stdout/stderr instead of by line - `quiet` - Don't show extra output - `silent` - Don't show any output including stdout and stderr from the task except for errors
- `prefix` - Print stdout/stderr by line, prefixed with the task's label
- `interleave` - Print directly to stdout/stderr instead of by line
- `keep-order` - Print stdout/stderr by line, prefixed with the task's label, but keep the order of the output
- `quiet` - Don't show extra output
- `silent` - Don't show any output including stdout and stderr from the task except for errors

Examples:

Expand Down
6 changes: 5 additions & 1 deletion docs/cli/tasks/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ Don't show any output except for errors

Change how tasks information is output when running tasks

- `prefix` - Print stdout/stderr by line, prefixed with the task's label - `interleave` - Print directly to stdout/stderr instead of by line - `quiet` - Don't show extra output - `silent` - Don't show any output including stdout and stderr from the task except for errors
- `prefix` - Print stdout/stderr by line, prefixed with the task's label
- `interleave` - Print directly to stdout/stderr instead of by line
- `keep-order` - Print stdout/stderr by line, prefixed with the task's label, but keep the order of the output
- `quiet` - Don't show extra output
- `silent` - Don't show any output including stdout and stderr from the task except for errors

Examples:

Expand Down
11 changes: 11 additions & 0 deletions e2e/tasks/test_task_keep_order
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

cat <<EOF >mise.toml
tasks.a = "echo a"
tasks.b = "echo b"
tasks.c = "echo c"
tasks.all.depends = ['a', 'b', 'c']
EOF
assert "mise run -o keep-order all" "[a] a
[b] b
[c] c"
4 changes: 2 additions & 2 deletions mise.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ The name of the script will be the name of the tasks.
flag "-q --quiet" help="Don't show extra output"
flag "-S --silent" help="Don't show any output except for errors"
flag "-o --output" help="Change how tasks information is output when running tasks" {
long_help "Change how tasks information is output when running tasks\n\n- `prefix` - Print stdout/stderr by line, prefixed with the task's label - `interleave` - Print directly to stdout/stderr instead of by line - `quiet` - Don't show extra output - `silent` - Don't show any output including stdout and stderr from the task except for errors"
long_help "Change how tasks information is output when running tasks\n\n- `prefix` - Print stdout/stderr by line, prefixed with the task's label\n- `interleave` - Print directly to stdout/stderr instead of by line\n- `keep-order` - Print stdout/stderr by line, prefixed with the task's label, but keep the order of the output\n- `quiet` - Don't show extra output\n- `silent` - Don't show any output including stdout and stderr from the task except for errors"
arg "<OUTPUT>"
}
mount run="mise tasks --usage"
Expand Down Expand Up @@ -1530,7 +1530,7 @@ The name of the script will be the name of the tasks.
flag "-q --quiet" help="Don't show extra output"
flag "-S --silent" help="Don't show any output except for errors"
flag "-o --output" help="Change how tasks information is output when running tasks" {
long_help "Change how tasks information is output when running tasks\n\n- `prefix` - Print stdout/stderr by line, prefixed with the task's label - `interleave` - Print directly to stdout/stderr instead of by line - `quiet` - Don't show extra output - `silent` - Don't show any output including stdout and stderr from the task except for errors"
long_help "Change how tasks information is output when running tasks\n\n- `prefix` - Print stdout/stderr by line, prefixed with the task's label\n- `interleave` - Print directly to stdout/stderr instead of by line\n- `keep-order` - Print stdout/stderr by line, prefixed with the task's label, but keep the order of the output\n- `quiet` - Don't show extra output\n- `silent` - Don't show any output including stdout and stderr from the task except for errors"
arg "<OUTPUT>"
}
arg "[TASK]" help="Tasks to run\nCan specify multiple tasks by separating with `:::`\ne.g.: mise run task1 arg1 arg2 ::: task2 arg1 arg2" required=false default="default"
Expand Down
2 changes: 1 addition & 1 deletion schema/mise.json
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@
"task_output": {
"description": "Change output style when executing tasks.",
"type": "string",
"enum": ["prefix", "interleave"]
"enum": ["prefix", "interleave", "keep-order", "quiet", "silent"]
},
"task_run_auto_install": {
"default": true,
Expand Down
12 changes: 12 additions & 0 deletions settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,18 @@ enum = [
[
"interleave",
"(default if jobs == 1 or all tasks run sequentially) print output as it comes in"
],
[
"keep-order",
"print output from tasks in the order they are defined"
],
[
"quiet",
"print only stdout/stderr from tasks and nothing from mise"
],
[
"silent",
"print nothing from tasks or mise"
]
]
docs = """
Expand Down
7 changes: 4 additions & 3 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub enum LevelFilter {
Error,
}

#[derive(clap::Parser, Debug)]
#[derive(clap::Parser)]
#[clap(name = "mise", about, long_about = LONG_ABOUT, after_long_help = AFTER_LONG_HELP, author = "Jeff Dickey <@jdx>", arg_required_else_help = true)]
pub struct Cli {
#[clap(subcommand)]
Expand Down Expand Up @@ -160,7 +160,7 @@ pub struct Cli {
pub global_output_flags: CliGlobalOutputFlags,
}

#[derive(Debug, clap::Args)]
#[derive(clap::Args)]
#[group(multiple = false)]
pub struct CliGlobalOutputFlags {
/// Sets log level to debug
Expand All @@ -182,7 +182,7 @@ pub struct CliGlobalOutputFlags {
pub verbose: u8,
}

#[derive(Debug, Subcommand, strum::Display)]
#[derive(Subcommand, strum::Display)]
#[strum(serialize_all = "kebab-case")]
pub enum Commands {
Activate(activate::Activate),
Expand Down Expand Up @@ -388,6 +388,7 @@ impl Cli {
timings: self.timings,
tmpdir: Default::default(),
tool: Default::default(),
keep_order_output: Default::default(),
}));
} else if let Some(cmd) = external::COMMANDS.get(&task) {
external::execute(
Expand Down
83 changes: 76 additions & 7 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use demand::{DemandOption, Select};
use duct::IntoExecutablePath;
use eyre::{bail, ensure, eyre, Result};
use glob::glob;
use indexmap::IndexMap;
use itertools::Itertools;
#[cfg(unix)]
use nix::sys::signal::SIGTERM;
Expand Down Expand Up @@ -57,7 +58,7 @@ use xx::regex;
/// npm run build
/// EOF
/// $ mise run build
#[derive(Debug, clap::Args)]
#[derive(clap::Args)]
#[clap(visible_alias = "r", verbatim_doc_comment, disable_help_flag = true, after_long_help = AFTER_LONG_HELP)]
pub struct Run {
/// Tasks to run
Expand Down Expand Up @@ -172,15 +173,21 @@ pub struct Run {
///
/// - `prefix` - Print stdout/stderr by line, prefixed with the task's label
/// - `interleave` - Print directly to stdout/stderr instead of by line
/// - `keep-order` - Print stdout/stderr by line, prefixed with the task's label, but keep the order of the output
/// - `quiet` - Don't show extra output
/// - `silent` - Don't show any output including stdout and stderr from the task except for errors
#[clap(short, long, env = "MISE_TASK_OUTPUT")]
#[clap(short, long, verbatim_doc_comment, env = "MISE_TASK_OUTPUT")]
pub output: Option<TaskOutput>,

#[clap(skip)]
pub tmpdir: PathBuf,

#[clap(skip)]
pub keep_order_output: Mutex<IndexMap<Task, KeepOrderOutputs>>,
}

type KeepOrderOutputs = (Vec<(String, String)>, Vec<(String, String)>);

impl Run {
pub fn run(mut self) -> Result<()> {
if self.task == "-h" {
Expand Down Expand Up @@ -222,6 +229,12 @@ impl Run {
let tasks = Deps::new(tasks)?;
for task in tasks.all() {
self.validate_task(task)?;
if self.output(Some(task)) == TaskOutput::KeepOrder {
self.keep_order_output
.lock()
.unwrap()
.insert(task.clone(), Default::default());
}
}

let num_tasks = tasks.all().count();
Expand Down Expand Up @@ -311,7 +324,27 @@ impl Run {
eprintln!("{}", style::edim(msg));
};

time!("paralellize_tasks done");
if self.output(None) == TaskOutput::KeepOrder {
// TODO: display these as tasks complete in order somehow rather than waiting until everything is done
let output = self.keep_order_output.lock().unwrap();
for (out, err) in output.values() {
for (prefix, line) in out {
if console::colors_enabled() {
prefix_println!(prefix, "{line}\x1b[0m");
} else {
prefix_println!(prefix, "{line}");
}
}
for (prefix, line) in err {
if console::colors_enabled_stderr() {
prefix_eprintln!(prefix, "{line}\x1b[0m");
} else {
prefix_eprintln!(prefix, "{line}");
}
}
}
}
time!("parallelize_tasks done");

Ok(())
}
Expand Down Expand Up @@ -538,7 +571,42 @@ impl Run {
.raw(self.raw(Some(task)));
cmd.with_pass_signals();
match self.output(Some(task)) {
TaskOutput::Prefix => cmd = cmd.prefix(prefix),
TaskOutput::Prefix => {
cmd = cmd.with_on_stdout(|line| {
if console::colors_enabled() {
prefix_println!(prefix, "{line}\x1b[0m");
} else {
prefix_println!(prefix, "{line}");
}
});
cmd = cmd.with_on_stderr(|line| {
if console::colors_enabled() {
prefix_eprintln!(prefix, "{line}\x1b[0m");
} else {
prefix_eprintln!(prefix, "{line}");
}
});
}
TaskOutput::KeepOrder => {
cmd = cmd.with_on_stdout(|line| {
self.keep_order_output
.lock()
.unwrap()
.get_mut(task)
.unwrap()
.0
.push((prefix.to_string(), line));
});
cmd = cmd.with_on_stderr(|line| {
self.keep_order_output
.lock()
.unwrap()
.get_mut(task)
.unwrap()
.1
.push((prefix.to_string(), line));
});
}
TaskOutput::Silent => {
cmd = cmd.stdout(Stdio::null()).stderr(Stdio::null());
}
Expand Down Expand Up @@ -841,12 +909,13 @@ static AFTER_LONG_HELP: &str = color_print::cstr!(
serde::Serialize,
serde::Deserialize,
)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
#[serde(rename_all = "kebab-case")]
#[strum(serialize_all = "kebab-case")]
pub enum TaskOutput {
Interleave,
KeepOrder,
#[default]
Prefix,
Interleave,
Quiet,
Silent,
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod info;
mod ls;

/// Manage tasks
#[derive(Debug, clap::Args)]
#[derive(clap::Args)]
#[clap(visible_alias = "t", alias = "task", verbatim_doc_comment)]
pub struct Tasks {
#[clap(subcommand)]
Expand All @@ -23,7 +23,7 @@ pub struct Tasks {
ls: ls::TasksLs,
}

#[derive(Debug, Subcommand)]
#[derive(Subcommand)]
enum Commands {
Add(add::TasksAdd),
Deps(deps::TasksDeps),
Expand Down
Loading

0 comments on commit d020c05

Please sign in to comment.