Skip to content

Commit

Permalink
added env._.source feature (#1538)
Browse files Browse the repository at this point in the history
* added `env._.source` feature

* Commit from GitHub Actions (test)

---------

Co-authored-by: mise[bot] <[email protected]>
  • Loading branch information
jdx and mise-en-dev authored Jan 27, 2024
1 parent 76f38ca commit 00b756a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
20 changes: 20 additions & 0 deletions e2e/test_env_source
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
set -euo pipefail
# shellcheck source-path=SCRIPTDIR
source "$(dirname "$0")/assert.sh"

export MISE_EXPERIMENTAL=1

mkdir -p "$(dirname "$MISE_GLOBAL_CONFIG_FILE")" "$MISE_CONFIG_DIR"

cat >"$MISE_GLOBAL_CONFIG_FILE" <<EOF
[env]
_.source = "{{ env.MISE_CONFIG_DIR }}/source.sh"
EOF

cat >"$MISE_CONFIG_DIR/source.sh" <<EOF
#!/usr/bin/env bash
export MISE_TEST_SOURCE=1234
EOF

assert_contains "mise env -s bash" "export MISE_TEST_SOURCE=1234"
4 changes: 2 additions & 2 deletions man/man1/mise.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.TH mise 1 "mise 2024.1.26"
.TH mise 1 "mise 2024.1.27"
.SH NAME
mise \- The front\-end to your dev env
.SH SYNOPSIS
Expand Down Expand Up @@ -179,6 +179,6 @@ Examples:
$ mise settings Show settings in use
$ mise settings set color 0 Disable color by modifying global config file
.SH VERSION
v2024.1.26
v2024.1.27
.SH AUTHORS
Jeff Dickey <@jdx>
24 changes: 22 additions & 2 deletions src/config/env_directive.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::config::config_file::trust_check;
use crate::config::Settings;
use crate::dirs;
use crate::env_diff::{EnvDiff, EnvDiffOperation};
use crate::file::display_path;
use crate::tera::{get_tera, BASE_CONTEXT};
use eyre::Context;
Expand Down Expand Up @@ -60,6 +62,7 @@ impl EnvResults {
initial: &HashMap<String, String>,
input: Vec<(EnvDirective, PathBuf)>,
) -> eyre::Result<Self> {
let settings = Settings::get();
let mut ctx = BASE_CONTEXT.clone();
let mut env = initial
.iter()
Expand All @@ -75,7 +78,11 @@ impl EnvResults {
for (directive, source) in input {
let config_root = source.parent().unwrap();
ctx.insert("config_root", config_root);
ctx.insert("env", &env);
let env_vars = env
.iter()
.map(|(k, (v, _))| (k.clone(), v.clone()))
.collect::<HashMap<_, _>>();
ctx.insert("env", &env_vars);
let normalize_path = |s: String| {
let s = s.strip_prefix("./").unwrap_or(&s);
match s.strip_prefix("~/") {
Expand Down Expand Up @@ -112,11 +119,24 @@ impl EnvResults {
}
}
EnvDirective::Source(input) => {
settings.ensure_experimental()?;
trust_check(&source)?;
let s = r.parse_template(&ctx, &source, input.to_string_lossy().as_ref())?;
let p = normalize_path(s);
r.env_scripts.push(p.clone());
// TODO: run script and apply diff
let env_diff = EnvDiff::from_bash_script(&p, env_vars.clone())?;
for p in env_diff.to_patches() {
match p {
EnvDiffOperation::Add(k, v) | EnvDiffOperation::Change(k, v) => {
r.env_remove.remove(&k);
env.insert(k.clone(), (v.clone(), Some(source.clone())));
}
EnvDiffOperation::Remove(k) => {
env.remove(&k);
r.env_remove.insert(k);
}
}
}
}
};
}
Expand Down

0 comments on commit 00b756a

Please sign in to comment.