|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# This file is a part of toml++ and is subject to the the terms of the MIT license. |
| 3 | +# Copyright (c) Mark Gillard <[email protected]> |
| 4 | +# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text. |
| 5 | +# SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +import sys |
| 8 | +import re |
| 9 | +from argparse import ArgumentParser |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +def read_text_file(path): |
| 15 | + print(rf'Reading {path}') |
| 16 | + with open(path, r'r', encoding=r'utf-8') as f: |
| 17 | + return f.read() |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +def write_text_file(path, text): |
| 22 | + print(rf'Writing {path}') |
| 23 | + with open(path, r'w', encoding=r'utf-8', newline='\n') as f: |
| 24 | + f.write(text) |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +if __name__ == '__main__': |
| 29 | + |
| 30 | + args = ArgumentParser(r'version.py', description=r'Sets the project version in all the necessary places.') |
| 31 | + args.add_argument(r'version', type=str) |
| 32 | + args = args.parse_args() |
| 33 | + |
| 34 | + version = re.fullmatch(r'\s*([0-9]+)\s*[.,;]\s*([0-9]+)\s*[.,;]\s*([0-9]+)\s*', args.version) |
| 35 | + if not version: |
| 36 | + print(rf"Couldn't parse version triplet from '{args.version}'", file=sys.stderr) |
| 37 | + sys.exit(1) |
| 38 | + version = (int(version[1]), int(version[2]), int(version[3])) |
| 39 | + version_str = rf'{version[0]}.{version[1]}.{version[2]}' |
| 40 | + print(rf'version: {version_str}') |
| 41 | + |
| 42 | + root = Path(__file__).parent.parent.resolve() |
| 43 | + |
| 44 | + path = root / r'meson.build' |
| 45 | + text = read_text_file(path) |
| 46 | + text = re.sub(r'''(\s|^)version\s*:\s*['"].*?['"]''', rf"\1version: '{version_str}'", text, count=1) |
| 47 | + write_text_file(path, text) |
| 48 | + |
| 49 | + path = root / r'CMakeLists.txt' |
| 50 | + text = read_text_file(path) |
| 51 | + text = re.sub(r'''(\s|^)VERSION\s+[0-9](?:[.][0-9]){2}''', rf"\1VERSION {version_str}", text, count=1, flags=re.I) |
| 52 | + write_text_file(path, text) |
| 53 | + |
| 54 | + path = root / r'include/toml++/impl/version.h' |
| 55 | + text = read_text_file(path) |
| 56 | + text = re.sub(r'''(\s*#\s*define\s+TOML_LIB_MAJOR)\s+[0-9]+''', rf"\1 {version[0]}", text) |
| 57 | + text = re.sub(r'''(\s*#\s*define\s+TOML_LIB_MINOR)\s+[0-9]+''', rf"\1 {version[1]}", text) |
| 58 | + text = re.sub(r'''(\s*#\s*define\s+TOML_LIB_PATCH)\s+[0-9]+''', rf"\1 {version[2]}", text) |
| 59 | + write_text_file(path, text) |
| 60 | + |
| 61 | + noop_sub = r'#$%^nbsp^%$#' |
| 62 | + for file in (r'README.md', r'docs/pages/main_page.dox'): |
| 63 | + path = root / file |
| 64 | + text = read_text_file(path) |
| 65 | + text = re.sub(r'''(toml(?:plusplus|\+\+|pp)\s*[/:^]\s*)[0-9](?:[.][0-9]){2}''', rf"\1{noop_sub}{version_str}", text, flags=re.I) |
| 66 | + text = re.sub(r'''(GIT_TAG\s+)(?:v\s*)?[0-9](?:[.][0-9]){2}''', rf"\1v{version_str}", text, flags=re.I) |
| 67 | + text = text.replace(noop_sub, '') |
| 68 | + write_text_file(path, text) |
0 commit comments