forked from NSoiffer/MathCAT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
85 lines (69 loc) · 2.72 KB
/
build.rs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! The build.rs file is necessary to generate rules.zip.
//! rules.zip are needed so there is a way to get the rules dir into the build since your can't get from the crate.
//! The expectation is that most builds (with the exception of WASM builds) will need a build.rs file to extract the rules.
use std::fs::{read_dir, DirEntry, File};
use std::io::{self, Read, Seek, Write};
use std::path::Path;
use std::path::PathBuf;
use zip::result::ZipResult;
use zip::write::FileOptions;
use zip::CompressionMethod;
use zip::ZipWriter;
fn zip_dir<T: Write + Seek>(
path: &Path,
target: T,
options: FileOptions,
) -> ZipResult<T> {
let mut zip = ZipWriter::new(target);
for entry in read_dir(path)? {
let entry = entry?;
eprintln!("trying dir entry {:?}", entry.path().to_str());
zip_entry(&mut zip, entry, options)?;
}
zip.finish()
}
#[allow(clippy::unused_io_amount)]
fn zip_entry<T: Write + Seek>(
zip: &mut ZipWriter<T>,
entry: DirEntry,
options: FileOptions,
) -> io::Result<()> {
let path = entry.path();
if path.is_dir() {
for entry in read_dir(path)? {
zip_entry(zip, entry?, options)?;
}
} else if let Some(suffix) =path.extension() {
let suffix = suffix.to_ascii_lowercase();
if suffix == "yaml" || suffix == "yml" {
let file_name = path.to_str().unwrap();
zip.start_file(file_name, options)?;
let mut file = File::open(&path)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
zip.write_all(&buffer)?;
}
}
Ok(())
}
fn main() {
// This doesn't work because the build claims OUT_DIR is not defined(?)
// let archive = PathBuf::from(concat!(env!("OUT_DIR"),"/rules.zip"));
let out_dir = std::env::var_os("OUT_DIR").unwrap();
let archive: PathBuf = [out_dir, std::ffi::OsString::from("rules.zip")].iter().collect();
eprintln!("zip file location: '{:?}'", archive.to_str());
let archive = match File::create(&archive) {
Ok(file) => file,
Err(e) => panic!("build.rs couldn't create {:?}: {}", archive.to_str(), e),
};
// let root_dir = std::env::var_os("CARGO_MANIFEST_DIR ").unwrap();
// let zip_directory: PathBuf = [root_dir.clone(), std::ffi::OsString::from("Rules")].iter().collect();
// eprintln!("rules dir: '{:?}'", zip_directory.to_str());
let zip_directory = Path::new("Rules");
let zip_options = FileOptions::default().compression_method(CompressionMethod::Deflated)
.compression_level(Some(9));
if let Err(e) = zip_dir(zip_directory, archive, zip_options) {
panic!("Error: {}", e);
}
println!("cargo:rerun-if-changed=Rules");
}