~vpzom/lotide

ref: a1ff72a5110b46028030c4e6d96d9bb8e7f232d6 lotide/build.rs -rw-r--r-- 1.0 KiB
a1ff72a5Colin Reeder Use MAIN_SEPARATOR in build script to fix build on windows a month ago
                                                                                
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
use std::io::Write;

const MIGRATIONS_DIR: &str = "migrations";

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("cargo:rerun-if-changed={}", MIGRATIONS_DIR);

    let out_dir = std::env::var("OUT_DIR")?;
    let mut out_file = std::fs::File::create(std::path::Path::new(&out_dir).join("migrations.rs"))?;
    writeln!(out_file, "&[")?;

    let paths: Result<Vec<_>, _> = std::fs::read_dir(MIGRATIONS_DIR)?.collect();
    let mut paths = paths?;

    paths.sort_by_cached_key(|entry| entry.file_name());

    for entry in paths {
        let filename = entry.file_name();
        let tag = filename.to_str().unwrap();

        let path = entry.path().canonicalize()?;
        let path = path.to_str().unwrap();

        writeln!(
            out_file,
            r##"StaticMigration {{ tag: r#"{}"#, up: include_str!(r#"{1}{2}up.sql"#), down: include_str!(r#"{1}{2}down.sql"#) }},"##,
            tag,
            path,
            std::path::MAIN_SEPARATOR
        )?;
    }

    write!(out_file, "]")?;

    Ok(())
}