5
5
#
6
6
# usage: cargo-version.py [-h] [-p PROJECT] [-r] [-n {major,minor,patch}] [-s SET] [-o] [-m PRERELEASE]
7
7
#
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
+
22
10
import toml
23
11
import semver
24
- import argparse
25
12
26
13
27
14
class Crate :
@@ -41,20 +28,22 @@ def finalize(cls, version):
41
28
42
29
@classmethod
43
30
def bump_level (cls , version , level ):
44
- v = semver .VersionInfo .parse (version )
31
+ ver = semver .VersionInfo .parse (version )
45
32
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.
53
42
54
43
@classmethod
55
44
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 ))
58
47
59
48
def finalize_version (self ):
60
49
return Crate (
@@ -94,25 +83,25 @@ def show_version(self):
94
83
def save (self , previous ):
95
84
contents = []
96
85
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 ():
99
88
if line .startswith ("version" ):
100
89
line = line .replace (previous .version , self .version )
101
90
else :
102
91
for dname , dversion in self .dependencies .items ():
103
92
if line .startswith (dname ):
104
93
line = line .replace (previous .dependencies [dname ], dversion )
105
94
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 ))
108
97
109
98
def __str__ (self ):
110
99
return f"Crate({ self .path } , { self .name } , { self .version } , { self .dependencies } )"
111
100
112
101
113
102
class Workspace :
114
103
def __init__ (self , crates ):
115
- names = set ([ c .name for c in crates ])
104
+ names = { c .name for c in crates }
116
105
self .crates = {c .name : c .with_dependencies (names ) for c in crates }
117
106
118
107
def finalize_version (self ):
@@ -136,8 +125,8 @@ def next_version(self):
136
125
return Workspace (Workspace .update_dependencies (crates ).values ())
137
126
138
127
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 ()
141
130
return "0.0.0"
142
131
143
132
@classmethod
@@ -151,26 +140,28 @@ def __str__(self):
151
140
return f"Workspace({ [str (c ) for c in self .crates .values ()]} )"
152
141
153
142
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 ])
156
145
157
146
158
147
def 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" ]]
172
152
)
173
153
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
+
174
165
175
166
def parse_args ():
176
167
parser = argparse .ArgumentParser (description = "Change versions of cargo projects." )
0 commit comments