Skip to content

Commit a2b9bb5

Browse files
authored
update one product build package logic (#423)
1 parent 8fba07a commit a2b9bb5

15 files changed

+4599
-27
lines changed

.github/workflows/package.yml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ on:
4141
env:
4242
# Use SHA256 for hashing files.
4343
hashCommand: "sha256sum"
44+
default_apis: 'analytics,auth,crashlytics,database,dynamic_links,firestore,functions,installations,messaging,remote_config,storage'
4445

4546
jobs:
4647
package_sdks:
@@ -49,6 +50,23 @@ jobs:
4950
strategy:
5051
fail-fast: false
5152
steps:
53+
- name: Check input
54+
# This check the apis input. It detect whether the package is only asking for subset of
55+
# of the products, like auth,firestore only etc. If the input apis are less than the
56+
# default_apis env attribute, we package only the provided apis, for test purposes only.
57+
id: check-input
58+
shell: bash
59+
run: |
60+
IFS=',' read -r -a input_apis <<< "${{ inputs.apis }}"
61+
IFS=',' read -r -a default_apis <<< "${{ env.default_apis }}"
62+
if [[ ${#input_apis[@]} != ${#default_apis[@]} ]]; then
63+
echo "::set-output name=package_for_checks::1"
64+
echo "::set-output name=package_apis::'--apis=${{ inputs.apis }}'"
65+
else
66+
echo "::set-output name=package_for_checks::0"
67+
echo "::set-output name=package_apis::"
68+
fi
69+
5270
- name: Print inputs
5371
shell: bash
5472
run: |
@@ -63,6 +81,8 @@ jobs:
6381
echo download_windows_run: ${{ inputs.download_windows_run }}
6482
echo platforms: ${{ inputs.platforms }}"
6583
echo apis: ${{ inputs.apis }}"
84+
echo is the package for checks: ${{ steps.check-input.outputs.package_for_checks }}
85+
echo package_apis: ${{ steps.check-input.outputs.package_apis }}
6686
6787
- name: Check out base branch
6888
uses: actions/[email protected]
@@ -135,7 +155,7 @@ jobs:
135155
136156
- name: Package unitypackage
137157
run: |
138-
python scripts/build_scripts/build_package.py --zip_dir=built_artifact
158+
python scripts/build_scripts/build_package.py --zip_dir=built_artifact ${{ steps.check-input.outputs.package_apis }}
139159
140160
- name: Commit Changes if there is any
141161
if: inputs.working_branch != '' || inputs.create_new_branch == '1'
@@ -210,15 +230,18 @@ jobs:
210230
path: firebase_unity_sdk.zip
211231

212232
- name: Package tgz
233+
if: ${{ steps.check-input.outputs.package_for_checks }} == '1'
213234
run: |
214235
python scripts/build_scripts/build_package.py --zip_dir=built_artifact --output_upm=True --output=output_tgz
215236
216237
- name: Listing output tgz
238+
if: ${{ steps.check-input.outputs.package_for_checks }} == '1'
217239
run: |
218240
ls -Rl
219241
working-directory: output_tgz
220242

221243
- name: compute SDK hash for tgz files
244+
if: ${{ steps.check-input.outputs.package_for_checks }} == '1'
222245
shell: bash
223246
run: |
224247
tgz_files_list=$(find -type f -name '*.tgz')
@@ -229,6 +252,7 @@ jobs:
229252
working-directory: output_tgz
230253

231254
- name: Upload Build tgz
255+
if: ${{ steps.check-input.outputs.package_for_checks }} == '1'
232256
uses: actions/upload-artifact@v2
233257
with:
234258
name: firebase_unity_sdk_tgz

scripts/build_scripts/build_package.py

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,18 @@
3939
from absl import flags
4040
from 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+
4249
FLAGS = flags.FLAGS
4350
flags.DEFINE_string('zip_dir', None,
4451
'Directory of zip files from build output to package')
4552
flags.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."))
4854
flags.DEFINE_string("guids_file", "guids.json",
4955
"Json file with stable guids cache.")
5056

@@ -55,7 +61,13 @@
5561
"Output folder for unitypackage.")
5662

5763
flags.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

6072
def 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+
7891
def 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+
92107
def 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+
120158
def 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+
196251
if __name__ == '__main__':
197252
app.run(main)

scripts/create_debug_export.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2022 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Based on the unity_packer/exports.json file, generate separate debug
18+
export config for each product.
19+
20+
Example usage:
21+
python scripts/build_scripts/create_debug_export.py
22+
"""
23+
24+
import os
25+
import json
26+
27+
from absl import app
28+
from absl import flags
29+
from absl import logging
30+
31+
API_PACKAGE_MAP = {
32+
"analytics": "FirebaseAnalytics.unitypackage",
33+
"auth": "FirebaseAuth.unitypackage",
34+
"crashlytics": "FirebaseCrashlytics.unitypackage",
35+
"database": "FirebaseDatabase.unitypackage",
36+
"dynamic_links": "FirebaseDynamicLinks.unitypackage",
37+
"firestore": "FirebaseFirestore.unitypackage",
38+
"functions": "FirebaseFunctions.unitypackage",
39+
"installations": "FirebaseInstallations.unitypackage",
40+
"messaging": "FirebaseMessaging.unitypackage",
41+
"remote_config": "FirebaseRemoteConfig.unitypackage",
42+
"storage": "FirebaseStorage.unitypackage",
43+
}
44+
45+
default_package_names = [
46+
"FirebaseApp.unitypackage", "SampleCommon.unitypackage"]
47+
48+
49+
FLAGS = flags.FLAGS
50+
flags.DEFINE_string('prod_export', "exports.json",
51+
'Production export json template to copy from')
52+
flags.DEFINE_string("json_folder", "unity_packer",
53+
("The root folder for json configs."))
54+
flags.DEFINE_string("output_folder", "debug_single_export_json",
55+
"Json file with stable guids cache.")
56+
57+
58+
def main(argv):
59+
if len(argv) > 1:
60+
raise app.UsageError('Too many command-line arguments.')
61+
prod_export_json_path = os.path.join(
62+
os.getcwd(), FLAGS.json_folder, FLAGS.prod_export)
63+
64+
with open(prod_export_json_path, "r") as fin:
65+
export_json = json.load(fin)
66+
67+
# making sure only dotnet4 exists
68+
builds = export_json["builds"]
69+
for idx, dict in enumerate(builds):
70+
if dict["name"] == "dotnet3":
71+
builds.pop(idx)
72+
73+
packages = export_json["packages"]
74+
75+
for api_name, package_name in API_PACKAGE_MAP.items():
76+
output_path = os.path.join(
77+
os.getcwd(), FLAGS.json_folder, FLAGS.output_folder, api_name + ".json")
78+
output_dict = {}
79+
output_package_list = []
80+
for idx, package_dict in enumerate(packages):
81+
if package_dict["name"] in default_package_names:
82+
output_package_list.append(packages[idx])
83+
elif package_dict["name"] == package_name:
84+
output_package_list.append(packages[idx])
85+
output_dict["packages"] = output_package_list
86+
output_dict["builds"] = builds
87+
88+
with open(output_path, 'w', encoding='utf-8') as fout:
89+
fout.write(json.dumps(output_dict, indent=2))
90+
logging.info("Write %s", output_path)
91+
92+
93+
if __name__ == '__main__':
94+
app.run(main)

0 commit comments

Comments
 (0)