Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hide task file extension in completions #3772

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions e2e/tasks/test_task_usage
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

# tests that "test.sh" gets mapped to "test" except when an existing task is there named "test"
assert "mise task add --file test.sh --description sh"
assert "mise task ls" "test sh"
assert "mise task ls --json" "[
{
\"name\": \"test\",
\"description\": \"sh\",
\"source\": \"$PWD/mise-tasks/test.sh\"
}
]"
assert "mise task ls --usage" 'cmd "test" help="sh"'

assert "mise task add --file test --description no-sh"
assert "mise task ls" "test no-sh
test.sh sh"
assert "mise task ls --json" "[
{
\"name\": \"test\",
\"description\": \"no-sh\",
\"source\": \"$PWD/mise-tasks/test\"
},
{
\"name\": \"test.sh\",
\"description\": \"sh\",
\"source\": \"$PWD/mise-tasks/test.sh\"
}
]"
assert "mise task ls --usage" 'cmd "test" help="no-sh"
cmd "test.sh" help="sh"'
2 changes: 1 addition & 1 deletion src/cli/tasks/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl TasksAdd {
lines.push("#MISE alias=[\"".to_string() + &self.alias.join("\", \"") + "\"]");
}
if let Some(description) = &self.description {
lines.push("#MISE description=".to_string() + description);
lines.push("#MISE description=\"".to_string() + description + "\"");
}
if self.dir.is_some() {
lines.push("#MISE dir=".to_string() + &self.dir.unwrap());
Expand Down
7 changes: 3 additions & 4 deletions src/cli/tasks/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use eyre::Result;
use itertools::Itertools;

use crate::config::Config;
use crate::file::{display_path, display_rel_path};
use crate::file::display_rel_path;
use crate::task::Task;
use crate::toolset::Toolset;
use crate::ui::table::MiseTable;
Expand Down Expand Up @@ -108,7 +108,7 @@ impl TasksLs {
usage
.cmd
.subcommands
.insert(task.name.clone(), task_spec.cmd);
.insert(task.display_name(), task_spec.cmd);
}
miseprintln!("{}", usage.to_string());
Ok(())
Expand All @@ -117,7 +117,6 @@ impl TasksLs {
fn display_json(&self, _ts: &Toolset, tasks: Vec<Task>) -> Result<()> {
let array_items = tasks
.into_iter()
.filter(|t| self.hidden || !t.hide)
.map(|task| {
let mut inner = serde_json::Map::new();
inner.insert("name".to_string(), task.display_name().into());
Expand All @@ -130,7 +129,7 @@ impl TasksLs {
inner.insert("description".to_string(), task.description.into());
inner.insert(
"source".to_string(),
display_path(task.config_source).into(),
task.config_source.to_string_lossy().into(),
);
inner
})
Expand Down
32 changes: 24 additions & 8 deletions src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,23 @@ impl Task {

/// prints the task name without an extension
pub fn display_name(&self) -> String {
self.name
let display_name = self
.name
.rsplitn(2, '.')
.last()
.unwrap_or_default()
.to_string()
.to_string();
let config = Config::get();
if config
.tasks()
.map(|t| t.contains_key(&display_name))
.unwrap_or_default()
{
// this means another task has the name without an extension so use the full name
self.name.clone()
} else {
display_name
}
}

pub fn is_match(&self, pat: &str) -> bool {
Expand Down Expand Up @@ -294,22 +306,21 @@ impl Task {
env: &EnvMap,
) -> Result<(usage::Spec, Vec<String>)> {
let (mut spec, scripts) = if let Some(file) = &self.file {
let mut spec = usage::Spec::parse_script(file)
let spec = usage::Spec::parse_script(file)
.inspect_err(|e| debug!("failed to parse task file with usage: {e}"))
.unwrap_or_default();
spec.cmd.name = self.name.clone();
(spec, vec![])
} else {
let (scripts, spec) =
TaskScriptParser::new(cwd).parse_run_scripts(self, self.run(), env)?;
(spec, scripts)
};
spec.name = self.name.clone();
spec.bin = self.name.clone();
spec.name = self.display_name();
spec.bin = self.display_name();
if spec.cmd.help.is_none() {
spec.cmd.help = Some(self.description.clone());
}
spec.cmd.name = self.name.clone();
spec.cmd.name = self.display_name();
spec.cmd.aliases = self.aliases.clone();
if spec.cmd.before_help.is_none()
&& spec.cmd.before_help_long.is_none()
Expand Down Expand Up @@ -371,7 +382,12 @@ impl Task {
Color::Red,
]
});
let idx = self.name.chars().map(|c| c as usize).sum::<usize>() % COLORS.len();
let idx = self
.display_name()
.chars()
.map(|c| c as usize)
.sum::<usize>()
% COLORS.len();
let prefix = style::ereset() + &style::estyle(self.prefix()).fg(COLORS[idx]).to_string();
if *env::MISE_TASK_LEVEL > 0 {
format!(
Expand Down
4 changes: 2 additions & 2 deletions tasks.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## `aqua-tester.fish`
## `aqua-tester`

- **Usage**: `aqua-tester.fish`
- **Usage**: `aqua-tester`

## `build`

Expand Down
Loading