Skip to content

Commit

Permalink
chore: Update build.rs to comply with crates.io publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
LeChatP committed Sep 23, 2024
1 parent d262423 commit 3cb1363
Showing 1 changed file with 44 additions and 52 deletions.
96 changes: 44 additions & 52 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use serde_json::Value;
use std::error::Error;
use std::fs::File;
use std::fs::{self, File};
use std::io::{BufRead, BufReader, Write};

use std::path::Path;

fn write_version<'a>(f: &'a mut File, doc: &'a Value) -> Result<&'a str, Box<dyn Error>> {
let package_version = doc
.get("version")
.ok_or("No version found")?
.as_str()
.unwrap();

fn package_version(json: &Value) -> Result<&str, Box<dyn Error>> {
json
.get("version")
.ok_or("No version found")?
.as_str()
.ok_or("Version is not a string".into())
}

fn write_version<'a>(f: &'a mut File, package_version: &'a str) -> Result<&'a str, Box<dyn Error>> {
f.write_all(
format!(
"pub const PACKAGE_VERSION: &'static str = \"{}\";\n",
Expand Down Expand Up @@ -63,60 +66,49 @@ fn main() {
let json: Value = include_str!("resources/rootasrole.json")
.parse()
.expect("Failed to parse rootasrole.json");
let package_version = package_version(&json).expect("Failed to get package version");
let dest_path = std::path::Path::new("src").join("version.rs");
if dest_path.exists() && fs::read_to_string(&dest_path).unwrap().ends_with(&format!("\"{}\";\n",package_version)) {
return;
}
let mut f = File::create(dest_path).unwrap();
f.write_all(b"// This file is generated by build.rs\n")
.unwrap();
f.write_all(b"// Do not edit this file directly\n").unwrap();
f.write_all(b"// Instead edit build.rs and run cargo build\n")
.unwrap();
match write_version(&mut f, &json) {
Ok(package_version) => {
if let Err(err) = set_cargo_version(package_version, "Cargo.toml") {
eprintln!("cargo:warning={}", err);
}
//if folder capable/ exists
if Path::new("capable/capable").is_dir() {
if let Err(err) = set_cargo_version(package_version, "capable/capable/Cargo.toml") {
eprintln!("cargo:warning={}", err);
}
if let Err(err) =
set_cargo_version(package_version, "capable/capable-ebpf/Cargo.toml")
{
eprintln!("cargo:warning={}", err);
}
if let Err(err) =
set_cargo_version(package_version, "capable/capable-common/Cargo.toml")
{
eprintln!("cargo:warning={}", err);
}
}
if Path::new("xtask").is_dir() {
if let Err(err) = set_cargo_version(package_version, "xtask/Cargo.toml") {
eprintln!("cargo:warning={}", err);
}
}
if Path::new("rar-common").is_dir() {
if let Err(err) = set_cargo_version(package_version, "rar-common/Cargo.toml") {
eprintln!("cargo:warning={}", err);
}
}
if let Err(err) = set_readme_version(package_version, "README.md") {
eprintln!("cargo:warning={}", err);
}
//if let Err(err) = set_pkgbuild_version(package_version, "PKGBUILD") {
//eprintln!("cargo:warning={}", err);
//}
write_version(&mut f, &package_version).expect("Failed to write version");
if let Err(err) = set_cargo_version(package_version, "Cargo.toml") {
eprintln!("cargo:warning={}", err);

Check warning on line 82 in build.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> build.rs:82:27 | 82 | write_version(&mut f, &package_version).expect("Failed to write version"); | ^^^^^^^^^^^^^^^^ help: change this to: `package_version` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
}
//if folder capable/ exists
if Path::new("capable/capable").is_dir() {
if let Err(err) = set_cargo_version(package_version, "capable/capable/Cargo.toml") {
eprintln!("cargo:warning={}", err);
}
if let Err(err) =
set_cargo_version(package_version, "capable/capable-ebpf/Cargo.toml")
{
eprintln!("cargo:warning={}", err);
}
Err(err) => {
if let Err(err) =
set_cargo_version(package_version, "capable/capable-common/Cargo.toml")
{
eprintln!("cargo:warning={}", err);
}
}

// let xml = include_str!("resources/rootasrole.xml");
// if let Err(err) = write_dtd(&mut f, xml) {
// eprintln!("cargo:warning={}", err);
// }

if Path::new("xtask").is_dir() {
if let Err(err) = set_cargo_version(package_version, "xtask/Cargo.toml") {
eprintln!("cargo:warning={}", err);
}
}
if Path::new("rar-common").is_dir() {
if let Err(err) = set_cargo_version(package_version, "rar-common/Cargo.toml") {
eprintln!("cargo:warning={}", err);
}
}
if let Err(err) = set_readme_version(package_version, "README.md") {
eprintln!("cargo:warning={}", err);
}
f.flush().unwrap();
}

0 comments on commit 3cb1363

Please sign in to comment.