|
| 1 | +use regex::Regex; |
| 2 | +use std::fs; |
| 3 | +use std::io::{self, Write}; |
| 4 | +use std::process::Command; |
| 5 | + |
| 6 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 7 | + println!("🔍 Version Manager - Solo"); |
| 8 | + println!("=========================\n"); |
| 9 | + |
| 10 | + // File paths |
| 11 | + let package_json_path = "package.json"; |
| 12 | + let cargo_toml_path = "src-tauri/Cargo.toml"; |
| 13 | + let tauri_conf_path = "src-tauri/tauri.conf.json"; |
| 14 | + |
| 15 | + // Read current version |
| 16 | + let current_version = get_current_version(package_json_path)?; |
| 17 | + println!("📦 Current version: {}\n", current_version); |
| 18 | + |
| 19 | + // Ask for new version |
| 20 | + print!("✏️ Enter the new version (e.g., 0.0.17): "); |
| 21 | + io::stdout().flush()?; |
| 22 | + |
| 23 | + let mut new_version = String::new(); |
| 24 | + io::stdin().read_line(&mut new_version)?; |
| 25 | + let new_version = new_version.trim(); |
| 26 | + |
| 27 | + // Validate version format |
| 28 | + if !is_valid_version(new_version) { |
| 29 | + eprintln!("❌ Error: Invalid version format! Use format: X.Y.Z"); |
| 30 | + std::process::exit(1); |
| 31 | + } |
| 32 | + |
| 33 | + println!( |
| 34 | + "\n🔄 Updating version from {} to {}...\n", |
| 35 | + current_version, new_version |
| 36 | + ); |
| 37 | + |
| 38 | + // Update files |
| 39 | + update_package_json(package_json_path, ¤t_version, new_version)?; |
| 40 | + println!("✅ Updated: {}", package_json_path); |
| 41 | + |
| 42 | + update_cargo_toml(cargo_toml_path, ¤t_version, new_version)?; |
| 43 | + println!("✅ Updated: {}", cargo_toml_path); |
| 44 | + |
| 45 | + update_tauri_conf(tauri_conf_path, ¤t_version, new_version)?; |
| 46 | + println!("✅ Updated: {}", tauri_conf_path); |
| 47 | + |
| 48 | + // Create commit |
| 49 | + println!("\n📝 Creating commit..."); |
| 50 | + create_commit(new_version)?; |
| 51 | + |
| 52 | + println!("\n🎉 Version successfully updated to {}!", new_version); |
| 53 | + println!("📌 Commit created: chore: bump version to {}", new_version); |
| 54 | + |
| 55 | + Ok(()) |
| 56 | +} |
| 57 | + |
| 58 | +fn get_current_version(package_json_path: &str) -> Result<String, Box<dyn std::error::Error>> { |
| 59 | + let content = fs::read_to_string(package_json_path)?; |
| 60 | + let re = Regex::new(r#""version":\s*"([^"]+)""#)?; |
| 61 | + |
| 62 | + if let Some(caps) = re.captures(&content) { |
| 63 | + Ok(caps[1].to_string()) |
| 64 | + } else { |
| 65 | + Err("Could not find version in package.json".into()) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +fn is_valid_version(version: &str) -> bool { |
| 70 | + let re = Regex::new(r"^\d+\.\d+\.\d+$").unwrap(); |
| 71 | + re.is_match(version) |
| 72 | +} |
| 73 | + |
| 74 | +fn update_package_json( |
| 75 | + path: &str, |
| 76 | + old_version: &str, |
| 77 | + new_version: &str, |
| 78 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 79 | + let content = fs::read_to_string(path)?; |
| 80 | + let pattern = format!(r#""version": "{}""#, old_version); |
| 81 | + let replacement = format!(r#""version": "{}""#, new_version); |
| 82 | + let updated = content.replace(&pattern, &replacement); |
| 83 | + fs::write(path, updated)?; |
| 84 | + Ok(()) |
| 85 | +} |
| 86 | + |
| 87 | +fn update_cargo_toml( |
| 88 | + path: &str, |
| 89 | + old_version: &str, |
| 90 | + new_version: &str, |
| 91 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 92 | + let content = fs::read_to_string(path)?; |
| 93 | + let pattern = format!(r#"version = "{}""#, old_version); |
| 94 | + let replacement = format!(r#"version = "{}""#, new_version); |
| 95 | + let updated = content.replace(&pattern, &replacement); |
| 96 | + fs::write(path, updated)?; |
| 97 | + Ok(()) |
| 98 | +} |
| 99 | + |
| 100 | +fn update_tauri_conf( |
| 101 | + path: &str, |
| 102 | + old_version: &str, |
| 103 | + new_version: &str, |
| 104 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 105 | + let content = fs::read_to_string(path)?; |
| 106 | + let pattern = format!(r#""version": "{}""#, old_version); |
| 107 | + let replacement = format!(r#""version": "{}""#, new_version); |
| 108 | + let updated = content.replace(&pattern, &replacement); |
| 109 | + fs::write(path, updated)?; |
| 110 | + Ok(()) |
| 111 | +} |
| 112 | + |
| 113 | +fn create_commit(version: &str) -> Result<(), Box<dyn std::error::Error>> { |
| 114 | + // Add modified files |
| 115 | + let status = Command::new("git") |
| 116 | + .args(&[ |
| 117 | + "add", |
| 118 | + "package.json", |
| 119 | + "src-tauri/Cargo.toml", |
| 120 | + "src-tauri/tauri.conf.json", |
| 121 | + ]) |
| 122 | + .status()?; |
| 123 | + |
| 124 | + if !status.success() { |
| 125 | + return Err("Error adding files to git".into()); |
| 126 | + } |
| 127 | + |
| 128 | + // Create commit |
| 129 | + let commit_message = format!("chore: bump version to {}", version); |
| 130 | + let status = Command::new("git") |
| 131 | + .args(&["commit", "-m", &commit_message]) |
| 132 | + .status()?; |
| 133 | + |
| 134 | + if !status.success() { |
| 135 | + return Err("Error creating commit".into()); |
| 136 | + } |
| 137 | + |
| 138 | + Ok(()) |
| 139 | +} |
0 commit comments