aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/mozart-spdx-licenses/build.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-02-22 12:27:20 +0900
committernsfisis <nsfisis@gmail.com>2026-02-22 12:37:41 +0900
commit1ef1ebdcf50ae1358ec06e3c6a2fb797a8461617 (patch)
tree97cc47fc216dc7e64697ffa74b6a2bb70f395abe /crates/mozart-spdx-licenses/build.rs
parent92fdff257d2c64f94600ba70bf17e429d46474b2 (diff)
downloadphp-mozart-1ef1ebdcf50ae1358ec06e3c6a2fb797a8461617.tar.gz
php-mozart-1ef1ebdcf50ae1358ec06e3c6a2fb797a8461617.tar.zst
php-mozart-1ef1ebdcf50ae1358ec06e3c6a2fb797a8461617.zip
feat(spdx): add mozart-spdx-licenses crate for SPDX license validation
Add new workspace crate that validates SPDX license expressions using data from composer/spdx-licenses (git submodule). Includes build.rs codegen from JSON, recursive descent expression parser supporting AND/OR/WITH/LicenseRef, and integrates into mozart-core's validate_license function. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'crates/mozart-spdx-licenses/build.rs')
-rw-r--r--crates/mozart-spdx-licenses/build.rs54
1 files changed, 54 insertions, 0 deletions
diff --git a/crates/mozart-spdx-licenses/build.rs b/crates/mozart-spdx-licenses/build.rs
new file mode 100644
index 0000000..27d4ed6
--- /dev/null
+++ b/crates/mozart-spdx-licenses/build.rs
@@ -0,0 +1,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();
+}