bump2version/src/utils.rs

101 lines
3.2 KiB
Rust
Raw Normal View History

2024-02-19 16:50:38 +02:00
use crate::cli::Cli;
use regex::Regex;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fs;
2024-02-19 16:50:38 +02:00
pub fn get_current_version_from_config(config_content: &str) -> Option<String> {
let current_version_regex =
Regex::new(r#"\[bumpversion\]\s*current_version\s*=\s*(?P<version>\d+(\.\d+){0,2})\s*"#)
.unwrap();
2024-02-19 16:50:38 +02:00
if let Some(captures) = current_version_regex.captures(config_content) {
if let Some(version) = captures.name("version") {
return Some(version.as_str().to_string());
}
}
None
}
2024-02-19 16:50:38 +02:00
// Function to read files from the configuration file
pub fn read_files_from_config(config_file: &str) -> Result<HashSet<String>, std::io::Error> {
let config_content = fs::read_to_string(config_file)?;
let mut config_files = HashSet::new();
2024-02-19 16:50:38 +02:00
for line in config_content.lines() {
if let Some(file_section) = line.strip_prefix("[bumpversion:file:") {
if let Some(file_name) = file_section.split(']').next() {
config_files.insert(file_name.trim().to_string());
}
}
}
Ok(config_files)
}
pub fn attempt_version_bump(args: Cli) -> Option<String> {
let parse_regex = args.parse.clone();
let regex = match Regex::new(&parse_regex) {
Ok(r) => r,
Err(_) => {
eprintln!("--patch '{}' is not a valid regex", args.parse.clone());
return None;
}
};
let config_content = fs::read_to_string(args.config_file.clone()).unwrap();
let current_version = get_current_version_from_config(&config_content).unwrap_or_else(|| {
panic!("Failed to extract current version from config file");
});
// let current_version = args.current_version.unwrap_or("".to_string());
let mut parsed: HashMap<String, String> = HashMap::new();
if let Some(captures) = regex.captures(&current_version) {
2024-11-14 06:02:29 +02:00
for name in regex.capture_names().flatten() {
if let Some(capture) = captures.name(name) {
parsed.insert(name.to_string(), capture.as_str().to_string());
}
2024-02-19 16:50:38 +02:00
}
}
let order: Vec<&str> = args
.serialize
.match_indices('{')
.map(|(i, _)| args.serialize[i + 1..].split('}').next().unwrap().trim())
2024-02-19 16:50:38 +02:00
.collect();
let mut bumped = false;
2024-02-19 16:50:38 +02:00
for label in order {
if let Some(part) = parsed.get_mut(label) {
if label == args.bump {
if let Ok(new_value) = part.parse::<u64>() {
*part = (new_value + 1).to_string();
bumped = true;
} else {
eprintln!("Failed to parse '{}' as u64", part);
return None;
}
} else if bumped {
*part = "0".to_string();
2024-02-19 16:50:38 +02:00
}
}
}
if bumped {
let new_version = format!(
"{}.{}.{}",
parsed.get("major").unwrap_or(&"0".to_string()),
parsed.get("minor").unwrap_or(&"0".to_string()),
parsed.get("patch").unwrap_or(&"0".to_string())
2024-02-19 16:50:38 +02:00
);
let version = args
.serialize
.replace("{major}.{minor}.{patch}", &new_version);
Some(version)
2024-02-19 16:50:38 +02:00
} else {
None
}
}