// Copyright (c) 2023 Jan Holthuis // // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy // of the MPL was not distributed with this file, You can obtain one at // http://mozilla.org/MPL/2.0/. // // SPDX-License-Identifier: MPL-2.0 use glob::glob; use std::env; use std::fs::File; use std::io::Write; use std::path::Path; macro_rules! write_test { ($output_filepath:expr, $glob_pattern:literal, $header:literal, $body_path:literal) => { let mut output_file = File::create($output_filepath).expect("failed to write test file"); writeln!(output_file, $header,).expect("failed to write test file header"); glob($glob_pattern) .expect("failed to read glob pattern") .map(|entry| entry.unwrap()) .for_each(|path| { let name = path .iter() .skip(2) .map(|component| component.to_str().unwrap()) .collect::>() .join("_") .replace(".", "_"); eprintln!("Writing setting test: {:?}", name); writeln!( output_file, include_str!($body_path), name = name, filepath = path.canonicalize().unwrap().to_str().unwrap(), ) .expect("failed to write test file body"); }); }; } fn main() { // Make cargo rerun the build script if the data directory changes. println!("cargo:rerun-if-changed=data"); let out_dir = env::var("OUT_DIR").unwrap(); let out_dir = Path::new(&out_dir); eprintln!("Writing tests to: {:?}", out_dir); write_test!( out_dir.join("tests_pdb.rs"), "data/complete_export/*/PIONEER/rekordbox/export.pdb", r#"// THIS FILE IS AUTOGENERATED - DO NOT EDIT! use binrw::{{BinRead}}; use rekordcrate::pdb::Header; "#, "./tests/tests_pdb.rs.in" ); write_test!( out_dir.join("tests_anlz.rs"), "data/complete_export/*/PIONEER/USBANLZ/*/*/ANLZ*.*", r#"// THIS FILE IS AUTOGENERATED - DO NOT EDIT! use rekordcrate::anlz::ANLZ; "#, "./tests/tests_anlz.rs.in" ); write_test!( out_dir.join("tests_setting.rs"), "data/complete_export/*/PIONEER/*.DAT", r#"// THIS FILE IS AUTOGENERATED - DO NOT EDIT! use binrw::{{BinWrite, io::Cursor}}; use rekordcrate::setting::Setting; "#, "./tests/tests_setting.rs.in" ); }