build/fix ~ (build.rs) fix 'feature => sub-crate/util' translation logic + improved output formatting

This commit is contained in:
Roy Ivy III 2020-05-21 17:10:28 -05:00
parent 0cb5fbd6b5
commit 1fe7cc5595

View file

@ -11,18 +11,22 @@ pub fn main() {
println!("cargo:rustc-cfg=build={:?}", profile);
}
let feature_prefix = "CARGO_FEATURE_";
let env_feature_prefix: &str = "CARGO_FEATURE_";
let feature_prefix: &str = "feat_";
let override_prefix: &str = "uu_";
let out_dir = env::var("OUT_DIR").unwrap();
let mut crates = Vec::new();
for (key, val) in env::vars() {
if val == "1" && key.starts_with(feature_prefix) {
let krate = key[feature_prefix.len()..].to_lowercase();
if val == "1" && key.starts_with(env_feature_prefix) {
let krate = key[env_feature_prefix.len()..].to_lowercase();
match krate.as_ref() {
"default" | "macos" | "unix" | "windows" => continue,
"nightly" | "test_unimplemented" => continue,
s if s.starts_with("feat_") => continue,
_ => {}
"default" | "macos" | "unix" | "windows" => continue, // common/standard feature names
"nightly" | "test_unimplemented" => continue, // crate-local custom features
"test" => continue, // over-ridden with 'uu_test' to avoid collision with rust core crate 'test'
s if s.starts_with(feature_prefix) => continue, // crate feature sets
_ => {} // util feature name
}
crates.push(krate.to_string());
}
@ -32,50 +36,55 @@ pub fn main() {
let mut mf = File::create(Path::new(&out_dir).join("uutils_map.rs")).unwrap();
mf.write_all(
"type UtilityMap = HashMap<&'static str, fn(Vec<String>) -> i32>;\n\
\n\
fn util_map() -> UtilityMap {\n\
\tlet mut map: UtilityMap = HashMap::new();\n\
"
type UtilityMap = HashMap<&'static str, fn(Vec<String>) -> i32>;
fn util_map() -> UtilityMap {
let mut map: UtilityMap = HashMap::new();\n"
.as_bytes(),
.as_bytes(),
)
.unwrap();
for krate in crates {
match krate.as_ref() {
// ToDO: add another bypass method for publishing name collisions requiring a non-`uu_` prefix for a util
// * use "uu_" prefix as bypass method to avoid name collisions with imported crates, when necessary (eg, 'test')
k if k.starts_with("uu_")
=> mf
.write_all(
format!("map.insert(\"{k}\", {krate}::uumain);\n", k = krate.clone().remove("uu_".len()), krate = krate)
.as_bytes(),
k if k.starts_with(override_prefix) => mf
.write_all(
format!(
"\tmap.insert(\"{k}\", {krate}::uumain);\n",
k = krate[override_prefix.len()..].to_string(),
krate = krate
)
.unwrap(),
.as_bytes(),
)
.unwrap(),
"false" | "true" => mf
.write_all(
format!("map.insert(\"{krate}\", r#{krate}::uumain);\n", krate = krate)
.as_bytes(),
format!(
"\tmap.insert(\"{krate}\", r#{krate}::uumain);\n",
krate = krate
)
.as_bytes(),
)
.unwrap(),
"hashsum" => mf
.write_all(
format!(
"
map.insert(\"{krate}\", {krate}::uumain);
map.insert(\"md5sum\", {krate}::uumain);
map.insert(\"sha1sum\", {krate}::uumain);
map.insert(\"sha224sum\", {krate}::uumain);
map.insert(\"sha256sum\", {krate}::uumain);
map.insert(\"sha384sum\", {krate}::uumain);
map.insert(\"sha512sum\", {krate}::uumain);
map.insert(\"sha3sum\", {krate}::uumain);
map.insert(\"sha3-224sum\", {krate}::uumain);
map.insert(\"sha3-256sum\", {krate}::uumain);
map.insert(\"sha3-384sum\", {krate}::uumain);
map.insert(\"sha3-512sum\", {krate}::uumain);
map.insert(\"shake128sum\", {krate}::uumain);
map.insert(\"shake256sum\", {krate}::uumain);\n",
"\
\tmap.insert(\"{krate}\", {krate}::uumain);\n\
\t\tmap.insert(\"md5sum\", {krate}::uumain);\n\
\t\tmap.insert(\"sha1sum\", {krate}::uumain);\n\
\t\tmap.insert(\"sha224sum\", {krate}::uumain);\n\
\t\tmap.insert(\"sha256sum\", {krate}::uumain);\n\
\t\tmap.insert(\"sha384sum\", {krate}::uumain);\n\
\t\tmap.insert(\"sha512sum\", {krate}::uumain);\n\
\t\tmap.insert(\"sha3sum\", {krate}::uumain);\n\
\t\tmap.insert(\"sha3-224sum\", {krate}::uumain);\n\
\t\tmap.insert(\"sha3-256sum\", {krate}::uumain);\n\
\t\tmap.insert(\"sha3-384sum\", {krate}::uumain);\n\
\t\tmap.insert(\"sha3-512sum\", {krate}::uumain);\n\
\t\tmap.insert(\"shake128sum\", {krate}::uumain);\n\
\t\tmap.insert(\"shake256sum\", {krate}::uumain);\n\
",
krate = krate
)
.as_bytes(),
@ -84,7 +93,7 @@ pub fn main() {
_ => mf
.write_all(
format!(
"map.insert(\"{krate}\", {krate}::uumain);\n",
"\tmap.insert(\"{krate}\", {krate}::uumain);\n",
krate = krate
)
.as_bytes(),