From 09deb2e19c361d026a3ce0d7d839fe7116ca298e Mon Sep 17 00:00:00 2001
From: Patrick Balestra <patrickb@spotify.com>
Date: Tue, 8 Nov 2022 23:41:33 +0100
Subject: [PATCH] Add non-module deps via bzlmod module extension

---
 MODULE.bazel           |  8 ++++++
 apple/extensions.bzl   | 22 ++++++++++++++
 apple/repositories.bzl | 65 ++++++++++++++++++++++--------------------
 3 files changed, 64 insertions(+), 31 deletions(-)
 create mode 100644 apple/extensions.bzl

diff --git a/MODULE.bazel b/MODULE.bazel
index 22f034f0..599546d8 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -8,3 +8,11 @@ module(
 bazel_dep(name = "bazel_skylib", version = "1.1.1")
 bazel_dep(name = "apple_support", repo_name = "build_bazel_apple_support", version = "1.3.2")
 bazel_dep(name = "rules_swift", repo_name = "build_bazel_rules_swift", version = "1.2.0")
+
+non_module_deps = use_extension("//apple:extensions.bzl", "non_module_deps")
+
+use_repo(
+    non_module_deps,
+    "subpar",
+    "xctestrunner",
+)
diff --git a/apple/extensions.bzl b/apple/extensions.bzl
new file mode 100644
index 00000000..7fdef2a3
--- /dev/null
+++ b/apple/extensions.bzl
@@ -0,0 +1,22 @@
+# Copyright 2022 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Definitions for bzlmod module extensions."""
+
+load("//apple:repositories.bzl", "apple_rules_dependencies")
+
+def _non_module_deps_impl(_):
+    apple_rules_dependencies(include_bzlmod_ready_dependencies = False)
+
+non_module_deps = module_extension(implementation = _non_module_deps_impl)
diff --git a/apple/repositories.bzl b/apple/repositories.bzl
index c6e99374..baf0d649 100644
--- a/apple/repositories.bzl
+++ b/apple/repositories.bzl
@@ -85,7 +85,7 @@ run into compatibility issues. To silence this warning, pass \
 
     repo_rule(name = name, **kwargs)
 
-def apple_rules_dependencies(ignore_version_differences = False):
+def apple_rules_dependencies(ignore_version_differences = False, include_bzlmod_ready_dependencies = True):
     """Fetches repositories that are dependencies of the `rules_apple` workspace.
 
     Users should call this macro in their `WORKSPACE` to ensure that all of the
@@ -95,37 +95,40 @@ def apple_rules_dependencies(ignore_version_differences = False):
     Args:
       ignore_version_differences: If `True`, warnings about potentially
           incompatible versions of depended-upon repositories will be silenced.
+      include_bzlmod_ready_dependencies: Whether or not bzlmod-ready
+             dependencies should be included.
     """
-    _maybe(
-        http_archive,
-        name = "bazel_skylib",
-        urls = [
-            "https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
-            "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
-        ],
-        sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d",
-        ignore_version_differences = ignore_version_differences,
-    )
-
-    _maybe(
-        http_archive,
-        name = "build_bazel_apple_support",
-        sha256 = "2e3dc4d0000e8c2f5782ea7bb53162f37c485b5d8dc62bb3d7d7fc7c276f0d00",
-        urls = [
-            "https://github.com/bazelbuild/apple_support/releases/download/1.3.2/apple_support.1.3.2.tar.gz",
-        ],
-        ignore_version_differences = ignore_version_differences,
-    )
-
-    _maybe(
-        http_archive,
-        name = "build_bazel_rules_swift",
-        urls = [
-            "https://github.com/bazelbuild/rules_swift/releases/download/1.2.0/rules_swift.1.2.0.tar.gz",
-        ],
-        sha256 = "51efdaf85e04e51174de76ef563f255451d5a5cd24c61ad902feeadafc7046d9",
-        ignore_version_differences = ignore_version_differences,
-    )
+    if include_bzlmod_ready_dependencies:
+        _maybe(
+            http_archive,
+            name = "bazel_skylib",
+            urls = [
+                "https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
+                "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
+            ],
+            sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d",
+            ignore_version_differences = ignore_version_differences,
+        )
+
+        _maybe(
+            http_archive,
+            name = "build_bazel_apple_support",
+            sha256 = "2e3dc4d0000e8c2f5782ea7bb53162f37c485b5d8dc62bb3d7d7fc7c276f0d00",
+            urls = [
+                "https://github.com/bazelbuild/apple_support/releases/download/1.3.2/apple_support.1.3.2.tar.gz",
+            ],
+            ignore_version_differences = ignore_version_differences,
+        )
+
+        _maybe(
+            http_archive,
+            name = "build_bazel_rules_swift",
+            urls = [
+                "https://github.com/bazelbuild/rules_swift/releases/download/1.2.0/rules_swift.1.2.0.tar.gz",
+            ],
+            sha256 = "51efdaf85e04e51174de76ef563f255451d5a5cd24c61ad902feeadafc7046d9",
+            ignore_version_differences = ignore_version_differences,
+        )
 
     _maybe(
         http_archive,
-- 
2.37.0 (Apple Git-136)

