1
1
"""
2
- Version Bumper for Cargo.toml and Changelog.md
2
+ Changelog Modifier and Version Bumper for Cargo.toml and Changelog.md
3
3
4
- This script automates the process of version bumping for Rust projects managed with Cargo. It reads the version
5
- from the cargo.toml file, increments it based on the specified component (major, minor, or patch), and updates
6
- both the cargo.toml and changelog.md files accordingly.
4
+ This script automates the process of version bumping and changelog updates for Rust projects managed with Cargo.
5
+ It reads the version from the cargo.toml file, increments it based on the specified component (major, minor, or patch),
6
+ and updates both the cargo.toml and changelog.md files accordingly.
7
7
8
- It will also print out the new version number.
9
-
10
- Additionally, this script can add PR entries to the UNRELEASED section of the changelog.
8
+ It can also add PR entries to the UNRELEASED section of the changelog.
11
9
12
10
Usage:
13
11
Version bumping:
14
- $ python increment_version .py [major|minor|patch]
12
+ $ python modify_changelog .py bump_version [major|minor|patch]
15
13
16
14
Adding PR entry:
17
- $ python increment_version.py --add-pr --pr-number=123 --pr-title="Fix bug" --pr-url="https://github.com/..."
18
-
19
- Arguments:
20
- ---------
21
- major - Increments the major component of the version, sets minor and patch to 0
22
- minor - Increments the minor component of the version, sets patch to 0
23
- patch - Increments the patch component of the version
24
- --add-pr - Add a PR entry to the UNRELEASED section
25
- --pr-number - PR number (required with --add-pr)
26
- --pr-title - PR title (required with --add-pr)
27
- --pr-url - PR URL (required with --add-pr)
15
+ $ python modify_changelog.py update_changelog <number> <title>
28
16
29
- From https://chat.openai.com/share/6b08906d-23a3-4193-9f4e-87076ce56ddb
17
+ Subcommands:
18
+ -----------
19
+ bump_version - Increments version and updates changelog
20
+ major - Increments the major component of the version, sets minor and patch to 0
21
+ minor - Increments the minor component of the version, sets patch to 0
22
+ patch - Increments the patch component of the version
23
+
24
+ update_changelog - Add a PR entry to the UNRELEASED section
25
+ number - PR number
26
+ title - PR title
30
27
28
+ From https://chat.openai.com/share/6b08906d-23a3-4193-9f4e-87076ce56ddb
31
29
32
30
"""
33
31
@@ -128,48 +126,20 @@ def update_changelog_pr(file_path: Path, pr_number: str, pr_title: str, pr_url:
128
126
return True
129
127
130
128
131
- def main ():
132
- parser = argparse .ArgumentParser (description = 'Version bumper and changelog updater' )
133
-
134
- # Create mutually exclusive group for version bump vs PR add
135
- group = parser .add_mutually_exclusive_group (required = True )
136
- group .add_argument ('bump_type' , nargs = '?' , choices = ['major' , 'minor' , 'patch' ],
137
- help = 'Type of version bump' )
138
- group .add_argument ('--add-pr' , action = 'store_true' , help = 'Add PR entry to changelog' )
139
-
140
- # PR-specific arguments
141
- parser .add_argument ('--pr-number' , help = 'Pull request number (required with --add-pr)' )
142
- parser .add_argument ('--pr-title' , help = 'Pull request title (required with --add-pr)' )
143
- parser .add_argument ('--pr-url' , help = 'Pull request URL (required with --add-pr)' )
144
- parser .add_argument ('--changelog-path' , default = 'docs/changelog.md' , help = 'Path to changelog file' )
145
-
146
- args = parser .parse_args ()
147
-
148
- # Handle PR addition
149
- if args .add_pr :
150
- if not all ([args .pr_number , args .pr_title , args .pr_url ]):
151
- print ("ERROR: --pr-number, --pr-title, and --pr-url are required with --add-pr" )
152
- sys .exit (1 )
153
-
154
- changelog_path = Path (args .changelog_path )
155
- if not changelog_path .exists ():
156
- print (f"ERROR: Changelog file not found: { changelog_path } " )
157
- sys .exit (1 )
158
-
159
- success = update_changelog_pr (changelog_path , args .pr_number , args .pr_title , args .pr_url )
160
- if not success :
161
- sys .exit (1 )
162
- return
163
-
164
- # Handle version bump (existing functionality)
165
- if not args .bump_type :
166
- print ("ERROR: Either specify bump type (major|minor|patch) or use --add-pr" )
167
- sys .exit (1 )
168
-
129
+ def handle_bump_version (args ):
130
+ """Handle version bump subcommand."""
169
131
part = args .bump_type
170
132
cargo_path = Path ("Cargo.toml" )
171
133
changelog_path = Path ("docs/changelog.md" )
172
134
135
+ if not cargo_path .exists ():
136
+ print ("ERROR: Cargo.toml not found." )
137
+ sys .exit (1 )
138
+
139
+ if not changelog_path .exists ():
140
+ print ("ERROR: Changelog file not found." )
141
+ sys .exit (1 )
142
+
173
143
cargo_content = cargo_path .read_text ()
174
144
version_match = re .search (r'version = "(\d+)\.(\d+)\.(\d+)"' , cargo_content )
175
145
if version_match :
@@ -179,31 +149,58 @@ def main():
179
149
sys .exit (1 )
180
150
181
151
new_version = bump_version (major , minor , patch , part )
182
- old_version = f"{ major } .{ minor } .{ patch } "
183
152
update_cargo_toml (cargo_path , new_version )
184
153
update_changelog_version (changelog_path , new_version )
185
154
print (new_version )
186
155
187
156
157
+ def handle_update_changelog (args ):
158
+ """Handle update changelog subcommand."""
159
+ pr_number = args .number
160
+ pr_title = args .title
161
+
162
+ # Construct PR URL from repository info and PR number
163
+ # Default to the egglog-python repository
164
+ pr_url = f"https://github.com/egraphs-good/egglog-python/pull/{ pr_number } "
165
+
166
+ changelog_path = Path (getattr (args , 'changelog_path' , 'docs/changelog.md' ))
167
+
168
+ if not changelog_path .exists ():
169
+ print (f"ERROR: Changelog file not found: { changelog_path } " )
170
+ sys .exit (1 )
171
+
172
+ success = update_changelog_pr (changelog_path , pr_number , pr_title , pr_url )
173
+ if not success :
174
+ sys .exit (1 )
175
+
176
+
177
+ def main ():
178
+ parser = argparse .ArgumentParser (description = 'Changelog modifier and version bumper' )
179
+ subparsers = parser .add_subparsers (dest = 'command' , help = 'Available commands' )
180
+
181
+ # Bump version subcommand
182
+ bump_parser = subparsers .add_parser ('bump_version' , help = 'Bump version and update changelog' )
183
+ bump_parser .add_argument ('bump_type' , choices = ['major' , 'minor' , 'patch' ],
184
+ help = 'Type of version bump' )
185
+
186
+ # Update changelog subcommand
187
+ changelog_parser = subparsers .add_parser ('update_changelog' , help = 'Add PR entry to changelog' )
188
+ changelog_parser .add_argument ('number' , help = 'Pull request number' )
189
+ changelog_parser .add_argument ('title' , help = 'Pull request title' )
190
+ changelog_parser .add_argument ('--changelog-path' , default = 'docs/changelog.md' ,
191
+ help = 'Path to changelog file' )
192
+
193
+ args = parser .parse_args ()
194
+
195
+ if not args .command :
196
+ parser .print_help ()
197
+ sys .exit (1 )
198
+
199
+ if args .command == 'bump_version' :
200
+ handle_bump_version (args )
201
+ elif args .command == 'update_changelog' :
202
+ handle_update_changelog (args )
203
+
204
+
188
205
if __name__ == "__main__" :
189
- # For backward compatibility, support old command line format
190
- if len (sys .argv ) == 2 and sys .argv [1 ] in ("major" , "minor" , "patch" ):
191
- part = sys .argv [1 ]
192
- cargo_path = Path ("Cargo.toml" )
193
- changelog_path = Path ("docs/changelog.md" )
194
-
195
- cargo_content = cargo_path .read_text ()
196
- version_match = re .search (r'version = "(\d+)\.(\d+)\.(\d+)"' , cargo_content )
197
- if version_match :
198
- major , minor , patch = map (int , version_match .groups ())
199
- else :
200
- print ("Current version not found in cargo.toml." )
201
- sys .exit (1 )
202
-
203
- new_version = bump_version (major , minor , patch , part )
204
- old_version = f"{ major } .{ minor } .{ patch } "
205
- update_cargo_toml (cargo_path , new_version )
206
- update_changelog_version (changelog_path , new_version )
207
- print (new_version )
208
- else :
209
- main ()
206
+ main ()
0 commit comments