Reset Xcode before `flutter doctor` to prevent Xcode corruption from causing it to fail.

Also, add check to verify Xcode is not damaged/corrupted and if it is, clear the cache and try to install. On second attempt to re-install, try using a different output path.

This fix is aimed at preventing continuous failures on subsequent builds if the Xcode package becomes corrupted.

Fixes: https://github.com/flutter/flutter/issues/139152
Related bug: https://github.com/flutter/flutter/issues/138238

Change-Id: I0f72974241916e9a66f26291ddce01b4b615121f
Reviewed-on: https://flutter-review.googlesource.com/c/recipes/+/52702
Reviewed-by: Keyong Han <keyonghan@google.com>
Commit-Queue: Victoria Ashworth <vashworth@google.com>
diff --git a/recipe_modules/flutter_deps/examples/full.expected/mac.json b/recipe_modules/flutter_deps/examples/full.expected/mac.json
index b1849e4..4de2f65 100644
--- a/recipe_modules/flutter_deps/examples/full.expected/mac.json
+++ b/recipe_modules/flutter_deps/examples/full.expected/mac.json
@@ -812,6 +812,10 @@
     "name": "Show xcode cache"
   },
   {
+    "cmd": [],
+    "name": "install xcode"
+  },
+  {
     "cmd": [
       "cipd",
       "ensure",
@@ -825,8 +829,9 @@
       "/path/to/tmp/json"
     ],
     "infra_step": true,
-    "name": "ensure_installed (7)",
+    "name": "install xcode.ensure_installed",
     "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@",
       "@@@STEP_LOG_LINE@json.output@{@@@",
       "@@@STEP_LOG_LINE@json.output@  \"result\": {@@@",
       "@@@STEP_LOG_LINE@json.output@    \"\": [@@@",
@@ -856,7 +861,11 @@
       "-verbose"
     ],
     "infra_step": true,
-    "name": "install xcode"
+    "name": "install xcode.install xcode from cipd",
+    "timeout": 1800,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@"
+    ]
   },
   {
     "cmd": [
@@ -875,7 +884,7 @@
       "[CACHE]/osx_sdk/xcode_9f2000/XCode.app"
     ],
     "infra_step": true,
-    "name": "select XCode"
+    "name": "select xcode"
   },
   {
     "cmd": [
@@ -910,6 +919,10 @@
     "name": "Cleaning up Xcode cache"
   },
   {
+    "cmd": [],
+    "name": "install xcode (2)"
+  },
+  {
     "cmd": [
       "cipd",
       "ensure",
@@ -923,8 +936,9 @@
       "/path/to/tmp/json"
     ],
     "infra_step": true,
-    "name": "ensure_installed (8)",
+    "name": "install xcode (2).ensure_installed",
     "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@",
       "@@@STEP_LOG_LINE@json.output@{@@@",
       "@@@STEP_LOG_LINE@json.output@  \"result\": {@@@",
       "@@@STEP_LOG_LINE@json.output@    \"\": [@@@",
@@ -954,7 +968,11 @@
       "-verbose"
     ],
     "infra_step": true,
-    "name": "install xcode (2)"
+    "name": "install xcode (2).install xcode from cipd",
+    "timeout": 1800,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@"
+    ]
   },
   {
     "cmd": [
@@ -973,7 +991,7 @@
       "[CACHE]/osx_sdk/xcode_9f2000/XCode.app"
     ],
     "infra_step": true,
-    "name": "select XCode (2)"
+    "name": "select xcode (2)"
   },
   {
     "cmd": [
@@ -1007,6 +1025,10 @@
     "name": "Cleaning up Xcode cache (2)"
   },
   {
+    "cmd": [],
+    "name": "install xcode (3)"
+  },
+  {
     "cmd": [
       "cipd",
       "ensure",
@@ -1020,8 +1042,9 @@
       "/path/to/tmp/json"
     ],
     "infra_step": true,
-    "name": "ensure_installed (9)",
+    "name": "install xcode (3).ensure_installed",
     "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@",
       "@@@STEP_LOG_LINE@json.output@{@@@",
       "@@@STEP_LOG_LINE@json.output@  \"result\": {@@@",
       "@@@STEP_LOG_LINE@json.output@    \"\": [@@@",
@@ -1051,7 +1074,11 @@
       "-verbose"
     ],
     "infra_step": true,
-    "name": "install xcode (3)"
+    "name": "install xcode (3).install xcode from cipd",
+    "timeout": 1800,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@"
+    ]
   },
   {
     "cmd": [
@@ -1070,7 +1097,7 @@
       "/opt/flutter/xcode/9f2000/XCode.app"
     ],
     "infra_step": true,
-    "name": "select XCode (3)"
+    "name": "select xcode (3)"
   },
   {
     "cmd": [
@@ -1105,6 +1132,10 @@
     "name": "Cleaning up Xcode cache (3)"
   },
   {
+    "cmd": [],
+    "name": "install xcode (4)"
+  },
+  {
     "cmd": [
       "cipd",
       "ensure",
@@ -1118,8 +1149,9 @@
       "/path/to/tmp/json"
     ],
     "infra_step": true,
-    "name": "ensure_installed (10)",
+    "name": "install xcode (4).ensure_installed",
     "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@",
       "@@@STEP_LOG_LINE@json.output@{@@@",
       "@@@STEP_LOG_LINE@json.output@  \"result\": {@@@",
       "@@@STEP_LOG_LINE@json.output@    \"\": [@@@",
@@ -1149,7 +1181,11 @@
       "-verbose"
     ],
     "infra_step": true,
-    "name": "install xcode (4)"
+    "name": "install xcode (4).install xcode from cipd",
+    "timeout": 1800,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@"
+    ]
   },
   {
     "cmd": [
@@ -1168,7 +1204,7 @@
       "/opt/flutter/xcode/9f2000/XCode.app"
     ],
     "infra_step": true,
-    "name": "select XCode (4)"
+    "name": "select xcode (4)"
   },
   {
     "cmd": [