Skip to content

Commit ef79aa6

Browse files
authored
cp_unity_testapp.py: add --hardlink command-line flag (#320)
1 parent ddc2191 commit ef79aa6

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

firestore/scripts/cp_unity_testapp.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@
9494
"will need to be manually set in Xcode.",
9595
)
9696

97+
FLAG_HARDLINK_CS_FILES = flags.DEFINE_boolean(
98+
name="hardlink",
99+
default=False,
100+
help="Instead of copying the .cs source files, hardlink them. This can be "
101+
"useful when developing the C# code for the testapp itself, as changes "
102+
"to those files will be instantly reflected both in the destination "
103+
"Unity project and the GitHub repository."
104+
)
105+
97106

98107
def main(argv: Sequence[str]) -> None:
99108
if len(argv) > 1:
@@ -118,6 +127,7 @@ def main(argv: Sequence[str]) -> None:
118127
google_service_info_plist_file=flags.google_service_info_plist_file,
119128
android_package_name=flags.android_package_name,
120129
apple_developer_team_id=flags.apple_developer_team_id,
130+
hardlink_cs_files=flags.hardlink_cs_files,
121131
)
122132

123133
try:
@@ -152,6 +162,7 @@ class ParsedFlags:
152162
google_service_info_plist_file: Optional[pathlib.Path]
153163
android_package_name: Optional[str]
154164
apple_developer_team_id: Optional[str]
165+
hardlink_cs_files: bool
155166

156167
def parse(self) -> ParsedFlags:
157168
self._load_defaults_file()
@@ -167,6 +178,7 @@ def _to_parsed_flags(self) -> ParsedFlags:
167178
google_service_info_plist_file = self.google_service_info_plist_file,
168179
android_package_name = self.android_package_name,
169180
apple_developer_team_id = self.apple_developer_team_id,
181+
hardlink_cs_files = FLAG_HARDLINK_CS_FILES.value,
170182
)
171183

172184
def _load_defaults_file(self) -> None:
@@ -218,6 +230,8 @@ def _load_flag_values(self) -> None:
218230
self._log_using_flag_from_command_line(FLAG_APPLE_DEVELOPER_TEAM_ID)
219231
self.apple_developer_team_id = FLAG_APPLE_DEVELOPER_TEAM_ID.value
220232

233+
self._log_using_flag_from_command_line(FLAG_HARDLINK_CS_FILES)
234+
221235
@classmethod
222236
def _log_using_flag_from_command_line(cls, flag: flags.Flag) -> None:
223237
logging.info("Using flag from command line: --%s=%s", flag.name, flag.value)
@@ -278,6 +292,7 @@ def __init__(
278292
google_service_info_plist_file: Optional[pathlib.Path],
279293
android_package_name: Optional[str],
280294
apple_developer_team_id: Optional[str],
295+
hardlink_cs_files: bool,
281296
) -> None:
282297
self.git_repo_dir = git_repo_dir
283298
self.dest_dir_2017 = dest_dir_2017
@@ -286,6 +301,7 @@ def __init__(
286301
self.google_service_info_plist_file = google_service_info_plist_file
287302
self.android_package_name = android_package_name
288303
self.apple_developer_team_id = apple_developer_team_id
304+
self.hardlink_cs_files = hardlink_cs_files
289305

290306
def run(self) -> None:
291307
something_done = False
@@ -349,15 +365,23 @@ def _run(self, dest_dir: pathlib.Path, unity_version: int) -> None:
349365
project_settings_file = dest_dir / "ProjectSettings" / "ProjectSettings.asset"
350366
self._update_unity_app_info(project_settings_file, android_package_name, bundle_id)
351367

352-
@classmethod
353-
def _copy_file(cls, src_file: pathlib.Path, dest_file: pathlib.Path) -> None:
368+
# A drop-in replacement for `shutil.copy()` that creates hard links for some files
369+
# if hardlink_cs_files=True was specified to __init__().
370+
def _copy(self, src, dst, *, follow_symlinks=True):
371+
if self.hardlink_cs_files and str(src).endswith(".cs"):
372+
src_file = pathlib.Path(src)
373+
dst_file = pathlib.Path(dst)
374+
src_file.link_to(dst_file)
375+
else:
376+
shutil.copy(src, dst, follow_symlinks=follow_symlinks)
377+
378+
def _copy_file(self, src_file: pathlib.Path, dest_file: pathlib.Path) -> None:
354379
logging.info("Copying %s to %s", src_file, dest_file)
355-
shutil.copy(src_file, dest_file)
380+
self._copy(src_file, dest_file)
356381

357-
@classmethod
358-
def _copy_tree(cls, src_dir: pathlib.Path, dest_dir: pathlib.Path) -> None:
382+
def _copy_tree(self, src_dir: pathlib.Path, dest_dir: pathlib.Path) -> None:
359383
logging.info("Copying %s to %s", src_dir, dest_dir)
360-
shutil.copytree(src_dir, dest_dir)
384+
shutil.copytree(src_dir, dest_dir, copy_function=self._copy)
361385

362386
@classmethod
363387
def _rmtree(cls, dir_path: pathlib.Path) -> None:

0 commit comments

Comments
 (0)