File size: 1,828 Bytes
3d99d06
 
ade7725
1e0f4c0
3d99d06
 
 
 
 
b578ecd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from pathlib import Path

import tomlkit
from packaging.version import Version

c_file = Path(__file__)
pyproject = c_file.parent.parent / "pyproject.toml"


def get_version(pyproject_path: Path = pyproject) -> str:
    with pyproject_path.open("r") as f:
        data = tomlkit.load(f)
        return data["project"]["version"]  # type: ignore


def update_pyproject_version(version: str, pyproject_path: Path) -> None:
    with pyproject_path.open("r") as f:
        data = tomlkit.load(f)
        data["project"]["version"] = version  # type: ignore

    with pyproject_path.open("w") as f:
        tomlkit.dump(data, f)


def update_readme(version: str, readme_path: Path) -> None:
    """Find version in README table and update it."""
    start = "<!-- START README TABLE -->"
    end = "<!-- END README TABLE -->"

    with readme_path.open("r") as f:
        lines = f.readlines()

    in_table = False
    for i, line in enumerate(lines):
        if start in line:
            in_table = True
        if in_table:
            if "**Version**" in line:
                lines[i] = f"| **Version** | {version} |\n"
                break
        if end in line:
            raise ValueError("**Version** not found in README table.")

    with readme_path.open("w") as f:
        f.writelines(lines)


def main(pyproject_path: Path, readme_path: Path) -> None:
    version = get_version(pyproject_path)
    version = Version(version)
    version = Version(f"{version.major}.{version.minor}.{version.micro + 1}")
    update_pyproject_version(str(version), pyproject_path)
    update_readme(str(version), readme_path)


if __name__ == "__main__":
    c_file = Path(__file__)
    pyproject_path = c_file.parent.parent / "pyproject.toml"
    readme_path = c_file.parent.parent / "README.md"
    main(pyproject_path, readme_path)