aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/mozart-spdx-licenses/build.rs
blob: 27d4ed69828aca64e4ba1e093f2e27eb09340be7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use serde_json::Value;
use std::collections::BTreeMap;
use std::env;
use std::fs;
use std::path::Path;

fn main() {
    let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    let res_dir = Path::new(&manifest_dir).join("composer-spdx-licenses/res");

    let licenses_path = res_dir.join("spdx-licenses.json");
    let exceptions_path = res_dir.join("spdx-exceptions.json");

    println!("cargo:rerun-if-changed={}", licenses_path.display());
    println!("cargo:rerun-if-changed={}", exceptions_path.display());

    let licenses_json: BTreeMap<String, Value> =
        serde_json::from_str(&fs::read_to_string(&licenses_path).unwrap()).unwrap();

    let exceptions_json: BTreeMap<String, Value> =
        serde_json::from_str(&fs::read_to_string(&exceptions_path).unwrap()).unwrap();

    let out_dir = env::var("OUT_DIR").unwrap();
    let out_path = Path::new(&out_dir).join("spdx_data.rs");

    let mut code = String::new();

    // Generate licenses array
    code.push_str("const LICENSES: &[(&str, &str, &str, bool, bool)] = &[\n");
    for (id, val) in &licenses_json {
        let arr = val.as_array().unwrap();
        let full_name = arr[0].as_str().unwrap();
        let osi_approved = arr[1].as_bool().unwrap();
        let deprecated = arr[2].as_bool().unwrap();
        let lower = id.to_lowercase();
        code.push_str(&format!(
            "    ({:?}, {:?}, {:?}, {}, {}),\n",
            lower, id, full_name, osi_approved, deprecated
        ));
    }
    code.push_str("];\n\n");

    // Generate exceptions array
    code.push_str("const EXCEPTIONS: &[(&str, &str, &str)] = &[\n");
    for (id, val) in &exceptions_json {
        let arr = val.as_array().unwrap();
        let full_name = arr[0].as_str().unwrap();
        let lower = id.to_lowercase();
        code.push_str(&format!("    ({:?}, {:?}, {:?}),\n", lower, id, full_name));
    }
    code.push_str("];\n");

    fs::write(out_path, code).unwrap();
}