|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Project values |
| 4 | +PROJECT_GO = "dataset.go" |
| 5 | +CODEMETA_JSON = "codemeta.json" |
| 6 | + |
| 7 | +# |
| 8 | +# No changes below this line |
| 9 | +# |
| 10 | +import sys |
| 11 | +import os |
| 12 | +import json |
| 13 | + |
| 14 | +def inc_patch_no(v = "0.0.0"): |
| 15 | + """inc_patch_no takes a symvar and increments the right most value in the dot touple""" |
| 16 | + parts = v.split(".") |
| 17 | + if len(parts) == 3: |
| 18 | + #major_no = parts[0] |
| 19 | + #minor_no = parts[1] |
| 20 | + patch_no = int(parts[2]) |
| 21 | + patch_no += 1 |
| 22 | + parts[2] = str(patch_no) |
| 23 | + return ".".join(parts) |
| 24 | + else: |
| 25 | + return v |
| 26 | + |
| 27 | +def update_codemeta_json(codemeta_json, current_version, next_version): |
| 28 | + with open(codemeta_json, mode = "r", encoding = "utf-8") as f: |
| 29 | + src = f.read() |
| 30 | + meta = json.loads(src) |
| 31 | + meta["version"] = next_version |
| 32 | + downloadURL = meta["downloadUrl"] |
| 33 | + meta["downloadUrl"] = downloadURL.replace(current_version, next_version) |
| 34 | + src = json.dumps(meta, indent = 4) |
| 35 | + with open(codemeta_json, mode = "w", encoding = "utf-8") as f: |
| 36 | + f.write(src) |
| 37 | + print(f"updated {codemeta_json} version from {current_version} to {next_version}") |
| 38 | + return True |
| 39 | + |
| 40 | +def update_project_go(project_go, current_version, next_version): |
| 41 | + with open(project_go, mode = "r", encoding = "utf-8") as f: |
| 42 | + src = f.read() |
| 43 | + txt = src.replace(f"Version = `v{current_version}`", f"Version = `v{next_version}`") |
| 44 | + with open(project_go, mode = "w", encoding = "utf-8") as f: |
| 45 | + f.write(txt) |
| 46 | + print(f"updated {project_go} Version from v{current_version} to v{next_version}") |
| 47 | + return True |
| 48 | + |
| 49 | +def usage(app_name): |
| 50 | + app_name = os.path.basename(app_name) |
| 51 | + print(f""" |
| 52 | +USAGE: {app_name} OPTIONS |
| 53 | +
|
| 54 | +SYNOPSIS |
| 55 | +
|
| 56 | +{app_name} shows or sets the proposed new value for a version number. |
| 57 | +By defaut it proposes a increment in the patch no of a symvar string. |
| 58 | +If the -y, --yes option is included it will commit the change in patch |
| 59 | +number to the codemeta.json and project's go file. |
| 60 | +
|
| 61 | +OPTIONS |
| 62 | +
|
| 63 | + --set VALUE explicitly set the value of the new version string |
| 64 | + -y, --yes commit the changes proposed to the Codemeta and Go file. |
| 65 | +""") |
| 66 | + |
| 67 | +# |
| 68 | +# Main processing |
| 69 | +# |
| 70 | +def main(args): |
| 71 | + if ("-h" in args) or ("-help" in args) or ("--help" in args): |
| 72 | + usage(args[0]) |
| 73 | + sys.exit(0) |
| 74 | + current_version = "" |
| 75 | + next_version = "" |
| 76 | + meta = {} |
| 77 | + with open(CODEMETA_JSON,"r") as f: |
| 78 | + src = f.read() |
| 79 | + meta = json.loads(src) |
| 80 | + |
| 81 | + current_version = meta["version"] |
| 82 | + |
| 83 | + if ("--set" in args): |
| 84 | + i = args.index("--set") |
| 85 | + i += 1 |
| 86 | + if len(args) < i: |
| 87 | + print("Missing new version number after set", args) |
| 88 | + sys.exit(1) |
| 89 | + next_version = args[i] |
| 90 | + if next_version[0] == "v": |
| 91 | + next_version = next_version[1:] |
| 92 | + else: |
| 93 | + next_version = inc_patch_no(current_version) |
| 94 | + |
| 95 | + if ("--yes" in args) or ("-yes" in args) or ("-y" in args): |
| 96 | + update_codemeta_json(CODEMETA_JSON, current_version, next_version) |
| 97 | + update_project_go(PROJECT_GO, current_version, next_version) |
| 98 | + else: |
| 99 | + print("current version:", current_version) |
| 100 | + print("proposed version:", next_version) |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + main(sys.argv[:]) |
0 commit comments