From b91bee8142f4373807aee4b165c88be743f65cd3 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Mon, 5 May 2025 00:26:32 +1000 Subject: [PATCH 1/4] feat: add `mise_env` tera variable for templates Allows the user to query what environment a task is running in. --- docs/templates.md | 1 + e2e/env/test_env_profiles | 8 ++++++++ src/tera.rs | 6 ++++++ 3 files changed, 15 insertions(+) diff --git a/docs/templates.md b/docs/templates.md index 767b123391..736d00c4fc 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -128,6 +128,7 @@ These variables offer key information about the current environment: - `config_root: PathBuf` – Locates the directory containing your `mise.toml` file, or in the case of something like `~/src/myproj/.config/mise.toml`, it will point to `~/src/myproj`. - `mise_bin: String` - Points to the path to the current mise executable - `mise_pid: String` - Points to the pid of the current mise process +- `mise_env: String` - The configuration environment as specified by `MISE_ENV`, `-E`, or `--env` - `xdg_cache_home: PathBuf` - Points to the directory of XDG cache home - `xdg_config_home: PathBuf` - Points to the directory of XDG config home - `xdg_data_home: PathBuf` - Points to the directory of XDG data home diff --git a/e2e/env/test_env_profiles b/e2e/env/test_env_profiles index 2bdcc94a6d..c12a61686d 100644 --- a/e2e/env/test_env_profiles +++ b/e2e/env/test_env_profiles @@ -7,3 +7,11 @@ echo 'env.AAA = "override-2"' >mise.override2.toml assert "mise env --json | jq -r .AAA" "main" MISE_ENV=override1 assert "mise env --json | jq -r .AAA" "override-1" MISE_ENV=override1,override2 assert "mise env --json | jq -r .AAA" "override-2" + +cat <mise.toml +[tasks.print] +run = 'echo {{mise_env}}' +EOF +assert "mise run print" "" +MISE_ENV=env1 assert "mise run print" "[env1]" +MISE_ENV=env1,env2 assert "mise run print" "[env1, env2]" diff --git a/src/tera.rs b/src/tera.rs index a5154d2ed1..ece3233d27 100644 --- a/src/tera.rs +++ b/src/tera.rs @@ -22,6 +22,7 @@ pub static BASE_CONTEXT: Lazy = Lazy::new(|| { context.insert("env", &*env::PRISTINE_ENV); context.insert("mise_bin", &*env::MISE_BIN); context.insert("mise_pid", &*env::MISE_PID); + context.insert("mise_env", &*env::MISE_ENV); if let Ok(dir) = env::current_dir() { context.insert("cwd", &dir); } @@ -381,6 +382,11 @@ mod tests { assert_eq!(render("{{config_root}}"), "/"); } + #[test] + fn test_mise_env() { + assert_eq!(render("{{mise_env}}"), "[]"); + } + #[test] fn test_cwd() { assert_eq!(render("{{cwd}}"), "/"); From 7ff8d464229bc81c0be47f560f6b6ce7830ad169 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Mon, 5 May 2025 23:22:42 +1000 Subject: [PATCH 2/4] feat: don't set `mise_env` if configuration environment not set Allows the use of conditionals: ``` {% if mise_env %} echo {{ mise_env }} {% endif %} ``` And easier use of other Tera functions: ``` {% if mise_env %} echo {{ mise_env | last }} {% endif %} ``` --- docs/templates.md | 2 +- e2e/env/test_env_profiles | 2 +- src/tera.rs | 6 ++++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/templates.md b/docs/templates.md index 736d00c4fc..450160082b 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -128,7 +128,7 @@ These variables offer key information about the current environment: - `config_root: PathBuf` – Locates the directory containing your `mise.toml` file, or in the case of something like `~/src/myproj/.config/mise.toml`, it will point to `~/src/myproj`. - `mise_bin: String` - Points to the path to the current mise executable - `mise_pid: String` - Points to the pid of the current mise process -- `mise_env: String` - The configuration environment as specified by `MISE_ENV`, `-E`, or `--env` +- `mise_env: String` - The configuration environment as specified by `MISE_ENV`, `-E`, or `--env`. Will be undefined if the configuration environment is not set. - `xdg_cache_home: PathBuf` - Points to the directory of XDG cache home - `xdg_config_home: PathBuf` - Points to the directory of XDG config home - `xdg_data_home: PathBuf` - Points to the directory of XDG data home diff --git a/e2e/env/test_env_profiles b/e2e/env/test_env_profiles index c12a61686d..e1147cfafe 100644 --- a/e2e/env/test_env_profiles +++ b/e2e/env/test_env_profiles @@ -10,7 +10,7 @@ MISE_ENV=override1,override2 assert "mise env --json | jq -r .AAA" "override-2" cat <mise.toml [tasks.print] -run = 'echo {{mise_env}}' +run = '{% if mise_env %}echo {{mise_env}}{% endif %}' EOF assert "mise run print" "" MISE_ENV=env1 assert "mise run print" "[env1]" diff --git a/src/tera.rs b/src/tera.rs index ece3233d27..1a1b5979b4 100644 --- a/src/tera.rs +++ b/src/tera.rs @@ -22,7 +22,9 @@ pub static BASE_CONTEXT: Lazy = Lazy::new(|| { context.insert("env", &*env::PRISTINE_ENV); context.insert("mise_bin", &*env::MISE_BIN); context.insert("mise_pid", &*env::MISE_PID); - context.insert("mise_env", &*env::MISE_ENV); + if !(&*env::MISE_ENV).is_empty() { + context.insert("mise_env", &*env::MISE_ENV); + } if let Ok(dir) = env::current_dir() { context.insert("cwd", &dir); } @@ -384,7 +386,7 @@ mod tests { #[test] fn test_mise_env() { - assert_eq!(render("{{mise_env}}"), "[]"); + assert_eq!(render("{% if mise_env %}{{mise_env}}{% endif %}"), ""); } #[test] From 223bad4494a09e3062d36183d5d0df81c60c8972 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 5 May 2025 13:30:32 +0000 Subject: [PATCH 3/4] [autofix.ci] apply automated fixes --- src/tera.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tera.rs b/src/tera.rs index 1a1b5979b4..628ab7dfb4 100644 --- a/src/tera.rs +++ b/src/tera.rs @@ -22,7 +22,7 @@ pub static BASE_CONTEXT: Lazy = Lazy::new(|| { context.insert("env", &*env::PRISTINE_ENV); context.insert("mise_bin", &*env::MISE_BIN); context.insert("mise_pid", &*env::MISE_PID); - if !(&*env::MISE_ENV).is_empty() { + if !(*env::MISE_ENV).is_empty() { context.insert("mise_env", &*env::MISE_ENV); } if let Ok(dir) = env::current_dir() { From 38eb2a689cf2c3856256812c3e4c6d29f6be096d Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Mon, 5 May 2025 23:45:45 +1000 Subject: [PATCH 4/4] test: add more test coverage for setting the configuration environment --- e2e/env/test_env_profiles | 2 ++ 1 file changed, 2 insertions(+) diff --git a/e2e/env/test_env_profiles b/e2e/env/test_env_profiles index e1147cfafe..e88d137561 100644 --- a/e2e/env/test_env_profiles +++ b/e2e/env/test_env_profiles @@ -15,3 +15,5 @@ EOF assert "mise run print" "" MISE_ENV=env1 assert "mise run print" "[env1]" MISE_ENV=env1,env2 assert "mise run print" "[env1, env2]" +assert "mise --env env3 run print" "[env3]" +assert "mise -E env3,env4 run print" "[env3, env4]"