Attempt at modifying the cargo tomls with the correct version

This commit is contained in:
Gwilym Kuiper 2022-10-01 21:39:34 +01:00
parent 9af8b19859
commit 2158091752
3 changed files with 94 additions and 3 deletions

7
tools/Cargo.lock generated
View file

@ -69,6 +69,12 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "glob"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.12.3" version = "0.12.3"
@ -152,6 +158,7 @@ name = "tools"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"glob",
"toml_edit", "toml_edit",
] ]

View file

@ -8,3 +8,4 @@ edition = "2021"
[dependencies] [dependencies]
clap = "4" clap = "4"
toml_edit = "0.14" toml_edit = "0.14"
glob = "0.3"

View file

@ -41,23 +41,106 @@ pub fn release(matches: &clap::ArgMatches) -> Result<(), Error> {
return Ok(()); 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!() 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::<toml_edit::Document>()
.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::<toml_edit::Document>()
.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<String, Error> { fn execute_git_command(root_directory: &Path, args: &[&str]) -> Result<String, Error> {
let git_cmd = Command::new("git") let git_cmd = Command::new("git")
.args(args) .args(args)
.current_dir(root_directory) .current_dir(root_directory)
.output() .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<Vec<std::path::PathBuf>, 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)] #[derive(Debug)]
pub enum Error { pub enum Error {
FindRootDirectory, FindRootDirectory,
GitError(&'static str), Git(&'static str),
Glob,
ReadTomlFile,
InvalidToml,
WriteTomlFile,
JustCiFailed,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]