Skip to content

Commit

Permalink
feat: added install_env tool option (jdx#3622)
Browse files Browse the repository at this point in the history
Fixes jdx#1798
  • Loading branch information
jdx authored and miguelmig committed Dec 21, 2024
1 parent 8f33cc7 commit 97a0ad5
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 42 deletions.
9 changes: 9 additions & 0 deletions docs/dev-tools/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,12 @@ mise config set tools.node.version 20
mise config set tools.node.postinstall 'corepack enable'
mise install
```

### `install_env`

`install_env` is a special option that can be used to set environment variables during tool installation:

```toml
[tools]
teleport-ent = { version = "11.3.11", install_env = { TELEPORT_ENT_ARCH = "amd64" } }
```
5 changes: 4 additions & 1 deletion src/backend/asdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,15 @@ impl AsdfBackend {
fn script_man_for_tv(&self, tv: &ToolVersion) -> Result<ScriptManager> {
let config = Config::get();
let mut sm = self.plugin.script_man.clone();
for (key, value) in &tv.request.options() {
for (key, value) in tv.request.options().opts {
let k = format!("RTX_TOOL_OPTS__{}", key.to_uppercase());
sm = sm.with_env(k, value.clone());
let k = format!("MISE_TOOL_OPTS__{}", key.to_uppercase());
sm = sm.with_env(k, value.clone());
}
for (key, value) in tv.request.options().env {
sm = sm.with_env(key, value.clone());
}
if let Some(project_root) = &config.project_root {
let project_root = project_root.to_string_lossy().to_string();
sm = sm.with_env("RTX_PROJECT_ROOT", project_root.clone());
Expand Down
2 changes: 1 addition & 1 deletion src/backend/external_plugin_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn render_cache_key(config: &Config, tv: &ToolVersion, cache_key: &[String]) ->
fn parse_template(config: &Config, tv: &ToolVersion, tmpl: &str) -> eyre::Result<String> {
let mut ctx = BASE_CONTEXT.clone();
ctx.insert("project_root", &config.project_root);
ctx.insert("opts", &tv.request.options());
ctx.insert("opts", &tv.request.options().opts);
get_tera(
config
.project_root
Expand Down
3 changes: 2 additions & 1 deletion src/cli/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl Tool {
if info.tool_options.is_empty() {
miseprintln!("[none]");
} else {
for (k, v) in info.tool_options {
for (k, v) in info.tool_options.opts {
miseprintln!("{k}={v:?}");
}
}
Expand All @@ -179,6 +179,7 @@ impl Tool {
table.push((
"Tool Options:",
info.tool_options
.opts
.into_iter()
.map(|(k, v)| format!("{k}={v:?}"))
.join(","),
Expand Down
68 changes: 57 additions & 11 deletions src/config/config_file/mise_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ impl ConfigFile for MiseToml {
} else {
let mut table = InlineTable::new();
table.insert("version", versions[0].version().into());
for (k, v) in versions[0].options() {
for (k, v) in versions[0].options().opts {
table.insert(k, v.into());
}
tools.insert_formatted(&key, table.into());
Expand All @@ -376,7 +376,7 @@ impl ConfigFile for MiseToml {
} else {
let mut table = InlineTable::new();
table.insert("version", v.to_string().into());
for (k, v) in tr.options() {
for (k, v) in tr.options().opts {
table.insert(k, v.clone().into());
}
arr.push(table);
Expand Down Expand Up @@ -417,7 +417,7 @@ impl ConfigFile for MiseToml {
for tool in &tvp.0 {
let version = self.parse_template(&tool.tt.to_string())?;
let mut tvr = if let Some(mut options) = tool.options.clone() {
for v in options.values_mut() {
for v in options.opts.values_mut() {
*v = self.parse_template(v)?;
}
ToolRequest::new_opts(ba.clone(), &version, options, source.clone())?
Expand Down Expand Up @@ -1040,7 +1040,7 @@ impl<'de> de::Deserialize<'de> for MiseTomlToolList {
where
M: de::MapAccess<'de>,
{
let mut options: BTreeMap<String, String> = Default::default();
let mut options: ToolVersionOptions = Default::default();
let mut os: Option<Vec<String>> = None;
let mut tt: Option<ToolVersionType> = None;
while let Some((k, v)) = map.next_entry::<String, toml::Value>()? {
Expand All @@ -1062,21 +1062,44 @@ impl<'de> de::Deserialize<'de> for MiseTomlToolList {
);
}
toml::Value::String(s) => {
options.insert(k, s);
options.opts.insert(k, s);
}
_ => {
return Err(de::Error::custom("os must be a string or array"));
}
},
"install_env" => match v {
toml::Value::Table(env) => {
for (k, v) in env {
match v {
toml::Value::Boolean(v) => {
options.env.insert(k, v.to_string());
}
toml::Value::Integer(v) => {
options.env.insert(k, v.to_string());
}
toml::Value::String(v) => {
options.env.insert(k, v);
}
_ => {
return Err(de::Error::custom("invalid value type"));
}
}
}
}
_ => {
return Err(de::Error::custom("env must be a table"));
}
},
_ => match v {
toml::Value::Boolean(v) => {
options.insert(k, v.to_string());
options.opts.insert(k, v.to_string());
}
toml::Value::Integer(v) => {
options.insert(k, v.to_string());
options.opts.insert(k, v.to_string());
}
toml::Value::String(v) => {
options.insert(k, v);
options.opts.insert(k, v);
}
_ => {
return Err(de::Error::custom("invalid value type"));
Expand Down Expand Up @@ -1196,7 +1219,7 @@ impl<'de> de::Deserialize<'de> for MiseTomlTool {
where
M: de::MapAccess<'de>,
{
let mut options: BTreeMap<String, String> = Default::default();
let mut options: ToolVersionOptions = Default::default();
let mut os: Option<Vec<String>> = None;
let mut tt = ToolVersionType::System;
while let Some((k, v)) = map.next_entry::<String, toml::Value>()? {
Expand All @@ -1216,14 +1239,37 @@ impl<'de> de::Deserialize<'de> for MiseTomlTool {
);
}
toml::Value::String(s) => {
options.insert(k, s);
options.opts.insert(k, s);
}
_ => {
return Err(de::Error::custom("os must be a string or array"));
}
},
"install_env" => match v {
toml::Value::Table(env) => {
for (k, v) in env {
match v {
toml::Value::Boolean(v) => {
options.env.insert(k, v.to_string());
}
toml::Value::Integer(v) => {
options.env.insert(k, v.to_string());
}
toml::Value::String(v) => {
options.env.insert(k, v);
}
_ => {
return Err(de::Error::custom("invalid value type"));
}
}
}
}
_ => {
return Err(de::Error::custom("env must be a table"));
}
},
_ => {
options.insert(k, v.as_str().unwrap().to_string());
options.opts.insert(k, v.as_str().unwrap().to_string());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ ToolRequestSet {
Version {
backend: BackendArg(terraform),
version: "1.0.0",
options: {},
options: ToolVersionOptions {
env: {},
opts: {},
},
source: MiseToml(
"~/fixtures/.mise.toml",
),
Expand All @@ -20,7 +23,10 @@ ToolRequestSet {
Version {
backend: BackendArg(node),
version: "18",
options: {},
options: ToolVersionOptions {
env: {},
opts: {},
},
source: MiseToml(
"~/fixtures/.mise.toml",
),
Expand All @@ -29,7 +35,10 @@ ToolRequestSet {
Prefix {
backend: BackendArg(node),
prefix: "20",
options: {},
options: ToolVersionOptions {
env: {},
opts: {},
},
source: MiseToml(
"~/fixtures/.mise.toml",
),
Expand All @@ -39,7 +48,10 @@ ToolRequestSet {
backend: BackendArg(node),
ref_: "master",
ref_type: "ref",
options: {},
options: ToolVersionOptions {
env: {},
opts: {},
},
source: MiseToml(
"~/fixtures/.mise.toml",
),
Expand All @@ -48,7 +60,10 @@ ToolRequestSet {
Path {
backend: BackendArg(node),
path: "~/.nodes/18",
options: {},
options: ToolVersionOptions {
env: {},
opts: {},
},
source: MiseToml(
"~/fixtures/.mise.toml",
),
Expand All @@ -59,7 +74,10 @@ ToolRequestSet {
Prefix {
backend: BackendArg(jq),
prefix: "1.6",
options: {},
options: ToolVersionOptions {
env: {},
opts: {},
},
source: MiseToml(
"~/fixtures/.mise.toml",
),
Expand All @@ -70,7 +88,10 @@ ToolRequestSet {
Version {
backend: BackendArg(shellcheck),
version: "0.9.0",
options: {},
options: ToolVersionOptions {
env: {},
opts: {},
},
source: MiseToml(
"~/fixtures/.mise.toml",
),
Expand All @@ -81,8 +102,11 @@ ToolRequestSet {
Version {
backend: BackendArg(python),
version: "3.10.0",
options: {
"venv": ".venv",
options: ToolVersionOptions {
env: {},
opts: {
"venv": ".venv",
},
},
source: MiseToml(
"~/fixtures/.mise.toml",
Expand All @@ -92,7 +116,10 @@ ToolRequestSet {
Version {
backend: BackendArg(python),
version: "3.9.0",
options: {},
options: ToolVersionOptions {
env: {},
opts: {},
},
source: MiseToml(
"~/fixtures/.mise.toml",
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ Toolset {
Version {
backend: BackendArg(node),
version: "16.0.1",
options: {},
options: ToolVersionOptions {
env: {},
opts: {},
},
source: MiseToml(
"/tmp/.mise.toml",
),
Expand All @@ -21,7 +24,10 @@ Toolset {
Version {
backend: BackendArg(node),
version: "18.0.1",
options: {},
options: ToolVersionOptions {
env: {},
opts: {},
},
source: MiseToml(
"/tmp/.mise.toml",
),
Expand Down
Loading

0 comments on commit 97a0ad5

Please sign in to comment.