2024-11-01 16:52:33 +01:00
|
|
|
use std::{env, fs, path::Path, process::Command};
|
|
|
|
|
|
|
|
fn main() -> Result<(), std::io::Error> {
|
|
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
|
|
|
|
if args.len() == 1 {
|
|
|
|
println!("Too few arguments");
|
|
|
|
println!("Please enter a year, or a year and day");
|
|
|
|
return Err(std::io::ErrorKind::InvalidInput.into());
|
|
|
|
} else if args.len() > 3 {
|
|
|
|
println!("Too many arguments");
|
|
|
|
println!("Please enter a year, or a year and day");
|
|
|
|
return Err(std::io::ErrorKind::InvalidInput.into());
|
|
|
|
} else if args.len() == 2 {
|
|
|
|
prepare_year(&args[1])?;
|
|
|
|
} else if args.len() == 3 {
|
|
|
|
prepare_day(&args[1], &args[2])?;
|
|
|
|
}
|
|
|
|
match Command::new("cargo").arg("fmt").status() {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prepare_year(year: &str) -> Result<(), std::io::Error> {
|
|
|
|
let root = env!("CARGO_MANIFEST_DIR");
|
|
|
|
if Path::new(&format!("{root}/y{year}")).exists() {
|
|
|
|
panic!("Folder y{year} already exists")
|
|
|
|
}
|
|
|
|
// create workspace
|
|
|
|
match Command::new("cargo")
|
|
|
|
.arg("new")
|
|
|
|
.arg(format!("y{year}"))
|
|
|
|
.status()
|
|
|
|
{
|
|
|
|
Ok(_status) => Ok(()),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}?;
|
|
|
|
// remove main.rs
|
|
|
|
fs::remove_file(format!("{root}/y{year}/src/main.rs"))?;
|
|
|
|
fs::create_dir(format!("{root}/y{year}/src/bin"))?;
|
|
|
|
fs::create_dir(format!("{root}/y{year}/src/days"))?;
|
|
|
|
fs::write(format!("{root}/y{year}/src/lib.rs"), "pub mod days;")?;
|
|
|
|
fs::write(format!("{root}/y{year}/src/days/mod.rs"), "")?;
|
|
|
|
prepare_day(year, "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prepare_day(year: &str, day: &str) -> Result<(), std::io::Error> {
|
|
|
|
let root = env!("CARGO_MANIFEST_DIR");
|
2024-11-04 11:32:48 +01:00
|
|
|
if Path::new(&format!("{root}/y{year}/src/bin/d{day}.rs")).exists() {
|
|
|
|
panic!("Day y{year}d{day} already exists")
|
|
|
|
}
|
2024-11-01 16:52:33 +01:00
|
|
|
let bin = fs::read_to_string(format!("{root}/template/bin/d.rs.tmpl"))?
|
|
|
|
.replace("{{YEAR}}", year)
|
|
|
|
.replace("{{DAY}}", day);
|
|
|
|
let dayfile = fs::read_to_string(format!("{root}/template/days/d.rs.tmpl"))?;
|
|
|
|
let mut modfile = fs::read_to_string(format!("{root}/y{year}/src/days/mod.rs"))?;
|
|
|
|
modfile.push_str(format!("\npub mod d{day};").as_str());
|
|
|
|
let bin_path = format!("{root}/y{year}/src/bin/d{day}.rs");
|
|
|
|
fs::write(bin_path, bin)?;
|
|
|
|
fs::write(format!("{root}/y{year}/src/days/d{day}.rs"), dayfile)?;
|
|
|
|
fs::write(format!("{root}/y{year}/src/days/mod.rs"), modfile)?;
|
|
|
|
Ok(())
|
|
|
|
}
|