2
2
3
3
import argparse
4
4
import re
5
- from pathlib import Path
6
5
from datetime import datetime
6
+ from pathlib import Path
7
+
7
8
import tomli
8
9
import tomli_w
9
10
11
+
10
12
def read_toml (file_path : Path ) -> dict :
11
13
"""Read and parse a TOML file."""
12
14
with open (file_path , "rb" ) as f :
13
15
return tomli .load (f )
14
16
17
+
15
18
def write_toml (file_path : Path , data : dict ) -> None :
16
19
"""Write data to a TOML file."""
17
20
with open (file_path , "wb" ) as f :
18
21
tomli_w .dump (data , f )
19
22
23
+
20
24
def bump_version (current_version : str , bump_type : str ) -> str :
21
25
"""Bump the version number based on semver rules."""
22
- major , minor , patch = map (int , current_version .split ('.' ))
23
-
26
+ major , minor , patch = map (int , current_version .split ("." ))
27
+
24
28
if bump_type == "major" :
25
29
return f"{ major + 1 } .0.0"
26
30
elif bump_type == "minor" :
27
31
return f"{ major } .{ minor + 1 } .0"
28
32
else : # patch
29
33
return f"{ major } .{ minor } .{ patch + 1 } "
30
34
35
+
31
36
def update_pyproject_toml (file_path : Path , new_version : str ) -> None :
32
37
"""Update version in pyproject.toml."""
33
38
data = read_toml (file_path )
34
39
data ["project" ]["version" ] = new_version
35
40
write_toml (file_path , data )
36
41
42
+
37
43
def update_server_py (file_path : Path , new_version : str ) -> None :
38
44
"""Update version in server.py."""
39
45
content = file_path .read_text ()
40
46
# Update version pattern in server.py
41
47
new_content = re .sub (
42
- r'__version__\s*=\s*["\'][\d.]+["\']' ,
43
- f'__version__ = "{ new_version } "' ,
44
- content
48
+ r'__version__\s*=\s*["\'][\d.]+["\']' , f'__version__ = "{ new_version } "' , content
45
49
)
46
50
file_path .write_text (new_content )
47
51
52
+
48
53
def update_changelog (file_path : Path , new_version : str ) -> None :
49
54
"""Update CHANGELOG.md with new version."""
50
55
content = file_path .read_text ()
51
56
today = datetime .now ().strftime ("%Y-%m-%d" )
52
57
new_entry = f"\n ## [{ new_version } ] - { today } \n \n "
53
-
58
+
54
59
if "# Changelog" in content :
55
60
# Insert after the first line containing "# Changelog"
56
61
parts = content .split ("# Changelog" , 1 )
57
62
content = parts [0 ] + "# Changelog" + new_entry + parts [1 ]
58
63
else :
59
64
# If no Changelog header exists, add it
60
65
content = f"# Changelog\n { new_entry } \n { content } "
61
-
66
+
62
67
file_path .write_text (content )
63
68
69
+
64
70
def main ():
65
71
parser = argparse .ArgumentParser (description = "Bump version numbers in the project" )
66
72
parser .add_argument (
67
73
"bump_type" ,
68
74
nargs = "?" ,
69
75
choices = ["major" , "minor" , "patch" ],
70
76
default = "minor" ,
71
- help = "Type of version bump (default: minor)"
77
+ help = "Type of version bump (default: minor)" ,
72
78
)
73
79
args = parser .parse_args ()
74
80
@@ -78,20 +84,21 @@ def main():
78
84
# Read current version from pyproject.toml
79
85
pyproject_path = root_dir / "pyproject.toml"
80
86
current_version = read_toml (pyproject_path )["project" ]["version" ]
81
-
87
+
82
88
# Calculate new version
83
89
new_version = bump_version (current_version , args .bump_type )
84
-
90
+
85
91
# Update files
86
92
update_pyproject_toml (pyproject_path , new_version )
87
93
update_server_py (root_dir / "src" / "semgrep_mcp" / "server.py" , new_version )
88
94
update_changelog (root_dir / "CHANGELOG.md" , new_version )
89
-
95
+
90
96
print (f"Successfully bumped version from { current_version } to { new_version } " )
91
97
print ("Files updated:" )
92
98
print ("- pyproject.toml" )
93
99
print ("- src/semgrep_mcp/server.py" )
94
100
print ("- CHANGELOG.md" )
95
101
102
+
96
103
if __name__ == "__main__" :
97
- main ()
104
+ main ()
0 commit comments