Skip to content

Commit

Permalink
feat: added os option to mise.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Nov 16, 2024
1 parent a47fc30 commit f27f71c
Show file tree
Hide file tree
Showing 15 changed files with 235 additions and 82 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ jobs:
- run: cargo test
continue-on-error: true
- run: mise install
continue-on-error: true
- run: mise test-tool --all
continue-on-error: true
windows-e2e:
Expand Down
2 changes: 1 addition & 1 deletion mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ shfmt = "3"
jq = "latest"
cargo-binstall = "latest"
"cargo:cargo-edit" = "latest"
"cargo:cargo-show" = "latest"
"cargo:cargo-show" = {version = "latest", os = ["linux", "macos"]}
"cargo:cargo-insta" = "latest"
"cargo:git-cliff" = "latest"
"npm:markdownlint-cli" = "latest"
Expand Down
1 change: 1 addition & 0 deletions registry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ updatecli.backends = ["aqua:updatecli/updatecli", "asdf:updatecli/asdf-updatecli
upt.backends = ["asdf:ORCID/asdf-upt"]
upx.backends = ["asdf:jimmidyson/asdf-upx"]
usage.backends = ["ubi:jdx/usage", "asdf:jdx/mise-usage"]
usage.os = ["linux", "macos"]
usql.backends = ["aqua:xo/usql", "asdf:itspngu/asdf-usql"]
uv.backends = ["aqua:astral-sh/uv", "asdf:asdf-community/asdf-uv"]
v.backends = ["asdf:jthegedus/asdf-v"]
Expand Down
8 changes: 4 additions & 4 deletions src/backend/asdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl AsdfBackend {

fn fetch_bin_paths(&self, tv: &ToolVersion) -> Result<Vec<String>> {
let list_bin_paths = self.plugin_path.join("bin/list-bin-paths");
let bin_paths = if matches!(tv.request, ToolRequest::System(..)) {
let bin_paths = if matches!(tv.request, ToolRequest::System { .. }) {
Vec::new()
} else if list_bin_paths.exists() {
let sm = self.script_man_for_tv(tv)?;
Expand Down Expand Up @@ -184,9 +184,9 @@ impl AsdfBackend {
let install_type = match &tv.request {
ToolRequest::Version { .. } | ToolRequest::Prefix { .. } => "version",
ToolRequest::Ref { .. } => "ref",
ToolRequest::Path(..) => "path",
ToolRequest::Path { .. } => "path",
ToolRequest::Sub { .. } => "sub",
ToolRequest::System(..) => {
ToolRequest::System { .. } => {
panic!("should not be called for system tool")
}
};
Expand Down Expand Up @@ -384,7 +384,7 @@ impl Backend for AsdfBackend {
ts: &Toolset,
tv: &ToolVersion,
) -> eyre::Result<BTreeMap<String, String>> {
if matches!(tv.request, ToolRequest::System(..)) {
if matches!(tv.request, ToolRequest::System { .. }) {
return Ok(BTreeMap::new());
}
if !self.plugin.script_man.script_exists(&ExecEnv) || *env::__MISE_SCRIPT {
Expand Down
25 changes: 17 additions & 8 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ pub trait Backend: Debug + Send + Sync {
.collect::<Vec<ABackend>>();
for dep in dep_backends {
// TODO: pass the right tvr
let tvr = ToolRequest::System(dep.id().into(), ToolSource::Unknown);
let tvr = ToolRequest::System {
backend: dep.id().into(),
source: ToolSource::Unknown,
os: None,
};
deps.extend(dep.get_all_dependencies(&tvr)?);
}
Ok(deps.into_iter().collect())
Expand Down Expand Up @@ -242,7 +246,7 @@ pub trait Backend: Debug + Send + Sync {
}
fn is_version_installed(&self, tv: &ToolVersion, check_symlink: bool) -> bool {
match tv.request {
ToolRequest::System(..) => true,
ToolRequest::System { .. } => true,
_ => {
let check_path = |install_path: &Path| {
let is_installed = install_path.exists();
Expand Down Expand Up @@ -338,7 +342,11 @@ pub trait Backend: Debug + Send + Sync {

fn ensure_dependencies_installed(&self) -> eyre::Result<()> {
let deps = self
.get_all_dependencies(&ToolRequest::System(self.id().into(), ToolSource::Unknown))?
.get_all_dependencies(&ToolRequest::System {
backend: self.id().into(),
source: ToolSource::Unknown,
os: None,
})?
.into_iter()
.collect::<HashSet<_>>();
if !deps.is_empty() {
Expand Down Expand Up @@ -474,7 +482,7 @@ pub trait Backend: Debug + Send + Sync {
}
fn list_bin_paths(&self, tv: &ToolVersion) -> eyre::Result<Vec<PathBuf>> {
match tv.request {
ToolRequest::System(..) => Ok(vec![]),
ToolRequest::System { .. } => Ok(vec![]),
_ => Ok(vec![tv.install_path().join("bin")]),
}
}
Expand Down Expand Up @@ -531,10 +539,11 @@ pub trait Backend: Debug + Send + Sync {
fn dependency_toolset(&self) -> eyre::Result<Toolset> {
let config = Config::get();
let dependencies = self
.get_all_dependencies(&ToolRequest::System(
self.name().into(),
ToolSource::Unknown,
))?
.get_all_dependencies(&ToolRequest::System {
backend: self.name().into(),
source: ToolSource::Unknown,
os: None,
})?
.into_iter()
.collect();
let mut ts: Toolset = config
Expand Down
1 change: 1 addition & 0 deletions src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl Install {
let tvr = ToolRequest::Version {
backend: ta.ba.clone(),
version: "latest".into(),
os: None,
options: ta.opts.clone().unwrap_or(Default::default()),
source: ToolSource::Argument,
};
Expand Down
114 changes: 84 additions & 30 deletions src/config/config_file/mise_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct MiseTomlToolList(Vec<MiseTomlTool>);
#[derive(Debug, Clone)]
pub struct MiseTomlTool {
pub tt: ToolVersionType,
pub os: Option<Vec<String>>,
pub options: Option<ToolVersionOptions>,
}

Expand Down Expand Up @@ -313,6 +314,7 @@ impl ConfigFile for MiseToml {
.iter()
.map(|(v, opts)| MiseTomlTool {
tt: ToolVersionType::Version(v.clone()),
os: None, // TODO: use existing os
options: if !output_empty_opts(opts) {
Some(opts.clone())
} else {
Expand Down Expand Up @@ -391,16 +393,16 @@ impl ConfigFile for MiseToml {
trust_check(&self.path)?;
}
let version = self.parse_template(&tool.tt.to_string())?;
if let Some(mut options) = tool.options.clone() {
let mut tvr = if let Some(mut options) = tool.options.clone() {
for v in options.values_mut() {
*v = self.parse_template(v)?;
}
let tvr = ToolRequest::new_opts(fa.clone(), &version, options, source.clone())?;
trs.add_version(tvr, &source);
ToolRequest::new_opts(fa.clone(), &version, options, source.clone())?
} else {
let tvr = ToolRequest::new(fa.clone(), &version, source.clone())?;
trs.add_version(tvr, &source);
}
ToolRequest::new(fa.clone(), &version, source.clone())?
};
tvr = tvr.with_os(tool.os.clone());
trs.add_version(tvr, &source);
}
}
Ok(trs)
Expand Down Expand Up @@ -759,7 +761,11 @@ impl<'de> de::Deserialize<'de> for MiseTomlToolList {
let tt: ToolVersionType = v
.parse()
.map_err(|e| de::Error::custom(format!("invalid tool: {e}")))?;
Ok(MiseTomlToolList(vec![MiseTomlTool { tt, options: None }]))
Ok(MiseTomlToolList(vec![MiseTomlTool {
tt,
os: None,
options: None,
}]))
}

fn visit_seq<S>(self, mut seq: S) -> std::result::Result<Self::Value, S::Error>
Expand All @@ -773,22 +779,44 @@ impl<'de> de::Deserialize<'de> for MiseTomlToolList {
Ok(MiseTomlToolList(tools))
}

fn visit_map<M>(self, map: M) -> std::result::Result<Self::Value, M::Error>
fn visit_map<M>(self, mut map: M) -> std::result::Result<Self::Value, M::Error>
where
M: de::MapAccess<'de>,
{
let mut options: BTreeMap<String, String> =
de::Deserialize::deserialize(de::value::MapAccessDeserializer::new(map))?;
let tt: ToolVersionType = options
.remove("version")
.or_else(|| options.remove("path").map(|p| format!("path:{p}")))
.or_else(|| options.remove("prefix").map(|p| format!("prefix:{p}")))
.or_else(|| options.remove("ref").map(|p| format!("ref:{p}")))
.ok_or_else(|| de::Error::custom("missing version"))?
.parse()
.map_err(de::Error::custom)?;
let mut options: BTreeMap<String, String> = 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>()? {
match k.as_str() {
"version" => {
tt = v.as_str().unwrap().parse().map_err(de::Error::custom)?;
}
"path" | "prefix" | "ref" => {
tt = format!("{k}:{}", v.as_str().unwrap())
.parse()
.map_err(de::Error::custom)?;
}
"os" => match v {
toml::Value::Array(s) => {
os = Some(
s.iter().map(|v| v.as_str().unwrap().to_string()).collect(),
);
}
toml::Value::String(s) => {
options.insert(k, s);
}
_ => {
return Err(de::Error::custom("os must be a string or array"));
}
},
_ => {
options.insert(k, v.to_string());
}
}
}
Ok(MiseTomlToolList(vec![MiseTomlTool {
tt,
os,
options: Some(options),
}]))
}
Expand Down Expand Up @@ -818,25 +846,51 @@ impl<'de> de::Deserialize<'de> for MiseTomlTool {
let tt: ToolVersionType = v
.parse()
.map_err(|e| de::Error::custom(format!("invalid tool: {e}")))?;
Ok(MiseTomlTool { tt, options: None })
Ok(MiseTomlTool {
tt,
os: None,
options: None,
})
}

fn visit_map<M>(self, map: M) -> Result<Self::Value, M::Error>
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
where
M: de::MapAccess<'de>,
{
let mut options: BTreeMap<String, String> =
de::Deserialize::deserialize(de::value::MapAccessDeserializer::new(map))?;
let tt: ToolVersionType = options
.remove("version")
.or_else(|| options.remove("path").map(|p| format!("path:{p}")))
.or_else(|| options.remove("prefix").map(|p| format!("prefix:{p}")))
.or_else(|| options.remove("ref").map(|p| format!("ref:{p}")))
.ok_or_else(|| de::Error::custom("missing version"))?
.parse()
.map_err(de::Error::custom)?;
let mut options: BTreeMap<String, String> = 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>()? {
match k.as_str() {
"version" => {
tt = v.as_str().unwrap().parse().map_err(de::Error::custom)?;
}
"path" | "prefix" | "ref" => {
tt = format!("{k}:{}", v.as_str().unwrap())
.parse()
.map_err(de::Error::custom)?;
}
"os" => match v {
toml::Value::Array(s) => {
os = Some(
s.iter().map(|v| v.as_str().unwrap().to_string()).collect(),
);
}
toml::Value::String(s) => {
options.insert(k, s);
}
_ => {
return Err(de::Error::custom("os must be a string or array"));
}
},
_ => {
options.insert(k, v.to_string());
}
}
}
Ok(MiseTomlTool {
tt,
os,
options: Some(options),
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ToolRequestSet {
source: MiseToml(
"~/fixtures/.mise.toml",
),
os: None,
},
],
BackendArg("node" -> "core:node"): [
Expand All @@ -23,6 +24,7 @@ ToolRequestSet {
source: MiseToml(
"~/fixtures/.mise.toml",
),
os: None,
},
Prefix {
backend: BackendArg("node" -> "core:node"),
Expand All @@ -31,6 +33,7 @@ ToolRequestSet {
source: MiseToml(
"~/fixtures/.mise.toml",
),
os: None,
},
Ref {
backend: BackendArg("node" -> "core:node"),
Expand All @@ -40,14 +43,16 @@ ToolRequestSet {
source: MiseToml(
"~/fixtures/.mise.toml",
),
os: None,
},
Path(
BackendArg("node" -> "core:node"),
"~/.nodes/18",
MiseToml(
Path {
backend: BackendArg("node" -> "core:node"),
path: "~/.nodes/18",
source: MiseToml(
"~/fixtures/.mise.toml",
),
),
os: None,
},
],
BackendArg("jq" -> "aqua:jqlang/jq"): [
Prefix {
Expand All @@ -57,6 +62,7 @@ ToolRequestSet {
source: MiseToml(
"~/fixtures/.mise.toml",
),
os: None,
},
],
BackendArg("shellcheck" -> "ubi:koalaman/shellcheck"): [
Expand All @@ -67,18 +73,20 @@ ToolRequestSet {
source: MiseToml(
"~/fixtures/.mise.toml",
),
os: None,
},
],
BackendArg("python" -> "core:python"): [
Version {
backend: BackendArg("python" -> "core:python"),
version: "3.10.0",
options: {
"venv": ".venv",
"venv": "\".venv\"",
},
source: MiseToml(
"~/fixtures/.mise.toml",
),
os: None,
},
Version {
backend: BackendArg("python" -> "core:python"),
Expand All @@ -87,6 +95,7 @@ ToolRequestSet {
source: MiseToml(
"~/fixtures/.mise.toml",
),
os: None,
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Toolset {
source: MiseToml(
"/tmp/.mise.toml",
),
os: None,
},
Version {
backend: BackendArg("node" -> "core:node"),
Expand All @@ -24,6 +25,7 @@ Toolset {
source: MiseToml(
"/tmp/.mise.toml",
),
os: None,
},
],
source: MiseToml(
Expand Down
Loading

0 comments on commit f27f71c

Please sign in to comment.