Skip to content

Commit

Permalink
feat: provide a default for Terraform formatter (#188)
Browse files Browse the repository at this point in the history
Also give users control of the 'jobs' parameter
  • Loading branch information
alexeagle authored Mar 30, 2024
1 parent b354f71 commit 84da30d
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 52 deletions.
3 changes: 2 additions & 1 deletion docs/format.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions example/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,13 @@ load(
"fetch_ktfmt",
"fetch_pmd",
"fetch_swiftformat",
"fetch_terraform",
"rules_lint_dependencies",
)

rules_lint_dependencies()

fetch_pmd()

fetch_terraform()

fetch_java_format()

fetch_ktfmt()
Expand Down
3 changes: 0 additions & 3 deletions example/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ load(
"fetch_ktfmt",
"fetch_pmd",
"fetch_swiftformat",
"fetch_terraform",
)
load("@aspect_rules_lint//lint:ruff.bzl", "fetch_ruff")

fetch_pmd()

fetch_terraform()

fetch_java_format()

fetch_ktfmt()
Expand Down
11 changes: 0 additions & 11 deletions example/tools/format/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ load("@rules_java//java:defs.bzl", "java_binary")

package(default_visibility = ["//:__subpackages__"])

alias(
name = "terraform",
actual = select({
"@bazel_tools//src/conditions:linux_aarch64": "@terraform_linux_aarch64//:terraform",
"@bazel_tools//src/conditions:linux_x86_64": "@terraform_linux_x86_64//:terraform",
"@bazel_tools//src/conditions:darwin_arm64": "@terraform_macos_aarch64//:terraform",
"@bazel_tools//src/conditions:darwin_x86_64": "@terraform_macos_x86_64//:terraform",
}),
)

prettier.prettier_binary(
name = "prettier",
# Allow the binary to be run outside bazel
Expand Down Expand Up @@ -75,7 +65,6 @@ format_multirun(
sql = ":prettier",
starlark = "@buildifier_prebuilt//:buildifier",
swift = ":swiftformat",
terraform = ":terraform",
)

format_test(
Expand Down
7 changes: 4 additions & 3 deletions format/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _format_attr_factory(target_name, lang, toolname, tool_label, mode):
"data": [tool_label],
}

def format_multirun(name, **kwargs):
def format_multirun(name, jobs = 4, **kwargs):
"""Create a multirun binary for the given formatters.
Intended to be used with `bazel run` to update source files in-place.
Expand All @@ -74,6 +74,7 @@ def format_multirun(name, **kwargs):
Args:
name: name of the resulting target, typically "format"
jobs: how many language formatters to spawn in parallel, ideally matching how many CPUs are available
**kwargs: attributes named for each language, providing Label of a tool that formats it
"""
commands = []
Expand All @@ -95,14 +96,14 @@ def format_multirun(name, **kwargs):
name = name,
buffer_output = True,
commands = commands,
# Run up to 4 formatters at the same time. This is an arbitrary choice, based on some idea that 4-core machines are typical.
jobs = 4,
jobs = jobs,
keep_going = True,
)

multirun(
name = name + ".check",
commands = [c + ".check" for c in commands],
jobs = jobs,
keep_going = True,
)

Expand Down
36 changes: 36 additions & 0 deletions format/multitool.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,41 @@
"file": "jsonnetfmt"
}
]
},
"terraform": {
"binaries": [
{
"kind": "archive",
"url": "https://releases.hashicorp.com/terraform/1.4.0/terraform_1.4.0_darwin_arm64.zip",
"sha256": "d4a1e564714c6acf848e86dc020ff182477b49f932e3f550a5d9c8f5da7636fb",
"os": "macos",
"cpu": "arm64",
"file": "terraform"
},
{
"kind": "archive",
"url": "https://releases.hashicorp.com/terraform/1.4.0/terraform_1.4.0_darwin_amd64.zip",
"sha256": "e897a4217f1c3bfe37c694570dcc6371336fbda698790bb6b0547ec8daf1ffb3",
"os": "macos",
"cpu": "x86_64",
"file": "terraform"
},
{
"kind": "archive",
"url": "https://releases.hashicorp.com/terraform/1.4.0/terraform_1.4.0_linux_amd64.zip",
"sha256": "5da60da508d6d1941ffa8b9216147456a16bbff6db7622ae9ad01d314cbdd188",
"os": "linux",
"cpu": "x86_64",
"file": "terraform"
},
{
"kind": "archive",
"url": "https://releases.hashicorp.com/terraform/1.4.0/terraform_1.4.0_linux_arm64.zip",
"sha256": "33e0f4f0b75f507fc19012111de008308df343153cd6a3992507f4566c0bb723",
"os": "linux",
"cpu": "arm64",
"file": "terraform"
}
]
}
}
1 change: 1 addition & 0 deletions format/private/formatter_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ DEFAULT_TOOL_LABELS = {
"Jsonnet": "@multitool//tools/jsonnetfmt",
"Go": "@multitool//tools/gofumpt",
"Shell": "@multitool//tools/shfmt",
"Terraform": "@multitool//tools/terraform",
"YAML": "@multitool//tools/yamlfmt",
}

Expand Down
32 changes: 1 addition & 31 deletions format/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def rules_lint_dependencies():
)

# Transitive of rules_multitool, included here for convenience
# Note that many WORKSPACE users will get an earlier (and incompatible) version from some other *_dependencies() helper
http_archive(
name = "bazel_features",
sha256 = "06f02b97b6badb3227df2141a4b4622272cdcd2951526f40a888ab5f43897f14",
Expand All @@ -46,37 +47,6 @@ def fetch_pmd():
url = "https://github.com/pmd/pmd/releases/download/pmd_releases/6.55.0/pmd-bin-6.55.0.zip",
)

def fetch_terraform():
tf_version = "1.4.0"

http_archive(
name = "terraform_macos_aarch64",
build_file_content = "exports_files([\"terraform\"])",
sha256 = "d4a1e564714c6acf848e86dc020ff182477b49f932e3f550a5d9c8f5da7636fb",
urls = ["https://releases.hashicorp.com/terraform/{0}/terraform_{0}_darwin_arm64.zip".format(tf_version)],
)

http_archive(
name = "terraform_macos_x86_64",
build_file_content = "exports_files([\"terraform\"])",
sha256 = "e897a4217f1c3bfe37c694570dcc6371336fbda698790bb6b0547ec8daf1ffb3",
urls = ["https://releases.hashicorp.com/terraform/{0}/terraform_{0}_darwin_amd64.zip".format(tf_version)],
)

http_archive(
name = "terraform_linux_x86_64",
build_file_content = "exports_files([\"terraform\"])",
sha256 = "5da60da508d6d1941ffa8b9216147456a16bbff6db7622ae9ad01d314cbdd188",
urls = ["https://releases.hashicorp.com/terraform/{0}/terraform_{0}_linux_amd64.zip".format(tf_version)],
)

http_archive(
name = "terraform_linux_aarch64",
build_file_content = "exports_files([\"terraform\"])",
sha256 = "33e0f4f0b75f507fc19012111de008308df343153cd6a3992507f4566c0bb723",
urls = ["https://releases.hashicorp.com/terraform/{0}/terraform_{0}_linux_arm64.zip".format(tf_version)],
)

def fetch_java_format():
http_jar(
name = "google-java-format",
Expand Down

0 comments on commit 84da30d

Please sign in to comment.