11import os
22import re
3- from typing import List , Set , Optional
3+ from typing import List , Optional , Set
44
55import click
66
1919
2020
2121class RestoreGradleDependencies (BaseRestoreDependencies ):
22- def __init__ (self , context : click .Context , is_git_diff : bool , command_timeout : int ,
23- projects : Set [str ] = set ()) -> None :
22+ def __init__ (
23+ self , context : click .Context , is_git_diff : bool , command_timeout : int , projects : Optional [Set [str ]] = None
24+ ) -> None :
2425 super ().__init__ (context , is_git_diff , command_timeout , create_output_file_manually = True )
25- self .projects = projects
26- self .projects = self .get_all_projects () if self .is_gradle_sub_projects () else set ()
26+ if projects is None :
27+ projects = set ()
28+ self .projects = self .get_all_projects () if self .is_gradle_sub_projects () else projects
2729
28- def is_gradle_sub_projects (self ):
30+ def is_gradle_sub_projects (self ) -> bool :
2931 return self .context .obj .get (SCA_GRADLE_ALL_SUB_PROJECTS_FLAG )
3032
3133 def is_project (self , document : Document ) -> bool :
3234 return document .path .endswith (BUILD_GRADLE_FILE_NAME ) or document .path .endswith (BUILD_GRADLE_KTS_FILE_NAME )
3335
3436 def get_commands (self , manifest_file_path : str ) -> List [List [str ]]:
35- return self .get_commands_for_sub_projects (manifest_file_path ) if self .is_gradle_sub_projects () else [
36- ['gradle' , 'dependencies' , '-b' , manifest_file_path , '-q' , '--console' , 'plain' ]]
37+ return (
38+ self .get_commands_for_sub_projects (manifest_file_path )
39+ if self .is_gradle_sub_projects ()
40+ else [['gradle' , 'dependencies' , '-b' , manifest_file_path , '-q' , '--console' , 'plain' ]]
41+ )
3742
3843 def get_lock_file_name (self ) -> str :
3944 return BUILD_GRADLE_DEP_TREE_FILE_NAME
@@ -44,9 +49,12 @@ def verify_restore_file_already_exist(self, restore_file_path: str) -> bool:
4449 def get_working_directory (self , document : Document ) -> Optional [str ]:
4550 return get_path_from_context (self .context ) if self .is_gradle_sub_projects () else None
4651
47- def get_all_projects (self ) -> List [str ]:
48- projects_output = shell (command = BUILD_GRADLE_ALL_PROJECTS_COMMAND , timeout = BUILD_GRADLE_ALL_PROJECTS_TIMEOUT ,
49- working_directory = get_path_from_context (self .context ))
52+ def get_all_projects (self ) -> Set [str ]:
53+ projects_output = shell (
54+ command = BUILD_GRADLE_ALL_PROJECTS_COMMAND ,
55+ timeout = BUILD_GRADLE_ALL_PROJECTS_TIMEOUT ,
56+ working_directory = get_path_from_context (self .context ),
57+ )
5058
5159 projects = re .findall (ALL_PROJECTS_REGEX , projects_output )
5260
@@ -55,5 +63,8 @@ def get_all_projects(self) -> List[str]:
5563 def get_commands_for_sub_projects (self , manifest_file_path : str ) -> List [List [str ]]:
5664 project_name = os .path .basename (os .path .dirname (manifest_file_path ))
5765 project_name = f':{ project_name } '
58- return [['gradle' , f'{ project_name } :dependencies' , '-q' , '--console' ,
59- 'plain' ]] if project_name in self .projects else []
66+ return (
67+ [['gradle' , f'{ project_name } :dependencies' , '-q' , '--console' , 'plain' ]]
68+ if project_name in self .projects
69+ else []
70+ )
0 commit comments