-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor out versioning crates
- Loading branch information
Showing
4 changed files
with
79 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#![allow(clippy::module_name_repetitions)] | ||
pub mod cargo; |