From 2158091752b8778884ee9096fb57c8144fc8cf3d Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Sat, 1 Oct 2022 21:39:34 +0100 Subject: [PATCH] Attempt at modifying the cargo tomls with the correct version --- tools/Cargo.lock | 7 ++++ tools/Cargo.toml | 1 + tools/src/release.rs | 89 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/tools/Cargo.lock b/tools/Cargo.lock index 9c1065ce..6b38b187 100644 --- a/tools/Cargo.lock +++ b/tools/Cargo.lock @@ -69,6 +69,12 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" +[[package]] +name = "glob" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" + [[package]] name = "hashbrown" version = "0.12.3" @@ -152,6 +158,7 @@ name = "tools" version = "0.1.0" dependencies = [ "clap", + "glob", "toml_edit", ] diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 7c53cbf7..f735cd0e 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -8,3 +8,4 @@ edition = "2021" [dependencies] clap = "4" toml_edit = "0.14" +glob = "0.3" \ No newline at end of file diff --git a/tools/src/release.rs b/tools/src/release.rs index c2113fc9..3c1d5ebb 100644 --- a/tools/src/release.rs +++ b/tools/src/release.rs @@ -41,23 +41,106 @@ pub fn release(matches: &clap::ArgMatches) -> Result<(), Error> { return Ok(()); } + let project_toml_files = glob_many(&root_directory, &["agb-*/Cargo.toml"])?; + + update_to_version( + &root_directory, + &root_directory.join("agb/Cargo.toml"), + version, + )?; + + for toml_file in project_toml_files { + update_to_version(&root_directory, &toml_file, version)?; + } + + Command::new("just") + .arg("ci") + .spawn() + .map_err(|_| Error::JustCiFailed)?; + todo!() } +fn update_to_version( + root_directory: &Path, + toml_file: &Path, + new_version: &Version, +) -> Result<(), Error> { + let directory_name = toml_file.parent().unwrap(); + let project_name = directory_name.to_string_lossy().replace('-', "_"); + + let toml_file_content = std::fs::read_to_string(toml_file).map_err(|_| Error::ReadTomlFile)?; + let mut cargo_toml = toml_file_content + .parse::() + .map_err(|_| Error::InvalidToml)?; + + let new_version = format!("{new_version}"); + cargo_toml["package"]["version"] = toml_edit::value(&new_version); + + std::fs::write(toml_file, cargo_toml.to_string()).map_err(|_| Error::WriteTomlFile)?; + + for cargo_toml_file in glob_many( + root_directory, + &[ + "agb-*/Cargo.toml", + "agb/Cargo.toml", + "examples/*/Cargo.toml", + "book/games/*/Cargo.toml", + "template/Cargo.toml", + ], + )? { + let toml_file_content = + std::fs::read_to_string(&cargo_toml_file).map_err(|_| Error::ReadTomlFile)?; + let mut cargo_toml = toml_file_content + .parse::() + .map_err(|_| Error::InvalidToml)?; + + if let Some(this_dep) = cargo_toml["dependencies"].get_mut(&project_name) { + match this_dep { + toml_edit::Item::Value(value) => *value = new_version.clone().into(), + toml_edit::Item::Table(t) => t["version"] = toml_edit::value(&new_version), + _ => return Err(Error::InvalidToml), + } + } + + std::fs::write(cargo_toml_file, cargo_toml.to_string()) + .map_err(|_| Error::WriteTomlFile)?; + } + + Ok(()) +} + fn execute_git_command(root_directory: &Path, args: &[&str]) -> Result { let git_cmd = Command::new("git") .args(args) .current_dir(root_directory) .output() - .map_err(|_| Error::GitError("Failed to run command"))?; + .map_err(|_| Error::Git("Failed to run command"))?; - String::from_utf8(git_cmd.stdout).map_err(|_| Error::GitError("Output not utf-8")) + String::from_utf8(git_cmd.stdout).map_err(|_| Error::Git("Output not utf-8")) +} + +fn glob_many(root_directory: &Path, globs: &[&str]) -> Result, Error> { + let mut result = vec![]; + + for g in globs.iter() { + for path in glob::glob(&root_directory.join(g).to_string_lossy()).expect("Invalid glob") { + result.push(path.map_err(|_| Error::Glob)?); + } + } + + Ok(result) } #[derive(Debug)] pub enum Error { FindRootDirectory, - GitError(&'static str), + Git(&'static str), + Glob, + ReadTomlFile, + InvalidToml, + WriteTomlFile, + JustCiFailed, } #[derive(Debug, Clone, PartialEq, Eq)]