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

Enable maven_install to use the Starlark version of aar_import to facilitate migrating to the Starlark Android rules #526

Merged
merged 5 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
21 changes: 21 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,27 @@ maven_install(
],
)

maven_install(
name = "starlark_aar_import_test",
artifacts = [
"com.android.support:appcompat-v7:28.0.0",
],
repositories = [
"https://jcenter.bintray.com/",
"https://maven.google.com",
],
use_starlark_android_rules = True,
)

# for the above "starlark_aar_import_test" maven_install with
# use_starlark_android_rules = True
http_archive(
name = "build_bazel_rules_android",
urls = ["https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip"],
sha256 = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806",
strip_prefix = "rules_android-0.1.1",
)

# https://github.com/bazelbuild/rules_jvm_external/issues/351
maven_install(
name = "json_artifacts_testing",
Expand Down
20 changes: 20 additions & 0 deletions coursier.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,18 @@ package(default_visibility = ["//visibility:{visibility}"])

load("@rules_jvm_external//private/rules:jvm_import.bzl", "jvm_import")
load("@rules_jvm_external//private/rules:jetifier.bzl", "jetify_aar_import", "jetify_jvm_import")
jin marked this conversation as resolved.
Show resolved Hide resolved
{aar_import_statement}

{imports}
"""

DEFAULT_AAR_IMPORT_LABEL = "@build_bazel_rules_android//android:rules.bzl"

_AAR_IMPORT_STATEMENT = """\
load("%s", "aar_import")
"""


_BUILD_PIN = """
genrule(
name = "jq-binary",
Expand Down Expand Up @@ -119,6 +127,12 @@ def _relativize_and_symlink_file(repository_ctx, absolute_path):
repository_ctx.symlink(absolute_path, repository_ctx.path(artifact_relative_path))
return artifact_relative_path

def _get_aar_import_statement_or_empty_str(repository_ctx):
ahumesky marked this conversation as resolved.
Show resolved Hide resolved
if repository_ctx.attr.use_starlark_android_rules:
return _AAR_IMPORT_STATEMENT % repository_ctx.attr.aar_import_bzl_label
else:
return ""

# Generate the base `coursier` command depending on the OS, JAVA_HOME or the
# location of `java`.
def _generate_java_jar_command(repository_ctx, jar_path):
Expand Down Expand Up @@ -488,6 +502,7 @@ def _pinned_coursier_fetch_impl(repository_ctx):
visibility = "private" if repository_ctx.attr.strict_visibility else "public",
repository_name = repository_ctx.name,
imports = generated_imports,
aar_import_statement = _get_aar_import_statement_or_empty_str(repository_ctx),
),
executable = False,
)
Expand Down Expand Up @@ -948,6 +963,7 @@ def _coursier_fetch_impl(repository_ctx):
visibility = "private" if repository_ctx.attr.strict_visibility else "public",
repository_name = repository_name,
imports = generated_imports,
aar_import_statement = _get_aar_import_statement_or_empty_str(repository_ctx),
),
executable = False,
)
Expand Down Expand Up @@ -1048,6 +1064,8 @@ pinned_coursier_fetch = repository_rule(
"jetify_include_list": attr.string_list(doc = "List of artifacts that need to be jetified in `groupId:artifactId` format. By default all artifacts are jetified if `jetify` is set to True.", default = JETIFY_INCLUDE_LIST_JETIFY_ALL),
"additional_netrc_lines": attr.string_list(doc = "Additional lines prepended to the netrc file used by `http_file` (with `maven_install_json` only).", default = []),
"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),
"use_starlark_android_rules": attr.bool(default = False, doc = "Whether to use the native or Starlark version of the Android rules."),
"aar_import_bzl_label": attr.string(default = DEFAULT_AAR_IMPORT_LABEL, doc = "The label (as a string) to use to import aar_import from"),
ahumesky marked this conversation as resolved.
Show resolved Hide resolved
},
implementation = _pinned_coursier_fetch_impl,
)
Expand Down Expand Up @@ -1091,6 +1109,8 @@ coursier_fetch = repository_rule(
"resolve_timeout": attr.int(default = 600),
"jetify": attr.bool(doc = "Runs the AndroidX [Jetifier](https://developer.android.com/studio/command-line/jetifier) tool on artifacts specified in jetify_include_list. If jetify_include_list is not specified, run Jetifier on all artifacts.", default = False),
"jetify_include_list": attr.string_list(doc = "List of artifacts that need to be jetified in `groupId:artifactId` format. By default all artifacts are jetified if `jetify` is set to True.", default = JETIFY_INCLUDE_LIST_JETIFY_ALL),
"use_starlark_android_rules": attr.bool(default = False, doc = "Whether to use the native or Starlark version of the Android rules."),
"aar_import_bzl_label": attr.string(default = DEFAULT_AAR_IMPORT_LABEL, doc = "The label (as a string) to use to import aar_import from"),
},
environ = [
"JAVA_HOME",
Expand Down
10 changes: 8 additions & 2 deletions defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load(":coursier.bzl", "coursier_fetch", "pinned_coursier_fetch")
load(":coursier.bzl", "coursier_fetch", "pinned_coursier_fetch", "DEFAULT_AAR_IMPORT_LABEL")
load(":specs.bzl", "json", "parse")
load("//:private/dependency_tree_parser.bzl", "JETIFY_INCLUDE_LIST_JETIFY_ALL")
load("//private/rules:java_export.bzl", _java_export = "java_export")
Expand Down Expand Up @@ -40,7 +40,9 @@ def maven_install(
jetify = False,
jetify_include_list = JETIFY_INCLUDE_LIST_JETIFY_ALL,
additional_netrc_lines = [],
fail_if_repin_required = False):
fail_if_repin_required = False,
use_starlark_android_rules = False,
aar_import_bzl_label = DEFAULT_AAR_IMPORT_LABEL):
"""Resolves and fetches artifacts transitively from Maven repositories.

This macro runs a repository rule that invokes the Coursier CLI to resolve
Expand Down Expand Up @@ -77,6 +79,8 @@ def maven_install(
jetify_include_list: List of artifacts that need to be jetified in `groupId:artifactId` format. By default all artifacts are jetified if `jetify` is set to True.
additional_netrc_lines: Additional lines prepended to the netrc file used by `http_file` (with `maven_install_json` only).
fail_if_repin_required: Whether to fail the build if the required maven artifacts have been changed but not repinned. Requires the `maven_install_json` to have been set.
use_starlark_android_rules: Whether to use the native or Starlark version of the Android rules. Default is False.
aar_import_bzl_label: The label (as a string) to use to import aar_import from. Default is "@build_bazel_rules_android//rules:rules.bzl".
"""
repositories_json_strings = []
for repository in parse.parse_repository_spec_list(repositories):
Expand Down Expand Up @@ -125,6 +129,8 @@ def maven_install(
resolve_timeout = resolve_timeout,
jetify = jetify,
jetify_include_list = jetify_include_list,
use_starlark_android_rules = use_starlark_android_rules,
aar_import_bzl_label = aar_import_bzl_label,
)

if maven_install_json != None:
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/aar_import/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")

build_test(
name = "starlark_aar_import_test",
targets = [
"@starlark_aar_import_test//:com_android_support_appcompat_v7_28_0_0",
],
)