33import fnmatch
44import glob
55from pathlib import Path
6+ from typing import TYPE_CHECKING
67
7- import tomlkit
8+ from tomlkit import TOMLDocument , dumps , parse
9+ from tomlkit .exceptions import NonExistentKey
10+ from tomlkit .items import AoT
811
912from commitizen .providers .base_provider import TomlProvider
1013
1114
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-
1915class CargoProvider (TomlProvider ):
2016 """
2117 Cargo version management
@@ -30,65 +26,78 @@ class CargoProvider(TomlProvider):
3026 def lock_file (self ) -> Path :
3127 return Path () / self .lock_filename
3228
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]
29+ def get (self , document : TOMLDocument ) -> str :
30+ out = _try_get_workspace (document )["package" ]["version" ]
31+ if TYPE_CHECKING :
32+ assert isinstance (out , str )
33+ return out
4134
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]
35+ def set (self , document : TOMLDocument , version : str ) -> None :
36+ _try_get_workspace (document )["package" ]["version" ] = version
4937
5038 def set_version (self , version : str ) -> None :
5139 super ().set_version (version )
5240 if self .lock_file .exists ():
5341 self .set_lock_version (version )
5442
5543 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]
44+ cargo_toml_content = parse (self .file .read_text ())
45+ cargo_lock_content = parse (self .lock_file .read_text ())
46+ packages = cargo_lock_content ["package" ]
47+
48+ if TYPE_CHECKING :
49+ assert isinstance (packages , AoT )
50+
5951 try :
60- package_name = cargo_toml_content ["package" ]["name" ] # type: ignore[index]
52+ cargo_package_name = cargo_toml_content ["package" ]["name" ] # type: ignore[index]
53+ if TYPE_CHECKING :
54+ assert isinstance (cargo_package_name , str )
6155 for i , package in enumerate (packages ):
62- if package ["name" ] == package_name :
56+ if package ["name" ] == cargo_package_name :
6357 cargo_lock_content ["package" ][i ]["version" ] = version # type: ignore[index]
6458 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 = []
59+ except NonExistentKey :
60+ workspace = cargo_toml_content .get ("workspace" , {})
61+ if TYPE_CHECKING :
62+ assert isinstance (workspace , dict )
63+ workspace_members = workspace .get ("members" , [])
64+ excluded_workspace_members = workspace .get ("exclude" , [])
65+ members_inheriting : list [str ] = []
7366
7467 for member in workspace_members :
7568 for path in glob .glob (member , recursive = True ):
76- if matches_exclude (path , excluded_workspace_members ):
69+ if any (
70+ fnmatch .fnmatch (path , pattern )
71+ for pattern in excluded_workspace_members
72+ ):
7773 continue
74+
7875 cargo_file = Path (path ) / "Cargo.toml"
79- cargo_toml_content = tomlkit .parse (cargo_file .read_text ())
76+ package_content = parse (cargo_file .read_text ()).get ("package" , {})
77+ if TYPE_CHECKING :
78+ assert isinstance (package_content , dict )
8079 try :
81- version_workspace = cargo_toml_content ["package" ]["version" ][ # type: ignore[index]
82- "workspace"
83- ]
80+ version_workspace = package_content ["version" ]["workspace" ]
8481 if version_workspace is True :
85- package_name = cargo_toml_content ["package" ]["name" ] # type: ignore[index]
82+ package_name = package_content ["name" ]
83+ if TYPE_CHECKING :
84+ assert isinstance (package_name , str )
8685 members_inheriting .append (package_name )
87- except tomlkit . exceptions . NonExistentKey :
88- continue
86+ except NonExistentKey :
87+ pass
8988
9089 for i , package in enumerate (packages ):
9190 if package ["name" ] in members_inheriting :
9291 cargo_lock_content ["package" ][i ]["version" ] = version # type: ignore[index]
9392
94- self .lock_file .write_text (tomlkit .dumps (cargo_lock_content ))
93+ self .lock_file .write_text (dumps (cargo_lock_content ))
94+
95+
96+ def _try_get_workspace (document : TOMLDocument ) -> dict :
97+ try :
98+ workspace = document ["workspace" ]
99+ if TYPE_CHECKING :
100+ assert isinstance (workspace , dict )
101+ return workspace
102+ except NonExistentKey :
103+ return document
0 commit comments