|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Run in root of repository |
| 3 | +# scripts/release.py <version number> |
| 4 | + |
| 5 | +import sys |
| 6 | +import json |
| 7 | +import subprocess |
| 8 | +from datetime import datetime |
| 9 | + |
| 10 | +README_FILE = "README.md" |
| 11 | +CHANGELOG_FILE = "CHANGELOG.md" |
| 12 | +CARGO_FILE = "Cargo.toml" |
| 13 | +CARGO_LOCK_FILE = "Cargo.lock" |
| 14 | +PACKAGE_JSON_FILE = "stylua-npm-bin/package.json" |
| 15 | +PACKAGE_LOCK_JSON_FILE = "stylua-npm-bin/package-lock.json" |
| 16 | +WASM_PACKAGE_JSON_FILE = "wasm/package.json" |
| 17 | + |
| 18 | +assert len(sys.argv) == 2, "Usage: .github/release.py <version number>" |
| 19 | +VERSION = sys.argv[1] |
| 20 | + |
| 21 | +assert not VERSION.startswith("v"), "Do not prefix the version with 'v'" |
| 22 | + |
| 23 | +CHANGELOG_DATE = datetime.now().strftime("%Y-%m-%d") |
| 24 | + |
| 25 | +# Update version in Cargo.toml |
| 26 | +old_version = None |
| 27 | +new_cargo_toml_lines: list[str] = [] |
| 28 | +with open(CARGO_FILE, "r") as file: |
| 29 | + for line in file: |
| 30 | + if line.startswith("version = "): |
| 31 | + old_version = line.removeprefix('version = "').strip().removesuffix('"') |
| 32 | + new_line = f'version = "{VERSION}"\n' |
| 33 | + new_cargo_toml_lines.append(new_line) |
| 34 | + else: |
| 35 | + new_cargo_toml_lines.append(line) |
| 36 | + |
| 37 | +assert old_version != None, "Old version not found" |
| 38 | + |
| 39 | +with open(CARGO_FILE, "w") as file: |
| 40 | + file.writelines(new_cargo_toml_lines) |
| 41 | + |
| 42 | +# Update version in CHANGELOG.md |
| 43 | +new_changelog_lines: list[str] = [] |
| 44 | +with open(CHANGELOG_FILE, "r") as file: |
| 45 | + lines = file.readlines() |
| 46 | + |
| 47 | + for line in lines: |
| 48 | + if line.startswith("## [Unreleased]"): |
| 49 | + new_changelog_lines.append(line) |
| 50 | + new_changelog_lines.append("\n") |
| 51 | + new_changelog_lines.append(f"## [{VERSION}] - {CHANGELOG_DATE}") |
| 52 | + new_changelog_lines.append("\n") |
| 53 | + elif line.startswith("[unreleased]: "): |
| 54 | + new_changelog_lines.append( |
| 55 | + f"[unreleased]: https://github.com/JohnnyMorganz/StyLua/compare/v{VERSION}...HEAD\n" |
| 56 | + ) |
| 57 | + new_changelog_lines.append( |
| 58 | + f"[{VERSION}]: https://github.com/JohnnyMorganz/StyLua/releases/tag/v{VERSION}\n" |
| 59 | + ) |
| 60 | + else: |
| 61 | + new_changelog_lines.append(line) |
| 62 | + |
| 63 | +with open(CHANGELOG_FILE, "w") as file: |
| 64 | + file.writelines(new_changelog_lines) |
| 65 | + |
| 66 | +# Update version in README.md |
| 67 | +README_REPLACEMENTS = ( |
| 68 | + ("rev: v{old}", "rev: v{new}"), |
| 69 | + ("StyLua:{old}", "StyLua:{new}"), |
| 70 | + ("stylua@{old}", "stylua@{new}"), |
| 71 | +) |
| 72 | + |
| 73 | +new_readme_text = None |
| 74 | +with open(README_FILE, "r") as file: |
| 75 | + new_readme_text = file.read() |
| 76 | + for pattern, replacement in README_REPLACEMENTS: |
| 77 | + needle = pattern.format(old=old_version) |
| 78 | + repl = replacement.format(old=old_version, new=VERSION) |
| 79 | + if needle in new_readme_text: |
| 80 | + new_readme_text = new_readme_text.replace(needle, repl) |
| 81 | + |
| 82 | +assert new_readme_text != None |
| 83 | +with open(README_FILE, "w") as file: |
| 84 | + file.write(new_readme_text) |
| 85 | + |
| 86 | +# Update version in package.json |
| 87 | +package_json_data = None |
| 88 | +with open(PACKAGE_JSON_FILE, "r") as t: |
| 89 | + package_json_data = json.load(t) |
| 90 | + package_json_data["version"] = VERSION |
| 91 | + |
| 92 | +with open(PACKAGE_JSON_FILE, "w") as t: |
| 93 | + json.dump(package_json_data, t) |
| 94 | + |
| 95 | +# Update version in wasm package.json |
| 96 | +package_json_data = None |
| 97 | +with open(WASM_PACKAGE_JSON_FILE, "r") as t: |
| 98 | + package_json_data = json.load(t) |
| 99 | + package_json_data["version"] = VERSION |
| 100 | + |
| 101 | +with open(WASM_PACKAGE_JSON_FILE, "w") as t: |
| 102 | + json.dump(package_json_data, t) |
| 103 | + |
| 104 | +# Update lockfiles |
| 105 | +subprocess.run(["cargo", "check"], check=True) |
| 106 | +# we expect this command to fail: |
| 107 | +subprocess.run(["npm", "install"], cwd="stylua-npm-bin", check=False) |
| 108 | + |
| 109 | +# Run prettier |
| 110 | +subprocess.run( |
| 111 | + [ |
| 112 | + "npx", |
| 113 | + "prettier", |
| 114 | + "--write", |
| 115 | + README_FILE, |
| 116 | + CHANGELOG_FILE, |
| 117 | + PACKAGE_JSON_FILE, |
| 118 | + WASM_PACKAGE_JSON_FILE, |
| 119 | + ], |
| 120 | + check=True, |
| 121 | +) |
| 122 | + |
| 123 | +# Commit |
| 124 | +subprocess.run( |
| 125 | + [ |
| 126 | + "git", |
| 127 | + "add", |
| 128 | + CHANGELOG_FILE, |
| 129 | + README_FILE, |
| 130 | + PACKAGE_JSON_FILE, |
| 131 | + PACKAGE_LOCK_JSON_FILE, |
| 132 | + WASM_PACKAGE_JSON_FILE, |
| 133 | + CARGO_FILE, |
| 134 | + CARGO_LOCK_FILE, |
| 135 | + ], |
| 136 | + check=True, |
| 137 | +) |
| 138 | +subprocess.run(["git", "commit", "-m", f"v{VERSION}"], check=True) |
| 139 | + |
| 140 | +# Tag |
| 141 | +subprocess.run(["git", "tag", "-a", f"v{VERSION}", "-m", f"v{VERSION}"], check=True) |
| 142 | + |
| 143 | +print("Created commit and tag. Validate with 'git show'") |
| 144 | +print("Run `git push` and `git push --tags` to complete release") |
0 commit comments