4
4
import glob
5
5
from pathlib import Path
6
6
7
- import tomlkit
7
+ from tomlkit import TOMLDocument , dumps , parse
8
+ from tomlkit .exceptions import NonExistentKey
9
+ from tomlkit .items import AoT
8
10
9
11
from commitizen .providers .base_provider import TomlProvider
10
12
11
13
12
- def matches_exclude (path : str , exclude_patterns : list [str ]) -> bool :
13
- for pattern in exclude_patterns :
14
- if fnmatch .fnmatch (path , pattern ):
15
- return True
16
- return False
17
-
18
-
19
14
class CargoProvider (TomlProvider ):
20
15
"""
21
16
Cargo version management
@@ -30,65 +25,71 @@ class CargoProvider(TomlProvider):
30
25
def lock_file (self ) -> Path :
31
26
return Path () / self .lock_filename
32
27
33
- def get (self , document : tomlkit .TOMLDocument ) -> str :
34
- # If there is a root package, change its version (but not the workspace version)
35
- try :
36
- return document ["package" ]["version" ] # type: ignore[index,return-value]
37
- # Else, bump the workspace version
38
- except tomlkit .exceptions .NonExistentKey :
39
- ...
40
- return document ["workspace" ]["package" ]["version" ] # type: ignore[index,return-value]
28
+ def get (self , document : TOMLDocument ) -> str :
29
+ out = _try_get_workspace (document )["package" ]["version" ]
30
+ assert isinstance (out , str )
31
+ return out
41
32
42
- def set (self , document : tomlkit .TOMLDocument , version : str ) -> None :
43
- try :
44
- document ["workspace" ]["package" ]["version" ] = version # type: ignore[index]
45
- return
46
- except tomlkit .exceptions .NonExistentKey :
47
- ...
48
- document ["package" ]["version" ] = version # type: ignore[index]
33
+ def set (self , document : TOMLDocument , version : str ) -> None :
34
+ _try_get_workspace (document )["package" ]["version" ] = version
49
35
50
36
def set_version (self , version : str ) -> None :
51
37
super ().set_version (version )
52
38
if self .lock_file .exists ():
53
39
self .set_lock_version (version )
54
40
55
41
def set_lock_version (self , version : str ) -> None :
56
- cargo_toml_content = tomlkit .parse (self .file .read_text ())
57
- cargo_lock_content = tomlkit .parse (self .lock_file .read_text ())
58
- packages : tomlkit .items .AoT = cargo_lock_content ["package" ] # type: ignore[assignment]
42
+ cargo_toml_content = parse (self .file .read_text ())
43
+ cargo_lock_content = parse (self .lock_file .read_text ())
44
+ packages = cargo_lock_content ["package" ]
45
+
46
+ assert isinstance (packages , AoT )
47
+
59
48
try :
60
- package_name = cargo_toml_content ["package" ]["name" ] # type: ignore[index]
49
+ cargo_package_name = cargo_toml_content ["package" ]["name" ] # type: ignore[index]
50
+ assert isinstance (cargo_package_name , str )
61
51
for i , package in enumerate (packages ):
62
- if package ["name" ] == package_name :
52
+ if package ["name" ] == cargo_package_name :
63
53
cargo_lock_content ["package" ][i ]["version" ] = version # type: ignore[index]
64
54
break
65
- except tomlkit .exceptions .NonExistentKey :
66
- workspace_members = cargo_toml_content .get ("workspace" , {}).get (
67
- "members" , []
68
- )
69
- excluded_workspace_members = cargo_toml_content .get ("workspace" , {}).get (
70
- "exclude" , []
71
- )
72
- members_inheriting = []
55
+ except NonExistentKey :
56
+ workspace = cargo_toml_content .get ("workspace" , {})
57
+ assert isinstance (workspace , dict )
58
+ workspace_members = workspace .get ("members" , [])
59
+ excluded_workspace_members = workspace .get ("exclude" , [])
60
+ members_inheriting : list [str ] = []
73
61
74
62
for member in workspace_members :
75
63
for path in glob .glob (member , recursive = True ):
76
- if matches_exclude (path , excluded_workspace_members ):
64
+ if any (
65
+ fnmatch .fnmatch (path , pattern )
66
+ for pattern in excluded_workspace_members
67
+ ):
77
68
continue
69
+
78
70
cargo_file = Path (path ) / "Cargo.toml"
79
- cargo_toml_content = tomlkit .parse (cargo_file .read_text ())
71
+ package_content = parse (cargo_file .read_text ()).get ("package" , {})
72
+ assert isinstance (package_content , dict )
80
73
try :
81
- version_workspace = cargo_toml_content ["package" ]["version" ][ # type: ignore[index]
82
- "workspace"
83
- ]
74
+ version_workspace = package_content ["version" ]["workspace" ]
84
75
if version_workspace is True :
85
- package_name = cargo_toml_content ["package" ]["name" ] # type: ignore[index]
76
+ package_name = package_content ["name" ]
77
+ assert isinstance (package_name , str )
86
78
members_inheriting .append (package_name )
87
- except tomlkit . exceptions . NonExistentKey :
88
- continue
79
+ except NonExistentKey :
80
+ pass
89
81
90
82
for i , package in enumerate (packages ):
91
83
if package ["name" ] in members_inheriting :
92
84
cargo_lock_content ["package" ][i ]["version" ] = version # type: ignore[index]
93
85
94
- self .lock_file .write_text (tomlkit .dumps (cargo_lock_content ))
86
+ self .lock_file .write_text (dumps (cargo_lock_content ))
87
+
88
+
89
+ def _try_get_workspace (document : TOMLDocument ) -> dict :
90
+ try :
91
+ workspace = document ["workspace" ]
92
+ assert isinstance (workspace , dict )
93
+ return workspace
94
+ except NonExistentKey :
95
+ return document
0 commit comments