Repository navigation
Expand file tree
/
Copy pathmodify_version.py
More file actions
50 lines (37 loc) · 1.47 KB
/
Copy pathmodify_version.py
File metadata and controls
50 lines (37 loc) · 1.47 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import annotations
import json
import datetime
from pathlib import Path
VERSION_MAJOR = "1"
VERSION_MINOR = "0"
VERSION_DATA = datetime.datetime.now().strftime("%Y%m%d%H%M")
def modify_version(pathfile_json: Path, new_version: str) -> None:
"""
Modify the 'version' key in a JSON file with a new version string.
Args:
pathfile_json (Path): The path to the JSON file.
new_version (str): The new version string to be set.
Returns:
None
"""
try:
data = json.loads(pathfile_json.read_text())
data["version"] = new_version
pathfile_json.write_text(json.dumps(data, indent=4))
print(f"Version updated to {new_version} in {pathfile_json}")
except FileNotFoundError:
print(f"File not name: {pathfile_json}")
except json.JSONDecodeError:
print(f"Invalid JSON format in {pathfile_json}")
except KeyError:
print(f"Key 'version' not found in {pathfile_json}")
if __name__ == "__main__":
# Prompt the user to enter the path of the JSON file
current_path_py = Path(__file__).resolve().parent
# Generate a new version string based on the current date and time
pathfile_json = current_path_py.parent / "build/package.json"
new_version = f'{VERSION_MAJOR}.{VERSION_MINOR}.{VERSION_DATA}'
# Call the modify_version function with the provided path and new version
modify_version(pathfile_json, new_version)