diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml index cac67fe..82bd271 100644 --- a/.github/workflows/bazel.yml +++ b/.github/workflows/bazel.yml @@ -59,3 +59,9 @@ jobs: run: | cd examples/rules_swift_helpers bazelisk test //... + + - name: Test File Exclusion Example + shell: bash + run: | + cd examples/exclude_files + bazelisk test //... diff --git a/doc/rules_and_macros_overview.md b/doc/rules_and_macros_overview.md index aa8930d..1293684 100755 --- a/doc/rules_and_macros_overview.md +++ b/doc/rules_and_macros_overview.md @@ -61,7 +61,7 @@ Copies the formatted Swift sources to the workspace directory. ## swiftformat_binary
-swiftformat_binary(name, swiftformat_config, swiftformat_name, srcs, kwargs) +swiftformat_binary(name, swiftformat_config, swiftformat_exclude, srcs, kwargs)Defines a `swift_binary` along with a `swiftformat_pkg`. @@ -73,7 +73,7 @@ Defines a `swift_binary` along with a `swiftformat_pkg`. | :------------- | :------------- | :------------- | | 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"
|
+| swiftformat_exclude | A list
of files or glob patterns that should be ignored for formatting. | []
|
| srcs | The Swift sources that should be used by the swift_binary
and the swiftformat_pkg
. | None
|
| kwargs | The attributes for swift_binary
. | none |
@@ -83,7 +83,7 @@ Defines a `swift_binary` along with a `swiftformat_pkg`.
## swiftformat_library
-swiftformat_library(name, swiftformat_config, swiftformat_name, srcs, kwargs) +swiftformat_library(name, swiftformat_config, swiftformat_exclude, srcs, kwargs)Defines a `swift_library` along with a `swiftformat_pkg`. @@ -95,7 +95,7 @@ Defines a `swift_library` along with a `swiftformat_pkg`. | :------------- | :------------- | :------------- | | 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"
|
+| swiftformat_exclude | A list
of files or glob patterns that should be ignored for formatting. | []
|
| srcs | The Swift sources that should be used by the swift_library
and the swiftformat_pkg
. | None
|
| kwargs | The attributes for swift_library
. | none |
@@ -125,7 +125,7 @@ Defines targets that will format, test and update the specified Swift sources.
## swiftformat_test
-swiftformat_test(name, swiftformat_config, swiftformat_name, srcs, kwargs) +swiftformat_test(name, swiftformat_config, swiftformat_exclude, srcs, kwargs)Defines a `swift_test` along with a `swiftformat_pkg`. @@ -137,7 +137,7 @@ Defines a `swift_test` along with a `swiftformat_pkg`. | :------------- | :------------- | :------------- | | 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"
|
+| swiftformat_exclude | A list
of files or glob patterns that should be ignored for formatting. | []
|
| srcs | The Swift sources that should be used by the swift_test
and the swiftformat_pkg
. | None
|
| kwargs | The attributes for swift_test
. | none |
diff --git a/examples/README.md b/examples/README.md
index 96d6fb8..fcac212 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -5,3 +5,5 @@
[`swiftformat_library`](/doc/rules_and_macros_overview.md#swiftformat_library),
[`swiftformat_binary`](/doc/rules_and_macros_overview.md#swiftformat_binary), and
[`swiftformat_test`](/doc/rules_and_macros_overview.md#swiftformat_test).
+- [Exclude Files](exclude_files/) - Demonstrates the use of of the `rules_swift` helpers using
+ `swiftformat_exclude` attribute.
diff --git a/examples/exclude_files/.swiftformat b/examples/exclude_files/.swiftformat
new file mode 100644
index 0000000..5e3e1e7
--- /dev/null
+++ b/examples/exclude_files/.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/exclude_files/BUILD.bazel b/examples/exclude_files/BUILD.bazel
new file mode 100644
index 0000000..bd06b75
--- /dev/null
+++ b/examples/exclude_files/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/exclude_files/README.md b/examples/exclude_files/README.md
new file mode 100644
index 0000000..1bf631e
--- /dev/null
+++ b/examples/exclude_files/README.md
@@ -0,0 +1,7 @@
+# Example Demonstrating `rules_swift` Convenience Macros
+
+This example demonstrates the use of
+[`swiftformat_library`](/doc/rules_and_macros_overview.md#swiftformat_library),
+[`swiftformat_binary`](/doc/rules_and_macros_overview.md#swiftformat_binary), and
+[`swiftformat_test`](/doc/rules_and_macros_overview.md#swiftformat_test)
+to define `rules_swift` targets along with `rules_swiftformat` targets.
diff --git a/examples/exclude_files/Sources/App/BUILD.bazel b/examples/exclude_files/Sources/App/BUILD.bazel
new file mode 100644
index 0000000..080d9f4
--- /dev/null
+++ b/examples/exclude_files/Sources/App/BUILD.bazel
@@ -0,0 +1,14 @@
+load(
+ "@cgrindel_rules_swiftformat//swiftformat:swiftformat.bzl",
+ "swiftformat_binary",
+)
+
+# Defines a swift_binary and swiftformat_pkg.
+swiftformat_binary(
+ name = "simple",
+ swiftformat_exclude = ["PoorlyFormatted.swift"],
+ visibility = ["//:__subpackages__"],
+ deps = [
+ "//Sources/Foo",
+ ],
+)
diff --git a/examples/exclude_files/Sources/App/PoorlyFormatted.swift b/examples/exclude_files/Sources/App/PoorlyFormatted.swift
new file mode 100644
index 0000000..0b817f9
--- /dev/null
+++ b/examples/exclude_files/Sources/App/PoorlyFormatted.swift
@@ -0,0 +1,5 @@
+ // This file is purposely poorly formatted. It is excluded from formatting in
+ // the BUILD.bazel file.
+ struct PoorlyFormatted {
+ var name = ""
+ }
diff --git a/examples/exclude_files/Sources/App/main.swift b/examples/exclude_files/Sources/App/main.swift
new file mode 100644
index 0000000..23fbe23
--- /dev/null
+++ b/examples/exclude_files/Sources/App/main.swift
@@ -0,0 +1,9 @@
+import Foo
+
+var msg = Message()
+msg.value = "Hello World!"
+
+var pfmt = PoorlyFormatted()
+pfmt.name = "Larry"
+
+Swift.print(msg.value)
diff --git a/examples/exclude_files/Sources/Foo/BUILD.bazel b/examples/exclude_files/Sources/Foo/BUILD.bazel
new file mode 100644
index 0000000..ff5bb27
--- /dev/null
+++ b/examples/exclude_files/Sources/Foo/BUILD.bazel
@@ -0,0 +1,11 @@
+load(
+ "@cgrindel_rules_swiftformat//swiftformat:swiftformat.bzl",
+ "swiftformat_library",
+)
+
+swiftformat_library(
+ name = "Foo",
+ module_name = "Foo",
+ swiftformat_exclude = ["PoorlyFormatted.swift"],
+ visibility = ["//:__subpackages__"],
+)
diff --git a/examples/exclude_files/Sources/Foo/Message.swift b/examples/exclude_files/Sources/Foo/Message.swift
new file mode 100644
index 0000000..74142a6
--- /dev/null
+++ b/examples/exclude_files/Sources/Foo/Message.swift
@@ -0,0 +1,10 @@
+public struct Message {
+ public var value: String
+ var pfmt: PoorlyFormatted
+
+ public init(value: String = "") {
+ self.value = value
+ pfmt = PoorlyFormatted()
+ pfmt.name = value
+ }
+}
diff --git a/examples/exclude_files/Sources/Foo/PoorlyFormatted.swift b/examples/exclude_files/Sources/Foo/PoorlyFormatted.swift
new file mode 100644
index 0000000..0b817f9
--- /dev/null
+++ b/examples/exclude_files/Sources/Foo/PoorlyFormatted.swift
@@ -0,0 +1,5 @@
+ // This file is purposely poorly formatted. It is excluded from formatting in
+ // the BUILD.bazel file.
+ struct PoorlyFormatted {
+ var name = ""
+ }
diff --git a/examples/exclude_files/Tests/AppTests/BUILD.bazel b/examples/exclude_files/Tests/AppTests/BUILD.bazel
new file mode 100644
index 0000000..157ab34
--- /dev/null
+++ b/examples/exclude_files/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/exclude_files/Tests/AppTests/simple_test.sh b/examples/exclude_files/Tests/AppTests/simple_test.sh
new file mode 100755
index 0000000..341cbbc
--- /dev/null
+++ b/examples/exclude_files/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/exclude_files/Tests/FooTests/BUILD.bazel b/examples/exclude_files/Tests/FooTests/BUILD.bazel
new file mode 100644
index 0000000..4ce9164
--- /dev/null
+++ b/examples/exclude_files/Tests/FooTests/BUILD.bazel
@@ -0,0 +1,12 @@
+load(
+ "@cgrindel_rules_swiftformat//swiftformat:swiftformat.bzl",
+ "swiftformat_test",
+)
+
+swiftformat_test(
+ name = "FooTests",
+ swiftformat_exclude = ["PoorlyFormattedTests.swift"],
+ deps = [
+ "//Sources/Foo",
+ ],
+)
diff --git a/examples/exclude_files/Tests/FooTests/MessageTests.swift b/examples/exclude_files/Tests/FooTests/MessageTests.swift
new file mode 100644
index 0000000..7102e76
--- /dev/null
+++ b/examples/exclude_files/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 = Message(value: value)
+ XCTAssertEqual(msg.value, value)
+ }
+}
diff --git a/examples/exclude_files/Tests/FooTests/PoorlyFormattedTests.swift b/examples/exclude_files/Tests/FooTests/PoorlyFormattedTests.swift
new file mode 100644
index 0000000..9c9582d
--- /dev/null
+++ b/examples/exclude_files/Tests/FooTests/PoorlyFormattedTests.swift
@@ -0,0 +1,12 @@
+@testable import Foo
+import XCTest
+
+ // Purposely poorly formatted.
+ class PoorlyFormattedTests: XCTestCase {
+ func test_init() throws {
+ let value = "hello"
+ var pfmt = PoorlyFormatted()
+ pfmt.name = value
+ XCTAssertEqual(pfmt.name, value)
+ }
+ }
diff --git a/examples/exclude_files/WORKSPACE b/examples/exclude_files/WORKSPACE
new file mode 100644
index 0000000..5b37ab4
--- /dev/null
+++ b/examples/exclude_files/WORKSPACE
@@ -0,0 +1,35 @@
+workspace(name = "exclude_files_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/examples/rules_swift_helpers/README.md b/examples/rules_swift_helpers/README.md
index 1bf631e..fc6e8fd 100644
--- a/examples/rules_swift_helpers/README.md
+++ b/examples/rules_swift_helpers/README.md
@@ -1,7 +1,7 @@
-# Example Demonstrating `rules_swift` Convenience Macros
+# Example Demonstrating Exclusion of Select Files from Formatting
This example demonstrates the use of
[`swiftformat_library`](/doc/rules_and_macros_overview.md#swiftformat_library),
[`swiftformat_binary`](/doc/rules_and_macros_overview.md#swiftformat_binary), and
[`swiftformat_test`](/doc/rules_and_macros_overview.md#swiftformat_test)
-to define `rules_swift` targets along with `rules_swiftformat` targets.
+using `swiftformat_exclude` to skip the formatting of select files.
diff --git a/examples/rules_swift_helpers/Tests/FooTests/MessageTests.swift b/examples/rules_swift_helpers/Tests/FooTests/MessageTests.swift
index 38597e4..7102e76 100644
--- a/examples/rules_swift_helpers/Tests/FooTests/MessageTests.swift
+++ b/examples/rules_swift_helpers/Tests/FooTests/MessageTests.swift
@@ -4,7 +4,7 @@ import XCTest
class MessageTests: XCTestCase {
func test_init() throws {
let value = "hello"
- let msg = Messsage(value = value)
+ let msg = Message(value: value)
XCTAssertEqual(msg.value, value)
}
}
diff --git a/swiftformat/internal/swiftformat_binary.bzl b/swiftformat/internal/swiftformat_binary.bzl
index 9a8b329..0dc6382 100644
--- a/swiftformat/internal/swiftformat_binary.bzl
+++ b/swiftformat/internal/swiftformat_binary.bzl
@@ -4,7 +4,7 @@ load(":swiftformat_pkg.bzl", "swiftformat_pkg")
def swiftformat_binary(
name,
swiftformat_config = None,
- swiftformat_name = "swiftformat",
+ swiftformat_exclude = [],
srcs = None,
**kwargs):
"""Defines a `swift_binary` along with a `swiftformat_pkg`.
@@ -12,13 +12,16 @@ def swiftformat_binary(
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`.
+ swiftformat_exclude: A `list` of files or glob patterns that should
+ be ignored for formatting.
srcs: The Swift sources that should be used by the `swift_binary` and the `swiftformat_pkg`.
**kwargs: The attributes for `swift_binary`.
Returns:
None.
"""
+ if srcs == None:
+ srcs = native.glob(["*.swift"])
# Define the swift binary
swift_binary(
@@ -28,8 +31,9 @@ def swiftformat_binary(
)
# Define the swiftformat stuff
+ swiftformat_name = name + "_swiftformat"
swiftformat_pkg(
name = swiftformat_name,
- srcs = srcs,
+ srcs = native.glob(srcs, exclude = swiftformat_exclude),
config = swiftformat_config,
)
diff --git a/swiftformat/internal/swiftformat_library.bzl b/swiftformat/internal/swiftformat_library.bzl
index 597a276..c48f7c1 100644
--- a/swiftformat/internal/swiftformat_library.bzl
+++ b/swiftformat/internal/swiftformat_library.bzl
@@ -4,7 +4,7 @@ load(":swiftformat_pkg.bzl", "swiftformat_pkg")
def swiftformat_library(
name,
swiftformat_config = None,
- swiftformat_name = "swiftformat",
+ swiftformat_exclude = [],
srcs = None,
**kwargs):
"""Defines a `swift_library` along with a `swiftformat_pkg`.
@@ -12,13 +12,16 @@ def swiftformat_library(
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`.
+ swiftformat_exclude: A `list` of files or glob patterns that should
+ be ignored for formatting.
srcs: The Swift sources that should be used by the `swift_library` and the `swiftformat_pkg`.
**kwargs: The attributes for `swift_library`.
Returns:
None.
"""
+ if srcs == None:
+ srcs = native.glob(["*.swift"])
# Define the swift library
swift_library(
@@ -28,8 +31,9 @@ def swiftformat_library(
)
# Define the swiftformat stuff
+ swiftformat_name = name + "_swiftformat"
swiftformat_pkg(
name = swiftformat_name,
- srcs = srcs,
+ srcs = native.glob(srcs, exclude = swiftformat_exclude),
config = swiftformat_config,
)
diff --git a/swiftformat/internal/swiftformat_pkg.bzl b/swiftformat/internal/swiftformat_pkg.bzl
index a3a2b38..01c517a 100644
--- a/swiftformat/internal/swiftformat_pkg.bzl
+++ b/swiftformat/internal/swiftformat_pkg.bzl
@@ -6,6 +6,12 @@ load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
they are formatted and copies them to the workspace directory.
"""
+def _is_label(src):
+ return src.find(":") > -1
+
+def _is_path(src):
+ return not _is_label(src)
+
def swiftformat_pkg(name, srcs = None, config = None):
"""Defines targets that will format, test and update the specified Swift sources.
@@ -20,9 +26,12 @@ def swiftformat_pkg(name, srcs = None, config = None):
if srcs == None:
srcs = native.glob(["*.swift"])
+ # Only process paths; ignore labels
+ src_paths = [src for src in srcs if _is_path(src)]
+
format_names = []
- for src in srcs:
- src_name = src.replace("/", "_")
+ for src in src_paths:
+ src_name = src.replace("/", "_").replace(":", "")
format_name = name + "_fmt_" + src_name
format_names.append(":" + format_name)
diff --git a/swiftformat/internal/swiftformat_test.bzl b/swiftformat/internal/swiftformat_test.bzl
index d8ba2b1..aa35a67 100644
--- a/swiftformat/internal/swiftformat_test.bzl
+++ b/swiftformat/internal/swiftformat_test.bzl
@@ -4,7 +4,7 @@ load(":swiftformat_pkg.bzl", "swiftformat_pkg")
def swiftformat_test(
name,
swiftformat_config = None,
- swiftformat_name = "swiftformat",
+ swiftformat_exclude = [],
srcs = None,
**kwargs):
"""Defines a `swift_test` along with a `swiftformat_pkg`.
@@ -12,13 +12,16 @@ def swiftformat_test(
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`.
+ swiftformat_exclude: A `list` of files or glob patterns that should
+ be ignored for formatting.
srcs: The Swift sources that should be used by the `swift_test` and the `swiftformat_pkg`.
**kwargs: The attributes for `swift_test`.
Returns:
None.
"""
+ if srcs == None:
+ srcs = native.glob(["*.swift"])
# Define the swift binary
swift_test(
@@ -28,8 +31,9 @@ def swiftformat_test(
)
# Define the swiftformat stuff
+ swiftformat_name = name + "_swiftformat"
swiftformat_pkg(
name = swiftformat_name,
- srcs = srcs,
+ srcs = native.glob(srcs, exclude = swiftformat_exclude),
config = swiftformat_config,
)
diff --git a/swiftformat/internal/swiftformat_update.bzl b/swiftformat/internal/swiftformat_update.bzl
index 5aa2199..fa2f0f0 100644
--- a/swiftformat/internal/swiftformat_update.bzl
+++ b/swiftformat/internal/swiftformat_update.bzl
@@ -16,7 +16,9 @@ def _swiftformat_update_impl(ctx):
files = [format_map[src] for src in format_map],
)
- update = ctx.actions.declare_file("update_with_formatted.sh")
+ update = ctx.actions.declare_file(
+ ctx.label.name + "_update_with_formatted.sh",
+ )
ctx.actions.write(
output = update,
content = """