Skip to content

Commit

Permalink
Added swiftformat_library, swiftformat_binary, and swiftformat_test m…
Browse files Browse the repository at this point in the history
…acros (#13)

Added swiftformat_library, swiftformat_binary, and swiftformat_test macros that define rules_swift and rules_swiftformat targets.
Added examples/swift_rule_helpers to demonstrate the use of these macros.
  • Loading branch information
cgrindel committed Oct 13, 2021
1 parent e680a76 commit 6529cc8
Show file tree
Hide file tree
Showing 21 changed files with 390 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .github/workflows/bazel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 //...
3 changes: 3 additions & 0 deletions doc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
69 changes: 69 additions & 0 deletions doc/rules_and_macros_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -53,6 +56,50 @@ Copies the formatted Swift sources to the workspace directory.
| <a id="swiftformat_update-formats"></a>formats | The format build targets. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |


<a id="#swiftformat_binary"></a>

## swiftformat_binary

<pre>
swiftformat_binary(<a href="#swiftformat_binary-name">name</a>, <a href="#swiftformat_binary-swiftformat_config">swiftformat_config</a>, <a href="#swiftformat_binary-swiftformat_name">swiftformat_name</a>, <a href="#swiftformat_binary-srcs">srcs</a>, <a href="#swiftformat_binary-kwargs">kwargs</a>)
</pre>

Defines a `swift_binary` along with a `swiftformat_pkg`.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="swiftformat_binary-name"></a>name | The name for the swift_binary as a <code>string</code>. | none |
| <a id="swiftformat_binary-swiftformat_config"></a>swiftformat_config | A <code>label</code> for the SwiftFormat config file. | <code>None</code> |
| <a id="swiftformat_binary-swiftformat_name"></a>swiftformat_name | Optional. The name for the <code>swiftformat_pkg</code>. | <code>"swiftformat"</code> |
| <a id="swiftformat_binary-srcs"></a>srcs | The Swift sources that should be used by the <code>swift_binary</code> and the <code>swiftformat_pkg</code>. | <code>None</code> |
| <a id="swiftformat_binary-kwargs"></a>kwargs | The attributes for <code>swift_binary</code>. | none |


<a id="#swiftformat_library"></a>

## swiftformat_library

<pre>
swiftformat_library(<a href="#swiftformat_library-name">name</a>, <a href="#swiftformat_library-swiftformat_config">swiftformat_config</a>, <a href="#swiftformat_library-swiftformat_name">swiftformat_name</a>, <a href="#swiftformat_library-srcs">srcs</a>, <a href="#swiftformat_library-kwargs">kwargs</a>)
</pre>

Defines a `swift_library` along with a `swiftformat_pkg`.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="swiftformat_library-name"></a>name | The name for the swift_library as a <code>string</code>. | none |
| <a id="swiftformat_library-swiftformat_config"></a>swiftformat_config | A <code>label</code> for the SwiftFormat config file. | <code>None</code> |
| <a id="swiftformat_library-swiftformat_name"></a>swiftformat_name | Optional. The name for the <code>swiftformat_pkg</code>. | <code>"swiftformat"</code> |
| <a id="swiftformat_library-srcs"></a>srcs | The Swift sources that should be used by the <code>swift_library</code> and the <code>swiftformat_pkg</code>. | <code>None</code> |
| <a id="swiftformat_library-kwargs"></a>kwargs | The attributes for <code>swift_library</code>. | none |


<a id="#swiftformat_pkg"></a>

## swiftformat_pkg
Expand All @@ -73,6 +120,28 @@ Defines targets that will format, test and update the specified Swift sources.
| <a id="swiftformat_pkg-config"></a>config | Optional. The swiftformat YAML configuration file. | <code>None</code> |


<a id="#swiftformat_test"></a>

## swiftformat_test

<pre>
swiftformat_test(<a href="#swiftformat_test-name">name</a>, <a href="#swiftformat_test-swiftformat_config">swiftformat_config</a>, <a href="#swiftformat_test-swiftformat_name">swiftformat_name</a>, <a href="#swiftformat_test-srcs">srcs</a>, <a href="#swiftformat_test-kwargs">kwargs</a>)
</pre>

Defines a `swift_test` along with a `swiftformat_pkg`.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="swiftformat_test-name"></a>name | The name for the swift_test as a <code>string</code>. | none |
| <a id="swiftformat_test-swiftformat_config"></a>swiftformat_config | A <code>label</code> for the SwiftFormat config file. | <code>None</code> |
| <a id="swiftformat_test-swiftformat_name"></a>swiftformat_name | Optional. The name for the <code>swiftformat_pkg</code>. | <code>"swiftformat"</code> |
| <a id="swiftformat_test-srcs"></a>srcs | The Swift sources that should be used by the <code>swift_test</code> and the <code>swiftformat_pkg</code>. | <code>None</code> |
| <a id="swiftformat_test-kwargs"></a>kwargs | The attributes for <code>swift_test</code>. | none |


<a id="#swiftformat_update_all"></a>

## swiftformat_update_all
Expand Down
14 changes: 14 additions & 0 deletions examples/swift_rule_helpers/.swiftformat
Original file line number Diff line number Diff line change
@@ -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

16 changes: 16 additions & 0 deletions examples/swift_rule_helpers/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
)
4 changes: 4 additions & 0 deletions examples/swift_rule_helpers/README.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions examples/swift_rule_helpers/Sources/App/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
],
)
9 changes: 9 additions & 0 deletions examples/swift_rule_helpers/Sources/App/main.swift
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions examples/swift_rule_helpers/Sources/Foo/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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__"],
)
7 changes: 7 additions & 0 deletions examples/swift_rule_helpers/Sources/Foo/Message.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public struct Message {
public var value: String

public init(value: String = "") {
self.value = value
}
}
6 changes: 6 additions & 0 deletions examples/swift_rule_helpers/Tests/AppTests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sh_test(
name = "simple_test",
srcs = ["simple_test.sh"],
data = ["//Sources/App:simple"],
deps = ["@bazel_tools//tools/bash/runfiles"],
)
24 changes: 24 additions & 0 deletions examples/swift_rule_helpers/Tests/AppTests/simple_test.sh
Original file line number Diff line number Diff line change
@@ -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}"
11 changes: 11 additions & 0 deletions examples/swift_rule_helpers/Tests/FooTests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load(
"@cgrindel_rules_swiftformat//swiftformat:swiftformat.bzl",
"swiftformat_test",
)

swiftformat_test(
name = "FooTests",
deps = [
"//Sources/Foo",
],
)
10 changes: 10 additions & 0 deletions examples/swift_rule_helpers/Tests/FooTests/MessageTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
35 changes: 35 additions & 0 deletions examples/swift_rule_helpers/WORKSPACE
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 3 additions & 0 deletions swiftformat/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
27 changes: 27 additions & 0 deletions swiftformat/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
35 changes: 35 additions & 0 deletions swiftformat/internal/swiftformat_binary.bzl
Original file line number Diff line number Diff line change
@@ -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,
)
Loading

0 comments on commit 6529cc8

Please sign in to comment.