Skip to content

Commit

Permalink
Updated to use rules_updatesrc for file updates (#23)
Browse files Browse the repository at this point in the history
Replaced swiftformat_update and swiftformat_update_all with updatesrc_update and updatesrc_update_all from rules_updatesrc.
  • Loading branch information
cgrindel authored Oct 26, 2021
1 parent 4467d0a commit 3f79347
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 169 deletions.
11 changes: 11 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load(
"@cgrindel_rules_updatesrc//updatesrc:updatesrc.bzl",
"updatesrc_update_all",
)

updatesrc_update_all(
name = "update_all",
targets_to_run = [
"//doc:update",
],
)
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ swiftformat_rules_dependencies()

# Configure the dependencies for rules_swiftformat

load(
"@cgrindel_rules_updatesrc//updatesrc:deps.bzl",
"updatesrc_rules_dependencies",
)

updatesrc_rules_dependencies()

load(
"@cgrindel_rules_spm//spm:deps.bzl",
"spm_rules_dependencies",
Expand Down Expand Up @@ -69,15 +76,15 @@ following:

```python
load(
"@cgrindel_rules_swiftformat//swiftformat:swiftformat.bzl",
"swiftformat_update_all",
"@cgrindel_rules_updatesrc//updatesrc:updatesrc.bzl",
"updatesrc_update_all",
)

# We export this file to make it available to other Bazel packages in the workspace.
exports_files([".swiftformat"])

# Define a runnable target to copy all of the formatted files to the workspace directory.
swiftformat_update_all(
updatesrc_update_all(
name = "update_all",
)
```
Expand Down
7 changes: 7 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ load("//swiftformat:deps.bzl", "swiftformat_rules_dependencies")

swiftformat_rules_dependencies()

load(
"@cgrindel_rules_updatesrc//updatesrc:deps.bzl",
"updatesrc_rules_dependencies",
)

updatesrc_rules_dependencies()

load(
"@cgrindel_rules_spm//spm:deps.bzl",
"spm_rules_dependencies",
Expand Down
2 changes: 0 additions & 2 deletions doc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ _RULES_AND_MACROS_DOC_PROVIDER = doc_providers.create(
"swiftformat_library",
"swiftformat_pkg",
"swiftformat_test",
"swiftformat_update",
"swiftformat_update_all",
],
deps = ["//swiftformat"],
)
Expand Down
45 changes: 0 additions & 45 deletions doc/rules_and_macros_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ On this page:
* [swiftformat_library](#swiftformat_library)
* [swiftformat_pkg](#swiftformat_pkg)
* [swiftformat_test](#swiftformat_test)
* [swiftformat_update](#swiftformat_update)
* [swiftformat_update_all](#swiftformat_update_all)


<a id="#swiftformat_format"></a>
Expand All @@ -37,25 +35,6 @@ Formats the Swift source files using `nicklockwood/SwiftFormat`.
| <a id="swiftformat_format-swift_version"></a>swift_version | The Swift version to be used by <code>swiftformat</code>. You probably want to add this to your config file instead of adding it here. | String | optional | "" |


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

## swiftformat_update

<pre>
swiftformat_update(<a href="#swiftformat_update-name">name</a>, <a href="#swiftformat_update-formats">formats</a>)
</pre>

Copies the formatted Swift sources to the workspace directory.

**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="swiftformat_update-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
| <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
Expand Down Expand Up @@ -145,27 +124,3 @@ Defines a `swift_test` along with a `swiftformat_pkg`.
| <a id="swiftformat_test-kwargs"></a>kwargs | The attributes for <code>swift_test</code>. | none |


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

## swiftformat_update_all

<pre>
swiftformat_update_all(<a href="#swiftformat_update_all-name">name</a>)
</pre>

Defines a runnable target that will copy the formatted Swift files to the source tree.

The utility queries for all of the swiftformat_update rules in the
workspace and executes each one. Hence, only Swift source files that are
referenced by a swiftformat_update will be copied to the workspace
directory.


**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="swiftformat_update_all-name"></a>name | The name of the target. | none |


6 changes: 3 additions & 3 deletions examples/exclude_files/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load(
"@cgrindel_rules_swiftformat//swiftformat:swiftformat.bzl",
"swiftformat_update_all",
"@cgrindel_rules_updatesrc//updatesrc:updatesrc.bzl",
"updatesrc_update_all",
)

# MARK: - SwiftFormat Targets
Expand All @@ -11,6 +11,6 @@ exports_files([".swiftformat"])

# Defines a target that will copy all of the formatted Swift source files to
# the workspace directory.
swiftformat_update_all(
updatesrc_update_all(
name = "update_all",
)
7 changes: 7 additions & 0 deletions examples/exclude_files/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ load("@cgrindel_rules_swiftformat//swiftformat:deps.bzl", "swiftformat_rules_dep

swiftformat_rules_dependencies()

load(
"@cgrindel_rules_updatesrc//updatesrc:deps.bzl",
"updatesrc_rules_dependencies",
)

updatesrc_rules_dependencies()

load(
"@cgrindel_rules_spm//spm:deps.bzl",
"spm_rules_dependencies",
Expand Down
6 changes: 3 additions & 3 deletions examples/rules_swift_helpers/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load(
"@cgrindel_rules_swiftformat//swiftformat:swiftformat.bzl",
"swiftformat_update_all",
"@cgrindel_rules_updatesrc//updatesrc:updatesrc.bzl",
"updatesrc_update_all",
)

# MARK: - SwiftFormat Targets
Expand All @@ -11,6 +11,6 @@ exports_files([".swiftformat"])

# Defines a target that will copy all of the formatted Swift source files to
# the workspace directory.
swiftformat_update_all(
updatesrc_update_all(
name = "update_all",
)
7 changes: 7 additions & 0 deletions examples/rules_swift_helpers/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ load("@cgrindel_rules_swiftformat//swiftformat:deps.bzl", "swiftformat_rules_dep

swiftformat_rules_dependencies()

load(
"@cgrindel_rules_updatesrc//updatesrc:deps.bzl",
"updatesrc_rules_dependencies",
)

updatesrc_rules_dependencies()

load(
"@cgrindel_rules_spm//spm:deps.bzl",
"spm_rules_dependencies",
Expand Down
7 changes: 5 additions & 2 deletions examples/simple/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ load("@build_bazel_rules_swift//swift:swift.bzl", "swift_binary")
load(
"@cgrindel_rules_swiftformat//swiftformat:swiftformat.bzl",
"swiftformat_pkg",
"swiftformat_update_all",
)
load(
"@cgrindel_rules_updatesrc//updatesrc:updatesrc.bzl",
"updatesrc_update_all",
)

# MARK: - Swift Targets
Expand Down Expand Up @@ -31,7 +34,7 @@ exports_files([".swiftformat"])

# Defines a target that will copy all of the formatted Swift source files to
# the workspace directory.
swiftformat_update_all(
updatesrc_update_all(
name = "update_all",
)

Expand Down
7 changes: 7 additions & 0 deletions examples/simple/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ load("@cgrindel_rules_swiftformat//swiftformat:deps.bzl", "swiftformat_rules_dep

swiftformat_rules_dependencies()

load(
"@cgrindel_rules_updatesrc//updatesrc:deps.bzl",
"updatesrc_rules_dependencies",
)

updatesrc_rules_dependencies()

load(
"@cgrindel_rules_spm//spm:deps.bzl",
"spm_rules_dependencies",
Expand Down
2 changes: 0 additions & 2 deletions swiftformat/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@ bzl_library(
"//swiftformat/internal:swiftformat_library",
"//swiftformat/internal:swiftformat_pkg",
"//swiftformat/internal:swiftformat_test",
"//swiftformat/internal:swiftformat_update",
"//swiftformat/internal:swiftformat_update_all",
],
)
8 changes: 8 additions & 0 deletions swiftformat/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def swiftformat_rules_dependencies():
],
)

maybe(
http_archive,
name = "cgrindel_rules_updatesrc",
sha256 = "18eb6620ac4684c2bc722b8fe447dfaba76f73d73e2dfcaf837f542379ed9bc3",
strip_prefix = "rules_updatesrc-0.1.0",
urls = ["https://github.com/cgrindel/rules_updatesrc/archive/v0.1.0.tar.gz"],
)

maybe(
http_archive,
name = "cgrindel_bazel_doc",
Expand Down
17 changes: 2 additions & 15 deletions swiftformat/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bzl_library(
deps = [
":providers",
"@bazel_skylib//lib:paths",
"@cgrindel_rules_updatesrc//updatesrc",
],
)

Expand All @@ -27,25 +28,11 @@ bzl_library(
deps = [
":src_utils",
":swiftformat_format",
":swiftformat_update",
"@bazel_skylib//rules:diff_test",
"@cgrindel_rules_updatesrc//updatesrc",
],
)

bzl_library(
name = "swiftformat_update",
srcs = ["swiftformat_update.bzl"],
deps = [
":providers",
"@bazel_skylib//lib:dicts",
],
)

bzl_library(
name = "swiftformat_update_all",
srcs = ["swiftformat_update_all.bzl"],
)

bzl_library(
name = "swiftformat_library",
srcs = ["swiftformat_library.bzl"],
Expand Down
16 changes: 9 additions & 7 deletions swiftformat/internal/swiftformat_format.bzl
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
load("@bazel_skylib//lib:paths.bzl", "paths")
load(":providers.bzl", "SwiftFormatInfo")
load(
"@cgrindel_rules_updatesrc//updatesrc:updatesrc.bzl",
"UpdateSrcsInfo",
"update_srcs",
)

"""A build rule that formats Swift source files.
"""

def _swiftformat_format_impl(ctx):
outputs = []
format_map = {}
updsrcs = []
for src in ctx.files.srcs:
out = ctx.actions.declare_file(src.basename + ctx.attr.output_suffix)
outputs.append(out)
format_map[src] = out
updsrcs.append(update_srcs.create(src = src, out = out))
inputs = [src]

args = ctx.actions.args()
Expand Down Expand Up @@ -39,8 +41,8 @@ def _swiftformat_format_impl(ctx):
)

return [
DefaultInfo(files = depset(outputs)),
SwiftFormatInfo(format_map = format_map),
DefaultInfo(files = depset([updsrc.out for updsrc in updsrcs])),
UpdateSrcsInfo(update_srcs = depset(updsrcs)),
]

swiftformat_format = rule(
Expand Down
6 changes: 3 additions & 3 deletions swiftformat/internal/swiftformat_pkg.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load(":src_utils.bzl", "src_utils")
load(":swiftformat_format.bzl", "swiftformat_format")
load(":swiftformat_update.bzl", "swiftformat_update")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@cgrindel_rules_updatesrc//updatesrc:updatesrc.bzl", "updatesrc_update")

"""A macro which defines targets that format Swift source files, test that
they are formatted and copies them to the workspace directory.
Expand Down Expand Up @@ -43,7 +43,7 @@ def swiftformat_pkg(name, srcs = None, config = None):
file2 = ":" + format_name,
)

swiftformat_update(
updatesrc_update(
name = name + "_update",
formats = format_names,
deps = format_names,
)
50 changes: 0 additions & 50 deletions swiftformat/internal/swiftformat_update.bzl

This file was deleted.

Loading

0 comments on commit 3f79347

Please sign in to comment.