-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #2633
- Loading branch information
Showing
24 changed files
with
1,350 additions
and
569 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ registry/ | |
target/ | ||
CHANGELOG.md | ||
docs/node_modules/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
toml-sort --check settings.toml --spaces-indent-inline-array 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env node | ||
|
||
const fs = require('fs'); | ||
const toml = require('toml'); | ||
const child_process = require('child_process'); | ||
const Handlebars = require('handlebars'); | ||
|
||
const doc = toml.parse(fs.readFileSync('settings.toml', 'utf-8')); | ||
const settings = {}; | ||
|
||
function buildElement(key, props) { | ||
let type = props.type; | ||
if (type.startsWith('Option<')) { | ||
type = type.slice(7, -1); | ||
} | ||
type = type.replaceAll('PathBuf', 'String'); | ||
if (type === 'bool') { | ||
type = 'boolean'; | ||
} else if (type === "String" || type === "PathBuf") { | ||
type = 'string'; | ||
} else if (type === "usize" || type === "u64") { | ||
type = 'number'; | ||
} else if (type === "BTreeSet<String>" || type === "HashSet<String>" || type === "Vec<String>") { | ||
type = 'string[]'; | ||
} else { | ||
throw new Error(`Unknown type: ${type}`); | ||
} | ||
if (!props.description) { | ||
console.error(`Missing description for ${key}`); | ||
process.exit(1); | ||
} | ||
const ele = { | ||
default: props.default, | ||
description: props.description, | ||
deprecated: props.deprecated, | ||
type, | ||
}; | ||
if (props.enum) { | ||
ele.enum = props.enum.map((e) => e[0]); | ||
} | ||
if (type === 'string[]') { | ||
ele.type = 'array'; | ||
ele.items = { | ||
type: 'string', | ||
}; | ||
} | ||
return ele; | ||
} | ||
|
||
for (const key in doc) { | ||
const props = doc[key]; | ||
if (props.type) { | ||
settings[key] = buildElement(key, props); | ||
} else { | ||
for (const subkey in props) { | ||
settings[key] = settings[key] || { | ||
additionalProperties: false, | ||
description: props.description, | ||
properties: {}, | ||
}; | ||
settings[key].properties[subkey] = buildElement(`${key}.${subkey}`, props[subkey]); | ||
} | ||
} | ||
} | ||
|
||
const schema_tmpl = Handlebars.compile(fs.readFileSync('schema/mise.json.hbs', 'utf-8')); | ||
fs.writeFileSync('schema/mise.json.tmp', schema_tmpl({ | ||
settings_json: new Handlebars.SafeString(JSON.stringify(settings, null, 2)), | ||
})); | ||
|
||
child_process.execSync('jq . < schema/mise.json.tmp > schema/mise.json'); | ||
fs.unlinkSync('schema/mise.json.tmp'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,96 @@ | ||
use heck::ToUpperCamelCase; | ||
use indexmap::IndexMap; | ||
use std::path::Path; | ||
use std::{env, fs}; | ||
|
||
fn main() { | ||
cfg_aliases::cfg_aliases! { | ||
vfox: { any(feature = "vfox", target_os = "windows") }, | ||
asdf: { any(feature = "asdf", not(target_os = "windows")) }, | ||
} | ||
built::write_built_file().expect("Failed to acquire build-time information"); | ||
|
||
codegen_settings(); | ||
} | ||
|
||
fn codegen_settings() { | ||
let out_dir = env::var_os("OUT_DIR").unwrap(); | ||
let dest_path = Path::new(&out_dir).join("settings.rs"); | ||
let mut lines = vec![r#" | ||
#[derive(Config, Default, Debug, Clone, Serialize)] | ||
#[config(partial_attr(derive(Clone, Serialize, Default)))] | ||
pub struct Settings {"# | ||
.to_string()]; | ||
|
||
let settings: toml::Table = fs::read_to_string("settings.toml") | ||
.unwrap() | ||
.parse() | ||
.unwrap(); | ||
let props_to_code = |key: &str, props: &toml::Value| { | ||
let mut lines = vec![]; | ||
let props = props.as_table().unwrap(); | ||
if let Some(description) = props.get("description") { | ||
lines.push(format!(" /// {}", description.as_str().unwrap())); | ||
} | ||
if let Some(type_) = props.get("type") { | ||
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" { | ||
opts.insert("default".to_string(), "false".to_string()); | ||
} | ||
if let Some(parse_env) = props.get("parse_env") { | ||
opts.insert( | ||
"parse_env".to_string(), | ||
parse_env.as_str().unwrap().to_string(), | ||
); | ||
} | ||
dbg!(&opts); | ||
lines.push(format!( | ||
" #[config({})]", | ||
opts.iter() | ||
.map(|(k, v)| format!("{k} = {v}")) | ||
.collect::<Vec<_>>() | ||
.join(", ") | ||
)); | ||
lines.push(format!(" pub {}: {},", key, type_.as_str().unwrap())); | ||
} else { | ||
lines.push(" #[config(nested)]".to_string()); | ||
lines.push(format!( | ||
" pub {}: Settings{},", | ||
key, | ||
key.to_upper_camel_case() | ||
)); | ||
} | ||
lines.join("\n") | ||
}; | ||
for (key, props) in &settings { | ||
lines.push(props_to_code(key, props)); | ||
} | ||
lines.push("}".to_string()); | ||
|
||
let nested_settings = settings | ||
.iter() | ||
.filter(|(_, v)| !v.as_table().unwrap().contains_key("type")) | ||
.collect::<Vec<_>>(); | ||
for (child, props) in nested_settings { | ||
lines.push(format!( | ||
r#"#[derive(Config, Default, Debug, Clone, Serialize)] | ||
#[config(partial_attr(derive(Clone, Serialize, Default)))] | ||
#[config(partial_attr(serde(deny_unknown_fields)))] | ||
pub struct Settings{name} {{ | ||
"#, | ||
name = child.to_upper_camel_case() | ||
)); | ||
|
||
for (key, props) in props.as_table().unwrap() { | ||
lines.push(props_to_code(key, props)); | ||
} | ||
lines.push("}".to_string()); | ||
} | ||
|
||
fs::write(&dest_path, lines.join("\n")).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.