From 613cb5e2d7c3f1469f4f91b86fa982eda472465b Mon Sep 17 00:00:00 2001 From: Marvin W Date: Thu, 1 Aug 2024 15:33:56 +0200 Subject: Meson: Adjust version generation to match CMake and be compatible with vala-language-server --- libdino/meson.build | 11 ++------ libdino/src/version.vala.in | 5 ++++ libdino/version.py | 65 ++++++++++++++++++++++++++++++++------------- 3 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 libdino/src/version.vala.in (limited to 'libdino') diff --git a/libdino/meson.build b/libdino/meson.build index 85487d48..f88cbb06 100644 --- a/libdino/meson.build +++ b/libdino/meson.build @@ -1,18 +1,11 @@ # version_vala dot_git = meson.current_source_dir() / '../.git' version_file = meson.current_source_dir() / '../VERSION' -command = [prog_python, files('version.py'), version_file, '@OUTPUT@', '--git-repo', meson.current_source_dir()] +command = [prog_python, files('version.py'), version_file, '--git-repo', meson.current_source_dir()] if prog_git.found() command += ['--git', prog_git] endif -depend_files = [] -if fs.exists(dot_git) - depend_files += [dot_git] -endif -if fs.exists(version_file) - depend_files += [version_file] -endif -version_vala = custom_target('libdino_version_vala', command: command, output: 'version.vala', depend_files: depend_files) +version_vala = vcs_tag(command: command, input: 'src/version.vala.in', output: 'version.vala', replace_string: '%VERSION%') # libdino dependencies = [ diff --git a/libdino/src/version.vala.in b/libdino/src/version.vala.in new file mode 100644 index 00000000..2711d4a6 --- /dev/null +++ b/libdino/src/version.vala.in @@ -0,0 +1,5 @@ +namespace Dino { + +public const string VERSION = "%VERSION%"; + +} diff --git a/libdino/version.py b/libdino/version.py index d34db6a8..dfbadb76 100644 --- a/libdino/version.py +++ b/libdino/version.py @@ -1,36 +1,65 @@ import argparse import subprocess -VERSION_VALA = """\ -namespace Dino {{ +import re -public const string VERSION = "{}"; -}} -""" - -def compute_version(file, git_repo, git): +def compute_version_from_file(file): try: with open(file) as f: - return f.read().strip() + version_from_file = f.read().strip() + if version_from_file != "": + if version_from_file.startswith("RELEASE "): + return version_from_file[8:].strip() + if version_from_file.startswith("PRERELEASE "): + return version_from_file[11:].strip() + return version_from_file except FileNotFoundError: pass - return subprocess.check_output([git, "describe", "--tags"], cwd=git_repo, text=True).strip() + return None + + +def compute_version_from_git(git_repo, git): + try: + git_release_tag = subprocess.check_output([git, "describe", "--tags", "--abbrev=0"], + cwd=git_repo, text=True).strip() + if re.match("^v?([0-9]+[.]?[0-9]*[.]?[0-9]*)(-[.0-9A-Za-z-]+)?([+][.0-9A-Za-z-]+)?$", git_release_tag) is None: + return None + git_describe = subprocess.check_output([git, "describe", "--tags"], cwd=git_repo, text=True).strip() + if git_release_tag == git_describe: + return git_release_tag + matches = re.match("^.*-([0-9]+)-g([0-9a-f]+)$", git_describe) + if matches is None: + return None + git_tag_offset = matches.groups()[0] + git_commit_hash = matches.groups()[1] + git_commit_time = subprocess.check_output([git, "show", "--format=%cd", "--date=format:%Y%m%d", "-s"], + cwd=git_repo, text=True).strip() + return "%s~git%s.%s.%s" % (git_release_tag, git_tag_offset, git_commit_time, git_commit_hash) + except subprocess.CalledProcessError: + pass + return None + + +def compute_version(file, git_repo, git): + version_from_file = compute_version_from_file(file) + if version_from_file is not None: + return version_from_file + version_from_git = compute_version_from_git(git_repo, git) + if version_from_git is not None: + return version_from_git + return "" -def generate_version_vala(version): - if "\\" in version or "\"" in version: - raise ValueError(f"invalid version {version!r}") - return VERSION_VALA.format(version) def main(): p = argparse.ArgumentParser(description="Compute the Dino version") p.add_argument("--git-repo", help="Path to checked out git repository") p.add_argument("--git", help="Path to git executable", default="git") - p.add_argument("version_file", metavar="VERSION_FILE", help="Use this file's contents as version if the file exists") - p.add_argument("output", metavar="OUTPUT", help="Vala file to output to") + p.add_argument("version_file", metavar="VERSION_FILE", + help="Use this file's contents as version if the file exists") args = p.parse_args() - out = generate_version_vala(compute_version(args.version_file, args.git_repo, args.git)) - with open(args.output, "w") as f: - f.write(out) + version = compute_version(args.version_file, args.git_repo, args.git) + print(version) + if __name__ == "__main__": main() -- cgit v1.2.3-70-g09d2