-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.py
More file actions
99 lines (78 loc) · 2.9 KB
/
Copy pathrelease.py
File metadata and controls
99 lines (78 loc) · 2.9 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
#!/usr/bin/env python3
from __future__ import annotations
import os
import shutil
import subprocess
import sys
from pathlib import Path
from xml.etree import ElementTree
ROOT = Path(__file__).resolve().parents[1]
def existing(paths: list[Path]) -> list[str]:
return [str(path) for path in paths if path.exists()]
def maven_candidates() -> list[str]:
names = ["mvn.cmd", "mvn.bat"] if sys.platform.startswith("win") else ["mvn"]
candidates: list[str] = []
for env_name in ("MAVEN_HOME", "M2_HOME"):
home = os.environ.get(env_name)
if home:
candidates.extend(existing([Path(home) / "bin" / name for name in names]))
candidates.extend(existing([ROOT / "mvnw.cmd", ROOT / "mvnw"]))
if sys.platform.startswith("win"):
candidates.extend(str(path) for path in Path("C:/bin").glob("apache-maven-*/bin/mvn.cmd"))
candidates.extend(name for name in ("mvn", "mvn.cmd") if shutil.which(name))
return candidates
def command_path(name: str) -> str:
if name == "mvn":
candidates = maven_candidates()
else:
candidates = [name, f"{name}.exe", f"{name}.cmd"] if sys.platform.startswith("win") else [name]
for candidate in candidates:
path = shutil.which(candidate) if not Path(candidate).is_absolute() else candidate
if path:
return str(path)
raise SystemExit(f"Required command not found on PATH: {name}")
def run(*args: str, capture: bool = False) -> str:
command = command_path(args[0])
command_args = (command, *args[1:])
if sys.platform.startswith("win") and command.lower().endswith((".cmd", ".bat")):
command_args = ("cmd.exe", "/c", *command_args)
result = subprocess.run(
command_args,
cwd=ROOT,
check=True,
text=True,
stdout=subprocess.PIPE if capture else None,
)
return result.stdout.strip() if capture else ""
def require_clean_worktree() -> None:
status = run("git", "status", "--porcelain", capture=True)
if status:
raise SystemExit("Working tree has uncommitted changes")
def maven_version() -> str:
pom = ElementTree.parse(ROOT / "pom.xml")
version = pom.getroot().findtext("{http://maven.apache.org/POM/4.0.0}version", "").strip()
if not version or version.startswith("${"):
raise SystemExit("Could not read Maven project.version")
return version
def main() -> None:
require_clean_worktree()
version = maven_version()
tag = f"v{version}"
local_jar = ROOT / "target" / "json-editor.jar"
run("mvn", "-q", "clean", "package")
if not local_jar.exists():
raise SystemExit(f"Expected local JAR not found: {local_jar}")
require_clean_worktree()
run("git", "tag", tag)
run("git", "push", "origin", tag)
run(
"gh",
"release",
"create",
tag,
str(local_jar),
"--title",
tag,
)
if __name__ == "__main__":
main()