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

[bzlmod] Allow compat repos to be generated #880

Merged
merged 1 commit into from
Mar 24, 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
5 changes: 1 addition & 4 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ module(
)

bazel_dep(name = "bazel_skylib", version = "1.4.1")
bazel_dep(name = "stardoc", repo_name = "io_bazel_stardoc", version = "0.5.3")
bazel_dep(name = "stardoc", version = "0.5.3", repo_name = "io_bazel_stardoc")

non_module_deps = use_extension("//:non-module-deps.bzl", "non_module_deps")

use_repo(
non_module_deps,
"io_bazel_rules_kotlin",
)

maven = use_extension(":extensions.bzl", "maven")

maven.install(
name = "rules_jvm_external_deps",
artifacts = [
Expand All @@ -30,7 +28,6 @@ maven.install(
],
lock_file = "@rules_jvm_external//:rules_jvm_external_deps_install.json",
)

use_repo(
maven,
"rules_jvm_external_deps",
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,11 @@ maven_install(
# Must not be in any other maven_install where generate_compat_repositories = True
"com.google.http-client:google-http-client-gson:1.42.3",
],
generate_compat_repositories = True,
repositories = [
"https://repo1.maven.org/maven2",
],
strict_visibility = True,
generate_compat_repositories = True,
)

load("@strict_visibility_with_compat_testing//:compat.bzl", "compat_repositories")
Expand Down
7 changes: 5 additions & 2 deletions private/compat_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ alias(
"""

def _compat_repository_impl(repository_ctx):
target_name = repository_ctx.attr.target_name if repository_ctx.attr.target_name else repository_ctx.name

repository_ctx.file(
"jar/BUILD",
_JAR_BUILD.format(
generating_repository = repository_ctx.attr.generating_repository,
target_name = repository_ctx.name,
target_name = target_name,
),
executable = False,
)
Expand All @@ -29,7 +31,7 @@ def _compat_repository_impl(repository_ctx):
_ROOT_BUILD.format(
repository_name = repository_ctx.name,
generating_repository = repository_ctx.attr.generating_repository,
target_name = repository_ctx.name,
target_name = target_name,
),
executable = False,
)
Expand All @@ -38,5 +40,6 @@ compat_repository = repository_rule(
implementation = _compat_repository_impl,
attrs = {
"generating_repository": attr.string(default = "maven"),
"target_name": attr.string(),
},
)
20 changes: 20 additions & 0 deletions private/extensions/maven.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("//private:compat_repository.bzl", "compat_repository")
load("//private:coursier_utilities.bzl", "escape")
load("//private:dependency_tree_parser.bzl", "JETIFY_INCLUDE_LIST_JETIFY_ALL")
load("//private/rules:v1_lock_file.bzl", "v1_lock_file")
load("//private/rules:v2_lock_file.bzl", "v2_lock_file")
Expand Down Expand Up @@ -74,6 +76,9 @@ _install = tag_class(
"fail_if_repin_required": attr.bool(doc = "Whether to fail the build if the maven_artifact inputs have changed but the lock file has not been repinned.", default = False),
"lock_file": attr.label(),
"repositories": attr.string_list(default = DEFAULT_REPOSITORIES),
"generate_compat_repositories": attr.bool(
doc = "Additionally generate repository aliases in a .bzl file for all JAR artifacts. For example, `@maven//:com_google_guava_guava` can also be referenced as `@com_google_guava_guava//jar`.",
),

# When using an unpinned repo
"excluded_artifacts": attr.string_list(doc = "Artifacts to exclude, in `artifactId:groupId` format. Only used on unpinned installs", default = []), # list of artifacts to exclude
Expand Down Expand Up @@ -155,6 +160,7 @@ def _maven_impl(mctx):
# - fail_on_missing_checksum: bool. A logical OR over all `fail_on_missing_checksum` for all `install` tags with the same name.
# - fetch_javadoc: bool. A logical OR over all `fetch_javadoc` for all `install` tags with the same name.
# - fetch_sources: bool. A logical OR over all `fetch_sources` for all `install` tags with the same name.
# - generate_compat_repositories: bool. A logical OR over all `generate_compat_repositories` for all `install` tags with the same name.
# - jetify: bool. A logical OR over all `jetify` for all `install` tags with the same name.
# - jetify_include_list: string list. Accumulated from all `install` tags
# - lock_file: the lock file to use, if present. Multiple lock files will cause the build to fail.
Expand Down Expand Up @@ -242,6 +248,7 @@ def _maven_impl(mctx):
_logical_or(repo, "fail_on_missing_checksum", False, install.fail_on_missing_checksum)
_logical_or(repo, "fetch_sources", False, install.fetch_sources)
_logical_or(repo, "fetch_javadoc", False, install.fetch_javadoc)
_logical_or(repo, "generate_compat_repositories", False, install.generate_compat_repositories)
_logical_or(repo, "jetify", False, install.jetify)
_logical_or(repo, "strict_visibility", False, install.strict_visibility)
_logical_or(repo, "use_starlark_android_rules", False, install.use_starlark_android_rules)
Expand Down Expand Up @@ -322,6 +329,19 @@ def _maven_impl(mctx):
duplicate_version_warning = repo.get("duplicate_version_warning"),
)

if repo.get("generate_compat_repositories"):
seen = []
for artifact in artifacts:
versionless = escape(artifact["group"] + "_" + artifact["artifact"])
if versionless in seen:
continue
seen.append(versionless)
compat_repository(
name = versionless,
generating_repository = name,
target_name = versionless,
)

if repo.get("lock_file"):
lock_file = json.decode(mctx.read(mctx.path(repo.get("lock_file"))))

Expand Down