diff --git a/make_game.py b/make_game.py index 397fad2cf..cff1068e1 100644 --- a/make_game.py +++ b/make_game.py @@ -334,7 +334,19 @@ def choose_game() -> str: print(" That folder isn't a supported version of the game. Try again.") +def icloud_documents() -> bool: + """macOS: whether iCloud Drive holds Desktop & Documents. With that sync on, ~/Documents lives in iCloud Drive, + which uploads what goes in it and may evict files from the disk ("Optimize Mac Storage"). The folder can stay in + iCloud Drive after the sync is turned off; leaving Documents alone then costs nothing.""" + return sys.platform == "darwin" and os.path.isdir( + os.path.join(os.path.expanduser("~"), "Library", "Mobile Documents", "com~apple~CloudDocs", "Documents")) + + def default_out() -> str: + """Documents/Leonida, except on a Mac whose Documents iCloud syncs: the port and its work folder (hundreds of + GB made from the game) must stay on this computer, so they go to ~/Leonida there.""" + if icloud_documents(): + return os.path.join(os.path.expanduser("~"), "Leonida") return os.path.join(os.path.expanduser("~"), "Documents", "Leonida") @@ -1235,7 +1247,8 @@ def main() -> int: ap = argparse.ArgumentParser(description="Build the PC port from your own copy of the game.") ap.add_argument("--game", help="your package folder (found automatically if left out)") ap.add_argument("--pick", action="store_true", help="choose your package folder in a folder dialog") - ap.add_argument("--out", help="folder for the finished port (default Documents/Leonida)") + ap.add_argument("--out", help="folder for the finished port (default Documents/Leonida; ~/Leonida on a Mac " + "whose Documents iCloud syncs)") ap.add_argument("--work", help="folder for intermediate files (hundreds of GB; default /work)") ap.add_argument("--stages", help="comma list from: " + ",".join(STAGES)) ap.add_argument("--check", action="store_true", help="only check the game files") @@ -1336,6 +1349,8 @@ def main() -> int: fallbacks = fallback_flags(a) if "shaders" in stages else () preflight(stages, work, a.wav, runtime_build, emulate=emulate, keep_emulated=keep_emulated, fallbacks=fallbacks, jobs=a.jobs) + if not a.out and icloud_documents(): + log("iCloud syncs Documents on this Mac: building into ~/Leonida instead, so the game's files stay off iCloud") log(f"building into {out} (work files in {work})") t0 = time.time() if "shaders" in stages: diff --git a/tests/test_make_game_preflight.py b/tests/test_make_game_preflight.py index eb6964bb1..f1950cd34 100644 --- a/tests/test_make_game_preflight.py +++ b/tests/test_make_game_preflight.py @@ -654,9 +654,26 @@ class PathAndGameSelectionTest(PreflightTestBase): self.assert_fails(make_game.choose_game, match="More than one copy found: choose one with --game.") + def default_out_in(self, platform, icloud_documents): + """default_out() with a temporary home folder; icloud_documents makes the folder macOS keeps in iCloud + Drive when Desktop & Documents sync is on (~/Library/Mobile Documents/com~apple~CloudDocs/Documents).""" + with tempfile.TemporaryDirectory() as home: + if icloud_documents: + os.makedirs(os.path.join(home, "Library", "Mobile Documents", "com~apple~CloudDocs", "Documents")) + with mock.patch.object(make_game.os.path, "expanduser", lambda p: home + p[1:] if p.startswith("~") else p), \ + mock.patch.object(make_game.sys, "platform", platform): + return os.path.relpath(make_game.default_out(), home) + def test_default_out_matches_documents_folder(self): - expected = os.path.join(os.path.expanduser("~"), "Documents", "Leonida") - self.assertEqual(make_game.default_out(), expected) + for platform in ("win32", "linux", "darwin"): + self.assertEqual(self.default_out_in(platform, False), os.path.join("Documents", "Leonida"), platform) + + def test_default_out_leaves_icloud_synced_documents_on_a_mac(self): + # Before: with iCloud Desktop & Documents sync on, ~/Documents/Leonida (and its work folder, hundreds of GB + # made from the game) went into iCloud Drive, which uploads it and can evict the work files from the disk. + self.assertEqual(self.default_out_in("darwin", True), "Leonida") + # Only macOS keeps Documents in iCloud Drive this way; elsewhere the folder means nothing. + self.assertEqual(self.default_out_in("linux", True), os.path.join("Documents", "Leonida")) def test_constants_definitions(self): self.assertEqual(make_game.STAGES, ["check", "shaders", "assets", "runtime", "pipelines"])