Skip to content

Commit

Permalink
fix: bug with mise task edit and new tasks
Browse files Browse the repository at this point in the history
Fixes #3500
  • Loading branch information
jdx committed Dec 13, 2024
1 parent 5fc9d9d commit b6e3694
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
27 changes: 18 additions & 9 deletions src/cli/tasks/edit.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use eyre::Result;

use crate::config::Config;
use crate::task::Task;
use crate::{env, file};
use crate::{dirs, env, file};
use eyre::Result;
use indoc::formatdoc;

/// Edit a tasks with $EDITOR
///
Expand All @@ -22,22 +22,22 @@ pub struct TasksEdit {
impl TasksEdit {
pub fn run(self) -> Result<()> {
let config = Config::try_get()?;
let cwd = dirs::CWD.clone().unwrap_or_default();
let project_root = config.project_root.clone().unwrap_or(cwd);
let path = Task::task_dir().join(&self.task);

let task = config
.tasks_with_aliases()?
.remove(&self.task)
.cloned()
.or_else(|| Task::from_path(&path, path.parent().unwrap(), &project_root).ok())
.map_or_else(
|| {
let project_root = config.project_root.clone().unwrap_or(env::current_dir()?);
let path = project_root.join(".mise").join("tasks").join(&self.task);
Task::from_path(&path, path.parent().unwrap(), &project_root)
},
|| Task::new(&path, path.parent().unwrap(), &project_root),
Ok,
)?;
let file = &task.config_source;
if !file.exists() {
file::create(file)?;
file::write(file, default_task())?;
file::make_executable(file)?;
}
if self.path {
Expand All @@ -50,6 +50,15 @@ impl TasksEdit {
}
}

fn default_task() -> String {
formatdoc!(
r#"#!/usr/bin/env bash
set -euxo pipefail
"#
)
}

static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
Expand Down
23 changes: 22 additions & 1 deletion src/task/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::config::config_file::toml::{deserialize_arr, TomlParser};
use crate::config::Config;
use crate::file;
use crate::task::task_script_parser::{
has_any_args_defined, replace_template_placeholders_with_args, TaskScriptParser,
};
use crate::tera::{get_tera, BASE_CONTEXT};
use crate::ui::tree::TreeItem;
use crate::{dirs, file};
use console::{truncate_str, Color};
use either::Either;
use eyre::{eyre, Result};
Expand Down Expand Up @@ -120,6 +120,15 @@ impl Debug for EitherStringOrIntOrBool {
}

impl Task {
pub fn new(path: &Path, prefix: &Path, config_root: &Path) -> Result<Task> {
Ok(Self {
name: name_from_path(prefix, path)?,
config_source: path.to_path_buf(),
config_root: Some(config_root.to_path_buf()),
..Default::default()
})
}

pub fn from_path(path: &Path, prefix: &Path, config_root: &Path) -> Result<Task> {
let info = file::read_to_string(path)?
.lines()
Expand Down Expand Up @@ -170,6 +179,18 @@ impl Task {
Ok(task)
}

pub fn task_dir() -> PathBuf {
let config = Config::get();
let cwd = dirs::CWD.clone().unwrap_or_default();
let project_root = config.project_root.clone().unwrap_or(cwd);
for dir in config.task_includes_for_dir(&project_root) {
if dir.is_dir() && project_root.join(&dir).exists() {
return project_root.join(dir);
}
}
project_root.join("mise-tasks")
}

// pub fn args(&self) -> impl Iterator<Item = String> {
// if let Some(script) = &self.script {
// // TODO: cli_args
Expand Down

0 comments on commit b6e3694

Please sign in to comment.