2024-04-17 09:55:50 -04:00
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
use anyhow::{Context, Result};
|
|
|
|
|
|
|
|
use crate::{
|
2024-04-21 14:22:01 -04:00
|
|
|
cargo_toml::updated_cargo_toml,
|
2024-04-17 09:55:50 -04:00
|
|
|
info_file::{ExerciseInfo, InfoFile},
|
2024-04-21 13:26:19 -04:00
|
|
|
DEBUG_PROFILE,
|
2024-04-17 09:55:50 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
fn update_cargo_toml(
|
|
|
|
exercise_infos: &[ExerciseInfo],
|
|
|
|
current_cargo_toml: &str,
|
|
|
|
exercise_path_prefix: &[u8],
|
2024-04-21 14:22:01 -04:00
|
|
|
cargo_toml_path: &str,
|
2024-04-17 09:55:50 -04:00
|
|
|
) -> Result<()> {
|
2024-04-21 14:22:01 -04:00
|
|
|
let updated_cargo_toml =
|
|
|
|
updated_cargo_toml(exercise_infos, current_cargo_toml, exercise_path_prefix)?;
|
2024-04-17 09:55:50 -04:00
|
|
|
|
2024-04-21 14:22:01 -04:00
|
|
|
fs::write(cargo_toml_path, updated_cargo_toml)
|
|
|
|
.context("Failed to write the `Cargo.toml` file")?;
|
2024-04-17 09:55:50 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update() -> Result<()> {
|
|
|
|
let info_file = InfoFile::parse()?;
|
|
|
|
|
2024-04-21 13:26:19 -04:00
|
|
|
if DEBUG_PROFILE {
|
2024-04-17 09:55:50 -04:00
|
|
|
update_cargo_toml(
|
|
|
|
&info_file.exercises,
|
2024-04-25 13:58:55 -04:00
|
|
|
include_str!("../../dev-Cargo.toml"),
|
2024-04-17 09:55:50 -04:00
|
|
|
b"../",
|
2024-04-21 14:22:01 -04:00
|
|
|
"dev/Cargo.toml",
|
2024-04-17 09:55:50 -04:00
|
|
|
)
|
|
|
|
.context("Failed to update the file `dev/Cargo.toml`")?;
|
|
|
|
|
|
|
|
println!("Updated `dev/Cargo.toml`");
|
|
|
|
} else {
|
|
|
|
let current_cargo_toml =
|
|
|
|
fs::read_to_string("Cargo.toml").context("Failed to read the file `Cargo.toml`")?;
|
2024-04-21 14:22:01 -04:00
|
|
|
update_cargo_toml(&info_file.exercises, ¤t_cargo_toml, b"", "Cargo.toml")
|
2024-04-17 09:55:50 -04:00
|
|
|
.context("Failed to update the file `Cargo.toml`")?;
|
|
|
|
|
|
|
|
println!("Updated `Cargo.toml`");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|