diff --git a/.bumpversion.toml b/.bumpversion.toml index 0971522..26e90be 100644 --- a/.bumpversion.toml +++ b/.bumpversion.toml @@ -1,4 +1,4 @@ -current_version = "1.1.1" +current_version = "1.0.0" commit = true tag = true message = "chore: bump {current_version} → {new_version}" @@ -11,8 +11,8 @@ type = "string" values = ["alpha", "beta", "stable"] [file."Cargo.toml"] -formats = ['version = "{version}"'] +format = 'version = "{version}"' [file."Cargo.lock"] -formats = ["""name = "bump2version" -version = "{version}""""] +format = """name = "bump2version" +version = "{version}"""" diff --git a/Cargo.lock b/Cargo.lock index 63177c3..f5f2424 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -61,7 +61,7 @@ dependencies = [ [[package]] name = "bump2version" -version = "1.1.1" +version = "1.0.0" dependencies = [ "clap", "regex", diff --git a/Cargo.toml b/Cargo.toml index 5a74359..73bdd09 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bump2version" -version = "1.1.1" +version = "1.0.0" edition = "2021" description = "⬆️ Easily manage version numbers in your projects." license = "MIT" @@ -13,6 +13,10 @@ authors = [ "TECHNOFAB ", ] +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +crate-type = ["cdylib"] + [dependencies] clap = { version = "4.5.1", features = ["derive"] } regex = "1.10.3" diff --git a/src/cli.rs b/src/cli.rs old mode 100644 new mode 100755 index f07903f..cf8a99c --- a/src/cli.rs +++ b/src/cli.rs @@ -24,10 +24,6 @@ pub(crate) struct Cli { #[arg(short = 'n', long = "dry-run", default_value_t = false)] pub(crate) dry_run: bool, - /// Current version - #[arg(long = "current-version", value_name = "VERSION")] - pub(crate) current_version: Option, - /// New version that should be in the files. #[arg(long = "new-version", value_name = "VERSION")] pub(crate) new_version: Option, diff --git a/src/config.rs b/src/config.rs index a2e0e1d..3da1183 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,7 +4,7 @@ use serde_derive::Deserialize; #[derive(Debug, Clone, Deserialize)] pub(crate) struct Config { - pub(crate) current_version: Option, + pub(crate) current_version: String, pub(crate) message: Option, pub(crate) commit: bool, pub(crate) tag: bool, @@ -13,8 +13,8 @@ pub(crate) struct Config { #[serde(default = "default_serialize")] pub(crate) serialize: String, - pub(crate) part: Option>, - pub(crate) file: Option>, + pub(crate) part: HashMap, + pub(crate) file: HashMap, } #[derive(Debug, Clone, Deserialize)] @@ -35,8 +35,8 @@ pub(crate) struct Part { #[derive(Debug, Clone, Deserialize)] pub(crate) struct File { - /// Formats to replace, eg. `current_version = "{version}"` - pub(crate) formats: Vec, + /// Format to replace, eg. `current_version = "{version}"` + pub(crate) format: String, } fn default_parse() -> String { diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..022eb5e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,6 @@ +#![cfg_attr(docsrs, feature(doc_auto_cfg))] +#![doc = include_str!("../README.md")] + +pub(crate) mod cli; +pub(crate) mod config; +pub(crate) mod utils; diff --git a/src/main.rs b/src/main.rs index 45903fd..ace6f87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,23 +38,12 @@ fn main() -> Result<(), Box> { } }; - let current_version = args - .current_version + let current_version = config.current_version.clone(); + + let attempted_new_version = args + .new_version .clone() - .or(config.current_version.clone()); - - if current_version.is_none() { - error!("No current version could be determined, either set in config or pass as arg"); - exit(1); - } - - let current_version = current_version.unwrap(); - - let attempted_new_version = args.new_version.clone().or(attempt_version_bump( - args.clone(), - config.clone(), - current_version.clone(), - )); + .or(attempt_version_bump(args.clone(), config.clone())); if attempted_new_version.is_some() { let new_version = attempted_new_version.clone().unwrap(); @@ -68,8 +57,7 @@ fn main() -> Result<(), Box> { .or(config.message) .unwrap_or("Bump version: {current_version} → {new_version}".to_string()); - let config_files = config.file.unwrap_or_default(); - let files: Vec<&String> = config_files.keys().collect(); + let files: Vec<&String> = config.file.keys().collect(); // Check if Git working directory is clean if fs::metadata(".git").is_ok() { @@ -95,24 +83,22 @@ fn main() -> Result<(), Box> { // Update version in specified files info!(amount = &files.len(), "Updating version in files"); for path in files.clone() { - let mut content = fs::read_to_string(path)?; - let formats = &config_files.get(path).unwrap().formats.clone(); + let content = fs::read_to_string(path)?; + let format = &config.file.get(path).unwrap().format; - for format in formats { - let old_line = format.replace("{version}", ¤t_version); - if !content.contains(&old_line) { - warn!( - current_version, - path, "Did not find current version in file" - ); - } - - let new_line = format.replace("{version}", &new_version); - content = content.replace(&old_line, &new_line); + let old_line = format.replace("{version}", ¤t_version); + if !content.contains(&old_line) { + warn!( + current_version, + path, "Did not find current version in file" + ); } + let new_line = format.replace("{version}", &new_version); + let updated_content = content.replace(&old_line, &new_line); + if !dry_run { - fs::write(path, content)?; + fs::write(path, updated_content)?; } } diff --git a/src/utils.rs b/src/utils.rs index 15d0606..7e31e12 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -6,11 +6,7 @@ use std::collections::HashMap; use std::ops::Index; use tracing::{error, trace}; -pub(crate) fn attempt_version_bump( - args: Cli, - config: Config, - current_version: String, -) -> Option { +pub fn attempt_version_bump(args: Cli, config: Config) -> Option { let parse_regex = config.parse; let regex = match Regex::new(&parse_regex) { Ok(r) => r, @@ -20,6 +16,7 @@ pub(crate) fn attempt_version_bump( } }; + let current_version = config.current_version; let mut parsed: HashMap = HashMap::new(); if let Some(captures) = regex.captures(¤t_version) { @@ -44,7 +41,7 @@ pub(crate) fn attempt_version_bump( for label in order.clone() { if let Some(part) = parsed.get_mut(label) { - let part_cfg = part_configs.as_ref().and_then(|c| c.get(label)); + let part_cfg = part_configs.get(label); if label == args.bump { match part_cfg