Update osx_sdk module to add cache cleanup support.

Using xcode runtimes pollutes the cache causing the next builds to
fail finding the simulators. This CL ensures that after every build
using runtimes deletes the xcode cache.

Bug: https://github.com/flutter/flutter/issues/113539
Change-Id: Id42c365fe0d3b7cd5be06b4f4e83f93d7ef18d88
Reviewed-on: https://flutter-review.googlesource.com/c/recipes/+/34860
Reviewed-by: Nehal Patel <nehalvpatel@google.com>
Commit-Queue: Godofredo Contreras <godofredoc@google.com>
(cherry picked from commit e02f2b41ba98db6d230fee81689e4a6e89526453)
Reviewed-on: https://flutter-review.googlesource.com/c/recipes/+/35601
Reviewed-by: Keyong Han <keyonghan@google.com>
diff --git a/recipe_modules/osx_sdk/__init__.py b/recipe_modules/osx_sdk/__init__.py
index 27d77bd..2fdfe66 100644
--- a/recipe_modules/osx_sdk/__init__.py
+++ b/recipe_modules/osx_sdk/__init__.py
@@ -36,6 +36,8 @@
       # The CIPD toolchain tool package and version
       toolchain_pkg=Single(str),
       toolchain_ver=Single(str),
+      # Cleanup caches
+      cleanup_cache=Single(bool),
     ), default={},
     )
 }
diff --git a/recipe_modules/osx_sdk/api.py b/recipe_modules/osx_sdk/api.py
index 2f0f18d..2125f0e 100644
--- a/recipe_modules/osx_sdk/api.py
+++ b/recipe_modules/osx_sdk/api.py
@@ -46,6 +46,11 @@
     self._runtime_versions = None
     self._tool_pkg = 'infra/tools/mac_toolchain/${platform}'
     self._tool_ver = 'latest'
+    self._cleanup_cache = False
+
+  def _clean_cache(self):
+    if self._cleanup_cache or self._runtime_versions:
+      self.m.file.rmtree('Cleaning up Xcode cache', self.m.path['cache'].join('osx_sdk'))
 
   def initialize(self):
     """Initializes xcode, and ios versions.
@@ -56,6 +61,9 @@
     if not self.m.platform.is_mac:
       return
 
+    if 'cleanup_cache' in self._sdk_properties:
+      self._cleanup_cache = self._sdk_properties['cleanup_cache']
+
     if 'toolchain_ver' in self._sdk_properties:
       self._tool_ver = self._sdk_properties['toolchain_ver'].lower()
 
@@ -157,6 +165,7 @@
 
     try:
       with self.m.context(infra_steps=True):
+        self._clean_cache()
         app = self._ensure_sdk(kind)
         self.m.os_utils.kill_simulators()
         self.m.step('select XCode', ['sudo', 'xcode-select', '--switch', app])
@@ -165,6 +174,7 @@
     finally:
       with self.m.context(infra_steps=True):
         self.m.step('reset XCode', ['sudo', 'xcode-select', '--reset'])
+        self._clean_cache()
 
   def _ensure_sdk(self, kind):
     """Ensures the mac_toolchain tool and OS X SDK packages are installed.
diff --git a/recipe_modules/osx_sdk/examples/full.expected/explicit_runtime_version.json b/recipe_modules/osx_sdk/examples/full.expected/explicit_runtime_version.json
index daee234..edd3a8b 100644
--- a/recipe_modules/osx_sdk/examples/full.expected/explicit_runtime_version.json
+++ b/recipe_modules/osx_sdk/examples/full.expected/explicit_runtime_version.json
@@ -1,6 +1,19 @@
 [
   {
     "cmd": [
+      "vpython3",
+      "-u",
+      "RECIPE_MODULE[recipe_engine::file]/resources/fileutil.py",
+      "--json-output",
+      "/path/to/tmp/json",
+      "rmtree",
+      "[CACHE]/osx_sdk"
+    ],
+    "infra_step": true,
+    "name": "Cleaning up Xcode cache"
+  },
+  {
+    "cmd": [
       "cipd",
       "ensure",
       "-root",
@@ -211,6 +224,19 @@
     "name": "reset XCode"
   },
   {
+    "cmd": [
+      "vpython3",
+      "-u",
+      "RECIPE_MODULE[recipe_engine::file]/resources/fileutil.py",
+      "--json-output",
+      "/path/to/tmp/json",
+      "rmtree",
+      "[CACHE]/osx_sdk"
+    ],
+    "infra_step": true,
+    "name": "Cleaning up Xcode cache (2)"
+  },
+  {
     "name": "$result"
   }
 ]
\ No newline at end of file
diff --git a/recipe_modules/osx_sdk/examples/full.expected/explicit_runtime_version_nosymlink.json b/recipe_modules/osx_sdk/examples/full.expected/explicit_runtime_version_nosymlink.json
index 1af5484..e470be6 100644
--- a/recipe_modules/osx_sdk/examples/full.expected/explicit_runtime_version_nosymlink.json
+++ b/recipe_modules/osx_sdk/examples/full.expected/explicit_runtime_version_nosymlink.json
@@ -1,6 +1,19 @@
 [
   {
     "cmd": [
+      "vpython3",
+      "-u",
+      "RECIPE_MODULE[recipe_engine::file]/resources/fileutil.py",
+      "--json-output",
+      "/path/to/tmp/json",
+      "rmtree",
+      "[CACHE]/osx_sdk"
+    ],
+    "infra_step": true,
+    "name": "Cleaning up Xcode cache"
+  },
+  {
+    "cmd": [
       "cipd",
       "ensure",
       "-root",
@@ -211,6 +224,19 @@
     "name": "reset XCode"
   },
   {
+    "cmd": [
+      "vpython3",
+      "-u",
+      "RECIPE_MODULE[recipe_engine::file]/resources/fileutil.py",
+      "--json-output",
+      "/path/to/tmp/json",
+      "rmtree",
+      "[CACHE]/osx_sdk"
+    ],
+    "infra_step": true,
+    "name": "Cleaning up Xcode cache (2)"
+  },
+  {
     "name": "$result"
   }
 ]
\ No newline at end of file
diff --git a/recipe_modules/osx_sdk/examples/full.expected/explicit_version.json b/recipe_modules/osx_sdk/examples/full.expected/explicit_version.json
index 585b62f..4885716 100644
--- a/recipe_modules/osx_sdk/examples/full.expected/explicit_version.json
+++ b/recipe_modules/osx_sdk/examples/full.expected/explicit_version.json
@@ -1,6 +1,19 @@
 [
   {
     "cmd": [
+      "vpython3",
+      "-u",
+      "RECIPE_MODULE[recipe_engine::file]/resources/fileutil.py",
+      "--json-output",
+      "/path/to/tmp/json",
+      "rmtree",
+      "[CACHE]/osx_sdk"
+    ],
+    "infra_step": true,
+    "name": "Cleaning up Xcode cache"
+  },
+  {
+    "cmd": [
       "cipd",
       "ensure",
       "-root",
@@ -99,6 +112,19 @@
     "name": "reset XCode"
   },
   {
+    "cmd": [
+      "vpython3",
+      "-u",
+      "RECIPE_MODULE[recipe_engine::file]/resources/fileutil.py",
+      "--json-output",
+      "/path/to/tmp/json",
+      "rmtree",
+      "[CACHE]/osx_sdk"
+    ],
+    "infra_step": true,
+    "name": "Cleaning up Xcode cache (2)"
+  },
+  {
     "name": "$result"
   }
 ]
\ No newline at end of file
diff --git a/recipe_modules/osx_sdk/examples/full.py b/recipe_modules/osx_sdk/examples/full.py
index 975a9a9..7ddd350 100644
--- a/recipe_modules/osx_sdk/examples/full.py
+++ b/recipe_modules/osx_sdk/examples/full.py
@@ -30,7 +30,8 @@
       'explicit_version',
       api.platform.name('mac'),
       api.properties(**{'$flutter/osx_sdk': {
-          'sdk_version': 'deadbeef', 'toolchain_ver': '123abc'
+          'sdk_version': 'deadbeef', 'toolchain_ver': '123abc',
+          'cleanup_cache': True
       }})
   )