55#
66# usage: cargo-version.py [-h] [-p PROJECT] [-r] [-n {major,minor,patch}] [-s SET] [-o] [-m PRERELEASE]
77#
8- # Change versions of cargo projects.
9- #
10- # optional arguments:
11- # -h, --help show this help message and exit
12- # -p PROJECT, --project PROJECT
13- # Project folder
14- # -r, --release Version
15- # -n {major,minor,patch}, --next {major,minor,patch}
16- # Version
17- # -s SET, --set SET Version
18- # -o, --show Version
19- # -m PRERELEASE, --prerelease PRERELEASE
20- # Set pre-prelease string.
21- #
8+ import argparse
9+
2210import toml
2311import semver
24- import argparse
2512
2613
2714class Crate :
@@ -41,20 +28,22 @@ def finalize(cls, version):
4128
4229 @classmethod
4330 def bump_level (cls , version , level ):
44- v = semver .VersionInfo .parse (version )
31+ ver = semver .VersionInfo .parse (version )
4532 if level == "major" :
46- return str (v .bump_major ())
47- elif level == "minor" :
48- return str (v .bump_minor ())
49- elif level == "patch" :
50- return str (v .bump_patch ())
51- else :
52- return str (v .bump_prerelease ("nightly" ))[:- 2 ] # remove the .1 suffix that semver always adds to the prererelease.
33+ return str (ver .bump_major ())
34+ if level == "minor" :
35+ return str (ver .bump_minor ())
36+ if level == "patch" :
37+ return str (ver .bump_patch ())
38+
39+ return str (ver .bump_prerelease ("nightly" ))[
40+ :- 2
41+ ] # remove the .1 suffix that semver always adds to the prererelease.
5342
5443 @classmethod
5544 def prerelease (cls , version , prerelease ):
56- v = semver .VersionInfo .parse (version )
57- return str (semver .VersionInfo (v .major , v .minor , v .patch , prerelease ))
45+ ver = semver .VersionInfo .parse (version )
46+ return str (semver .VersionInfo (ver .major , ver .minor , ver .patch , prerelease ))
5847
5948 def finalize_version (self ):
6049 return Crate (
@@ -94,25 +83,25 @@ def show_version(self):
9483 def save (self , previous ):
9584 contents = []
9685 cargo_file = f"{ self .path } /Cargo.toml"
97- with open (cargo_file , "r" ) as r :
98- for line in r .readlines ():
86+ with open (cargo_file , "r" , encoding = "utf8" ) as ctl :
87+ for line in ctl .readlines ():
9988 if line .startswith ("version" ):
10089 line = line .replace (previous .version , self .version )
10190 else :
10291 for dname , dversion in self .dependencies .items ():
10392 if line .startswith (dname ):
10493 line = line .replace (previous .dependencies [dname ], dversion )
10594 contents .append (line )
106- with open (cargo_file , "w" ) as w :
107- w .write ("" .join (contents ))
95+ with open (cargo_file , "w" , encoding = "utf8" ) as ctl :
96+ ctl .write ("" .join (contents ))
10897
10998 def __str__ (self ):
11099 return f"Crate({ self .path } , { self .name } , { self .version } , { self .dependencies } )"
111100
112101
113102class Workspace :
114103 def __init__ (self , crates ):
115- names = set ([ c .name for c in crates ])
104+ names = { c .name for c in crates }
116105 self .crates = {c .name : c .with_dependencies (names ) for c in crates }
117106
118107 def finalize_version (self ):
@@ -136,8 +125,8 @@ def next_version(self):
136125 return Workspace (Workspace .update_dependencies (crates ).values ())
137126
138127 def show_version (self ):
139- for c in self .crates .values ():
140- return c .show_version ()
128+ for cts in self .crates .values ():
129+ return cts .show_version ()
141130 return "0.0.0"
142131
143132 @classmethod
@@ -151,26 +140,28 @@ def __str__(self):
151140 return f"Workspace({ [str (c ) for c in self .crates .values ()]} )"
152141
153142 def save (self , previous ):
154- for cn in self .crates .keys ():
155- self .crates [cn ].save (previous .crates [cn ])
143+ for crn in self .crates .keys ():
144+ self .crates [crn ].save (previous .crates [crn ])
156145
157146
158147def load (root ):
159- r = toml .load (f"{ root } /Cargo.toml" )
160- if "workspace" in r :
161- return Workspace ([load (f"{ root } /{ path } " ) for path in r ["workspace" ]["members" ]])
162- else :
163- return Crate (
164- path = root ,
165- name = r ["package" ]["name" ],
166- version = r ["package" ]["version" ],
167- dependencies = {
168- dn : r ["dependencies" ][dn ]["version" ]
169- for dn in r ["dependencies" ]
170- if "version" in r ["dependencies" ][dn ]
171- },
148+ ctl = toml .load (f"{ root } /Cargo.toml" )
149+ if "workspace" in ctl :
150+ return Workspace (
151+ [load (f"{ root } /{ path } " ) for path in ctl ["workspace" ]["members" ]]
172152 )
173153
154+ return Crate (
155+ path = root ,
156+ name = ctl ["package" ]["name" ],
157+ version = ctl ["package" ]["version" ],
158+ dependencies = {
159+ dn : ctl ["dependencies" ][dn ]["version" ]
160+ for dn in ctl ["dependencies" ]
161+ if "version" in ctl ["dependencies" ][dn ]
162+ },
163+ )
164+
174165
175166def parse_args ():
176167 parser = argparse .ArgumentParser (description = "Change versions of cargo projects." )
0 commit comments