Compare commits

...

5 commits
1.0.1 ... main

8 changed files with 48 additions and 40 deletions

View file

@ -1,4 +1,4 @@
current_version = "1.0.1"
current_version = "1.1.1"
commit = true
tag = true
message = "chore: bump {current_version} → {new_version}"
@ -11,8 +11,8 @@ type = "string"
values = ["alpha", "beta", "stable"]
[file."Cargo.toml"]
format = 'version = "{version}"'
formats = ['version = "{version}"']
[file."Cargo.lock"]
format = """name = "bump2version"
version = "{version}""""
formats = ["""name = "bump2version"
version = "{version}""""]

2
Cargo.lock generated
View file

@ -61,7 +61,7 @@ dependencies = [
[[package]]
name = "bump2version"
version = "1.0.1"
version = "1.1.1"
dependencies = [
"clap",
"regex",

View file

@ -1,6 +1,6 @@
[package]
name = "bump2version"
version = "1.0.1"
version = "1.1.1"
edition = "2021"
description = "⬆️ Easily manage version numbers in your projects."
license = "MIT"
@ -13,10 +13,6 @@ authors = [
"TECHNOFAB <admin@technofab.de>",
]
# 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"

4
src/cli.rs Executable file → Normal file
View file

@ -24,6 +24,10 @@ 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<String>,
/// New version that should be in the files.
#[arg(long = "new-version", value_name = "VERSION")]
pub(crate) new_version: Option<String>,

View file

@ -4,7 +4,7 @@ use serde_derive::Deserialize;
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct Config {
pub(crate) current_version: String,
pub(crate) current_version: Option<String>,
pub(crate) message: Option<String>,
pub(crate) commit: bool,
pub(crate) tag: bool,
@ -35,8 +35,8 @@ pub(crate) struct Part {
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct File {
/// Format to replace, eg. `current_version = "{version}"`
pub(crate) format: String,
/// Formats to replace, eg. `current_version = "{version}"`
pub(crate) formats: Vec<String>,
}
fn default_parse() -> String {

View file

@ -1,6 +0,0 @@
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc = include_str!("../README.md")]
pub(crate) mod cli;
pub(crate) mod config;
pub(crate) mod utils;

View file

@ -2,8 +2,6 @@ use self::cli::Cli;
use crate::config::Config;
use crate::utils::attempt_version_bump;
use clap::Parser;
use config::File;
use std::collections::HashMap;
use std::fs;
use std::process::{exit, Command};
use tracing::{error, info, level_filters::LevelFilter, warn};
@ -40,12 +38,23 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
};
let current_version = config.current_version.clone();
let attempted_new_version = args
.new_version
let current_version = args
.current_version
.clone()
.or(attempt_version_bump(args.clone(), config.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(),
));
if attempted_new_version.is_some() {
let new_version = attempted_new_version.clone().unwrap();
@ -86,22 +95,24 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Update version in specified files
info!(amount = &files.len(), "Updating version in files");
for path in files.clone() {
let content = fs::read_to_string(path)?;
let format = &config_files.get(path).unwrap().format.clone();
let mut content = fs::read_to_string(path)?;
let formats = &config_files.get(path).unwrap().formats.clone();
let old_line = format.replace("{version}", &current_version);
if !content.contains(&old_line) {
warn!(
current_version,
path, "Did not find current version in file"
);
for format in formats {
let old_line = format.replace("{version}", &current_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 new_line = format.replace("{version}", &new_version);
let updated_content = content.replace(&old_line, &new_line);
if !dry_run {
fs::write(path, updated_content)?;
fs::write(path, content)?;
}
}

View file

@ -6,7 +6,11 @@ use std::collections::HashMap;
use std::ops::Index;
use tracing::{error, trace};
pub fn attempt_version_bump(args: Cli, config: Config) -> Option<String> {
pub(crate) fn attempt_version_bump(
args: Cli,
config: Config,
current_version: String,
) -> Option<String> {
let parse_regex = config.parse;
let regex = match Regex::new(&parse_regex) {
Ok(r) => r,
@ -16,7 +20,6 @@ pub fn attempt_version_bump(args: Cli, config: Config) -> Option<String> {
}
};
let current_version = config.current_version;
let mut parsed: HashMap<String, String> = HashMap::new();
if let Some(captures) = regex.captures(&current_version) {