updated README and a bunch of other stuff
This commit is contained in:
19
parts/mos.bash_config_cache/Cargo.toml
Normal file
19
parts/mos.bash_config_cache/Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
cargo-features = ["profile-rustflags"]
|
||||
|
||||
[package]
|
||||
name = "mize-part-mos-bash-config-cache"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
name = "mize_part_mos_bash_config_cache"
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
mize = { workspace = true }
|
||||
|
||||
[profile.dev]
|
||||
rustflags = ["-C", "prefer-dynamic", "-C", "link-args=-Wl,--export-dynamic"]
|
||||
|
||||
[profile.release]
|
||||
rustflags = ["-C", "prefer-dynamic"]
|
||||
1
parts/mos.bash_config_cache/part
Normal file
1
parts/mos.bash_config_cache/part
Normal file
@@ -0,0 +1 @@
|
||||
id = "mos.bash_config_cache"
|
||||
89
parts/mos.bash_config_cache/src/lib.rs
Normal file
89
parts/mos.bash_config_cache/src/lib.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
use std::fs::{create_dir_all, write};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use mize::prelude::*;
|
||||
|
||||
fn bash_double_quote(s: &str) -> String {
|
||||
let mut out = String::with_capacity(s.len() + 2);
|
||||
out.push('"');
|
||||
for c in s.chars() {
|
||||
match c {
|
||||
'\\' | '"' | '`' => {
|
||||
out.push('\\');
|
||||
out.push(c);
|
||||
}
|
||||
'\n' => out.push_str("\\n"),
|
||||
// leave `$` unescaped so values like $CONFIG_DIR expand when sourced
|
||||
_ => out.push(c),
|
||||
}
|
||||
}
|
||||
out.push('"');
|
||||
out
|
||||
}
|
||||
|
||||
fn opt_value_for_bash(mize: &mut Mize, opt_id: &str) -> MizeResult<String> {
|
||||
println!("hiiii");
|
||||
let val = mize.get_config(opt_id)?;
|
||||
if let Some(arr) = val.cbor().as_array() {
|
||||
println!("as arr!!");
|
||||
let mut quoted = Vec::with_capacity(arr.len());
|
||||
for entry in arr {
|
||||
let s = entry.as_text().ok_or_else(|| {
|
||||
mize_err!("mos.bash_config_cache: list opt '{opt_id}' has a non-string entry")
|
||||
})?;
|
||||
quoted.push(bash_double_quote(s));
|
||||
}
|
||||
return Ok(quoted.join(" "));
|
||||
}
|
||||
val.value_string()
|
||||
}
|
||||
|
||||
fn write_cache(_args: ItemData) -> MizeResult<ItemData> {
|
||||
let mut mize = get_global_mize()?;
|
||||
let store = mize.get_config("store")?.value_string()?;
|
||||
let opts_data = mize.get_config("mos.bash_config_cache.opts")?;
|
||||
|
||||
let arr = opts_data
|
||||
.cbor()
|
||||
.as_array()
|
||||
.ok_or_else(|| mize_err!("mos.bash_config_cache.opts is not a list"))?;
|
||||
|
||||
let mut body = String::from("# generated by mos.bash_config_cache.write_cache — do not edit\n");
|
||||
body.push_str("declare -A myconfig\n");
|
||||
|
||||
for entry in arr {
|
||||
let opt_id = entry
|
||||
.as_text()
|
||||
.ok_or_else(|| mize_err!("mos.bash_config_cache.opts entry is not a string"))?;
|
||||
let val = opt_value_for_bash(&mut mize, opt_id)?;
|
||||
body.push_str(&format!(
|
||||
"myconfig[{}]={}\n",
|
||||
bash_double_quote(opt_id),
|
||||
bash_double_quote(&val)
|
||||
));
|
||||
}
|
||||
|
||||
let out_dir = PathBuf::from(&store).join("misc");
|
||||
create_dir_all(&out_dir)?;
|
||||
let out_path = out_dir.join("mos.bash_config_cache");
|
||||
write(&out_path, body)?;
|
||||
Ok(ItemData::new())
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn part_mos_bash_config_cache() -> MizeResult<()> {
|
||||
// toml may already have populated opts; opt() inserts empty and would clobber it
|
||||
let preexisting = get_global_mize()?
|
||||
.get_config("mos.bash_config_cache.opts")
|
||||
.ok();
|
||||
let opts_id = opt("opts");
|
||||
if let Some(val) = preexisting {
|
||||
opts_id.default_val(val);
|
||||
}
|
||||
|
||||
apply("write_cache", write_cache);
|
||||
|
||||
// until apply hooks run at startup automatically
|
||||
write_cache(ItemData::new())?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user