From add12e74a55cc7884264d01f3122f9d4e34c4347 Mon Sep 17 00:00:00 2001 From: Nicholas Entin Date: Fri, 19 Jan 2024 21:13:12 -0800 Subject: [PATCH] Add support for installing via Bazel * Adds support for installing `Stagehand` via Bazel * Adds support for installing `StagehandTesting` via Bazel _when using `iOSSnapshotTestCase` as the snapshotting engine_. Support for `SnapshotTesting` is currently blocked. --- .github/workflows/ci.yml | 8 +++ .gitignore | 3 + BUILD.bazel | 71 +++++++++++++++++++ Bazel/0001-Patch-testonly-swift_library.patch | 20 ++++++ Bazel/BUILD.bazel | 0 Bazel/non_bzlmod_deps.bzl | 28 ++++++++ Example/.bazelrc | 8 +++ Example/.bazelversion | 1 + MODULE.bazel | 29 ++++++++ Sources/Info.plist | 24 +++++++ .../Core/AnimationSnapshotting.swift | 2 + .../FBSnapshotTestCase+Animation.swift | 5 ++ .../FBSnapshotTestCase+AnimationGIF.swift | 6 ++ WORKSPACE.bzlmod | 1 + 14 files changed, 206 insertions(+) create mode 100644 BUILD.bazel create mode 100644 Bazel/0001-Patch-testonly-swift_library.patch create mode 100644 Bazel/BUILD.bazel create mode 100644 Bazel/non_bzlmod_deps.bzl create mode 100644 Example/.bazelrc create mode 100644 Example/.bazelversion create mode 100644 MODULE.bazel create mode 100644 Sources/Info.plist create mode 100644 WORKSPACE.bzlmod diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 780b984..ad589ac 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -59,3 +59,11 @@ jobs: run: Scripts/github/prepare-simulators.sh ${{ matrix.platform }} - name: Build run: Scripts/build.swift spm ${{ matrix.platform }} + bazel: + name: Bazel + runs-on: macOS-13 + steps: + - name: Checkout Repo + uses: actions/checkout@v2 + - name: Build + run: bazel build //... diff --git a/.gitignore b/.gitignore index 7e83334..e743b1f 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,6 @@ timeline.xctimeline generated/ .build/ .swiftpm/ + +# Bazel +bazel-* diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000..2967554 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,71 @@ +load( + "@rules_apple//apple:ios.bzl", + "ios_framework", + "ios_unit_test", +) +load( + "@build_bazel_rules_swift//swift:swift.bzl", + "swift_library", +) + +swift_library( + name = "Stagehand.lib", + srcs = glob(["Sources/Stagehand/**/*.swift"]), + module_name = "Stagehand", + tags = ["manual"], + visibility = ["//visibility:public"], + deps = [], +) + +swift_library( + name = "StagehandTestingCore.lib", + testonly = True, + srcs = glob(["Sources/StagehandTesting/Core/**/*.swift"]), + defines = ["BAZEL_PACKAGE"], + module_name = "StagehandTestingCore", + tags = ["manual"], + visibility = ["//visibility:public"], + deps = [":Stagehand.lib"], +) + +swift_library( + name = "StagehandTesting_iOSSnapshotTestCase.lib", + testonly = True, + srcs = glob(["Sources/StagehandTesting/iOSSnapshotTestCase/**/*.swift"]), + defines = ["BAZEL_PACKAGE"], + module_name = "StagehandTesting_iOSSnapshotTestCase", + tags = ["manual"], + visibility = ["//visibility:public"], + deps = [ + ":Stagehand.lib", + ":StagehandTestingCore.lib", + "@ios_snapshot_test_case//:iOSSnapshotTestCase", + ], +) + +ios_framework( + name = "Stagehand", + bundle_id = "com.squareup.Stagehand", + families = [ + "iphone", + "ipad", + ], + infoplists = ["Sources/Info.plist"], + minimum_os_version = "12.0", + visibility = ["//visibility:public"], + deps = [":Stagehand.lib"], +) + +ios_framework( + name = "StagehandTesting_iOSSnapshotTestCase", + testonly = True, + bundle_id = "com.squareup.StagehandTesting", + families = [ + "iphone", + "ipad", + ], + infoplists = ["Sources/Info.plist"], + minimum_os_version = "12.0", + visibility = ["//visibility:public"], + deps = [":StagehandTesting_iOSSnapshotTestCase.lib"], +) diff --git a/Bazel/0001-Patch-testonly-swift_library.patch b/Bazel/0001-Patch-testonly-swift_library.patch new file mode 100644 index 0000000..da7fae2 --- /dev/null +++ b/Bazel/0001-Patch-testonly-swift_library.patch @@ -0,0 +1,20 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Luis Padron +Date: Fri, 19 Jan 2024 15:17:39 -0500 +Subject: Patch testonly swift_library + + +diff --git a/BUILD.bazel b/BUILD.bazel +index 0ae5406..42e81b5 100644 +--- a/BUILD.bazel ++++ b/BUILD.bazel +@@ -25,5 +25,6 @@ swift_library( + deps = [ + ":iOSSnapshotTestCaseCore" + ], ++ testonly = True, + visibility = ["//visibility:public"] + ) +-- +2.42.1 + diff --git a/Bazel/BUILD.bazel b/Bazel/BUILD.bazel new file mode 100644 index 0000000..e69de29 diff --git a/Bazel/non_bzlmod_deps.bzl b/Bazel/non_bzlmod_deps.bzl new file mode 100644 index 0000000..cde5b2b --- /dev/null +++ b/Bazel/non_bzlmod_deps.bzl @@ -0,0 +1,28 @@ +"""Defines extensions and macros for MODULE.bazel""" + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +# -- Non-bzlmod versions + +IOS_SNAPSHOT_TEST_CASE_VERSION = "57b023c8bb3df361e2fae01532cd066ec0b65d2e" + +# -- Module extension + +def non_bzlmod_repositories(): + """Defines external dependencies which do not support bzlmod""" + + http_archive( + name = "ios_snapshot_test_case", + url = "https://github.com/uber/ios-snapshot-test-case/archive/%s.zip" % IOS_SNAPSHOT_TEST_CASE_VERSION, + strip_prefix = "ios-snapshot-test-case-%s" % IOS_SNAPSHOT_TEST_CASE_VERSION, + sha256 = "fae7ec6bfdc35bb026a2e898295c16240eeb001bed188972ddcc0d7dc388cda3", + patches = ["//Bazel:0001-Patch-testonly-swift_library.patch"], + patch_args = ["-p1"], + ) + +def _non_bzlmod_deps_impl(_): + non_bzlmod_repositories() + +non_bzlmod_deps = module_extension( + implementation = _non_bzlmod_deps_impl, +) diff --git a/Example/.bazelrc b/Example/.bazelrc new file mode 100644 index 0000000..08658cc --- /dev/null +++ b/Example/.bazelrc @@ -0,0 +1,8 @@ +# TODO: remove when using Bazel 7 where bzlmod is on by default +common --enable_bzlmod + +# Use the apple_support macOS toolchains +common --enable_platform_specific_config +common:macos --apple_crosstool_top=@local_config_apple_cc//:toolchain +common:macos --crosstool_top=@local_config_apple_cc//:toolchain +common:macos --host_crosstool_top=@local_config_apple_cc//:toolchain diff --git a/Example/.bazelversion b/Example/.bazelversion new file mode 100644 index 0000000..19b860c --- /dev/null +++ b/Example/.bazelversion @@ -0,0 +1 @@ +6.4.0 diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 0000000..9035335 --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,29 @@ +module( + name = "stagehand", + version = "0.0.0", + compatibility_level = 1, +) + +bazel_dep( + name = "apple_support", + version = "1.11.1", +) +bazel_dep( + name = "rules_apple", + version = "3.1.1", +) +bazel_dep( + name = "rules_swift", + version = "1.14.0", + repo_name = "build_bazel_rules_swift", +) + +# Load non-bzlmod dependencies +non_bzlmod_deps = use_extension("//Bazel:non_bzlmod_deps.bzl", "non_bzlmod_deps") +use_repo( + non_bzlmod_deps, + "ios_snapshot_test_case", +) + +apple_cc_configure = use_extension("@apple_support//crosstool:setup.bzl", "apple_cc_configure_extension") +use_repo(apple_cc_configure, "local_config_apple_cc") diff --git a/Sources/Info.plist b/Sources/Info.plist new file mode 100644 index 0000000..609619b --- /dev/null +++ b/Sources/Info.plist @@ -0,0 +1,24 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + FMWK + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1.0 + NSPrincipalClass + + + diff --git a/Sources/StagehandTesting/Core/AnimationSnapshotting.swift b/Sources/StagehandTesting/Core/AnimationSnapshotting.swift index a3231f0..a8ee1cc 100644 --- a/Sources/StagehandTesting/Core/AnimationSnapshotting.swift +++ b/Sources/StagehandTesting/Core/AnimationSnapshotting.swift @@ -14,7 +14,9 @@ // limitations under the License. // +import Foundation import MobileCoreServices +import UIKit @testable import Stagehand diff --git a/Sources/StagehandTesting/iOSSnapshotTestCase/FBSnapshotTestCase+Animation.swift b/Sources/StagehandTesting/iOSSnapshotTestCase/FBSnapshotTestCase+Animation.swift index d788440..88b55bd 100644 --- a/Sources/StagehandTesting/iOSSnapshotTestCase/FBSnapshotTestCase+Animation.swift +++ b/Sources/StagehandTesting/iOSSnapshotTestCase/FBSnapshotTestCase+Animation.swift @@ -14,7 +14,12 @@ // limitations under the License. // +#if BAZEL_PACKAGE +@testable import StagehandTestingCore +import iOSSnapshotTestCase +#else import FBSnapshotTestCase +#endif @testable import Stagehand diff --git a/Sources/StagehandTesting/iOSSnapshotTestCase/FBSnapshotTestCase+AnimationGIF.swift b/Sources/StagehandTesting/iOSSnapshotTestCase/FBSnapshotTestCase+AnimationGIF.swift index a944d15..021ab20 100644 --- a/Sources/StagehandTesting/iOSSnapshotTestCase/FBSnapshotTestCase+AnimationGIF.swift +++ b/Sources/StagehandTesting/iOSSnapshotTestCase/FBSnapshotTestCase+AnimationGIF.swift @@ -14,7 +14,13 @@ // limitations under the License. // +#if BAZEL_PACKAGE +@testable import StagehandTestingCore +import iOSSnapshotTestCase +#else import FBSnapshotTestCase +#endif + import ImageIO @testable import Stagehand diff --git a/WORKSPACE.bzlmod b/WORKSPACE.bzlmod new file mode 100644 index 0000000..1fa1775 --- /dev/null +++ b/WORKSPACE.bzlmod @@ -0,0 +1 @@ +"""Defines the WORKSPACE when using bzlmod, this should be empty. Use MODULE.bazel instead"""