Skip to content

Commit

Permalink
Cleaned up the code that detects labels in swiftformat_pkg (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrindel committed Oct 21, 2021
1 parent fc7b729 commit d5254f8
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 9 deletions.
4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ swift_rules_extra_dependencies()
load("//swiftformat:load_package.bzl", "swiftformat_load_package")

swiftformat_load_package()

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()
50 changes: 49 additions & 1 deletion doc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,30 @@ _RULES_AND_MACROS_DOC_PROVIDER = doc_providers.create(
deps = ["//swiftformat"],
)

_API_SRCS = [
"src_utils",
]

_API_DOC_PROVIDERS = [
doc_providers.create(
name = name,
stardoc_input = "//swiftformat:swiftformat.bzl",
symbols = [name],
deps = ["//swiftformat"],
)
for name in _API_SRCS
]

_ALL_DOC_PROVIDERS = [
_RULES_AND_MACROS_DOC_PROVIDER,
_PROVIDERS_DOC_PROVIDER,
]
doc_providers.create(
name = "api",
is_stardoc = False,
stardoc_input = "//swiftformat:swiftformat.bzl",
deps = ["//swiftformat"],
),
] + _API_DOC_PROVIDERS

# MARK: - Headers

Expand All @@ -61,6 +81,34 @@ write_header(
symbols = _RULES_AND_MACROS_DOC_PROVIDER.symbols,
)

# Write the API headers
[
write_header(
name = doc_prov.header_label,
out = doc_prov.header_basename,
header_content = [
"# `{name}` API".format(name = doc_prov.name),
],
)
for doc_prov in _API_DOC_PROVIDERS
if doc_prov.is_stardoc
]

# MARK: - Special Case api.md

# Write the api.md_ file as a special case.
write_file_list(
name = "api_doc",
out = "api.md_",
doc_provs = _API_DOC_PROVIDERS,
header_content = [
"# Build API",
"",
"The APIs list below are used by rules_swiftformat.",
"",
],
)

# MARK: - Generate Documentation from Providers

doc_for_provs(doc_provs = _ALL_DOC_PROVIDERS)
1 change: 1 addition & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

- [Rules and Macros](/doc/rules_and_macros_overview.md)
- [Providers](/doc/providers_overview.md)
- [APIs](/doc/api.md)
7 changes: 7 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- Generated with Stardoc, Do Not Edit! -->
# Build API

The APIs list below are used by rules_swiftformat.

* [src_utils](/doc/src_utils.md)

3 changes: 3 additions & 0 deletions doc/rules_and_macros_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ swiftformat_pkg(<a href="#swiftformat_pkg-name">name</a>, <a href="#swiftformat_

Defines targets that will format, test and update the specified Swift sources.

NOTE: Any labels detected in the `srcs` will be ignored.


**PARAMETERS**


Expand Down
40 changes: 40 additions & 0 deletions doc/src_utils.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!-- Generated with Stardoc, Do Not Edit! -->
# `src_utils` API


<a id="#src_utils.is_path"></a>

## src_utils.is_path

<pre>
src_utils.is_path(<a href="#src_utils.is_path-src">src</a>)
</pre>

Determines whether the provided string is a path.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="src_utils.is_path-src"></a>src | A <code>string</code> value. | none |


<a id="#src_utils.is_label"></a>

## src_utils.is_label

<pre>
src_utils.is_label(<a href="#src_utils.is_label-src">src</a>)
</pre>

Determines whether the provided string is a label.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="src_utils.is_label-src"></a>src | A <code>string</code> value. | none |


11 changes: 11 additions & 0 deletions examples/exclude_files/Sources/Foo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@ load(
"swiftformat_library",
)

genrule(
name = "poorly_formatted_gen_file",
srcs = [],
outs = ["PoorlyFormattedGenFile.swift"],
cmd = """\
echo ' struct PoorlyFormattedGenFile { var name = "" }' > $@
""",
)

swiftformat_library(
name = "Foo",
# The generated file should be skipped for formatting because it is a label.
srcs = glob(["*.swift"]) + [":poorly_formatted_gen_file"],
module_name = "Foo",
swiftformat_exclude = ["PoorlyFormatted.swift"],
visibility = ["//:__subpackages__"],
Expand Down
1 change: 1 addition & 0 deletions swiftformat/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ bzl_library(
srcs = ["swiftformat.bzl"],
deps = [
"//swiftformat/internal:providers",
"//swiftformat/internal:src_utils",
"//swiftformat/internal:swiftformat_binary",
"//swiftformat/internal:swiftformat_format",
"//swiftformat/internal:swiftformat_library",
Expand Down
6 changes: 6 additions & 0 deletions swiftformat/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ bzl_library(
srcs = ["providers.bzl"],
)

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

bzl_library(
name = "swiftformat_format",
srcs = ["swiftformat_format.bzl"],
Expand All @@ -20,6 +25,7 @@ bzl_library(
name = "swiftformat_pkg",
srcs = ["swiftformat_pkg.bzl"],
deps = [
":src_utils",
":swiftformat_format",
":swiftformat_update",
"@bazel_skylib//rules:diff_test",
Expand Down
26 changes: 26 additions & 0 deletions swiftformat/internal/src_utils.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def _is_label(src):
"""Determines whether the provided string is a label.
Args:
src: A `string` value.
Returns:
A `bool` specifying whether the `string` value looks like a label.
"""
return src.find("//") > -1 or src.find(":") > -1

def _is_path(src):
"""Determines whether the provided string is a path.
Args:
src: A `string` value.
Returns:
A `bool` specifying whether the `string` value looks like a path.
"""
return not _is_label(src)

src_utils = struct(
is_path = _is_path,
is_label = _is_label,
)
13 changes: 5 additions & 8 deletions swiftformat/internal/swiftformat_pkg.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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")
Expand All @@ -6,15 +7,11 @@ 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.
NOTE: Any labels detected in the `srcs` will be ignored.
Args:
name: The base name for the targets that will be defined.
srcs: Optional. The Swift source files that should be formatted.
Expand All @@ -27,11 +24,11 @@ def swiftformat_pkg(name, srcs = None, config = None):
srcs = native.glob(["*.swift"])

# Only process paths; ignore labels
src_paths = [src for src in srcs if _is_path(src)]
src_paths = [src for src in srcs if src_utils.is_path(src)]

format_names = []
for src in src_paths:
src_name = src.replace("/", "_").replace(":", "")
src_name = src.replace("/", "_")
format_name = name + "_fmt_" + src_name
format_names.append(":" + format_name)

Expand Down
7 changes: 7 additions & 0 deletions swiftformat/swiftformat.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ load(
"//swiftformat/internal:swiftformat_test.bzl",
_swiftformat_test = "swiftformat_test",
)
load(
"//swiftformat/internal:src_utils.bzl",
_src_utils = "src_utils",
)

# Macros
swiftformat_pkg = _swiftformat_pkg
Expand All @@ -44,3 +48,6 @@ swiftformat_update = _swiftformat_update

# Providers
SwiftFormatInfo = _SwiftFormatInfo

# APIs
src_utils = _src_utils
3 changes: 3 additions & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load(":src_utils_tests.bzl", "src_utils_test_suite")

build_test(
name = "public_api",
Expand All @@ -8,3 +9,5 @@ build_test(
"//swiftformat:swiftformat",
],
)

src_utils_test_suite()
35 changes: 35 additions & 0 deletions tests/src_utils_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load("//swiftformat/internal:src_utils.bzl", "src_utils")

def _is_label_test(ctx):
env = unittest.begin(ctx)

asserts.true(env, src_utils.is_label("//Sources/Foo"))
asserts.true(env, src_utils.is_label(":Foo"))
asserts.true(env, src_utils.is_label("//Sources/Foo:bar"))
asserts.false(env, src_utils.is_label("Bar.swift"))
asserts.false(env, src_utils.is_label("path/to/Bar.swift"))

return unittest.end(env)

is_label_test = unittest.make(_is_label_test)

def _is_path_test(ctx):
env = unittest.begin(ctx)

asserts.true(env, src_utils.is_path("Bar.swift"))
asserts.true(env, src_utils.is_path("path/to/Bar.swift"))
asserts.false(env, src_utils.is_path("//Sources/Foo"))
asserts.false(env, src_utils.is_path(":Foo"))
asserts.false(env, src_utils.is_path("//Sources/Foo:bar"))

return unittest.end(env)

is_path_test = unittest.make(_is_path_test)

def src_utils_test_suite():
return unittest.suite(
"src_utils_tests",
is_label_test,
is_path_test,
)

0 comments on commit d5254f8

Please sign in to comment.