Skip to content

Commit

Permalink
feat: improve dynamic settings
Browse files Browse the repository at this point in the history
Main functionality this provides is making `mise settings set` dynamic so we no longer need to manually specify each setting for that command.
  • Loading branch information
jdx committed Oct 12, 2024
1 parent da8d62d commit 063daea
Show file tree
Hide file tree
Showing 28 changed files with 408 additions and 290 deletions.
5 changes: 4 additions & 1 deletion .mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
min_version = "2024.1.1"
[env]
_.file = [".env"]
_.path = ["./target/debug"]
_.path = ["./target/debug", "./node_modules/.bin"]
FOO = "bar"
FOO_NUM = 1
THIS_PROJECT = "{{config_root}}-{{cwd}}"
Expand Down Expand Up @@ -75,6 +75,9 @@ mise completion fish > completions/mise.fish
depends = ["build"]
run = "./scripts/render-registry.js"

[tasks."render:settings"]
run = "tsx tasks/render/settings.ts"

[tasks."render:mangen"]
depends = ["build"]
env = { NO_COLOR = "1" }
Expand Down
63 changes: 59 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,33 @@ pub struct Settings {"#
if let Some(description) = props.get("description") {
lines.push(format!(" /// {}", description.as_str().unwrap()));
}
if let Some(type_) = props.get("type") {
let type_ = props
.get("rust_type")
.map(|rt| rt.as_str().unwrap())
.or(props.get("type").map(|t| match t.as_str().unwrap() {
"Bool" => "bool",
"String" => "String",
"Integer" => "i64",
"Url" => "String",
"Path" => "PathBuf",
"Duration" => "String",
"ListString" => "Vec<String>",
"ListPath" => "Vec<PathBuf>",
t => panic!("Unknown type: {}", t),
}));
if let Some(type_) = type_ {
let type_ = if props.get("optional").is_some_and(|v| v.as_bool().unwrap()) {
format!("Option<{}>", type_)
} else {
type_.to_string()
};
let mut opts = IndexMap::new();
if let Some(env) = props.get("env") {
opts.insert("env".to_string(), env.to_string());
}
if let Some(default) = props.get("default") {
opts.insert("default".to_string(), default.to_string());
} else if type_.as_str().unwrap() == "bool" {
} else if type_ == "bool" {
opts.insert("default".to_string(), "false".to_string());
}
if let Some(parse_env) = props.get("parse_env") {
Expand All @@ -56,7 +75,7 @@ pub struct Settings {"#
.collect::<Vec<_>>()
.join(", ")
));
lines.push(format!(" pub {}: {},", key, type_.as_str().unwrap()));
lines.push(format!(" pub {}: {},", key, type_));
} else {
lines.push(" #[config(nested)]".to_string());
lines.push(format!(
Expand All @@ -76,7 +95,7 @@ pub struct Settings {"#
.iter()
.filter(|(_, v)| !v.as_table().unwrap().contains_key("type"))
.collect::<Vec<_>>();
for (child, props) in nested_settings {
for (child, props) in &nested_settings {
lines.push(format!(
r#"#[derive(Config, Default, Debug, Clone, Serialize)]
#[config(partial_attr(derive(Clone, Serialize, Default)))]
Expand All @@ -92,5 +111,41 @@ pub struct Settings{name} {{
lines.push("}".to_string());
}

lines.push(
r#"
pub static SETTINGS_META: Lazy<IndexMap<String, SettingsMeta>> = Lazy::new(|| {
indexmap!{
"#
.to_string(),
);
for (name, props) in &settings {
let props = props.as_table().unwrap();
if let Some(type_) = props.get("type").map(|v| v.as_str().unwrap()) {
lines.push(format!(
r#" "{name}".to_string() => SettingsMeta {{
type_: SettingsType::{type_},
}},"#,
));
}
}
for (name, props) in &nested_settings {
for (key, props) in props.as_table().unwrap() {
let props = props.as_table().unwrap();
if let Some(type_) = props.get("type").map(|v| v.as_str().unwrap()) {
lines.push(format!(
r#" "{name}.{key}".to_string() => SettingsMeta {{
type_: SettingsType::{type_},
}},"#,
));
}
}
}
lines.push(
r#" }
});
"#
.to_string(),
);

fs::write(&dest_path, lines.join("\n")).unwrap();
}
103 changes: 53 additions & 50 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,50 @@ export default defineConfig({
// https://vitepress.dev/reference/default-theme-config
outline: "deep",
nav: [
{text: "Dev Tools", link: "/dev-tools/"},
{text: "Environments", link: "/environments"},
{text: "Tasks", link: "/tasks/"},
{ text: "Dev Tools", link: "/dev-tools/" },
{ text: "Environments", link: "/environments" },
{ text: "Tasks", link: "/tasks/" },
],
sidebar: [
{text: "Getting Started", link: "/getting-started"},
{text: "About", link: "/about"},
{text: "Configuration", link: "/configuration"},
{text: "Continuous Integration", link: "/continuous-integration"},
{text: "Demo", link: "/demo"},
{text: "FAQs", link: "/faq"},
{text: "Troubleshooting", link: "/troubleshooting"},
{text: "How I Use mise", link: "/how-i-use-mise"},
{text: "IDE Integration", link: "/ide-integration"},
{text: "Paranoid", link: "/paranoid"},
{text: "Registry", link: "/registry"},
{text: "Settings", link: "/settings"},
{text: "Plugins", link: "/plugins"},
{text: "Coming from rtx", link: "/rtx"},
{text: "Team", link: "/team"},
{text: "Contributing", link: "/contributing"},
{text: "Tips & Tricks", link: "/tips-and-tricks"},
{ text: "Getting Started", link: "/getting-started" },
{ text: "About", link: "/about" },
{ text: "Continuous Integration", link: "/continuous-integration" },
{ text: "Demo", link: "/demo" },
{ text: "FAQs", link: "/faq" },
{ text: "Troubleshooting", link: "/troubleshooting" },
{ text: "How I Use mise", link: "/how-i-use-mise" },
{ text: "IDE Integration", link: "/ide-integration" },
{ text: "Paranoid", link: "/paranoid" },
{ text: "Registry", link: "/registry" },
{ text: "Plugins", link: "/plugins" },
{ text: "Coming from rtx", link: "/rtx" },
{ text: "Team", link: "/team" },
{ text: "Contributing", link: "/contributing" },
{ text: "Tips & Tricks", link: "/tips-and-tricks" },
{
text: "Configuration",
link: "/configuration",
items: [{ text: "Settings", link: "/configuration/settings" }],
},
{
text: "Dev Tools",
link: "/dev-tools/",
items: [
{text: "Aliases", link: "/dev-tools/aliases"},
{text: "Comparison to asdf", link: "/dev-tools/comparison-to-asdf"},
{text: "Shims", link: "/dev-tools/shims"},
{ text: "Aliases", link: "/dev-tools/aliases" },
{ text: "Comparison to asdf", link: "/dev-tools/comparison-to-asdf" },
{ text: "Shims", link: "/dev-tools/shims" },
{
text: "Backends",
link: "/dev-tools/backends/",
items: [
{text: "asdf", link: "/dev-tools/backends/asdf"},
{text: "cargo", link: "/dev-tools/backends/cargo"},
{text: "go", link: "/dev-tools/backends/go"},
{text: "npm", link: "/dev-tools/backends/npm"},
{text: "pipx", link: "/dev-tools/backends/pipx"},
{text: "spm", link: "/dev-tools/backends/spm"},
{text: "ubi", link: "/dev-tools/backends/ubi"},
{text: "vfox", link: "/dev-tools/backends/vfox"},
{ text: "asdf", link: "/dev-tools/backends/asdf" },
{ text: "cargo", link: "/dev-tools/backends/cargo" },
{ text: "go", link: "/dev-tools/backends/go" },
{ text: "npm", link: "/dev-tools/backends/npm" },
{ text: "pipx", link: "/dev-tools/backends/pipx" },
{ text: "spm", link: "/dev-tools/backends/spm" },
{ text: "ubi", link: "/dev-tools/backends/ubi" },
{ text: "vfox", link: "/dev-tools/backends/vfox" },
],
},
],
Expand All @@ -64,40 +67,40 @@ export default defineConfig({
text: "Environments",
link: "/environments",
items: [
{text: "direnv", link: "/direnv"},
{text: "Profiles", link: "/profiles"},
{text: "Templates", link: "/templates"},
{ text: "direnv", link: "/direnv" },
{ text: "Profiles", link: "/profiles" },
{ text: "Templates", link: "/templates" },
],
},
{
text: "Tasks",
link: "/tasks/",
items: [
{text: "Running Tasks", link: "/tasks/running-tasks"},
{text: "File Tasks", link: "/tasks/file-tasks"},
{text: "TOML Tasks", link: "/tasks/toml-tasks"},
{ text: "Running Tasks", link: "/tasks/running-tasks" },
{ text: "File Tasks", link: "/tasks/file-tasks" },
{ text: "TOML Tasks", link: "/tasks/toml-tasks" },
],
},
{
text: "Languages",
items: [
{text: "Bun", link: "/lang/bun"},
{text: "Deno", link: "/lang/deno"},
{text: "Erlang", link: "/lang/erlang"},
{text: "Go", link: "/lang/go"},
{text: "Java", link: "/lang/java"},
{text: "Node.js", link: "/lang/node"},
{text: "Python", link: "/lang/python"},
{text: "Ruby", link: "/lang/ruby"},
{text: "Rust", link: "/lang/rust"},
{ text: "Bun", link: "/lang/bun" },
{ text: "Deno", link: "/lang/deno" },
{ text: "Erlang", link: "/lang/erlang" },
{ text: "Go", link: "/lang/go" },
{ text: "Java", link: "/lang/java" },
{ text: "Node.js", link: "/lang/node" },
{ text: "Python", link: "/lang/python" },
{ text: "Ruby", link: "/lang/ruby" },
{ text: "Rust", link: "/lang/rust" },
],
},
{
text: "Internals",
items: [
{text: "Cache Behavior", link: "/cache-behavior"},
{text: "Directory Structure", link: "/directories"},
{text: "Project Roadmap", link: "/project-roadmap"},
{ text: "Cache Behavior", link: "/cache-behavior" },
{ text: "Directory Structure", link: "/directories" },
{ text: "Project Roadmap", link: "/project-roadmap" },
],
},
{
Expand All @@ -107,7 +110,7 @@ export default defineConfig({
},
],

socialLinks: [{icon: "github", link: "https://github.com/jdx/mise"}],
socialLinks: [{ icon: "github", link: "https://github.com/jdx/mise" }],

editLink: {
pattern: "https://github.com/jdx/mise/edit/main/docs/:path",
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ version files since they're version files not specific to asdf/mise and can be u

## Settings

See [Settings](/settings) for the full list of settings.
See [Settings](/configuration/settings) for the full list of settings.

## Tasks

Expand Down
File renamed without changes.
19 changes: 5 additions & 14 deletions docs/settings.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,19 @@ export default {

function buildElement(key, props) {
let type = props.type;
let optional = false;
if (type.startsWith("Option<")) {
type = type.slice(7, -1);
optional = true;
}
type = type.replaceAll("PathBuf", "String");
let default_ = props.default;
if (default_ === undefined && type === "bool" && !optional) {
if (default_ === undefined && type === "Bool" && !props.optional) {
default_ = false;
}
if (default_ === undefined && optional) {
if (default_ === undefined && props.optional) {
default_ = "None";
}
if (type === "u64" || type === "usize") {
if (type === "Integer") {
type = "integer";
} else if (type === "String") {
type = "string";
} else if (
type === "BTreeSet<String>" ||
type === "HashSet<String>" ||
type === "Vec<String>"
) {
} else if (type === "ListString" || type === "ListPath") {
type = "string[]";
}
// } else if (type === "String" || type === "PathBuf") {
Expand All @@ -51,7 +42,7 @@ export default {
deprecated: props.deprecated,
enum: props.enum,
env: props.env,
optional,
optional: props.optional,
type,
};
return ele;
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
},
"author": "",
"license": "ISC",
"dependencies": {
"devDependencies": {
"handlebars": "^4.7.8",
"toml": "^3.0.0"
"toml": "^3.0.0",
"ts-pattern": "^5.4.0",
"tsx": "^4.19.1",
"typescript": "^5.6.3"
},
"dependencies": {
"@types/node": "^22.7.5"
}
}
14 changes: 12 additions & 2 deletions schema/mise.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@
"description": "Enable experimental mise features which are incomplete or unstable—breakings changes may occur",
"type": "boolean"
},
"fetch_remote_versions_cache": {
"default": "1h",
"description": "How long to cache remote versions for tools.",
"type": "string"
},
"fetch_remote_versions_timeout": {
"default": "10s",
"description": "Timeout in seconds for HTTP requests to fetch new tool versions in mise.",
"type": "string"
},
"go_default_packages_file": {
"default": "~/.default-go-packages",
"description": "Path to a file containing default go packages to install when installing go",
Expand Down Expand Up @@ -224,9 +234,9 @@
"type": "boolean"
},
"http_timeout": {
"default": 30,
"default": "30s",
"description": "Timeout in seconds for all HTTP requests in mise.",
"type": "number"
"type": "string"
},
"jobs": {
"default": 4,
Expand Down
Loading

0 comments on commit 063daea

Please sign in to comment.