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