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

feat: default task directory config #3238

Merged
merged 2 commits into from
Nov 27, 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
2 changes: 2 additions & 0 deletions docs/tasks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ You can configure how tasks are used in mise with the `[task_config]` section of

```toml
[task_config]
# change the default directory tasks are run from
dir = "{{cwd}}"

# add toml files containing toml tasks, or file tasks to include when looking for tasks
includes = [
Expand Down
9 changes: 9 additions & 0 deletions e2e/tasks/test_task_config_dir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

cat <<EOF >mise.toml
task_config.dir = "{{config_root}}/mywork"
tasks.a.run = "pwd"
EOF

mkdir -p mywork
assert "mise run a" "$(pwd)/mywork"
4 changes: 4 additions & 0 deletions schema/mise.json
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,10 @@
"type": "object",
"additionalProperties": false,
"properties": {
"dir": {
"description": "default directory to run tasks in defined in this file",
"type": "string"
},
"includes": {
"description": "files/directories to include searching for tasks",
"items": {
Expand Down
1 change: 1 addition & 0 deletions src/config/config_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ impl Hash for dyn ConfigFile {
#[derive(Clone, Debug, Default, Deserialize)]
pub struct TaskConfig {
pub includes: Option<Vec<PathBuf>>,
pub dir: Option<String>,
}

#[cfg(test)]
Expand Down
25 changes: 21 additions & 4 deletions src/task/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::config_file::toml::{deserialize_arr, TomlParser};
use crate::config::Config;
use crate::config::{Config, CONFIG};
use crate::file;
use crate::task::task_script_parser::{
has_any_args_defined, replace_template_placeholders_with_args, TaskScriptParser,
Expand All @@ -20,12 +20,14 @@ use std::collections::{BTreeMap, HashSet};
use std::fmt::{Debug, Display, Formatter};
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::{ffi, fmt, path};
use xx::regex;

mod deps;
mod task_script_parser;

use crate::config::config_file::ConfigFile;
use crate::file::display_path;
use crate::ui::style;
pub use deps::Deps;
Expand All @@ -41,6 +43,8 @@ pub struct Task {
#[serde(skip)]
pub config_source: PathBuf,
#[serde(skip)]
pub cf: Option<Arc<dyn ConfigFile>>,
#[serde(skip)]
pub config_root: Option<PathBuf>,
#[serde(default)]
pub depends: Vec<String>,
Expand Down Expand Up @@ -310,9 +314,7 @@ impl Task {
}

pub fn dir(&self) -> Result<Option<PathBuf>> {
if let Some(dir) = &self.dir {
// TODO: memoize
// let dir = self.dir_rendered.get_or_try_init(|| -> Result<PathBuf> {
let render = |dir| {
let mut tera = get_tera(self.config_root.as_deref());
let mut ctx = BASE_CONTEXT.clone();
if let Some(config_root) = &self.config_root {
Expand All @@ -327,10 +329,24 @@ impl Task {
} else {
Ok(Some(dir.clone()))
}
};
if let Some(dir) = &self.dir {
render(dir)
} else if let Some(dir) = self
.cf()
.as_ref()
.and_then(|cf| cf.task_config().dir.clone())
{
render(&dir)
} else {
Ok(self.config_root.clone())
}
}

#[allow(clippy::borrowed_box)]
pub fn cf(&self) -> Option<&Box<dyn ConfigFile>> {
CONFIG.config_files.get(&self.config_source)
}
}

fn name_from_path(prefix: impl AsRef<Path>, path: impl AsRef<Path>) -> Result<String> {
Expand Down Expand Up @@ -368,6 +384,7 @@ impl Default for Task {
description: "".to_string(),
aliases: vec![],
config_source: PathBuf::new(),
cf: None,
config_root: None,
depends: vec![],
wait_for: vec![],
Expand Down