From 4efb5f1bd1d509075fdf6ae5cd7f5b07544287db Mon Sep 17 00:00:00 2001 From: Dotan Nahum Date: Tue, 22 Oct 2024 11:49:29 +0300 Subject: [PATCH] xtask: bump version takes into account the subcrates --- xtask/src/bump_version.rs | 59 +++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/xtask/src/bump_version.rs b/xtask/src/bump_version.rs index 1d20cf6b0..c55392cd1 100644 --- a/xtask/src/bump_version.rs +++ b/xtask/src/bump_version.rs @@ -40,8 +40,9 @@ impl BumpVersion { /// # Errors /// Returns an error when it could not update one of the resources. pub fn run(&self) -> Result<()> { - self.bump_loco_framework()?; - println!("Bump Loco lib updated successfully"); + self.bump_loco_framework(".")?; + self.bump_loco_framework("loco-gen")?; + self.bump_subcrates_version(&["loco-gen"])?; // change starters from fixed (v0.1.x) to local ("../../") in order // to test all starters against what is going to be released @@ -85,10 +86,12 @@ impl BumpVersion { /// # Errors /// Returns an error when it could not parse the loco Cargo.toml file or has /// an error updating the file. - fn bump_loco_framework(&self) -> Result<()> { + fn bump_loco_framework(&self, path: &str) -> Result<()> { + println!("bumping to `{}` on `{path}`", self.version); + let mut content = String::new(); - let cargo_toml_file = self.base_dir.join("Cargo.toml"); + let cargo_toml_file = self.base_dir.join(path).join("Cargo.toml"); fs::File::open(&cargo_toml_file)?.read_to_string(&mut content)?; if !REPLACE_LOCO_LIB_VERSION_.is_match(&content) { @@ -98,9 +101,10 @@ impl BumpVersion { }); } - let content = REPLACE_LOCO_LIB_VERSION_.replace(&content, |captures: ®ex::Captures| { - format!("{}{}", &captures["name"], self.version) - }); + let content = REPLACE_LOCO_LIB_VERSION_ + .replace(&content, |captures: ®ex::Captures<'_>| { + format!("{}{}", &captures["name"], self.version) + }); let mut modified_file = fs::File::create(cargo_toml_file)?; modified_file.write_all(content.as_bytes())?; @@ -108,6 +112,45 @@ impl BumpVersion { Ok(()) } + fn bump_subcrates_version(&self, crates: &[&str]) -> Result<()> { + let mut content = String::new(); + + let cargo_toml_file = self.base_dir.join("Cargo.toml"); + fs::File::open(&cargo_toml_file)?.read_to_string(&mut content)?; + + println!("in root package:"); + for subcrate in crates { + println!("bumping subcrate `{}` to `{}`", subcrate, self.version); + let re = Regex::new(&format!( + r#"{subcrate}\s*=\s*\{{\s*version\s*=\s*"[0-9]+\.[0-9]+\.[0-9]+",\s*path\s*=\s*"[^"]+"\s*\}}"#, + )) + .unwrap(); + + if !re.is_match(&content) { + return Err(Error::BumpVersion { + path: cargo_toml_file.clone(), + package: subcrate.to_string(), + }); + } + + // Replace the full version line with the new version, keeping the structure + // intact + content = re + .replace( + &content, + format!( + r#"{subcrate} = {{ version = "{}", path = "./{subcrate}" }}"#, + self.version + ), + ) + .to_string(); + } + + let mut modified_file = fs::File::create(cargo_toml_file)?; + modified_file.write_all(content.as_bytes())?; + Ok(()) + } + /// Update the dependencies of loco-rs in all starter projects to the given /// version. /// @@ -149,7 +192,7 @@ impl BumpVersion { }); } content = REPLACE_LOCO_PACKAGE_VERSION - .replace_all(&content, |_captures: ®ex::Captures| { + .replace_all(&content, |_captures: ®ex::Captures<'_>| { replace_with.to_string() }) .to_string();