Skip to content

Commit

Permalink
chore: install buildifier pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Sep 22, 2023
1 parent b3414e8 commit ce60bcf
Show file tree
Hide file tree
Showing 71 changed files with 252 additions and 152 deletions.
18 changes: 18 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# See CONTRIBUTING.md for instructions.
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks

# Use a slightly older version of node by default
# as the default uses a very new version of GLIBC
default_language_version:
node: 16.18.0

repos:
# Check formatting and lint for starlark code
- repo: https://github.com/keith/pre-commit-buildifier
rev: 6.3.3
hooks:
- id: buildifier
- id: buildifier-lint
args:
- --warnings=-module-docstring,-provider-params,-name-conventions,-unnamed-macro
2 changes: 1 addition & 1 deletion android/jetpack-compose/app/src/main/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@build_bazel_rules_android//android:rules.bzl", "android_binary", "android_library")
load("@build_bazel_rules_android//android:rules.bzl", "android_binary")
load("@io_bazel_rules_kotlin//kotlin:android.bzl", "kt_android_library")

kt_android_library(
Expand Down
2 changes: 1 addition & 1 deletion android/ndk/app/src/main/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@build_bazel_rules_android//android:rules.bzl", "android_binary", "android_library")
load("@rules_cc//cc:defs.bzl", "cc_library")

android_library(
name = "lib",
Expand Down
14 changes: 8 additions & 6 deletions bzlmod/02-override_bazel_module/BUILD
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
load("@bazel_skylib//:version.bzl", "hello_msg")

# buildifier: disable=print
print(hello_msg)

cc_binary(
name = "main",
srcs = ["main.cc"],
deps = [
"@lib_a//:lib_a",
"@com_github_google_glog//:glog",
],
name = "main",
srcs = ["main.cc"],
deps = [
"@com_github_google_glog//:glog",
"@lib_a",
],
)
11 changes: 6 additions & 5 deletions bzlmod/02-override_bazel_module/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,35 @@ module(
)

bazel_dep(name = "glog", version = "0.5.0", repo_name = "com_github_google_glog")

# Override glog to a fork version with archive_override.
archive_override(
module_name = "glog",
urls = ["https://github.com/meteorcloudy/glog/archive/9401faa19e0424791243827b8e95efd3d0d8db23.tar.gz"],
integrity = "sha256-EH4o3n+qkfcsEFODkkRzs1/XAH9ej2V77gv05eplB5k=",
strip_prefix = "glog-9401faa19e0424791243827b8e95efd3d0d8db23",
urls = ["https://github.com/meteorcloudy/glog/archive/9401faa19e0424791243827b8e95efd3d0d8db23.tar.gz"],
)

# Override gflag to a fork version with git_override.
# gflag is still an indirect dependency, the override itself doesn't give the root module visibility on gflags.
git_override(
module_name = "gflags",
remote = "https://github.com/meteorcloudy/gflags.git",
commit = "95995169e86f3fedd19696df5b1811d441c462a2",
remote = "https://github.com/meteorcloudy/gflags.git",
)

# Patch bazel skylib 1.2.0 with a local patch file.
bazel_dep(name = "bazel_skylib", version = "1.2.0")
single_version_override(
module_name = "bazel_skylib",
version = "1.2.0",
patches = ["//:bazel_skylib.patch"],
patch_strip = 1,
patches = ["//:bazel_skylib.patch"],
version = "1.2.0",
)

# Depend on module lib_a at a local path.
# The bazel_dep statement is necessary because the root module must have visibility on lib_a.
bazel_dep(name = "lib_a", version="")
bazel_dep(name = "lib_a", version = "")
local_path_override(
module_name = "lib_a",
path = "./lib_a",
Expand Down
10 changes: 5 additions & 5 deletions bzlmod/02-override_bazel_module/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Use a fork version of glog with http_archive.
http_archive(
Expand All @@ -12,17 +12,17 @@ http_archive(
# Use a fork version of gflags with git_repository.
git_repository(
name = "com_github_gflags_gflags",
remote = "https://github.com/meteorcloudy/gflags.git",
commit = "95995169e86f3fedd19696df5b1811d441c462a2",
remote = "https://github.com/meteorcloudy/gflags.git",
)

# Use a patched version of bazel_skylib
http_archive(
name = "bazel_skylib",
urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz"],
sha256 = "af87959afe497dc8dfd4c6cb66e1279cb98ccc84284619ebfec27d9c09a903de",
patches = ["//:bazel_skylib.patch"],
patch_args = ["-p1"],
patches = ["//:bazel_skylib.patch"],
sha256 = "af87959afe497dc8dfd4c6cb66e1279cb98ccc84284619ebfec27d9c09a903de",
urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz"],
)

# Depend on lib_a at a local path.
Expand Down
8 changes: 4 additions & 4 deletions bzlmod/03-introduce_dependencies_with_module_extension/BUILD
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Count how many cities are listed in the file
genrule(
name = "city_count",
cmd = "wc -l < $< > $@",
outs = ["city_number"],
srcs = ["@world_cities//file"],
outs = ["city_number"],
cmd = "wc -l < $< > $@",
)

# Count how many emojis are listed in the file
genrule(
name = "emoji_count",
cmd = "wc -l < $< > $@",
outs = ["emoji_number"],
# To build without --expiremental_enable_bzlmod, this has to be
# srcs = ["@emojis//file"],
srcs = ["@com_foo_bar_emojis//file"],
outs = ["emoji_number"],
cmd = "wc -l < $< > $@",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ module(
)

data_deps_ext = use_extension("//:deps.bzl", "data_deps_ext")

# The module extension can generate multiple repositories, due to strict deps, you need to declare which ones you need.
use_repo(data_deps_ext, "world_cities")

# It's fine to declare extension usages before defining module lib_a.
data_deps_ext_from_a = use_extension("@lib_a//:deps.bzl", "data_deps_ext")

# The emojis repo will be accessible with the "com_foo_bar_emojis" repo name instead.
use_repo(data_deps_ext_from_a, com_foo_bar_emojis="emojis")
use_repo(data_deps_ext_from_a, com_foo_bar_emojis = "emojis")

bazel_dep(name = "lib_a", version = "")
local_path_override(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//:deps.bzl", "world_cities")

world_cities()

# Depend on lib_a at a local path.
Expand All @@ -9,4 +10,5 @@ local_repository(

# This has to be defined after defining lib_a.
load("@lib_a//:deps.bzl", "emojis")

emojis()
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def world_cities():
sha256 = "4d2469729be61b55fcc758ab16bf590196733ff99f1c80e361623decb34ac35d",
)

def _data_deps_extension_impl(ctx):
def _data_deps_extension_impl(_):
world_cities()

data_deps_ext = module_extension(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
genrule(
name = "emoji_count",
cmd = "wc -l < $< > $@",
outs = ["emoji_number"],
srcs = ["@emojis//file"],
outs = ["emoji_number"],
cmd = "wc -l < $< > $@",
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ module(

sh_config_ext = use_extension("//:local_config_sh.bzl", "my_sh_config_extension")
use_repo(sh_config_ext, "my_local_config_sh")

register_toolchains("@my_local_config_sh//:local_sh_toolchain")
1 change: 1 addition & 0 deletions bzlmod/04-local_config_and_register_toolchains/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
load("//:local_config_sh.bzl", "sh_configure")

sh_configure()
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ def _is_windows(repository_ctx):

def _my_sh_config_impl(repository_ctx):
"""
Detects the path of the shell interpreter via a env var
and stores it in a sh_toolchain rule.
Detects the path of the shell interpreter via a env var.
Stores it in a sh_toolchain rule.
"""
sh_path = repository_ctx.os.environ.get("MY_SHELL_BIN_PATH")
if not sh_path:
Expand Down
11 changes: 8 additions & 3 deletions bzlmod/05-integrate_third_party_package_manager/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ local_path_override(
)

librarian_extension = use_extension("@librarian//:librarian.bzl", "librarian_extension")

librarian_extension.book(name="the_great_gatsby", edition="1995.12")
librarian_extension.book(name="hamlet", edition="2005.1")
librarian_extension.book(
name = "the_great_gatsby",
edition = "1995.12",
)
librarian_extension.book(
name = "hamlet",
edition = "2005.1",
)
use_repo(librarian_extension, "the_great_gatsby")
use_repo(librarian_extension, "hamlet")
9 changes: 6 additions & 3 deletions bzlmod/05-integrate_third_party_package_manager/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ local_repository(
name = "librarian",
path = "../utils/librarian",
)

local_repository(
name = "lib_a",
path = "./lib_a",
)

load("@lib_a//:deps.bzl","fetch_book_for_lib_a")

load("@lib_a//:deps.bzl", "fetch_book_for_lib_a")
load("@librarian//:librarian.bzl", "fetch_book")

fetch_book(
name = "the_great_gatsby",
edition = "1995.12",
Expand All @@ -31,7 +32,9 @@ local_repository(
name = "lib_b",
path = "./lib_b",
)
load("@lib_b//:deps.bzl","fetch_book_for_lib_b")

load("@lib_b//:deps.bzl", "fetch_book_for_lib_b")

fetch_book_for_lib_b()

# Answer: You'll get [email protected] and [email protected].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ module(
bazel_dep(name = "librarian", version = "")

librarian_extension = use_extension("@librarian//:librarian.bzl", "librarian_extension")

librarian_extension.book(name="the_great_gatsby", edition="2003.7")
librarian_extension.book(
name = "the_great_gatsby",
edition = "2003.7",
)
use_repo(librarian_extension, "the_great_gatsby")

librarian_extension.book(name="hamlet", edition="1800.1")
librarian_extension.book(
name = "hamlet",
edition = "1800.1",
)
use_repo(librarian_extension, "hamlet")
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ load("@librarian//:librarian.bzl", "fetch_book")

def fetch_book_for_lib_a():
fetch_book(
name="the_great_gatsby",
edition="2003.7",
name = "the_great_gatsby",
edition = "2003.7",
)
fetch_book(
name="hamlet",
edition="1800.1",
name = "hamlet",
edition = "1800.1",
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ module(
bazel_dep(name = "librarian", version = "")

librarian_extension = use_extension("@librarian//:librarian.bzl", "librarian_extension")

librarian_extension.book(name="the_great_gatsby", edition="2020.5")
librarian_extension.book(
name = "the_great_gatsby",
edition = "2020.5",
)
use_repo(librarian_extension, "the_great_gatsby")

librarian_extension.book(name="hamlet", edition="1603.1")
librarian_extension.book(
name = "hamlet",
edition = "1603.1",
)
use_repo(librarian_extension, "hamlet")
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ load("@librarian//:librarian.bzl", "fetch_book")

def fetch_book_for_lib_b():
fetch_book(
name="the_great_gatsby",
edition="2020.5",
name = "the_great_gatsby",
edition = "2020.5",
)
fetch_book(
name="hamlet",
edition="1603.1",
name = "hamlet",
edition = "1603.1",
)
1 change: 1 addition & 0 deletions bzlmod/06-specify_dev_dependency/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@bazel_skylib//:version.bzl", "version")

# buildifier: disable=print
print("Bazel Skylib version:", version)

genrule(
Expand Down
12 changes: 8 additions & 4 deletions bzlmod/06-specify_dev_dependency/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ bazel_dep(
name = "bazel_skylib",
version = "1.1.1",
)

bazel_dep(name = "librarian", version = "")
local_path_override(
module_name = "librarian",
path = "../utils/librarian",
)

librarian_extension = use_extension("@librarian//:librarian.bzl", "librarian_extension")
librarian_extension.book(
name = "the_great_gatsby",
edition = "1995.12",
)

librarian_extension.book(name="the_great_gatsby", edition="1995.12")
# The root module will get [email protected] because [email protected] is only a dev dependency of lib_a.
librarian_extension.book(name="hamlet", edition="1800.1")

librarian_extension.book(
name = "hamlet",
edition = "1800.1",
)
use_repo(librarian_extension, "the_great_gatsby")
use_repo(librarian_extension, "hamlet")
6 changes: 5 additions & 1 deletion bzlmod/06-specify_dev_dependency/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ local_repository(
)

load("@librarian//:librarian.bzl", "fetch_book")

fetch_book(
name = "the_great_gatsby",
edition = "1995.12",
)

fetch_book(
name = "hamlet",
edition = "1800.1",
Expand All @@ -19,11 +21,13 @@ local_repository(
name = "lib_a",
path = "./lib_a",
)

load("@lib_a//:deps.bzl", "deps")

deps()

http_archive(
name = "bazel_skylib",
urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz"],
sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d",
urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz"],
)
Loading

0 comments on commit ce60bcf

Please sign in to comment.