|
| 1 | +/// Release automation script for GDScript Formatter |
| 2 | +/// |
| 3 | +/// This script automates the release process by: |
| 4 | +/// 1. Prompting for version bump type (major, minor, or patch) |
| 5 | +/// 2. Verifying the git tag doesn't already exist |
| 6 | +/// 3. Updating the version in Cargo.toml |
| 7 | +/// 4. Running `cargo update` to update Cargo.lock |
| 8 | +/// 5. Running `cargo build --release` to verify the build works |
| 9 | +/// 6. Committing the changes with a standardized message |
| 10 | +/// 7. Creating a git tag with the new version |
| 11 | +/// 8. Generating a changelog and copying it to clipboard |
| 12 | +/// |
| 13 | +/// Usage: |
| 14 | +/// cargo run --bin make_release |
| 15 | +/// |
| 16 | +/// After the script completes, you'll need to manually push: |
| 17 | +/// git push origin main |
| 18 | +/// git push origin <version> |
| 19 | +use std::fs; |
| 20 | +use std::io::{self, Write}; |
| 21 | +use std::process::{Command, exit}; |
| 22 | + |
| 23 | +fn main() { |
| 24 | + println!("=== GDScript Formatter Release Script ===\n"); |
| 25 | + |
| 26 | + let cargo_toml = fs::read_to_string("Cargo.toml").expect("Failed to read Cargo.toml"); |
| 27 | + |
| 28 | + let current_version = cargo_toml |
| 29 | + .lines() |
| 30 | + .find(|line| line.starts_with("version = ")) |
| 31 | + .and_then(|line| line.split('"').nth(1)) |
| 32 | + .expect("Failed to find version in Cargo.toml"); |
| 33 | + |
| 34 | + println!("Current version: {}", current_version); |
| 35 | + |
| 36 | + let parts: Vec<&str> = current_version.split('.').collect(); |
| 37 | + if parts.len() != 3 { |
| 38 | + eprintln!("Invalid version format"); |
| 39 | + exit(1); |
| 40 | + } |
| 41 | + let (major, minor, patch) = ( |
| 42 | + parts[0].parse::<u32>().unwrap(), |
| 43 | + parts[1].parse::<u32>().unwrap(), |
| 44 | + parts[2].parse::<u32>().unwrap(), |
| 45 | + ); |
| 46 | + |
| 47 | + print!("\nVersion bump type? [major/minor/patch]: "); |
| 48 | + io::stdout().flush().unwrap(); |
| 49 | + let mut bump_type = String::new(); |
| 50 | + io::stdin().read_line(&mut bump_type).unwrap(); |
| 51 | + let bump_type = bump_type.trim().to_lowercase(); |
| 52 | + |
| 53 | + let new_version = match bump_type.as_str() { |
| 54 | + "major" => format!("{}.0.0", major + 1), |
| 55 | + "minor" => format!("{}.{}.0", major, minor + 1), |
| 56 | + "patch" => format!("{}.{}.{}", major, minor, patch + 1), |
| 57 | + _ => { |
| 58 | + eprintln!("Invalid bump type. Use major, minor, or patch."); |
| 59 | + exit(1); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + println!("\nNew version will be: {}", new_version); |
| 64 | + |
| 65 | + let tag_exists = Command::new("git") |
| 66 | + .args(&["rev-parse", &new_version]) |
| 67 | + .output() |
| 68 | + .map(|output| output.status.success()) |
| 69 | + .unwrap_or(false); |
| 70 | + |
| 71 | + if tag_exists { |
| 72 | + eprintln!("Error: Git tag '{}' already exists!", new_version); |
| 73 | + exit(1); |
| 74 | + } |
| 75 | + |
| 76 | + println!("✓ Git tag does not exist"); |
| 77 | + |
| 78 | + let updated_cargo_toml = cargo_toml.replace( |
| 79 | + &format!("version = \"{}\"", current_version), |
| 80 | + &format!("version = \"{}\"", new_version), |
| 81 | + ); |
| 82 | + fs::write("Cargo.toml", updated_cargo_toml).expect("Failed to write Cargo.toml"); |
| 83 | + println!("✓ Updated Cargo.toml"); |
| 84 | + |
| 85 | + println!("\nRunning cargo update..."); |
| 86 | + let update_status = Command::new("cargo") |
| 87 | + .arg("update") |
| 88 | + .status() |
| 89 | + .expect("Failed to run cargo update"); |
| 90 | + |
| 91 | + if !update_status.success() { |
| 92 | + eprintln!("cargo update failed"); |
| 93 | + exit(1); |
| 94 | + } |
| 95 | + println!("✓ cargo update successful"); |
| 96 | + |
| 97 | + println!("\nRunning cargo build..."); |
| 98 | + let build_status = Command::new("cargo") |
| 99 | + .args(&["build", "--release"]) |
| 100 | + .status() |
| 101 | + .expect("Failed to run cargo build"); |
| 102 | + |
| 103 | + if !build_status.success() { |
| 104 | + eprintln!("cargo build failed"); |
| 105 | + exit(1); |
| 106 | + } |
| 107 | + println!("✓ cargo build successful"); |
| 108 | + |
| 109 | + println!("\nAdding files to git..."); |
| 110 | + let add_status = Command::new("git") |
| 111 | + .args(&["add", "Cargo.toml", "Cargo.lock"]) |
| 112 | + .status() |
| 113 | + .expect("Failed to run git add"); |
| 114 | + |
| 115 | + if !add_status.success() { |
| 116 | + eprintln!("git add failed"); |
| 117 | + exit(1); |
| 118 | + } |
| 119 | + |
| 120 | + let commit_msg = format!("Update to version {}", new_version); |
| 121 | + let commit_status = Command::new("git") |
| 122 | + .args(&["commit", "-m", &commit_msg]) |
| 123 | + .status() |
| 124 | + .expect("Failed to run git commit"); |
| 125 | + |
| 126 | + if !commit_status.success() { |
| 127 | + eprintln!("git commit failed"); |
| 128 | + exit(1); |
| 129 | + } |
| 130 | + println!("✓ Committed changes"); |
| 131 | + |
| 132 | + let tag_status = Command::new("git") |
| 133 | + .args(&["tag", &new_version]) |
| 134 | + .status() |
| 135 | + .expect("Failed to run git tag"); |
| 136 | + |
| 137 | + if !tag_status.success() { |
| 138 | + eprintln!("git tag failed"); |
| 139 | + exit(1); |
| 140 | + } |
| 141 | + println!("✓ Created tag '{}'", new_version); |
| 142 | + |
| 143 | + println!("\nGenerating changelog..."); |
| 144 | + let shortlog_output = Command::new("git") |
| 145 | + .args(&["shortlog", &format!("{}..HEAD", current_version)]) |
| 146 | + .output() |
| 147 | + .expect("Failed to run git shortlog"); |
| 148 | + |
| 149 | + let shortlog = String::from_utf8_lossy(&shortlog_output.stdout); |
| 150 | + println!("\n--- Changelog ---"); |
| 151 | + println!("{}", shortlog); |
| 152 | + println!("--- End Changelog ---\n"); |
| 153 | + |
| 154 | + let clipcopy_status = Command::new("clipcopy") |
| 155 | + .stdin(std::process::Stdio::piped()) |
| 156 | + .spawn() |
| 157 | + .and_then(|mut child| { |
| 158 | + if let Some(mut stdin) = child.stdin.take() { |
| 159 | + stdin.write_all(shortlog.as_bytes())?; |
| 160 | + } |
| 161 | + child.wait() |
| 162 | + }); |
| 163 | + |
| 164 | + match clipcopy_status { |
| 165 | + Ok(status) if status.success() => { |
| 166 | + println!("✓ Changelog copied to clipboard"); |
| 167 | + } |
| 168 | + _ => { |
| 169 | + println!("⚠ Failed to copy to clipboard (clipcopy might not be available)"); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + println!("\n=== Release {} ready! ===", new_version); |
| 174 | + println!("\nNext steps:"); |
| 175 | + println!(" git push origin main"); |
| 176 | + println!(" git push origin {}", new_version); |
| 177 | +} |
0 commit comments