~vpzom/lotide

ref: 8117e09fdf459694da08482cc386a88af7b3087b lotide/build.rs -rw-r--r-- 985 bytes
8117e09f — Colin Reeder Add parameter to filter communities by follow state (#134) 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
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}/up.sql"#), down: include_str!(r#"{1}/down.sql"#) }},"##,
            tag, path
        )?;
    }

    write!(out_file, "]")?;

    Ok(())
}