Skip to content

Commit

Permalink
refactor: refactor out versioning crates
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jun 3, 2024
1 parent deb0b6a commit 5b6d1cf
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod cargo_command;
mod publish;
mod update;
mod versioning;

use bpaf::Bpaf;

Expand Down
51 changes: 10 additions & 41 deletions src/update.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
fs::{self, File},
path::{Path, PathBuf},
str::FromStr,
time::{SystemTime, UNIX_EPOCH},
};

Expand All @@ -14,7 +13,8 @@ use git_cliff_core::{
};
use git_cmd::Repo as GitCommand;
use semver::Version;
use toml_edit::{DocumentMut, Formatted, Value};

use crate::versioning::cargo::CargoToml;

const CHANGELOG_NAME: &str = "CHANGELOG.md";

Expand Down Expand Up @@ -216,57 +216,26 @@ impl Update {
Ok(())
}

fn update_toml(manifest_path: &Path, cb: impl FnOnce(&mut DocumentMut)) -> Result<()> {
let manifest = fs::read_to_string(manifest_path)?;
let mut manifest = DocumentMut::from_str(&manifest)?;
cb(&mut manifest);
let serialized = manifest.to_string();
fs::write(manifest_path, serialized)?;
Ok(())
}

fn update_cargo_toml_version_for_workspace(
&self,
packages: &[&Package],
next_version: &str,
) -> Result<()> {
let manifest_path = self.metadata.workspace_root.as_std_path().join("Cargo.toml");
Self::update_toml(&manifest_path, |manifest| {
let Some(table) = manifest
.get_mut("workspace")
.and_then(|item| item.as_table_mut())
.and_then(|table| table.get_mut("dependencies"))
.and_then(|item| item.as_table_mut())
else {
return;
};
for package in packages {
if let Some(version) = table
.get_mut(&package.name)
.and_then(|item| item.as_inline_table_mut())
.and_then(|item| item.get_mut("version"))
{
*version = Value::String(Formatted::new(next_version.to_string()));
}
}
})
let mut workspace_toml = CargoToml::new(&manifest_path)?;
for package in packages {
workspace_toml.set_workspace_dependency_version(&package.name, next_version)?;
}
Ok(())
}

fn update_cargo_toml_version_for_package(
manifest_path: &Path,
next_version: &str,
) -> Result<()> {
Self::update_toml(manifest_path, |manifest| {
let Some(version) = manifest
.get_mut("package")
.and_then(|item| item.as_table_mut())
.and_then(|table| table.get_mut("version"))
.and_then(|item| item.as_value_mut())
else {
return;
};
*version = Value::String(Formatted::new(next_version.to_string()));
})
let mut cargo_toml = CargoToml::new(manifest_path)?;
cargo_toml.set_version(next_version)?;
cargo_toml.save()
}

/// # Errors
Expand Down
66 changes: 66 additions & 0 deletions src/versioning/cargo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::{
fs,
path::{Path, PathBuf},
str::FromStr,
};

use anyhow::Result;
use toml_edit::{DocumentMut, Formatted, Value};

pub struct CargoToml {
manifest_path: PathBuf,
toml: DocumentMut,
}

impl CargoToml {
pub fn new(manifest_path: &Path) -> Result<Self> {
let manifest = fs::read_to_string(manifest_path)?;
let toml = DocumentMut::from_str(&manifest)?;
Ok(Self { manifest_path: manifest_path.to_path_buf(), toml })
}

pub fn save(self) -> Result<()> {
let serialized = self.toml.to_string();
fs::write(self.manifest_path, serialized)?;
Ok(())
}

pub fn set_version(&mut self, version: &str) -> Result<()> {
let Some(version_field) = self
.toml
.get_mut("package")
.and_then(|item| item.as_table_mut())
.and_then(|table| table.get_mut("version"))
.and_then(|item| item.as_value_mut())
else {
anyhow::bail!("No `package.version` field found: {:?}", self.manifest_path);
};
*version_field = Value::String(Formatted::new(version.to_string()));
Ok(())
}

pub fn set_workspace_dependency_version(
&mut self,
crate_name: &str,
version: &str,
) -> Result<()> {
let Some(table) = self
.toml
.get_mut("workspace")
.and_then(|item| item.as_table_mut())
.and_then(|table| table.get_mut("dependencies"))
.and_then(|item| item.as_table_mut())
else {
anyhow::bail!("`workspace.dependencies` field not found: {:?}", self.manifest_path);
};
let Some(version_field) = table
.get_mut(crate_name)
.and_then(|item| item.as_inline_table_mut())
.and_then(|item| item.get_mut("version"))
else {
anyhow::bail!("dependency `{}` not found: {:?}", crate_name, self.manifest_path);
};
*version_field = Value::String(Formatted::new(version.to_string()));
Ok(())
}
}
2 changes: 2 additions & 0 deletions src/versioning/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#![allow(clippy::module_name_repetitions)]
pub mod cargo;

0 comments on commit 5b6d1cf

Please sign in to comment.