Skip to content

Commit 084bccf

Browse files
committed
change the version number by counting commits with a file
allow overriding the major and minor version in the file (resetting the commits count to the last time the version line was changed)
1 parent f5ad523 commit 084bccf

1 file changed

Lines changed: 39 additions & 17 deletions

File tree

build.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ def date_to_version(tag):
5252
# TODO: give each file a different version number possibly
5353
# (that of the latest released change if possible)
5454
TAG = get_current_version() or datetime.date.today().strftime("%Y%m%d")
55-
VERSION_NUMBER = date_to_version(TAG)
5655
# the dirs for putting the things in it
5756
BUILD_DIR = "_build"
5857
BUILD_DEPS = os.path.join(BUILD_DIR, "deps")
@@ -111,19 +110,37 @@ def file_version_tag(path):
111110
"""
112111
Find a suitable version tag for a file using commit dates.
113112
"""
114-
hash = get_git_command(["git", "log", "-1", "--pretty=%H", path])
115-
# ptag = get_git_command(["git", "describe", "--tags", "--always", hash])
116-
# pdate = re.split(r"[~-]", ptag)[0]
117-
ctag = get_git_command(
118-
["git", "describe", "--tags", "--always", "--contains", hash]
119-
)
120-
cdate = re.split(r"[~-]", ctag)[0]
121-
if "." in cdate:
122-
ver = cdate
123-
elif hash.startswith(cdate):
124-
ver = date_to_version(TAG)
125-
else:
126-
ver = date_to_version(cdate[0:8])
113+
logs = get_git_command(["git", "log", "--pretty=%H", path])
114+
num_commits = len(logs.split("\n"))
115+
#
116+
major = 0
117+
minor = num_commits // 10
118+
patch = num_commits % 10
119+
#
120+
with open(path, "r") as fp:
121+
data = fp.read()
122+
m = re.search(r'__version__ = ["\']([0-9.]+)-auto\.0["\']', data)
123+
if m:
124+
file_version = semver.VersionInfo.parse(m.group(1))
125+
#
126+
line = len(data.split("__version__")[0].split("\n"))
127+
vtag = get_git_command(["git", "blame", "-L", f"{line},{line}", path]).split(" ")[0]
128+
logs = get_git_command(["git", "log", "--pretty=%H", f"--after={vtag}", path]).split("\n")
129+
print(f"{vtag} {line:2d} {num_commits:2d} {len(logs):2d} {path}")
130+
#
131+
if file_version.major:
132+
major = file_version.major
133+
num_commits = len(logs)
134+
minor = num_commits // 10
135+
patch = num_commits % 10
136+
if file_version.minor:
137+
minor = file_version.minor
138+
num_commits = len(logs)
139+
patch = num_commits
140+
# if file_version.patch:
141+
# patch = file_version.patch
142+
#
143+
ver = f"{major}.{minor}.{patch}"
127144
return ver
128145

129146

@@ -173,9 +190,10 @@ def write_version_to(module_local, file_tag):
173190
with open(module_file, "r") as fp:
174191
data = fp.read()
175192
if "__version__" in data:
176-
data = data.replace(
177-
'\n__version__ = "0.0.0-auto.0"\n',
193+
data = re.sub(
194+
r'\n__version__ = ["\']([0-9.]+)-auto\.0["\']\n',
178195
SET_VERSION_PATTERN.format(file_tag),
196+
data,
179197
)
180198
with open(module_file, "w") as fp:
181199
fp.write(data)
@@ -186,8 +204,10 @@ def make_bundle_files():
186204
# copy all the layouts and keycodes
187205
shutil.copytree(MODULES_DIR, fmt(BUNDLE_LIB_DIR))
188206

207+
module_versions = {}
189208
# change the version number of all the bundles
190209
for module_local in glob.glob(MODULES_DIR + "/*"):
210+
module_name = os.path.basename(module_local).replace(".py","")
191211
if os.path.isdir(module_local):
192212
# get all versions
193213
versions = []
@@ -201,9 +221,11 @@ def make_bundle_files():
201221
for sub_module in list_all_files(module_local):
202222
sub_local_file = os.path.join(module_local, sub_module)
203223
write_version_to(sub_local_file, file_tag)
224+
module_versions[module_name] = file_tag
204225
elif module_local.endswith(".py"):
205226
file_tag = file_version_tag(module_local)
206227
write_version_to(module_local, file_tag)
228+
module_versions[module_name] = file_tag
207229

208230
# list of the modules
209231
all_modules = [
@@ -225,7 +247,7 @@ def make_bundle_files():
225247
json_data[module] = {
226248
"package": False,
227249
"pypi_name": "",
228-
"version": VERSION_NUMBER,
250+
"version": module_versions[module],
229251
"repo": THIS_REPOSITORY,
230252
"path": "lib/" + module,
231253
"dependencies": [], # "adafruit_hid"

0 commit comments

Comments
 (0)