// this can probably replaced with a bash script fn help() { print([ "to build the bindings use:", "- download_dependencies", "- build_bindings " ]); } fn download_dependencies() { let apt = cmd(["sudo", "apt", "install", "wget", "tar"]); apt.execute(); let cargo = cmd(["cargo", "install", "dbus-codegen"]); cargo.execute(); } fn build_bindings() { print("you need to specify a version format: 1.37.3"); } fn build_bindings(version) { clean(); // https://www.freedesktop.org/software/ModemManager/ModemManager-1.6.8.tar.xz let tar_file = "target/mm-" + version + ".tar.xz"; // download release file let short_version_reg = regex("\\d+.\\d+"); let short_version = short_version_reg.find(version); let wget = cmd([ "wget", "-O", tar_file, "https://www.freedesktop.org/software/ModemManager/ModemManager-" + version + ".tar.xz" ]); wget.execute(); // extract the tar let folder = "target/mm-" + version; fs::delete(folder); fs::create_dir(folder); let tar = cmd(["tar", "-xf", tar_file, "-C", folder]); tar.execute(); let inner_folder = folder + "/ModemManager-" + version; let xml_folder = inner_folder + "/introspection"; let meson_file = xml_folder + "/meson.build"; let files = fs::read_dir(xml_folder); files = sort_strs(files); let short_name_regex = regex("org.freedesktop.ModemManager1.([a-zA-Z0-9.]+).xml"); let replace_dot = regex("\\."); let shorten_type_space = regex("OrgFreedesktopModemManager1\\s"); let shorten_type = regex("OrgFreedesktopModemManager1"); let replace_comment = regex("// This code was.*?\\n"); let lib_ctn = "//! dbus types for the ModemManager api\n"; lib_ctn += "//! with the version " + version + "\n"; lib_ctn += "pub use dbus;\n"; for file in files { if !file.ends_with(".xml") || !file.starts_with("org.freedesktop") { continue } let short_name; if file == "org.freedesktop.ModemManager1.xml" { short_name = "ModemManager"; } else { short_name = short_name_regex.captures(file)[1]; } short_name = replace_dot.replace(short_name, "_"); short_name = short_name.lowercase(); let codegen = cmd([ "dbus-codegen-rust", "-c", "blocking", "--file", xml_folder + "/" + file ]); let rust = codegen.output(); rust = replace_comment.replace( rust, "// This code was autogenerated with dbus-codegen-rust\n" ); rust = shorten_type_space.replace(rust, "ModemManager "); rust = shorten_type.replace(rust, ""); fs::write("src/" + short_name + ".rs", rust); if short_name == "modemmanager" { lib_ctn += "mod modemmanager;\n"; lib_ctn += "pub use modemmanager::*;\n"; } else { lib_ctn += "pub mod " + short_name + ";\n"; } } fs::write("src/lib.rs", lib_ctn); let cargo_check = cmd(["cargo", "check"]); cargo_check.execute(); } fn clean() { fs::delete("./src"); fs::create_dir("./src"); }