|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# A script that increments the language server version, |
| 4 | +# updates the npm package versions of the editor extensions, |
| 5 | +# updates the changelog and creates an annotated Git tag. |
| 6 | + |
| 7 | +import argparse |
| 8 | +import subprocess |
| 9 | +import re |
| 10 | +import os |
| 11 | +import tempfile |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +from utils.cli import prompt_by, title |
| 15 | +from utils.properties import PropertiesFile |
| 16 | +from utils.changelog import ChangelogFile |
| 17 | + |
| 18 | +class Version: |
| 19 | + def __init__(self, major, minor, patch): |
| 20 | + self.major = major |
| 21 | + self.minor = minor |
| 22 | + self.patch = patch |
| 23 | + |
| 24 | + def __str__(self): |
| 25 | + return f"{self.major}.{self.minor}.{self.patch}" |
| 26 | + |
| 27 | +def parse_version(s): |
| 28 | + match = re.search(r"(\d+)\.(\d+)\.(\d+)", s) |
| 29 | + if match == None: |
| 30 | + raise ValueError(f"Incorrectly formatted version: {s}") |
| 31 | + return Version(int(match.group(1)), int(match.group(2)), int(match.group(3))) |
| 32 | + |
| 33 | +def increment_major(ver): |
| 34 | + return Version(ver.major + 1, 0, 0) |
| 35 | + |
| 36 | +def increment_minor(ver): |
| 37 | + return Version(ver.major, ver.minor + 1, 0) |
| 38 | + |
| 39 | +def increment_patch(ver): |
| 40 | + return Version(ver.major, ver.minor, ver.patch + 1) |
| 41 | + |
| 42 | +def command_output(cmd, cwd): |
| 43 | + return subprocess.check_output(cmd, cwd=cwd).decode("utf-8").strip() |
| 44 | + |
| 45 | +def git_last_tag(repo_path): |
| 46 | + return command_output(["git", "describe", "--abbrev=0"], cwd=repo_path) |
| 47 | + |
| 48 | +def git_history_since_last_tag(repo_path): |
| 49 | + return re.split(r"[\r\n]+", command_output(["git", "log", "--oneline", f"{git_last_tag(repo_path)}..HEAD"], cwd=repo_path)) |
| 50 | + |
| 51 | +def git_branch(repo_path): |
| 52 | + return command_output(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=repo_path) |
| 53 | + |
| 54 | +def git_working_dir_is_clean(repo_path): |
| 55 | + return len(command_output(["git", "status", "--porcelain"], cwd=repo_path)) == 0 |
| 56 | + |
| 57 | +INCREMENTS = { |
| 58 | + "major": increment_major, |
| 59 | + "minor": increment_minor, |
| 60 | + "patch": increment_patch |
| 61 | +} |
| 62 | +EDITOR = os.environ.get("EDITOR", "vim") # https://stackoverflow.com/questions/6309587/call-up-an-editor-vim-from-a-python-script |
| 63 | +PROJECT_DIR = Path(__file__).parent.parent |
| 64 | +PROJECT_VERSION_KEY = "projectVersion" |
| 65 | + |
| 66 | +def main(): |
| 67 | + parser = argparse.ArgumentParser(description="A small utility for updating the project's version and creating tags.") |
| 68 | + parser.add_argument("--bump-only", action="store_true", help="Whether only the version should be bumped, without tagging the current version.") |
| 69 | + |
| 70 | + args = parser.parse_args() |
| 71 | + |
| 72 | + title("Project Version Updater") |
| 73 | + |
| 74 | + if not git_working_dir_is_clean(PROJECT_DIR): |
| 75 | + print("Commit any pending changes first to make sure the working directory is in a clean state!") |
| 76 | + return |
| 77 | + if git_branch(PROJECT_DIR) != "main": |
| 78 | + print("Switch to the main branch first!") |
| 79 | + return |
| 80 | + |
| 81 | + properties = PropertiesFile(str(PROJECT_DIR / "gradle.properties")) |
| 82 | + version = parse_version(properties[PROJECT_VERSION_KEY]) |
| 83 | + |
| 84 | + # Current version |
| 85 | + |
| 86 | + if not args.bump_only: |
| 87 | + print() |
| 88 | + print(f"Releasing version {version}.") |
| 89 | + print() |
| 90 | + |
| 91 | + # Fetch new changelog message from user |
| 92 | + temp = tempfile.NamedTemporaryFile(delete=False) |
| 93 | + temp_path = Path(temp.name).absolute() |
| 94 | + |
| 95 | + history = git_history_since_last_tag(PROJECT_DIR) |
| 96 | + formatted_history = [f"# {commit}" for commit in history] |
| 97 | + initial_message = [ |
| 98 | + "", |
| 99 | + "", |
| 100 | + "# Please enter a changelog/release message.", |
| 101 | + f"# This is the history since the last tag:" |
| 102 | + ] + formatted_history |
| 103 | + |
| 104 | + with open(temp_path, "w") as temp_contents: |
| 105 | + temp_contents.write("\n".join(initial_message)) |
| 106 | + |
| 107 | + subprocess.call([EDITOR, str(temp_path)]) |
| 108 | + |
| 109 | + with open(temp_path, "r") as temp_contents: |
| 110 | + changelog_message = [line.strip() for line in temp_contents.readlines() if not line.startswith("#") and len(line.strip()) > 0] |
| 111 | + |
| 112 | + temp.close() |
| 113 | + temp_path.unlink() |
| 114 | + |
| 115 | + if not changelog_message: |
| 116 | + print("No message, exiting...") |
| 117 | + return |
| 118 | + |
| 119 | + print("Updating changelog...") |
| 120 | + changelog = ChangelogFile(PROJECT_DIR / "CHANGELOG.md") |
| 121 | + changelog.prepend_version(version, changelog_message) |
| 122 | + |
| 123 | + print("Creating Git tag...") |
| 124 | + tag_message = "\n".join([f"Version {version}", ""] + changelog_message) |
| 125 | + subprocess.run(["git", "tag", "-a", f"{version}", "-m", tag_message], cwd=PROJECT_DIR) |
| 126 | + |
| 127 | + # Next version |
| 128 | + |
| 129 | + increment = None |
| 130 | + while increment not in INCREMENTS.keys(): |
| 131 | + increment = input("How do you want to increment? [major/minor/patch] ") |
| 132 | + |
| 133 | + new_version = INCREMENTS[increment](version) |
| 134 | + |
| 135 | + # Apply new (development) version to project |
| 136 | + print(f"Updating next dev version to {new_version}...") |
| 137 | + properties[PROJECT_VERSION_KEY] = str(new_version) |
| 138 | + |
| 139 | + print("Creating Git commit for next dev version...") |
| 140 | + commit_message = f"Bump version to {new_version}" |
| 141 | + subprocess.run(["git", "add", "."], cwd=PROJECT_DIR) |
| 142 | + subprocess.run(["git", "commit", "-m", commit_message], cwd=PROJECT_DIR) |
| 143 | + |
| 144 | +main() |
0 commit comments