Skip to content

Commit

Permalink
allow build.rs to be ran in other projects
Browse files Browse the repository at this point in the history
  • Loading branch information
LegitCamper committed Sep 23, 2024
1 parent 421a8a0 commit fff5ec4
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
use std::env::var;
use std::fs::{self, DirEntry};
use std::path::Path;

const PATH: &str = env!("CARGO_MANIFEST_DIR");

fn main() {
let out = var("OUT_DIR").unwrap();
// This allows it to work in this project but also other projects too
let path = Path::new(&out)
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap();

println!("cargo:warning={:?}", path.as_os_str());

// ensure runtime resources exist
#[cfg(feature = "ultralight")]
{
let mut possible_directories = Vec::new();

let target = Path::new(PATH).join("target");
let target = Path::new(path).join("target");
let debug_path = target.clone().join("debug");
let release_path = target.clone().join("release");

Expand All @@ -33,33 +48,24 @@ fn main() {

assert!(!possible_directories.is_empty());

let local_resources = Path::new(PATH).join("resources");
let local_resources = Path::new(path).join("resources");

for path in possible_directories {
if let Ok(resources) = fs::exists(path.path().join("out/ul-sdk/resources")) {
let resources_dir = path.path().join("out/ul-sdk/resources");
if let Ok(resources) = fs::exists(resources_dir.clone()) {
if resources {
if let Ok(local_resources_exist) = fs::exists(local_resources.clone()) {
if local_resources_exist {
fs::remove_dir_all(local_resources.clone())
fs::remove_file(local_resources.clone())
.expect("Failed to delete resources dir")
}
}

fs::create_dir(local_resources.clone())
.expect("Failed to create resources dir");

copy_file(
path.path().join("out/ul-sdk/resources").as_path(),
local_resources.clone().join("").as_path(),
"cacert.pem",
)
.expect("Failed to copy cacert.pem");
copy_file(
path.path().join("out/ul-sdk/resources").as_path(),
local_resources.clone().join("").as_path(),
"icudt67l.dat",
)
.expect("Failed to copy icudt67l.dat");
#[cfg(unix)]
{
std::os::unix::fs::symlink(resources_dir, local_resources)
.expect("Failed to sym link resource dir")
}

break;
}
Expand All @@ -69,15 +75,11 @@ fn main() {
}
}

println!("cargo:rerun-if-changed=resources");
println!("cargo:rerun-if-changed=targets");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=Cargo.lock");
}

fn copy_file(from: &Path, to: &Path, file_name: &str) -> Result<u64, std::io::Error> {
fs::copy(from.join(file_name), to.join(file_name))
}

fn get_paths(possible_paths: &mut Vec<fs::DirEntry>, path_str: String) {
let mut paths: Vec<DirEntry> = fs::read_dir(path_str)
.expect("Could not read dir")
Expand Down

0 comments on commit fff5ec4

Please sign in to comment.