Skip to content

Commit

Permalink
chore: create pre-commit file
Browse files Browse the repository at this point in the history
Similar to bazelbuild/examples#310
This ensures that contributors outside google3 have a means to easily make buildifier-clean commits.
It is purely opt-in, and we assume that google3 users will have buildifier run for them in another way.
  • Loading branch information
alexeagle committed Oct 29, 2023
1 parent 77bf7ab commit 09a5433
Show file tree
Hide file tree
Showing 27 changed files with 124 additions and 85 deletions.
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# See CONTRIBUTING.md for instructions.
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks

repos:
# Check formatting and lint for starlark code
- repo: https://github.com/keith/pre-commit-buildifier
rev: 6.3.3
hooks:
- id: buildifier
args: ["-mode=fix", "-lint=fix"]
- id: buildifier-lint
14 changes: 10 additions & 4 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_license//:version.bzl", "version")
load("@rules_license//rules:license.bzl", "license")
load("@rules_license//rules:package_info.bzl", "package_info")
load("@rules_license//:version.bzl", "version")

package(
default_applicable_licenses = [":license", ":package_info"],
default_applicable_licenses = [
":license",
":package_info",
],
default_visibility = ["//visibility:public"],
)

Expand All @@ -38,7 +41,10 @@ package_info(
)

exports_files(
["LICENSE", "WORKSPACE"],
[
"LICENSE",
"WORKSPACE",
],
visibility = ["//visibility:public"],
)

Expand All @@ -55,9 +61,9 @@ filegroup(
"*.bzl",
"*.md",
]) + [
"MODULE.bazel",
"BUILD",
"LICENSE",
"MODULE.bazel",
"WORKSPACE.bzlmod",
],
visibility = ["//distro:__pkg__"],
Expand Down
16 changes: 8 additions & 8 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ workspace(name = "rules_license")
# If you want to use any of the reporting or SBOM tools, and you are using a
# WORKSPACE file instead of bzlmod, they you must explicitly depend on
# rules_python in your WORKSPACE.

### INTERNAL ONLY - lines after this are not included in the release packaging.

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "rules_python",
sha256 = "ffc7b877c95413c82bfd5482c017edcf759a6250d8b24e82f41f3c8b8d9e287e",
strip_prefix = "rules_python-0.19.0",
urls = [
"https://github.com/bazelbuild/rules_python/releases/download/0.19.0/rules_python-0.19.0.tar.gz",
],
name = "rules_python",
sha256 = "ffc7b877c95413c82bfd5482c017edcf759a6250d8b24e82f41f3c8b8d9e287e",
strip_prefix = "rules_python-0.19.0",
urls = [
"https://github.com/bazelbuild/rules_python/releases/download/0.19.0/rules_python-0.19.0.tar.gz",
],
)

http_archive(
name = "rules_pkg",
sha256 = "eea0f59c28a9241156a47d7a8e32db9122f3d50b505fae0f33de6ce4d9b61834",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.8.0/rules_pkg-0.8.0.tar.gz",
"https://github.com/bazelbuild/rules_pkg/releases/download/0.8.0/rules_pkg-0.8.0.tar.gz",
],
sha256 = "eea0f59c28a9241156a47d7a8e32db9122f3d50b505fae0f33de6ce4d9b61834",
)

load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
Expand Down
6 changes: 3 additions & 3 deletions distro/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("//:version.bzl", "version")
load("@rules_pkg//pkg:pkg.bzl", "pkg_tar")
load("@rules_pkg//pkg/releasing:defs.bzl", "print_rel_notes")
load("//:version.bzl", "version")

package(
default_visibility = ["//visibility:public"],
default_applicable_licenses = ["//:license"],
default_visibility = ["//visibility:public"],
)

licenses(["notice"])
Expand All @@ -37,8 +37,8 @@ pkg_tar(
"//licenses/generic:standard_package",
"//licenses/spdx:standard_package",
"//rules:standard_package",
"//rules_gathering:standard_package",
"//rules/private:standard_package",
"//rules_gathering:standard_package",
"//sample_reports:standard_package",
"//tools:standard_package",
],
Expand Down
27 changes: 14 additions & 13 deletions doc_build/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ How to:

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@io_bazel_stardoc//stardoc:stardoc.bzl", "stardoc")
load("@rules_python//python:defs.bzl", "py_library")
load("//:version.bzl", "version")

package(default_package_metadata = ["//:license", "//:package_info"])
package(default_package_metadata = [
"//:license",
"//:package_info",
])

filegroup(
name = "standard_package",
Expand All @@ -50,18 +51,18 @@ exports_files(
# Must put macro wrapped rules after their wrapper
# buildifier: leave-alone, do not sort
ORDER = [
("license", "//rules:license.bzl"),
("_license", "//rules:license.bzl"),
("license_kind", "//rules:license_kind.bzl"),
("_license_kind", "//rules:license_kind.bzl"),
("package_info", "//rules:package_info.bzl"),
("_package_info", "//rules:package_info.bzl"),
("LicenseInfo", "//rules:providers.bzl"),
("license", "//rules:license.bzl"),
("_license", "//rules:license.bzl"),
("license_kind", "//rules:license_kind.bzl"),
("_license_kind", "//rules:license_kind.bzl"),
("package_info", "//rules:package_info.bzl"),
("_package_info", "//rules:package_info.bzl"),
("LicenseInfo", "//rules:providers.bzl"),
("LicenseKindInfo", "//rules:providers.bzl"),
("PackageInfo", "//rules:providers.bzl"),
("gather_metadata_info", "//rules_gathering:gather_metadata.bzl"),
("PackageInfo", "//rules:providers.bzl"),
("gather_metadata_info", "//rules_gathering:gather_metadata.bzl"),
("gather_metadata_info_and_write", "//rules_gathering:gather_metadata.bzl"),
("trace", "//rules_gathering:trace.bzl"),
("trace", "//rules_gathering:trace.bzl"),
]

genrule(
Expand Down
6 changes: 6 additions & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ implementation.

This project follows
[Google's Open Source Community Guidelines](https://opensource.google/conduct/).

## Formatting and linting

We recommend that you install [pre-commit](https://pre-commit.com) so that your code is automatically
formatted when you `git commit`.
We may enforce that code is formatted and/or lint-free in our CI at some point.
11 changes: 5 additions & 6 deletions examples/manifest/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
load(":android_mock.bzl", "android_binary", "android_library")
load("@rules_license//tools:test_helpers.bzl", "golden_cmd_test")

load(":android_mock.bzl", "android_binary", "android_library")

# These two rules today capture what an android_binary would look like.
# This rule represents the Android specific code that displays licenses
Expand All @@ -13,7 +12,7 @@ android_library(
"license_display.sh",
],
data = [
"@rules_license//distro:distro",
"@rules_license//distro",
],
)

Expand All @@ -25,17 +24,17 @@ android_library(
android_binary(
name = "main",
srcs = ["main.sh"],
deps = [
],
data = [
":licenses",
],
deps = [
],
)

golden_cmd_test(
name = "main_test",
srcs = [],
cmd = "$(location :main)",
tools = [":main"],
golden = "main_golden.txt",
tools = [":main"],
)
5 changes: 3 additions & 2 deletions examples/manifest/android_mock.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
load("manifest.bzl", "manifest")

"""This is a proof of concept to show how to modify a macro definition to
create a sub-graph allowing for build time injection of license information. We
use Android-inspired rule names since these are a likely candidate for this
sort of injection."""

load("manifest.bzl", "manifest")

def android_library(name, **kwargs):
# This is an approximation for demo purposes.

Expand All @@ -21,6 +21,7 @@ def android_library(name, **kwargs):
**kwargs
)

# buildifier: disable=function-docstring
def android_binary(name, **kwargs):
# Same observation about not being sloppy with mapping deps, but I think the only important attribute
# in android_binary is deps, but need to double-check.
Expand Down
3 changes: 1 addition & 2 deletions examples/manifest/manifest.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def get_licenses_mapping(deps, warn = False):
if type(lic.license_text) == "File":
mappings[lic.license_text] = lic.package_name
elif warn:
# buildifier: disable=print
print("Legacy license %s not included, rule needs updating" % lic.license_text)
return mappings


def _manifest_impl(ctx):
# Gather all licenses and make it available as deps for downstream rules
# Additionally write the list of license filenames to a file that can
Expand Down Expand Up @@ -86,4 +86,3 @@ def manifest(name, deps, out = None, **kwargs):
if not out:
out = name + ".manifest"
_manifest(name = name, deps = deps, out = out, **kwargs)

6 changes: 4 additions & 2 deletions examples/policy_checker/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
load("@rules_license//examples/policy_checker:license_policy.bzl", "license_policy")
load("@rules_license//examples/policy_checker:license_policy_check.bzl", "license_policy_check")

package(default_package_metadata = ["//:license", "//:package_info"])
package(default_package_metadata = [
"//:license",
"//:package_info",
])

# license_policy rules generally appear in a central location per workspace. That
# should be access controlled by the policy team.
Expand Down Expand Up @@ -50,7 +53,6 @@ license_policy_check(
targets = ["//examples/src:my_server"],
)


# This is marked manual, so bazel test ... does not fail. Try it yourself with
# bazel build :check_violating_server
license_policy_check(
Expand Down
2 changes: 1 addition & 1 deletion examples/policy_checker/license_policy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ application type (e.g. production_server, mobile_application, ...)

load(
"@rules_license//examples/policy_checker:license_policy_provider.bzl",
"LicensePolicyInfo"
"LicensePolicyInfo",
)

def _license_policy_impl(ctx):
Expand Down
5 changes: 4 additions & 1 deletion examples/src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ load("@rules_license//rules:compliance.bzl", "check_license")
load("@rules_license//sample_reports:licenses_used.bzl", "licenses_used")

package(
default_package_metadata = ["//:license", "//:package_info"],
default_package_metadata = [
"//:license",
"//:package_info",
],
default_visibility = ["//examples:__subpackages__"],
)

Expand Down
4 changes: 2 additions & 2 deletions examples/vndor/acme/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
load("@rules_license//rules:license.bzl", "license")

package(
default_applicable_licenses = [":license"],
default_visibility = ["//examples:__subpackages__"],
default_applicable_licenses = [":license"],
default_visibility = ["//examples:__subpackages__"],
)

# The default license for an entire package is typically named "license".
Expand Down
2 changes: 1 addition & 1 deletion examples/vndor/constant_gen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ py_test(
name = "verify_licenses_test",
srcs = ["verify_licenses_test.py"],
data = [
":generator_licenses.json",
":generated_code_licenses.json",
":generator_licenses.json",
],
python_version = "PY3",
deps = [
Expand Down
2 changes: 1 addition & 1 deletion rules/check_licenses_shim.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""This module provides a custom Starlark rule used to create wrappers for targets that
can have blaze build --check_licenses executed against them."""

def _shim_rule_impl(ctx):
def _shim_rule_impl(_):
# This rule doesn't need to return anything. It only exists to propagate the dependency supplied
# by the label_flag
return []
Expand Down
2 changes: 1 addition & 1 deletion rules/compliance.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
load(
"@rules_license//rules:gather_licenses_info.bzl",
"gather_licenses_info",
"gather_licenses_info_and_write",
"write_licenses_info",
)
load(
Expand Down Expand Up @@ -157,6 +156,7 @@ def get_licenses_mapping(deps, warn = False):
if type(lic.license_text) == "File":
mappings[lic.license_text] = lic.package_name
elif warn:
# buildifier: disable=print
print("Legacy license %s not included, rule needs updating" % lic.license_text)

return mappings
7 changes: 4 additions & 3 deletions rules/gather_licenses_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def _strip_null_repo(label):
The is to make str(label) compatible between bazel 5.x and 6.x
"""
s = str(label)
if s.startswith('@//'):
if s.startswith("@//"):
return s[1:]
elif s.startswith('@@//'):
elif s.startswith("@@//"):
return s[2:]
return s

Expand Down Expand Up @@ -150,7 +150,7 @@ def write_licenses_info(ctx, deps, json_out):
licenses_files = []
for dep in deps:
if TransitiveLicensesInfo in dep:
transitive_licenses_info = dep[TransitiveLicensesInfo]
transitive_licenses_info = dep[TransitiveLicensesInfo]
lic_info, _ = licenses_info_to_json(transitive_licenses_info)
licenses_json.extend(lic_info)
for info in transitive_licenses_info.licenses.to_list():
Expand Down Expand Up @@ -253,6 +253,7 @@ def licenses_info_to_json(licenses_info):
label = _strip_null_repo(license.label),
used_by = ",\n ".join(sorted(['"%s"' % x for x in used_by[str(license.label)]])),
))

# Additionally return all File references so that other rules invoking
# this method can load license text file contents from external repos
# using runfiles
Expand Down
1 change: 1 addition & 0 deletions rules/gather_metadata.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
To be deleted before version 0.1.0
"""

load(
"@rules_license//rules_gathering:gather_metadata.bzl",
_gather_metadata_info = "gather_metadata_info",
Expand Down
10 changes: 5 additions & 5 deletions rules/license.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
"""

load(
"@rules_license//rules:providers.bzl",
"LicenseKindInfo",
)
load(
"@rules_license//rules:license_impl.bzl",
"license_rule_impl",
)
load(
"@rules_license//rules:providers.bzl",
"LicenseKindInfo",
)

# Enable this if your organization requires the license text to be a file
# checked into source control instead of, possibly, another rule.
Expand Down Expand Up @@ -67,7 +67,7 @@ _license = rule(
doc = "A human readable version string identifying this package." +
" This may be used to produce an index of OSS packages used" +
" by an applicatation. It should be a value that" +
" increases over time, rather than a commit hash."
" increases over time, rather than a commit hash.",
),
},
)
Expand Down
Loading

0 comments on commit 09a5433

Please sign in to comment.