Skip to content

Commit

Permalink
feat: added *:_default task name (#3690)
Browse files Browse the repository at this point in the history
Fixes #2212
  • Loading branch information
jdx authored Dec 18, 2024
1 parent 829f224 commit d78313e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
8 changes: 5 additions & 3 deletions docs/tasks/file-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ when loaded.
mise-tasks
├── build
└── test
├── _default
├── integration
└── units
```
Expand All @@ -105,9 +106,10 @@ Running `mise tasks` will give the below output:
```text
$ mise tasks
Name Description Source
build .../mise-tasks/build
test:integration .../mise-tasks/test/integration
test:units .../mise-tasks/test/units
build ./mise-tasks/build
test ./mise-tasks/test/_default
test:integration ./mise-tasks/test/integration
test:units ./mise-tasks/test/units
```

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

assert "mise task add --file foo:bar:baz -- echo baz"
assert "mise task add --file foo:bar -- echo bar"
ls -lR .

assert "mise tasks" "foo:bar
foo:bar:baz"
assert "mise task run foo:bar:baz" "baz"
assert "mise task run foo:bar" "bar"
7 changes: 6 additions & 1 deletion src/cli/tasks/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::config::config_file;
use crate::task::Task;
use crate::{config, file};
use eyre::Result;
use std::path::MAIN_SEPARATOR_STR;
use toml_edit::Item;

/// Create a new task
Expand Down Expand Up @@ -69,7 +70,10 @@ pub struct TasksAdd {
impl TasksAdd {
pub fn run(self) -> Result<()> {
if self.file {
let path = Task::task_dir().join(&self.task);
let mut path = Task::task_dir().join(self.task.replace(':', MAIN_SEPARATOR_STR));
if path.is_dir() {
path = path.join("_default");
}
let mut lines = vec![format!(
"#!/usr/bin/env {}",
self.shell.clone().unwrap_or("bash".into())
Expand Down Expand Up @@ -123,6 +127,7 @@ impl TasksAdd {
}
file::create_dir_all(path.parent().unwrap())?;
file::write(&path, lines.join("\n"))?;
file::make_executable(&path)?;
} else {
let path = config::local_toml_config_path();
let mut doc: toml_edit::DocumentMut =
Expand Down
9 changes: 7 additions & 2 deletions src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ impl Task {
}

fn name_from_path(prefix: impl AsRef<Path>, path: impl AsRef<Path>) -> Result<String> {
Ok(path
let name = path
.as_ref()
.strip_prefix(prefix)
.map(|p| match p {
Expand All @@ -476,7 +476,12 @@ fn name_from_path(prefix: impl AsRef<Path>, path: impl AsRef<Path>) -> Result<St
.map(path::Component::as_os_str)
.map(ffi::OsStr::to_string_lossy)
.map(|s| s.replace(':', "_"))
.join(":"))
.join(":");
if let Some(name) = name.strip_suffix(":_default") {
Ok(name.to_string())
} else {
Ok(name)
}
}

fn match_tasks(tasks: &BTreeMap<String, &Task>, td: &TaskDep) -> Result<Vec<Task>> {
Expand Down

0 comments on commit d78313e

Please sign in to comment.