Skip to content
Open
Empty file added WORKSPACE
Empty file.
1 change: 1 addition & 0 deletions cmd/gazelle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ go_library(
"//cmd/gazelle/internal/wspace",
"//language/proto_go_modules",
"//language/protobuf",
"//language/starlarkmodules",
"@bazel_gazelle//config:go_default_library",
"@bazel_gazelle//flag:go_default_library",
"@bazel_gazelle//label:go_default_library",
Expand Down
2 changes: 2 additions & 0 deletions cmd/gazelle/langs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
"github.com/bazelbuild/bazel-gazelle/language/proto"
"github.com/stackb/rules_proto/language/proto_go_modules"
"github.com/stackb/rules_proto/language/protobuf"
"github.com/stackb/rules_proto/language/starlarkrepository"
)

var languages = []language.Language{
proto.NewLanguage(),
protobuf.NewLanguage(),
golang.NewLanguage(),
proto_go_modules.NewLanguage(),
starlarkrepository.NewLanguage(),
}
90 changes: 90 additions & 0 deletions extensions/starlark_repository.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""proto_repostitory.bzl provides the starlark_repository rule."""

# Copyright 2014 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@bazel_features//:features.bzl", "bazel_features")
load("@build_stack_rules_proto//rules/proto:starlark_repository.bzl", "starlark_repository_attrs", starlark_repository_repo_rule = "starlark_repository")

def _extension_metadata(
module_ctx,
*,
root_module_direct_deps = None,
root_module_direct_dev_deps = None,
reproducible = False):
"""returns the module_ctx.extension_metadata in a bazel-version-aware way

This function was copied from the bazel-gazelle repository.
"""

if not hasattr(module_ctx, "extension_metadata"):
return None
metadata_kwargs = {}
if bazel_features.external_deps.extension_metadata_has_reproducible:
metadata_kwargs["reproducible"] = reproducible
return module_ctx.extension_metadata(
root_module_direct_deps = root_module_direct_deps,
root_module_direct_dev_deps = root_module_direct_dev_deps,
**metadata_kwargs
)

def _starlark_repository_impl(module_ctx):
# named_repos is a dict<K,V> where V is the kwargs for the actual
# "starlark_repository" repo rule and K is the tag.name (the name given by the
# MODULE.bazel author)
named_archives = {}

# iterate all the module tags and gather a list of named_archives.
#
# TODO(pcj): what is the best practice for version selection here? Do I need
# to check if module.is_root and handle that differently?
#
for module in module_ctx.modules:
for tag in module.tags.archive:
kwargs = {
attr: getattr(tag, attr)
for attr in _starlark_repository_archive_attrs.keys()
if hasattr(tag, attr)
}
named_archives[tag.name] = kwargs

# declare a repository rule foreach one
for apparent_name, kwargs in named_archives.items():
starlark_repository_repo_rule(
apparent_name = apparent_name,
**kwargs
)

return _extension_metadata(
module_ctx,
reproducible = True,
)

_starlark_repository_archive_attrs = starlark_repository_attrs | {
"name": attr.string(
doc = "The repo name.",
mandatory = True,
),
}
_starlark_repository_archive_attrs.pop("apparent_name")

starlark_repository = module_extension(
implementation = _starlark_repository_impl,
tag_classes = dict(
archive = tag_class(
doc = "declare an http_archive repository that is post-processed by a custom version of gazelle that includes the 'protobuf' language",
attrs = _starlark_repository_archive_attrs,
),
),
)
18 changes: 18 additions & 0 deletions language/starlarkrepository/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "starlarkrepository",
srcs = ["language.go"],
importpath = "github.com/stackb/rules_proto/language/starlarkrepository",
visibility = ["//visibility:public"],
deps = [
"@bazel_gazelle//config:go_default_library",
"@bazel_gazelle//label:go_default_library",
"@bazel_gazelle//language:go_default_library",
"@bazel_gazelle//pathtools:go_default_library",
"@bazel_gazelle//repo:go_default_library",
"@bazel_gazelle//resolve:go_default_library",
"@bazel_gazelle//rule:go_default_library",
"@com_github_bazelbuild_buildtools//build:go_default_library",
],
)
Loading
Loading