Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: introduce buildifier format/linter for starlark #18

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 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
- id: buildifier-lint
6 changes: 3 additions & 3 deletions docs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ load("@io_bazel_stardoc//stardoc:stardoc.bzl", "stardoc")

stardoc(
name = "rules",
input = "//ruby:defs.bzl",
out = "rules.md",
deps = ["//:ruby"]
input = "//ruby:defs.bzl",
deps = ["//:ruby"],
)

stardoc(
name = "repository_rules",
out = "repository_rules.md",
input = "//ruby:deps.bzl",
deps = ["//:ruby"]
deps = ["//:ruby"],
)
1 change: 1 addition & 0 deletions examples/gem/ruby_version.bzl
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
"Provide a default version of an interpreter used for the example"
RUBY_VERSION = "3.2.1"
2 changes: 2 additions & 0 deletions ruby/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"Public API for rules"

load("//ruby/private:binary.bzl", _rb_binary = "rb_binary")
load("//ruby/private:gem_build.bzl", _rb_gem_build = "rb_gem_build")
load("//ruby/private:gem_push.bzl", _rb_gem_push = "rb_gem_push")
Expand Down
2 changes: 2 additions & 0 deletions ruby/deps.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"Public API for repository rules"

load("//ruby/private:bundle.bzl", _rb_bundle = "rb_bundle")
load("//ruby/private:download.bzl", _rb_register_toolchains = "rb_register_toolchains")

Expand Down
6 changes: 5 additions & 1 deletion ruby/private/binary.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"Implementation details for rb_binary"

load("//ruby/private:library.bzl", LIBRARY_ATTRS = "ATTRS")
load(
"//ruby/private:providers.bzl",
"RubyFilesInfo",
"get_bundle_env",
"get_transitive_data",
"get_transitive_deps",
"get_transitive_srcs",
"get_bundle_env",
)

ATTRS = {
Expand Down Expand Up @@ -39,6 +41,7 @@ Use a built-in `args` attribute to pass extra arguments to the script.
),
}

# buildifier: disable=function-docstring
def generate_rb_binary_script(ctx, binary, bundler = False, args = []):
windows_constraint = ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]
is_windows = ctx.target_platform_has_constraint(windows_constraint)
Expand Down Expand Up @@ -82,6 +85,7 @@ def generate_rb_binary_script(ctx, binary, bundler = False, args = []):

return script

# buildifier: disable=function-docstring
def rb_binary_impl(ctx):
bundler = False
env = {}
Expand Down
2 changes: 2 additions & 0 deletions ruby/private/bundle.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"Implementation details for calling the bundler"

# https://github.com/rubygems/rubygems/blob/f8c76eae24bbeabb9c9cb5387dbd89df45566eb9/bundler/lib/bundler/installer.rb#L147
_BINSTUB_CMD = """@ruby -x "%~f0" %*
@exit /b %ERRORLEVEL%
Expand Down
5 changes: 5 additions & 0 deletions ruby/private/download.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"Repository rule for fetching Ruby interpreters"
_JRUBY_BINARY_URL = "https://repo1.maven.org/maven2/org/jruby/jruby-dist/{version}/jruby-dist-{version}-bin.tar.gz"
_RUBY_BUILD_URL = "https://github.com/rbenv/ruby-build/archive/refs/tags/v{version}.tar.gz"
_RUBY_INSTALLER_URL = "https://github.com/oneclick/rubyinstaller2/releases/download/RubyInstaller-{version}-1/rubyinstaller-devkit-{version}-1-x64.exe"
Expand All @@ -19,6 +20,10 @@ def rb_register_toolchains(version = None, **kwargs):
version = "2.7.5"
)
```

Args:
version: a semver version of Matz Ruby Interpreter, or a string like [interpreter type]-[version]
**kwargs: additional parameters to the downloader for this interpreter type
"""
repo_name = "rules_ruby_dist"
proxy_repo_name = "rules_ruby_toolchains"
Expand Down
4 changes: 3 additions & 1 deletion ruby/private/gem_build.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"Implementation details for rb_gem_build"

load("//ruby/private:library.bzl", LIBRARY_ATTRS = "ATTRS")
load(
"//ruby/private:providers.bzl",
"RubyFilesInfo",
"get_bundle_env",
"get_transitive_data",
"get_transitive_deps",
"get_transitive_srcs",
"get_bundle_env",
)

def _rb_gem_build_impl(ctx):
Expand Down
2 changes: 2 additions & 0 deletions ruby/private/gem_push.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"Implementation details for gem_push"

load("//ruby/private:binary.bzl", "generate_rb_binary_script", BINARY_ATTRS = "ATTRS")
load("//ruby/private:library.bzl", LIBRARY_ATTRS = "ATTRS")

Expand Down
4 changes: 3 additions & 1 deletion ruby/private/library.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"Implementation details for rb_library"

load(
"//ruby/private:providers.bzl",
"RubyFilesInfo",
"get_bundle_env",
"get_transitive_data",
"get_transitive_deps",
"get_transitive_srcs",
"get_bundle_env",
)

ATTRS = {
Expand Down
31 changes: 16 additions & 15 deletions ruby/private/providers.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"Providers for Interoperability between rules"
RubyFilesInfo = provider(
"Provider for Ruby files",
fields = ["transitive_data", "transitive_deps", "transitive_srcs", "bundle_env"],
Expand Down Expand Up @@ -47,22 +48,22 @@ def get_transitive_deps(deps):
)

def get_bundle_env(envs, deps):
"""Obtain the BUNDLE_* environment variables for a target and its transitive dependencies.
"""Obtain the BUNDLE_* environment variables for a target and its transitive dependencies.

Args:
envs: a list of environment variables
deps: a list of targets that are direct dependencies
Returns:
a collection of the transitive environment variables
"""
bundle_env = {}
Args:
envs: a list of environment variables
deps: a list of targets that are direct dependencies
Returns:
a collection of the transitive environment variables
"""
bundle_env = {}

transitive_deps = get_transitive_deps(deps).to_list()
for dep in transitive_deps:
bundle_env.update(dep[RubyFilesInfo].bundle_env)
transitive_deps = get_transitive_deps(deps).to_list()
for dep in transitive_deps:
bundle_env.update(dep[RubyFilesInfo].bundle_env)

for env in envs:
if env.startswith("BUNDLE_"):
bundle_env[env] = envs[env]
for env in envs:
if env.startswith("BUNDLE_"):
bundle_env[env] = envs[env]

return bundle_env
return bundle_env
2 changes: 2 additions & 0 deletions ruby/private/test.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"Implementation details for rb_test"

load("//ruby/private:binary.bzl", "ATTRS", "rb_binary_impl")
load("//ruby/private:library.bzl", LIBRARY_ATTRS = "ATTRS")

Expand Down
2 changes: 2 additions & 0 deletions ruby/toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"Define a Bazel toolchain for a Ruby interpreter"

def _rb_toolchain_impl(ctx):
return platform_common.ToolchainInfo(
ruby = ctx.executable.ruby,
Expand Down