|
| 1 | +import argparse, re, sys |
1 | 2 | from os import path |
2 | | -import re, sys |
3 | 3 | from types import SimpleNamespace as sns |
4 | 4 | import tomli, tomli_w |
5 | 5 |
|
6 | 6 | # Init logger |
7 | 7 | sys.path.insert(0, path.join(path.dirname(__file__), '../src')) |
8 | 8 | from translate_messages.lib import log # type: ignore |
| 9 | +msgs = sns(err_invalid_arg='You must pass --<major|minor|patch> as an argument.') |
9 | 10 |
|
10 | 11 | # Init project data |
11 | 12 | pyproject_path = path.join(path.dirname(__file__), '../pyproject.toml') |
12 | 13 | log.info(f'Loading {pyproject_path}...') |
13 | 14 | with open(pyproject_path, 'rb') as file : pyproject = tomli.load(file) |
14 | 15 | project = sns(**pyproject['project']) |
15 | 16 |
|
16 | | -def update_changelog_url(): |
17 | | - new_ver_tag = f'{project.name}-{project.version}' |
| 17 | +def parse_args(): |
| 18 | + parser = argparse.ArgumentParser(description="Bump versions in pyproject.toml + README.md") |
| 19 | + parser.add_argument('--major', action='store_true', help='Bump the major version') |
| 20 | + parser.add_argument('--minor', action='store_true', help='Bump the minor version') |
| 21 | + parser.add_argument('--patch', action='store_true', help='Bump the patch version') |
| 22 | + return parser.parse_args() |
| 23 | + |
| 24 | +def bump_pyproject_versions(bump_type): # project.version + .urls['Changelog'] |
| 25 | + |
| 26 | + # Bump project.version |
| 27 | + current_ver = project.version |
| 28 | + major, minor, patch = map(int, current_ver.split('.')) |
| 29 | + if bump_type == 'major' : major += 1 ; minor = 0 ; patch = 0 |
| 30 | + elif bump_type == 'minor' : minor += 1 ; patch = 0 |
| 31 | + elif bump_type == 'patch' : patch += 1 |
| 32 | + else : raise ValueError(msgs.err_invalid_arg) |
| 33 | + new_ver = f'{major}.{minor}.{patch}' |
| 34 | + pyproject['project']['version'] = new_ver |
| 35 | + with open(pyproject_path, 'wb') as file : tomli_w.dump(pyproject, file) |
| 36 | + log.success(f"Bumped project.version in pyproject.toml from [{current_ver}] to [{new_ver}]") |
| 37 | + |
| 38 | + # Bump version tag in Changelog URL |
| 39 | + new_ver_tag = f'{project.name}-{new_ver}' |
18 | 40 | new_changelog_url = f"{project.urls['Releases']}/tag/{new_ver_tag}" |
19 | | - log.data(f'Generated changelog URL: {new_changelog_url}') |
| 41 | + log.data(f'Generated Changelog URL: {new_changelog_url}') |
20 | 42 | log.info(f"{ 'Updating' if 'Changelog' in project.urls else 'Adding new' } Changelog URL in pyproject.toml...") |
21 | 43 | project.urls['Changelog'] = new_changelog_url |
22 | | - pyproject['project'] = vars(project) # update og dict for dumping |
23 | 44 | with open(pyproject_path, 'wb') as file : tomli_w.dump(pyproject, file) |
24 | 45 | log.success(f'Bumped Changelog URL version tag to [{new_ver_tag}]!') |
25 | 46 |
|
26 | | -def update_readme_versions(): |
| 47 | + return new_ver |
| 48 | + |
| 49 | +def update_readme_versions(new_ver): # in shield URLs |
27 | 50 | readme_path = path.join(path.dirname(__file__), '../README.md') |
28 | 51 | log.info('Updating versions in README.md...') |
29 | 52 | with open(readme_path, 'r', encoding='utf-8') as file : readme_content = file.read() |
30 | | - updated_readme_content = re.sub(r'\d+\.\d+\.\d+', project.version, readme_content) |
| 53 | + updated_readme_content = re.sub(r'\d+\.\d+\.\d+', new_ver, readme_content) |
31 | 54 | with open(readme_path, 'w', encoding='utf-8') as file : file.write(updated_readme_content) |
32 | | - log.success(f'Updated versions in README URLs to [{project.version}]!') |
| 55 | + log.success(f'Updated versions in README URLs to [{new_ver}]!') |
33 | 56 |
|
34 | | -update_changelog_url() |
35 | | -update_readme_versions() |
| 57 | +# Run MAIN routine |
| 58 | +args = parse_args() |
| 59 | +bump_type = 'major' if args.major else 'minor' if args.minor else 'patch' if args.patch else None |
| 60 | +if not bump_type : raise ValueError(msgs.err_invalid_arg) |
| 61 | +update_readme_versions(bump_pyproject_versions(bump_type)) |
0 commit comments