-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_version.py
executable file
·113 lines (97 loc) · 3.4 KB
/
update_version.py
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
#!/usr/bin/env python3
# Project values
CODEMETA_JSON = "codemeta.json"
#
# No changes below this line
#
import sys
import os
import json
def inc_patch_no(v = "0.0.0"):
"""inc_patch_no takes a symvar and increments the right most value in the dot touple"""
suffix = ''
if "-" in v:
(v, suffix) = v.split("-")
parts = v.split(".")
if len(parts) == 3:
#major_no = parts[0]
#minor_no = parts[1]
patch_no = int(parts[2])
patch_no += 1
parts[2] = str(patch_no)
v = ".".join(parts)
if suffix != '':
return f'{v}-{suffix}'
return v
def update_codemeta_json(codemeta_json, current_version, next_version):
with open(codemeta_json, mode = "r", encoding = "utf-8") as f:
src = f.read()
meta = json.loads(src)
meta["version"] = next_version
if "downloadUrl" in meta:
downloadURL = meta["downloadUrl"]
meta["downloadUrl"] = downloadURL.replace(current_version, next_version)
src = json.dumps(meta, indent = 4)
with open(codemeta_json, mode = "w", encoding = "utf-8") as f:
f.write(src)
print(f"updated {codemeta_json} version from {current_version} to {next_version}")
return True
def update_project_go(project_go, current_version, next_version):
with open(project_go, mode = "r", encoding = "utf-8") as f:
src = f.read()
txt = src.replace(f"Version = `v{current_version}`", f"Version = `v{next_version}`")
with open(project_go, mode = "w", encoding = "utf-8") as f:
f.write(txt)
print(f"updated {project_go} Version from v{current_version} to v{next_version}")
return True
def usage(app_name):
app_name = os.path.basename(app_name)
print(f"""
USAGE: {app_name} OPTIONS
SYNOPSIS
{app_name} shows or sets the proposed new value for a version number.
By defaut it proposes a increment in the patch no of a symvar string.
If the -y, --yes option is included it will commit the change in patch
number to the codemeta.json and project's go file.
OPTIONS
--set VALUE explicitly set the value of the new version string
-y, --yes commit the changes proposed to the Codemeta and Go file.
""")
#
# Main processing
#
def main(args):
if ("-h" in args) or ("-help" in args) or ("--help" in args):
usage(args[0])
sys.exit(0)
current_version = ""
next_version = ""
meta = {}
project_go = ""
with open(CODEMETA_JSON,"r") as f:
src = f.read()
meta = json.loads(src)
if "name" in meta and meta["name"] != "":
project_go = meta["name"] + ".go"
current_version = meta["version"]
if ("--set" in args):
i = args.index("--set")
i += 1
if len(args) < i:
print("Missing new version number after set", args)
sys.exit(1)
next_version = args[i]
if next_version[0] == "v":
next_version = next_version[1:]
else:
next_version = inc_patch_no(current_version)
if ("--yes" in args) or ("-yes" in args) or ("-y" in args):
print("current version:", current_version)
print("new version:", next_version)
update_codemeta_json(CODEMETA_JSON, current_version, next_version)
update_project_go(project_go, current_version, next_version)
else:
print("current version:", current_version)
print("proposed version:", next_version)
if __name__ == "__main__":
main(sys.argv[:])