1+ import re
12import sys
23from pathlib import Path
34from typing import Literal
45
56import semver
6- import tomlkit
77
88LITERAL_PART = Literal ["major" , "minor" , "patch" ]
99
1010
11- def get_version_from_pyproject_toml ( file_path : Path ) -> str :
12- with file_path .open ("r" ) as file :
13- data = tomlkit . parse ( file .read () )
14- version = data [ "project" ][ "version" ] # pyright: ignore[reportIndexIssue]
15- assert isinstance ( version , str )
16- return version
11+ def get_version_from_init ( init_path : Path ) -> str :
12+ with init_path .open ("r" ) as file :
13+ data = file .readline ( )
14+ match = re . search ( r'^__version__ = "(.*)"' , data )
15+ assert match is not None
16+ return match . group ( 1 )
1717
1818
1919def bump_version (version : str , part : LITERAL_PART ) -> str :
@@ -26,12 +26,15 @@ def bump_version(version: str, part: LITERAL_PART) -> str:
2626 return semver .bump_patch (version )
2727
2828
29- def update_toml_project_version (file_path : Path , new_version : str ) -> None :
30- with file_path .open ("r" ) as file :
31- data = tomlkit .parse (file .read ())
32- data ["project" ]["version" ] = new_version # pyright: ignore[reportIndexIssue]
33- with file_path .open ("w" ) as file :
34- file .write (tomlkit .dumps (data ))
29+ def update_init_version (init_path : Path , new_version : str ) -> None :
30+ with init_path .open ("r" ) as file :
31+ lines = file .readlines ()
32+
33+ # Update the first line with the new version
34+ lines [0 ] = f'__version__ = "{ new_version } "\n '
35+
36+ with init_path .open ("w" ) as file :
37+ file .writelines (lines )
3538
3639
3740def update_readme_badge (readme_path : Path , new_version : str ) -> None :
@@ -48,23 +51,23 @@ def update_readme_badge(readme_path: Path, new_version: str) -> None:
4851 file .writelines (lines )
4952
5053
51- def main (part : LITERAL_PART , pyproject_path : Path , readme_path : Path ) -> None :
52- current_version = get_version_from_pyproject_toml ( pyproject_path )
54+ def main (part : LITERAL_PART , init_path : Path , readme_path : Path ) -> None :
55+ current_version = get_version_from_init ( init_path )
5356 new_version = bump_version (current_version , part )
54- update_toml_project_version ( pyproject_path , new_version )
57+ update_init_version ( init_path , new_version )
5558 update_readme_badge (readme_path , new_version )
5659 print (f"Successfully bumped from { current_version } to { new_version } " ) # noqa: T201
5760
5861
5962if __name__ == "__main__" :
6063 if len (sys .argv ) != 4 :
6164 raise ValueError (
62- "Usage: bump-version.py <major|minor|patch> <pyproject.toml -path> <readme-path>"
65+ "Usage: bump-version.py <major|minor|patch> <package.__init__.py -path> <readme-path>"
6366 )
6467 part = sys .argv [1 ]
6568 assert part in ("major" , "minor" , "patch" )
66- pyproject_path = Path (sys .argv [2 ])
67- assert pyproject_path .exists ()
69+ init_path = Path (sys .argv [2 ])
70+ assert init_path .exists ()
6871 readme_path = Path (sys .argv [3 ])
6972 assert readme_path .exists ()
70- main (part , pyproject_path , readme_path )
73+ main (part , init_path , readme_path )
0 commit comments