Skip to content

Commit

Permalink
build: remove -Dversion_fallback and introduce .VERSION file
Browse files Browse the repository at this point in the history
The version script will now first try to read the version from a
.VERSION file in the source root. If that file does not exist, it will
query git. The .VERSION file is not included in the git repository, it
is generated for inclusion in source tarballs.

Package maintainers who had any use for -Dversion_fallback may want to
create the .VERSION file themselves.

Additionally, some git-specific files have been removed from source
tarballs.
  • Loading branch information
Akaricchi committed Dec 16, 2022
1 parent d32332a commit 0c47f6d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -14,3 +14,4 @@ __pycache__
*~
*-autosave.kra
*.swp
.VERSION
4 changes: 0 additions & 4 deletions meson.build
Expand Up @@ -4,10 +4,6 @@ project('taisei', 'c',
version : run_command([
files('scripts/version.py'),
'--rootdir', meson.project_source_root(),
'--fallback-version', (
get_option('version_fallback').strip() != ''
? get_option('version_fallback').strip()
: 'v1.4-dev')
], check : true).stdout().strip(),
meson_version : '>=0.56.2',
default_options : [
Expand Down
5 changes: 0 additions & 5 deletions meson_options.txt
@@ -1,8 +1,3 @@
option(
'version_fallback',
type : 'string',
description : 'Overrides the version string when not building in a git repository'
)

option(
'developer',
Expand Down
7 changes: 3 additions & 4 deletions scripts/meson.build
@@ -1,10 +1,6 @@

ver_fb = get_option('version_fallback').strip()
version_fallback = ver_fb != '' ? ver_fb : meson.project_version()

common_taiseilib_args = [
'--rootdir', meson.project_source_root(),
'--fallback-version', version_fallback
]

common_taiseilib_defs = [
Expand Down Expand Up @@ -137,3 +133,6 @@ fix_path_command = [fix_path_script]

format_array_script = find_program(files('format-array.py'))
format_array_command = [format_array_script]

on_dist_script = find_program(files('on-meson-dist.py'))
meson.add_dist_script(on_dist_script)
46 changes: 46 additions & 0 deletions scripts/on-meson-dist.py
@@ -0,0 +1,46 @@
#!/usr/bin/env python3

import os
import sys
import pathlib
import taiseilib.version
import shutil


def main(args):
def env_dir_path(v):
try:
s = os.environ[v]
except KeyError:
print("{} is not set".format(v))

p = pathlib.Path(s)
assert p.is_dir()
return p

src_root = env_dir_path('MESON_PROJECT_SOURCE_ROOT')
dist_root = env_dir_path('MESON_PROJECT_DIST_ROOT')
src_version = taiseilib.version.get(rootdir=src_root)

(dist_root / taiseilib.version.OVERRIDE_FILE_NAME).write_text(src_version.string)

remove_files = [
'.dockerignore',
'.gitattributes',
'.gitignore',
'.gitmodules',
'.mailmap',
'checkout',
'pull',
]

for fname in remove_files:
(dist_root / fname).unlink(fname)

shutil.rmtree(str(dist_root / '.github'))


if __name__ == '__main__':
from taiseilib.common import run_main
run_main(main)

20 changes: 18 additions & 2 deletions scripts/taiseilib/version.py
Expand Up @@ -5,12 +5,17 @@
import subprocess
import shlex
import re
import os


class VersionFormatError(common.TaiseiError):
pass


VERSION_FALLBACK = 'v1.4-dev'
OVERRIDE_FILE_NAME = '.VERSION'


class Version(object):
regex = re.compile(r'^v?(\d+)(?:\.(\d+)(?:\.(\d+))?)?(?:[-+](\d+))?(?:[-+](.*))?$')

Expand Down Expand Up @@ -53,13 +58,23 @@ def format(self, template='{string}'):
return template.format(**self.__dict__)


def get(*, rootdir=None, fallback=None, args=common.default_args):
def get(*, rootdir=None, fallback=VERSION_FALLBACK, args=common.default_args):
rootdir = rootdir if rootdir is not None else args.rootdir
fallback = fallback if fallback is not None else args.fallback_version

if rootdir is None:
import pathlib
rootdir = pathlib.Path(__file__).parent
elif not isinstance(rootdir, os.PathLike):
rootdir = pathlib.Path(rootdir)

version_override_path = rootdir / OVERRIDE_FILE_NAME

try:
version_str = version_override_path.read_text().strip()
except FileNotFoundError:
pass
else:
return Version(version_str)

try:
version_str = subprocess.check_output(
Expand All @@ -80,6 +95,7 @@ def get(*, rootdir=None, fallback=None, args=common.default_args):

print(e, file=sys.stderr)
print("Warning: git not found or not a git repository; using fallback version {0}".format(fallback), file=sys.stderr)
print("Hint: if you are packaging Taisei, write the appropriate version into", str(version_override_path), file=sys.stderr)
version_str = fallback

return Version(version_str)
Expand Down

0 comments on commit 0c47f6d

Please sign in to comment.