Add swift-format dependency

This will be used first in the packages repo, but likely in the framework and engine in the future.

https://chrome-infra-packages.appspot.com/p/infra/3pp/tools/swift-format

Bug: https://github.com/flutter/flutter/issues/41129
Change-Id: I04d3120dbdb7a63cfc66280bd3473428ab1be1cb
Reviewed-on: https://flutter-review.googlesource.com/c/recipes/+/54020
Reviewed-by: Godofredo Contreras <godofredoc@google.com>
Commit-Queue: Jenn Magder <magder@google.com>
diff --git a/recipe_modules/flutter_deps/api.py b/recipe_modules/flutter_deps/api.py
index d4f4af8..175a558 100644
--- a/recipe_modules/flutter_deps/api.py
+++ b/recipe_modules/flutter_deps/api.py
@@ -100,6 +100,7 @@
         'ninja': self.ninja,
         'open_jdk': self.open_jdk,
         'ruby': self.ruby,
+        'swift_format': self.swift_format,
         'vs_build': self.vs_build,
     }
     parsed_deps = []
@@ -545,6 +546,26 @@
           ],
       )
 
+  def swift_format(self, env, env_prefixes, version=None):
+    """Installs swift-format Swift formatter and linter.
+
+    Args:
+      env(dict): Current environment variables.
+      env_prefixes(dict):  Current environment prefixes variables.
+    """
+    if not self.m.platform.is_mac:
+      # noop for non Mac platforms.
+      return
+    version = version or 'latest'
+    swift_format_path = self.m.path['cache'].join('swift_format')
+    sf = self.m.cipd.EnsureFile()
+    sf.add_package("infra/3pp/tools/swift-format/${platform}", version)
+    with self.m.step.nest('Install swift-format'):
+      self.m.cipd.ensure(swift_format_path, sf)
+    paths = env_prefixes.get('PATH', [])
+    paths.append(swift_format_path)
+    env_prefixes['PATH'] = paths
+
   def vs_build(self, env, env_prefixes, version=None):
     """Installs visual studio build.
 
diff --git a/recipe_modules/flutter_deps/examples/full.expected/mac.json b/recipe_modules/flutter_deps/examples/full.expected/mac.json
index efb41d4..c6687b0 100644
--- a/recipe_modules/flutter_deps/examples/full.expected/mac.json
+++ b/recipe_modules/flutter_deps/examples/full.expected/mac.json
@@ -803,6 +803,39 @@
     ]
   },
   {
+    "cmd": [],
+    "name": "Install swift-format"
+  },
+  {
+    "cmd": [
+      "cipd",
+      "ensure",
+      "-root",
+      "[CACHE]/swift_format",
+      "-ensure-file",
+      "infra/3pp/tools/swift-format/${platform} latest",
+      "-max-threads",
+      "0",
+      "-json-output",
+      "/path/to/tmp/json"
+    ],
+    "name": "Install swift-format.ensure_installed",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@",
+      "@@@STEP_LOG_LINE@json.output@{@@@",
+      "@@@STEP_LOG_LINE@json.output@  \"result\": {@@@",
+      "@@@STEP_LOG_LINE@json.output@    \"\": [@@@",
+      "@@@STEP_LOG_LINE@json.output@      {@@@",
+      "@@@STEP_LOG_LINE@json.output@        \"instance_id\": \"resolved-instance_id-of-latest----------\",@@@",
+      "@@@STEP_LOG_LINE@json.output@        \"package\": \"infra/3pp/tools/swift-format/resolved-platform\"@@@",
+      "@@@STEP_LOG_LINE@json.output@      }@@@",
+      "@@@STEP_LOG_LINE@json.output@    ]@@@",
+      "@@@STEP_LOG_LINE@json.output@  }@@@",
+      "@@@STEP_LOG_LINE@json.output@}@@@",
+      "@@@STEP_LOG_END@json.output@@@"
+    ]
+  },
+  {
     "cmd": [
       "ls",
       "-al",
diff --git a/recipe_modules/flutter_deps/examples/full.expected/mac_old.json b/recipe_modules/flutter_deps/examples/full.expected/mac_old.json
index 54ecd93..dd3eb0c 100644
--- a/recipe_modules/flutter_deps/examples/full.expected/mac_old.json
+++ b/recipe_modules/flutter_deps/examples/full.expected/mac_old.json
@@ -803,6 +803,39 @@
     ]
   },
   {
+    "cmd": [],
+    "name": "Install swift-format"
+  },
+  {
+    "cmd": [
+      "cipd",
+      "ensure",
+      "-root",
+      "[CACHE]/swift_format",
+      "-ensure-file",
+      "infra/3pp/tools/swift-format/${platform} latest",
+      "-max-threads",
+      "0",
+      "-json-output",
+      "/path/to/tmp/json"
+    ],
+    "name": "Install swift-format.ensure_installed",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@",
+      "@@@STEP_LOG_LINE@json.output@{@@@",
+      "@@@STEP_LOG_LINE@json.output@  \"result\": {@@@",
+      "@@@STEP_LOG_LINE@json.output@    \"\": [@@@",
+      "@@@STEP_LOG_LINE@json.output@      {@@@",
+      "@@@STEP_LOG_LINE@json.output@        \"instance_id\": \"resolved-instance_id-of-latest----------\",@@@",
+      "@@@STEP_LOG_LINE@json.output@        \"package\": \"infra/3pp/tools/swift-format/resolved-platform\"@@@",
+      "@@@STEP_LOG_LINE@json.output@      }@@@",
+      "@@@STEP_LOG_LINE@json.output@    ]@@@",
+      "@@@STEP_LOG_LINE@json.output@  }@@@",
+      "@@@STEP_LOG_LINE@json.output@}@@@",
+      "@@@STEP_LOG_END@json.output@@@"
+    ]
+  },
+  {
     "cmd": [
       "ls",
       "-al",
diff --git a/recipe_modules/flutter_deps/examples/full.py b/recipe_modules/flutter_deps/examples/full.py
index 2d7d39c..c0cb748 100644
--- a/recipe_modules/flutter_deps/examples/full.py
+++ b/recipe_modules/flutter_deps/examples/full.py
@@ -76,6 +76,7 @@
   api.flutter_deps.vs_build(env, env_prefixes, '')
   api.flutter_deps.ruby(env, env_prefixes, '')
   api.flutter_deps.android_virtual_device(env, env_prefixes, '34')
+  api.flutter_deps.swift_format(env, env_prefixes, '')
 
   with contextlib.ExitStack() as exit_stack:
     api.flutter_deps.enter_contexts(exit_stack, ['osx_sdk'], env, env_prefixes)