Can read project version from a file.

This commit is contained in:
Jussi Pakkanen 2021-01-27 20:01:43 +02:00
parent 9eb8b6be28
commit 3f0a0c1582
6 changed files with 47 additions and 6 deletions

View File

@ -1432,7 +1432,9 @@ Project supports the following keyword arguments.
- `version`: which is a free form string describing the version of - `version`: which is a free form string describing the version of
this project. You can access the value in your Meson build files this project. You can access the value in your Meson build files
with `meson.project_version()`. with `meson.project_version()`. Since 0.57.0 this can also be a
`File` object pointing to a file that contains exactly one line of
text.
### run_command() ### run_command()

View File

@ -0,0 +1,12 @@
## Project version can be specified with a file
Meson can be instructed to load project's version string from an
external file like this:
```meson
project('foo', 'c' version: files('VERSION'))
```
The version file must contain exactly one line of text and that will
be set as the project's version. If the line ends in a newline
character, it is removed.

View File

@ -1,4 +1,4 @@
# Copyright 2012-2019 The Meson development team # Copyright 2012-2021 The Meson development team
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
@ -3181,9 +3181,29 @@ external dependencies (including libraries) must go to "dependencies".''')
if not self.is_subproject(): if not self.is_subproject():
self.build.project_name = proj_name self.build.project_name = proj_name
self.active_projectname = proj_name self.active_projectname = proj_name
self.project_version = kwargs.get('version', 'undefined') version = kwargs.get('version', 'undefined')
if not isinstance(self.project_version, str): if isinstance(version, list):
raise InvalidCode('The version keyword argument must be a string.') if len(version) != 1:
raise InvalidCode('Version argument is an array with more than one entry.')
version = version[0]
if isinstance(version, mesonlib.File):
FeatureNew.single_use('version from file', '0.57.0', self.subproject)
self.add_build_def_file(version)
ifname = version.absolute_path(self.environment.source_dir,
self.environment.build_dir)
try:
ver_data = Path(ifname).read_text(encoding='utf-8').split('\n')
except FileNotFoundError:
raise InterpreterException('Version file not found.')
if len(ver_data) == 2 and ver_data[1] == '':
ver_data = ver_data[0:1]
if len(ver_data) != 1:
raise InterpreterException('Version file must contain exactly one line of text.')
self.project_version = ver_data[0]
elif isinstance(version, str):
self.project_version = version
else:
raise InvalidCode('The version keyword argument must be a string or a file.')
if self.build.project_version is None: if self.build.project_version is None:
self.build.project_version = self.project_version self.build.project_version = self.project_version
proj_license = mesonlib.stringlistify(kwargs.get('license', 'unknown')) proj_license = mesonlib.stringlistify(kwargs.get('license', 'unknown'))

View File

@ -5212,6 +5212,12 @@ class AllPlatformTests(BasePlatformTests):
os.utime(str(cmakefile)) os.utime(str(cmakefile))
self.assertReconfiguredBuildIsNoop() self.assertReconfiguredBuildIsNoop()
def test_version_file(self):
srcdir = os.path.join(self.common_test_dir, '2 cpp')
self.init(srcdir)
projinfo = self.introspect('--projectinfo')
self.assertEqual(projinfo['version'], '1.0.0')
class FailureTests(BasePlatformTests): class FailureTests(BasePlatformTests):
''' '''

View File

@ -0,0 +1 @@
1.0.0

View File

@ -1,4 +1,4 @@
project('c++ test', 'cpp') project('c++ test', 'cpp', version: files('VERSION'))
cpp = meson.get_compiler('cpp') cpp = meson.get_compiler('cpp')
if cpp.get_id() == 'intel' if cpp.get_id() == 'intel'