Skip to content

Commit 762e9d6

Browse files
committed
generate commit-dates-based version tags and update the version strings of each module for the bundle zip
1 parent 415d7bc commit 762e9d6

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

build.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@
1515

1616
import requests
1717

18-
19-
def get_current_version():
18+
def get_git_command(command):
2019
path = os.getcwd()
2120
procs = subprocess.run(
22-
"git describe --tags --exact-match",
21+
command,
2322
stdout=subprocess.PIPE,
2423
stderr=subprocess.PIPE,
2524
cwd=path,
26-
shell=True
2725
)
2826
if procs.returncode != 0:
2927
return None
3028
return procs.stdout.decode("utf8").strip()
3129

30+
def get_current_version():
31+
return get_git_command("git describe --tags --exact-match".split())
32+
3233
def date_to_version(tag):
3334
# YYYYMMDD
3435
if re.match('\d\d\d\d\d\d\d\d', tag):
@@ -60,11 +61,12 @@ def date_to_version(tag):
6061
# py platform directory
6162
BUNDLE_REQ_DIR = os.path.join(BUNDLE_DIR.format(platform="py"), "requirements")
6263
BUNDLE_ZIP_JSON = os.path.join(BUNDLE_DIR.format(platform="py"), f"{BUNDLE_NAME}.json")
63-
64+
# where the modules are
6465
MODULES_DIR = "libraries"
66+
# the base requirement file
6567
REQUIREMENTS_FILE = "requirements-modules.txt"
66-
67-
SET_VERSION = f"__version__ = '{VERSION_NUMBER}'"
68+
# in-file informations to update (or not)
69+
SET_VERSION_PATTERN = "\n__version__ = '{}'\n"
6870
THIS_REPOSITORY = "https://github.com/Neradoc/Circuitpython_Keyboard_Layouts.git"
6971

7072
PLATFORMS = ["mpy6", "mpy7"]
@@ -98,6 +100,21 @@ def date_to_version(tag):
98100
MPYCROSS = MPYCROSSES[sys.platform]
99101

100102

103+
def file_version_tag(path):
104+
hash = get_git_command(["git", "log", "-1", '--pretty=%H', path])
105+
#ptag = get_git_command(["git", "describe", "--tags", "--always", hash])
106+
#pdate = re.split(r"[~-]", ptag)[0]
107+
ctag = get_git_command(["git", "describe", "--tags", "--always", "--contains", hash])
108+
cdate = re.split(r"[~-]", ctag)[0]
109+
if "." in cdate:
110+
ver = cdate
111+
elif hash.startswith(cdate):
112+
ver = date_to_version(TAG)
113+
else:
114+
ver = date_to_version(cdate[0:8])
115+
return ver
116+
117+
101118
def fmt(path, platform="py"):
102119
"""shortcut for the py directory"""
103120
return path.format(platform=PLATFORM_NAMES[platform])
@@ -141,16 +158,18 @@ def make_bundle_files():
141158
shutil.copytree(MODULES_DIR, fmt(BUNDLE_LIB_DIR))
142159

143160
# change the version number of all the bundles
144-
py_files = os.path.join(fmt(BUNDLE_LIB_DIR), "**", "*.py")
145-
for module in glob.glob(py_files, recursive=True):
146-
with open(module, "r") as fp:
161+
for module_local in list_all_files(MODULES_DIR):
162+
module_file = os.path.join(fmt(BUNDLE_LIB_DIR), module_local)
163+
file_tag = file_version_tag(os.path.join(MODULES_DIR, module_local))
164+
with open(module_file, "r") as fp:
147165
data = fp.read()
148-
data = data.replace(
149-
'\n__version__ = "0.0.0-auto.0"\n',
150-
f"\n{SET_VERSION}\n",
151-
)
152-
with open(module, "w") as fp:
153-
fp.write(data)
166+
if "__version__" in data:
167+
data = data.replace(
168+
'\n__version__ = "0.0.0-auto.0"\n',
169+
SET_VERSION_PATTERN.format(file_tag),
170+
)
171+
with open(module_file, "w") as fp:
172+
fp.write(data)
154173

155174
# list of the modules
156175
all_modules = [

0 commit comments

Comments
 (0)