diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml index 0130692..ddbe8cd 100644 --- a/.github/workflows/bazel.yml +++ b/.github/workflows/bazel.yml @@ -54,4 +54,8 @@ jobs: # Revert the code change cp main.swift.orig main.swift - + - name: Test rules_swift Helpers Example + shell: bash + run: | + cd examples/swift_rule_helpers + bazelisk test //... diff --git a/doc/BUILD.bazel b/doc/BUILD.bazel index 600c712..be28a28 100644 --- a/doc/BUILD.bazel +++ b/doc/BUILD.bazel @@ -21,8 +21,11 @@ _RULES_AND_MACROS_DOC_PROVIDER = doc_providers.create( name = "rules_and_macros_overview", stardoc_input = "//swiftformat:swiftformat.bzl", symbols = [ + "swiftformat_binary", "swiftformat_format", + "swiftformat_library", "swiftformat_pkg", + "swiftformat_test", "swiftformat_update", "swiftformat_update_all", ], diff --git a/doc/rules_and_macros_overview.md b/doc/rules_and_macros_overview.md index ab6318f..6ad26cb 100755 --- a/doc/rules_and_macros_overview.md +++ b/doc/rules_and_macros_overview.md @@ -6,8 +6,11 @@ copy Swift source files. On this page: + * [swiftformat_binary](#swiftformat_binary) * [swiftformat_format](#swiftformat_format) + * [swiftformat_library](#swiftformat_library) * [swiftformat_pkg](#swiftformat_pkg) + * [swiftformat_test](#swiftformat_test) * [swiftformat_update](#swiftformat_update) * [swiftformat_update_all](#swiftformat_update_all) @@ -53,6 +56,50 @@ Copies the formatted Swift sources to the workspace directory. | formats | The format build targets. | List of labels | optional | [] | + + +## swiftformat_binary + +
+swiftformat_binary(name, swiftformat_config, swiftformat_name, srcs, kwargs)
+
+ +Defines a `swift_binary` along with a `swiftformat_pkg`. + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| name | The name for the swift_binary as a string. | none | +| swiftformat_config | A label for the SwiftFormat config file. | None | +| swiftformat_name | Optional. The name for the swiftformat_pkg. | "swiftformat" | +| srcs | The Swift sources that should be used by the swift_binary and the swiftformat_pkg. | None | +| kwargs | The attributes for swift_binary. | none | + + + + +## swiftformat_library + +
+swiftformat_library(name, swiftformat_config, swiftformat_name, srcs, kwargs)
+
+ +Defines a `swift_library` along with a `swiftformat_pkg`. + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| name | The name for the swift_library as a string. | none | +| swiftformat_config | A label for the SwiftFormat config file. | None | +| swiftformat_name | Optional. The name for the swiftformat_pkg. | "swiftformat" | +| srcs | The Swift sources that should be used by the swift_library and the swiftformat_pkg. | None | +| kwargs | The attributes for swift_library. | none | + + ## swiftformat_pkg @@ -73,6 +120,28 @@ Defines targets that will format, test and update the specified Swift sources. | config | Optional. The swiftformat YAML configuration file. | None | + + +## swiftformat_test + +
+swiftformat_test(name, swiftformat_config, swiftformat_name, srcs, kwargs)
+
+ +Defines a `swift_test` along with a `swiftformat_pkg`. + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| name | The name for the swift_test as a string. | none | +| swiftformat_config | A label for the SwiftFormat config file. | None | +| swiftformat_name | Optional. The name for the swiftformat_pkg. | "swiftformat" | +| srcs | The Swift sources that should be used by the swift_test and the swiftformat_pkg. | None | +| kwargs | The attributes for swift_test. | none | + + ## swiftformat_update_all diff --git a/examples/swift_rule_helpers/.swiftformat b/examples/swift_rule_helpers/.swiftformat new file mode 100644 index 0000000..5e3e1e7 --- /dev/null +++ b/examples/swift_rule_helpers/.swiftformat @@ -0,0 +1,14 @@ +# For information on the rules, see +# https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md + +--swiftversion 5.4 + +--allman false +--indent 2 +--semicolons never +--stripunusedargs always +--maxwidth 100 +--wraparguments before-first +--wrapparameters before-first +--wrapcollections before-first + diff --git a/examples/swift_rule_helpers/BUILD.bazel b/examples/swift_rule_helpers/BUILD.bazel new file mode 100644 index 0000000..bd06b75 --- /dev/null +++ b/examples/swift_rule_helpers/BUILD.bazel @@ -0,0 +1,16 @@ +load( + "@cgrindel_rules_swiftformat//swiftformat:swiftformat.bzl", + "swiftformat_update_all", +) + +# MARK: - SwiftFormat Targets + +# We only need to export this file if there are any other packages that need to +# reference the config file. +exports_files([".swiftformat"]) + +# Defines a target that will copy all of the formatted Swift source files to +# the workspace directory. +swiftformat_update_all( + name = "update_all", +) diff --git a/examples/swift_rule_helpers/README.md b/examples/swift_rule_helpers/README.md new file mode 100644 index 0000000..5889ed9 --- /dev/null +++ b/examples/swift_rule_helpers/README.md @@ -0,0 +1,4 @@ +# Example Demonstrating `rules_swift` Convenience Macros + +This example demonstrates the use of `swiftformat_library`, `swiftformat_binary`, and +`swiftformat_test` to define `rules_swift` targets along with `rules_swiftformat` targets. diff --git a/examples/swift_rule_helpers/Sources/App/BUILD.bazel b/examples/swift_rule_helpers/Sources/App/BUILD.bazel new file mode 100644 index 0000000..824a78c --- /dev/null +++ b/examples/swift_rule_helpers/Sources/App/BUILD.bazel @@ -0,0 +1,15 @@ +load( + "@cgrindel_rules_swiftformat//swiftformat:swiftformat.bzl", + "swiftformat_binary", +) + +# Defines a swift_binary and swiftformat_pkg. +swiftformat_binary( + name = "simple", + srcs = ["main.swift"], + swiftformat_config = "//:.swiftformat", + visibility = ["//:__subpackages__"], + deps = [ + "//Sources/Foo", + ], +) diff --git a/examples/swift_rule_helpers/Sources/App/main.swift b/examples/swift_rule_helpers/Sources/App/main.swift new file mode 100644 index 0000000..b4c79ab --- /dev/null +++ b/examples/swift_rule_helpers/Sources/App/main.swift @@ -0,0 +1,9 @@ +import Foo + +var msg = Message() +msg.value = "Hello World!" + +Swift.print(msg.value) + +// Uncomment the next line to demonstrate formatting. +// let foo = 1 diff --git a/examples/swift_rule_helpers/Sources/Foo/BUILD.bazel b/examples/swift_rule_helpers/Sources/Foo/BUILD.bazel new file mode 100644 index 0000000..546a61e --- /dev/null +++ b/examples/swift_rule_helpers/Sources/Foo/BUILD.bazel @@ -0,0 +1,12 @@ +load( + "@cgrindel_rules_swiftformat//swiftformat:swiftformat.bzl", + "swiftformat_library", +) + +swiftformat_library( + name = "Foo", + srcs = glob(["*.swift"]), + module_name = "Foo", + swiftformat_config = "//:.swiftformat", + visibility = ["//:__subpackages__"], +) diff --git a/examples/swift_rule_helpers/Sources/Foo/Message.swift b/examples/swift_rule_helpers/Sources/Foo/Message.swift new file mode 100644 index 0000000..e8a634a --- /dev/null +++ b/examples/swift_rule_helpers/Sources/Foo/Message.swift @@ -0,0 +1,7 @@ +public struct Message { + public var value: String + + public init(value: String = "") { + self.value = value + } +} diff --git a/examples/swift_rule_helpers/Tests/AppTests/BUILD.bazel b/examples/swift_rule_helpers/Tests/AppTests/BUILD.bazel new file mode 100644 index 0000000..157ab34 --- /dev/null +++ b/examples/swift_rule_helpers/Tests/AppTests/BUILD.bazel @@ -0,0 +1,6 @@ +sh_test( + name = "simple_test", + srcs = ["simple_test.sh"], + data = ["//Sources/App:simple"], + deps = ["@bazel_tools//tools/bash/runfiles"], +) diff --git a/examples/swift_rule_helpers/Tests/AppTests/simple_test.sh b/examples/swift_rule_helpers/Tests/AppTests/simple_test.sh new file mode 100755 index 0000000..341cbbc --- /dev/null +++ b/examples/swift_rule_helpers/Tests/AppTests/simple_test.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +# --- begin runfiles.bash initialization v2 --- +# Copy-pasted from the Bazel Bash runfiles library v2. +set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash +source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ + source "$0.runfiles/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e +# --- end runfiles.bash initialization v2 --- + +err_msg() { + local msg="$1" + echo >&2 "${msg}" + exit 1 +} + +workspace="${TEST_WORKSPACE}" +binary="$(rlocation "${workspace}/Sources/App/simple")" + +expected="Hello World" +"${binary}" | grep "${expected}" || err_msg "Failed to find expected output. ${expected}" diff --git a/examples/swift_rule_helpers/Tests/FooTests/BUILD.bazel b/examples/swift_rule_helpers/Tests/FooTests/BUILD.bazel new file mode 100644 index 0000000..0f8d215 --- /dev/null +++ b/examples/swift_rule_helpers/Tests/FooTests/BUILD.bazel @@ -0,0 +1,11 @@ +load( + "@cgrindel_rules_swiftformat//swiftformat:swiftformat.bzl", + "swiftformat_test", +) + +swiftformat_test( + name = "FooTests", + deps = [ + "//Sources/Foo", + ], +) diff --git a/examples/swift_rule_helpers/Tests/FooTests/MessageTests.swift b/examples/swift_rule_helpers/Tests/FooTests/MessageTests.swift new file mode 100644 index 0000000..aea90cc --- /dev/null +++ b/examples/swift_rule_helpers/Tests/FooTests/MessageTests.swift @@ -0,0 +1,10 @@ +@testable import Foo +import XCTest + +class MessageTests: XCTestCase { + func test_init() throws { + let value = "hello" + let msg = Messsage(value = value) + XCTAssertEqual(msg.value, value) + } +} diff --git a/examples/swift_rule_helpers/WORKSPACE b/examples/swift_rule_helpers/WORKSPACE new file mode 100644 index 0000000..87faa21 --- /dev/null +++ b/examples/swift_rule_helpers/WORKSPACE @@ -0,0 +1,35 @@ +workspace(name = "swift_rule_helpers_example") + +local_repository( + name = "cgrindel_rules_swiftformat", + path = "../..", +) + +load("@cgrindel_rules_swiftformat//swiftformat:deps.bzl", "swiftformat_rules_dependencies") + +swiftformat_rules_dependencies() + +load( + "@cgrindel_rules_spm//spm:deps.bzl", + "spm_rules_dependencies", +) + +spm_rules_dependencies() + +load( + "@build_bazel_rules_swift//swift:repositories.bzl", + "swift_rules_dependencies", +) + +swift_rules_dependencies() + +load( + "@build_bazel_rules_swift//swift:extras.bzl", + "swift_rules_extra_dependencies", +) + +swift_rules_extra_dependencies() + +load("@cgrindel_rules_swiftformat//swiftformat:load_package.bzl", "swiftformat_load_package") + +swiftformat_load_package() diff --git a/swiftformat/BUILD.bazel b/swiftformat/BUILD.bazel index 10c808d..ab24d99 100644 --- a/swiftformat/BUILD.bazel +++ b/swiftformat/BUILD.bazel @@ -17,8 +17,11 @@ bzl_library( srcs = ["swiftformat.bzl"], deps = [ "//swiftformat/internal:providers", + "//swiftformat/internal:swiftformat_binary", "//swiftformat/internal:swiftformat_format", + "//swiftformat/internal:swiftformat_library", "//swiftformat/internal:swiftformat_pkg", + "//swiftformat/internal:swiftformat_test", "//swiftformat/internal:swiftformat_update", "//swiftformat/internal:swiftformat_update_all", ], diff --git a/swiftformat/internal/BUILD.bazel b/swiftformat/internal/BUILD.bazel index fc2eb51..76831c1 100644 --- a/swiftformat/internal/BUILD.bazel +++ b/swiftformat/internal/BUILD.bazel @@ -39,3 +39,30 @@ bzl_library( name = "swiftformat_update_all", srcs = ["swiftformat_update_all.bzl"], ) + +bzl_library( + name = "swiftformat_library", + srcs = ["swiftformat_library.bzl"], + deps = [ + ":swiftformat_pkg", + "@build_bazel_rules_swift//swift", + ], +) + +bzl_library( + name = "swiftformat_binary", + srcs = ["swiftformat_binary.bzl"], + deps = [ + ":swiftformat_pkg", + "@build_bazel_rules_swift//swift", + ], +) + +bzl_library( + name = "swiftformat_test", + srcs = ["swiftformat_test.bzl"], + deps = [ + ":swiftformat_pkg", + "@build_bazel_rules_swift//swift", + ], +) diff --git a/swiftformat/internal/swiftformat_binary.bzl b/swiftformat/internal/swiftformat_binary.bzl new file mode 100644 index 0000000..9a8b329 --- /dev/null +++ b/swiftformat/internal/swiftformat_binary.bzl @@ -0,0 +1,35 @@ +load("@build_bazel_rules_swift//swift:swift.bzl", "swift_binary") +load(":swiftformat_pkg.bzl", "swiftformat_pkg") + +def swiftformat_binary( + name, + swiftformat_config = None, + swiftformat_name = "swiftformat", + srcs = None, + **kwargs): + """Defines a `swift_binary` along with a `swiftformat_pkg`. + + Args: + name: The name for the swift_binary as a `string`. + swiftformat_config: A `label` for the SwiftFormat config file. + swiftformat_name: Optional. The name for the `swiftformat_pkg`. + srcs: The Swift sources that should be used by the `swift_binary` and the `swiftformat_pkg`. + **kwargs: The attributes for `swift_binary`. + + Returns: + None. + """ + + # Define the swift binary + swift_binary( + name = name, + srcs = srcs, + **kwargs + ) + + # Define the swiftformat stuff + swiftformat_pkg( + name = swiftformat_name, + srcs = srcs, + config = swiftformat_config, + ) diff --git a/swiftformat/internal/swiftformat_library.bzl b/swiftformat/internal/swiftformat_library.bzl new file mode 100644 index 0000000..597a276 --- /dev/null +++ b/swiftformat/internal/swiftformat_library.bzl @@ -0,0 +1,35 @@ +load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library") +load(":swiftformat_pkg.bzl", "swiftformat_pkg") + +def swiftformat_library( + name, + swiftformat_config = None, + swiftformat_name = "swiftformat", + srcs = None, + **kwargs): + """Defines a `swift_library` along with a `swiftformat_pkg`. + + Args: + name: The name for the swift_library as a `string`. + swiftformat_config: A `label` for the SwiftFormat config file. + swiftformat_name: Optional. The name for the `swiftformat_pkg`. + srcs: The Swift sources that should be used by the `swift_library` and the `swiftformat_pkg`. + **kwargs: The attributes for `swift_library`. + + Returns: + None. + """ + + # Define the swift library + swift_library( + name = name, + srcs = srcs, + **kwargs + ) + + # Define the swiftformat stuff + swiftformat_pkg( + name = swiftformat_name, + srcs = srcs, + config = swiftformat_config, + ) diff --git a/swiftformat/internal/swiftformat_test.bzl b/swiftformat/internal/swiftformat_test.bzl new file mode 100644 index 0000000..d8ba2b1 --- /dev/null +++ b/swiftformat/internal/swiftformat_test.bzl @@ -0,0 +1,35 @@ +load("@build_bazel_rules_swift//swift:swift.bzl", "swift_test") +load(":swiftformat_pkg.bzl", "swiftformat_pkg") + +def swiftformat_test( + name, + swiftformat_config = None, + swiftformat_name = "swiftformat", + srcs = None, + **kwargs): + """Defines a `swift_test` along with a `swiftformat_pkg`. + + Args: + name: The name for the swift_test as a `string`. + swiftformat_config: A `label` for the SwiftFormat config file. + swiftformat_name: Optional. The name for the `swiftformat_pkg`. + srcs: The Swift sources that should be used by the `swift_test` and the `swiftformat_pkg`. + **kwargs: The attributes for `swift_test`. + + Returns: + None. + """ + + # Define the swift binary + swift_test( + name = name, + srcs = srcs, + **kwargs + ) + + # Define the swiftformat stuff + swiftformat_pkg( + name = swiftformat_name, + srcs = srcs, + config = swiftformat_config, + ) diff --git a/swiftformat/swiftformat.bzl b/swiftformat/swiftformat.bzl index 6661b11..8d0f0ee 100644 --- a/swiftformat/swiftformat.bzl +++ b/swiftformat/swiftformat.bzl @@ -18,10 +18,25 @@ load( "//swiftformat/internal:providers.bzl", _SwiftFormatInfo = "SwiftFormatInfo", ) +load( + "//swiftformat/internal:swiftformat_library.bzl", + _swiftformat_library = "swiftformat_library", +) +load( + "//swiftformat/internal:swiftformat_binary.bzl", + _swiftformat_binary = "swiftformat_binary", +) +load( + "//swiftformat/internal:swiftformat_test.bzl", + _swiftformat_test = "swiftformat_test", +) # Macros swiftformat_pkg = _swiftformat_pkg swiftformat_update_all = _swiftformat_update_all +swiftformat_library = _swiftformat_library +swiftformat_binary = _swiftformat_binary +swiftformat_test = _swiftformat_test # Rules swiftformat_format = _swiftformat_format