@@ -79,7 +79,7 @@ def get_files_from_directory(dirpath, file_extension, file_name=None,
7979 file_extension (str): File extension to search for.
8080 Eg: '.gradle'
8181 file_name (str, optional): Exact file name to search for.
82- Defaults to None.
82+ Defaults to None. Eg: 'foo.gradle'
8383 absolute_paths (bool, optional): Return absolute paths to files.
8484 Defaults to True.
8585 If False, just filenames are returned.
@@ -110,6 +110,10 @@ def get_files(dirs_and_files, file_extension, file_name=None):
110110 Args:
111111 dirs_and_files (iterable(str)): List of paths which could be files or
112112 directories.
113+ file_extension (str): File extension to search for.
114+ Eg: '.gradle'
115+ file_name (str, optional): Exact file name to search for.
116+ Defaults to None. Eg: 'foo.gradle'
113117
114118 Returns:
115119 iterable(str): Final list of files after recursively searching dirs.
@@ -506,6 +510,66 @@ def replace_module_line(m):
506510 print ()
507511
508512
513+ # Regex to match lines like:
514+ # implementation 'com.google.firebase:firebase-auth:1.2.3'
515+ RE_GRADLE_COMPILE_MODULE = re .compile (
516+ r"implementation\s*\'(?P<pkg>[a-zA-Z0-9._-]+:[a-zA-Z0-9._-]+):([0-9.]+)\'" )
517+
518+
519+ def modify_gradle_file (gradle_filepath , version_map , dryrun = True ):
520+ """Modify a build.gradle file to reference the correct module versions.
521+
522+ Looks for lines like: implementation 'com.google.firebase:firebase-auth:1.2.3'
523+ for modules matching the ones in the version map, and modifies them in-place
524+ (with a g4 edit) to reference the released version.
525+
526+ Args:
527+ gradle_filename: Relative path to build.gradle file to edit.
528+ version_map: Dictionary of packages to version numbers, e.g. {
529+ 'com.google.firebase.firebase_auth': '15.0.0' }
530+ dryrun (bool, optional): Just print the substitutions.
531+ Do not write to file. Defaults to True.
532+ """
533+ logging .debug ("Reading gradle file: %s" , gradle_filepath )
534+
535+ lines = None
536+ with open (gradle_filepath , "r" ) as gradle_file :
537+ lines = gradle_file .readlines ()
538+ if not lines :
539+ logging .fatal ('Update failed. ' +
540+ 'Could not read contents from file {0}.' .format (gradle_filepath ))
541+ output_lines = []
542+
543+ # Replacement function, look up the version number of the given pkg.
544+ def replace_module_line (m ):
545+ if not m .group ("pkg" ):
546+ return m .group (0 )
547+ pkg = m .group ("pkg" ).replace ("-" , "_" ).replace (":" , "." )
548+ if pkg not in version_map :
549+ return m .group (0 )
550+ return "implementation '%s:%s'" % (m .group ("pkg" ), version_map [pkg ])
551+
552+ substituted_pairs = []
553+ to_update = False
554+ for line in lines :
555+ substituted_line = re .sub (RE_GRADLE_COMPILE_MODULE , replace_module_line ,
556+ line )
557+ output_lines .append (substituted_line )
558+ if substituted_line != line :
559+ substituted_pairs .append ((line , substituted_line ))
560+ to_update = True
561+
562+ if to_update :
563+ print ('Updating contents of {0}' .format (gradle_filepath ))
564+ for original , substituted in substituted_pairs :
565+ print ('(-) ' + original + '(+) ' + substituted )
566+
567+ if not dryrun :
568+ with open (gradle_filepath , 'w' ) as gradle_file :
569+ gradle_file .writelines (output_lines )
570+ print ()
571+
572+
509573def parse_cmdline_args ():
510574 parser = argparse .ArgumentParser (description = 'Update pod files with '
511575 'latest pod versions' )
@@ -528,6 +592,9 @@ def parse_cmdline_args():
528592 'release_build_files/Android/firebase_dependencies.gradle' ),
529593 help = 'List of android dependency files or directories'
530594 'containing them.' )
595+ parser .add_argument ('--gradlefiles' , nargs = '+' ,
596+ default = (os .getcwd (),),
597+ help = 'List of android build.gradle files to update.' )
531598 parser .add_argument ('--readmefiles' , nargs = '+' ,
532599 default = ('release_build_files/readme.md' ,),
533600 help = 'List of release readme markdown files or directories'
@@ -575,7 +642,13 @@ def main():
575642 modify_dependency_file (dep_file , latest_android_versions_map , args .dryrun )
576643
577644 for readme_file in readme_files :
578- modify_readme_file_android (readme_file , latest_android_versions_map , args .dryrun )
645+ modify_readme_file_android (readme_file , latest_android_versions_map ,
646+ args .dryrun )
647+
648+ gradle_files = get_files (args .gradlefiles , file_extension = '.gradle' ,
649+ file_name = 'build.gradle' )
650+ for gradle_file in gradle_files :
651+ modify_gradle_file (gradle_file , latest_android_versions_map , args .dryrun )
579652
580653if __name__ == '__main__' :
581654 main ()
0 commit comments