-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.rs
70 lines (57 loc) · 1.88 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
use std::env;
use std::io::{self, Write, BufRead};
use std::fs::{self, DirEntry};
use std::ffi::OsString;
use std::path::{Path, PathBuf};
// one possible implementation of walking a directory only visiting files
fn visit_dirs(dir: &Path, cb: &dyn Fn(&DirEntry) -> io::Result<()>)
-> io::Result<()> {
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
visit_dirs(&path, cb)?;
} else {
cb(&entry)?;
}
}
}
Ok(())
}
fn manifest_dir() -> PathBuf {
PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap())
}
fn out_dir() -> PathBuf {
env::var_os("OUT_DIR").unwrap().into()
}
fn lib_path() -> PathBuf {
out_dir().join("src/lib.rs")
}
fn translate_path(src: &Path) -> PathBuf {
let src = src.to_str().unwrap();
format!("{}rs", &src[..src.len() - 2]).into()
}
fn translate2rs(src: &DirEntry) -> io::Result<()> {
let path = src.path();
if path.extension() != Some(&OsString::from("md")) {
return Ok(());
}
let sink_filename = out_dir().join(
translate_path(&path).strip_prefix(&manifest_dir()).unwrap());
eprintln!("{:?} -> {:?}", path, sink_filename);
println!("rerun-if-changed={}", path.to_str().unwrap());
fs::create_dir_all(sink_filename.parent().unwrap())?;
let mut sink = fs::File::create(sink_filename)?;
for line in io::BufReader::new(fs::File::open(&path)?).lines() {
writeln!(&mut sink, "//! {}", line?)?;
}
let mut lib =
fs::OpenOptions::new().create(true).append(true).open(&lib_path())?;
writeln!(&mut lib, "pub mod {};", path.file_stem().unwrap().to_str().unwrap())?;
Ok(())
}
fn main() {
let _ = fs::remove_file(&lib_path());
visit_dirs(&manifest_dir().join("src"), &translate2rs).unwrap();
}