3939from absl import flags
4040from absl import logging
4141
42+
43+ SUPPORT_TARGETS = [
44+ "analytics" , "auth" , "crashlytics" , "database" , "dynamic_links" ,
45+ "firestore" , "functions" , "installations" , "messaging" , "remote_config" ,
46+ "storage"
47+ ]
48+
4249FLAGS = flags .FLAGS
4350flags .DEFINE_string ('zip_dir' , None ,
4451 'Directory of zip files from build output to package' )
4552flags .DEFINE_string ("config_file" , "exports.json" ,
46- ("Config file that describes how to "
47- "pack the unity assets." ))
53+ ("Config file that describes how to pack the unity assets." ))
4854flags .DEFINE_string ("guids_file" , "guids.json" ,
4955 "Json file with stable guids cache." )
5056
5561 "Output folder for unitypackage." )
5662
5763flags .DEFINE_boolean ("output_upm" , False , "Whether output packages as tgz for"
58- "Unity Package Manager." )
64+ "Unity Package Manager." )
65+
66+ flags .DEFINE_string ("apis" , None , "which firebase products to pack, if not"
67+ "set means package all. Value should be items in [{}],"
68+ "connected with ',', eg 'auth,firestore'" .format (
69+ "," .join (SUPPORT_TARGETS )))
70+
5971
6072def get_zip_files ():
6173 """Get all zip files from FLAGS.zip_dir.
@@ -75,12 +87,14 @@ def get_zip_files():
7587
7688 return zip_file_paths
7789
90+
7891def get_last_version ():
7992 """Get the last version number in guids file if exists.
8093 Returns:
8194 version number.
8295 """
83- version_cmake_path = os .path .join (os .getcwd (), "cmake" , "firebase_unity_version.cmake" )
96+ version_cmake_path = os .path .join (
97+ os .getcwd (), "cmake" , "firebase_unity_version.cmake" )
8498 with open (version_cmake_path , "r" ) as f :
8599 datafile = f .readlines ()
86100 for line in datafile :
@@ -89,45 +103,68 @@ def get_last_version():
89103 return result [- 1 ].strip ("\" " )
90104 return None
91105
106+
92107def find_pack_script ():
93108 """Get the pack script either from intermediate build folder or download from unity-jar-resolver.
94109
95110 Returns:
96111 path of the pack script. None if not found.
97112 """
98113 built_folder_ext = "_unity"
99- built_folder_postion = os .path .join ("external" , "src" , "google_unity_jar_resolver" )
114+ built_folder_postion = os .path .join (
115+ "external" , "src" , "google_unity_jar_resolver" )
100116 built_folder = None
101117 resolver_root_folder = "unity-jar-resolver"
102118 for folder in os .listdir ("." ):
103119 if folder .endswith (built_folder_ext ):
104120 built_folder = folder
105121 break
106-
122+
107123 if built_folder != None :
108124 resolver_root_folder = os .path .join (built_folder , built_folder_postion )
109125 elif not os .path .exists (resolver_root_folder ):
110126 git_clone_script = ["git" , "clone" ,
111- "--depth" , "1" ,
112- "https://github.com/googlesamples/unity-jar-resolver.git" ]
127+ "--depth" , "1" ,
128+ "https://github.com/googlesamples/unity-jar-resolver.git" ]
113129 subprocess .call (git_clone_script )
114130
115131 if resolver_root_folder != None :
116- script_path = os .path .join (resolver_root_folder , "source" , "ExportUnityPackage" , "export_unity_package.py" )
132+ script_path = os .path .join (
133+ resolver_root_folder , "source" , "ExportUnityPackage" , "export_unity_package.py" )
117134 return script_path
118135 return None
119136
137+
138+ def _debug_create_target_package (target , packer_script_path , guids_file_path , output_folder , zip_file_list , last_version ):
139+ debug_config_path = os .path .join (os .getcwd (), FLAGS .script_folder , "debug_single_export_json" ,
140+ target + ".json" )
141+ debug_cmd_args = [
142+ sys .executable ,
143+ packer_script_path ,
144+ "--assets_dir=" + FLAGS .zip_dir ,
145+ "--config_file=" + debug_config_path ,
146+ "--guids_file=" + guids_file_path ,
147+ "--output_dir=" + output_folder ,
148+ "--output_upm=False" ,
149+ ]
150+ debug_cmd_args .extend (
151+ ["--assets_zip=" + zip_file for zip_file in zip_file_list ])
152+ debug_cmd_args .append ("--enabled_sections=build_dotnet4 asset_package_only" )
153+ debug_cmd_args .append ("--plugins_version=" + last_version )
154+ subprocess .call (debug_cmd_args )
155+ logging .info ("Debug Packaging done for target %s" , target )
156+
157+
120158def main (argv ):
121159 if len (argv ) > 1 :
122160 raise app .UsageError ('Too many command-line arguments.' )
123161
124162 packer_script_path = find_pack_script ()
125163 if packer_script_path == None :
126- raise app .UsageError ('Cannot find pack script. Please build the project first.' )
127-
164+ raise app .UsageError (
165+ 'Cannot find pack script. Please build the project first.' )
166+
128167 packer_script_path = os .path .join (os .getcwd (), packer_script_path )
129- config_file_path = os .path .join (os .getcwd (), FLAGS .script_folder ,
130- FLAGS .config_file )
131168 guids_file_path = os .path .join (os .getcwd (), FLAGS .script_folder ,
132169 FLAGS .guids_file )
133170
@@ -141,6 +178,21 @@ def main(argv):
141178 if os .path .exists (output_folder ):
142179 shutil .rmtree (output_folder )
143180
181+ if FLAGS .apis :
182+ # If told to only build a subset, package just those products and exit early.
183+ api_list = FLAGS .apis .split ("," )
184+ if not set (api_list ).issubset (set (SUPPORT_TARGETS )):
185+ raise app .UsageError ("apis parameter error, Value should be items in [{}],"
186+ "connected with ',', eg 'auth,firestore'" .format (
187+ "," .join (SUPPORT_TARGETS )))
188+
189+ for target in api_list :
190+ _debug_create_target_package (
191+ target , packer_script_path , guids_file_path , output_folder , zip_file_list , last_version )
192+ return
193+
194+ config_file_path = os .path .join (os .getcwd (), FLAGS .script_folder ,
195+ FLAGS .config_file )
144196 cmd_args = [
145197 sys .executable ,
146198 packer_script_path ,
@@ -151,16 +203,17 @@ def main(argv):
151203 "--output_upm=" + str (FLAGS .output_upm ),
152204 ]
153205 cmd_args .extend (["--assets_zip=" + zip_file for zip_file in zip_file_list ])
154-
206+
155207 if last_version :
156208 cmd_args .append ("--plugins_version=" + last_version )
157209
158210 if FLAGS .output_upm :
159211 cmd_args .append ("--enabled_sections=build_dotnet4" )
160212 cmd_args .append ("--output_unitypackage=False" )
161213 else :
162- cmd_args .append ("--enabled_sections=build_dotnet3 build_dotnet4 asset_package_only" )
163-
214+ cmd_args .append (
215+ "--enabled_sections=build_dotnet3 build_dotnet4 asset_package_only" )
216+
164217 # Check if need to gen new guids
165218 p = subprocess .Popen (cmd_args , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
166219 output , error = p .communicate ()
@@ -170,28 +223,30 @@ def main(argv):
170223 error_str = error_str .split ("assets:" )[- 1 ]
171224 error_str = error_str .rstrip ("\\ n\' " )
172225 split_string = error_str .split (" " )
173- split_string = split_string [3 :] # exclude first 3 lines
174- gen_guids_script_path = os .path .join (os .getcwd (), "scripts" , "build_scripts" , "gen_guids.py" )
226+ split_string = split_string [3 :] # exclude first 3 lines
227+ gen_guids_script_path = os .path .join (
228+ os .getcwd (), "scripts" , "build_scripts" , "gen_guids.py" )
175229 gen_cmd_args = [
176- sys .executable ,
177- gen_guids_script_path ,
178- "--guids_file=" + guids_file_path ,
179- "--version=" + last_version ,
180- "--generate_new_guids=True" ,
230+ sys .executable ,
231+ gen_guids_script_path ,
232+ "--guids_file=" + guids_file_path ,
233+ "--version=" + last_version ,
234+ "--generate_new_guids=True" ,
181235 ]
182236 for file in split_string :
183- file = file .strip ("\" " )
237+ file = file .strip ("\" " )
184238 print (file )
185239 gen_cmd_args .append (file )
186240 subprocess .call (gen_cmd_args )
187241
188242 # Need to package again if has that error
189- subprocess .call (cmd_args )
243+ subprocess .call (cmd_args )
190244 else :
191245 logging .info ("No new guid generated." )
192246 error_list = str (error ).split ("\\ n" )
193247 logging .info ("\n " .join (error_list ))
194248 logging .info ("Packaging done for version %s" , last_version )
195249
250+
196251if __name__ == '__main__' :
197252 app .run (main )
0 commit comments