-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added swiftformat_library, swiftformat_binary, and swiftformat_test m…
…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
Showing
21 changed files
with
390 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
examples/swift_rule_helpers/Tests/FooTests/MessageTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
Oops, something went wrong.