Skip to content
This repository has been archived by the owner on Mar 14, 2019. It is now read-only.

Add java_library statements for providing a lib with transitive deps #9

Merged
merged 6 commits into from
Feb 11, 2019
65 changes: 58 additions & 7 deletions coursier.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ def generate_imports(repository_ctx, dep_tree, srcs_dep_tree = None):
artifact_deps = artifact["dependencies"]
# Dedupe dependencies here. Sometimes coursier will return "x.y:z:aar:version" and "x.y:z:version" in the
# same list of dependencies.
seen_dep_labels = {}
target_import_labels = []
for dep in artifact_deps:
dep_target_label = _escape(_strip_packaging_and_classifier(dep))
if dep_target_label not in seen_dep_labels:
seen_dep_labels[dep_target_label] = True
target_import_string.append("\t\t\":%s\"," % dep_target_label)
target_import_string.append("\t],")
target_import_labels.append("\t\t\":%s\",\n" % dep_target_label)
target_import_labels = _deduplicate_list(target_import_labels)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!


# 5. Conclude.
target_import_string.append("".join(target_import_labels) + "\t],")

# 5. Finish the java_import rule.
#
# java_import(
# name = "org_hamcrest_hamcrest_library_1_3",
Expand All @@ -184,10 +184,46 @@ def generate_imports(repository_ctx, dep_tree, srcs_dep_tree = None):

all_imports.append("\n".join(target_import_string))

# Also create a versionless alias target
# 6. Create a versionless alias target
#
# alias(
# name = "org_hamcrest_hamcrest_library",
# actual = "org_hamcrest_hamcrest_library_1_3",
# )
target_alias_label = _escape(_strip_packaging_and_classifier_and_version(artifact["coord"]))
davidsantiago marked this conversation as resolved.
Show resolved Hide resolved
all_imports.append("alias(\n\tname = \"%s\",\n\tactual = \"%s\",\n)" % (target_alias_label, target_label))

# 7. Create java_library rule (includes transitive dependencies)
#
# java_library(
# name = "org_hamcrest_hamcrest_library_lib",
# exports = [
# ":org_hamcrest_hamcrest_core_1_3",
# ":org_hamcrest_hamcrest_library_1_3",
# ]
# )
t_dep_aliases = ["\t\t\":" + target_alias_label + "\""]
davidsantiago marked this conversation as resolved.
Show resolved Hide resolved
for transitive_dep_coord in artifact["dependencies"]:
t_dep = _find_dependency_by_coord(dep_tree, transitive_dep_coord)
davidsantiago marked this conversation as resolved.
Show resolved Hide resolved
if t_dep == None:
fail("Could not find transitive dependency of " + artifact["coord"] + ": " + transitive_dep_coord + "\n" +
"Parsed artifact data:" + repr(artifact) + "\n" +
"Parsed dependency output:" + repr(dep_tree))

t_dep_aliases.append("\t\t\":" + _escape(_strip_packaging_and_classifier_and_version(t_dep["coord"])) + "\"")

target_library = \
"""
java_library(
name = "{}",
exports = [
{}
]
)
""".lstrip()
t_dep_aliases = _deduplicate_list(t_dep_aliases)
all_imports.append(target_library.format(target_alias_label + "_lib", ",\n".join(t_dep_aliases)))


elif absolute_path_to_artifact == None:
fail("The artifact for " +
Expand All @@ -197,6 +233,21 @@ def generate_imports(repository_ctx, dep_tree, srcs_dep_tree = None):

return ("\n".join(all_imports), checksums)

def _deduplicate_list(items):
seen_items = {}
unique_items = []
for item in items:
if item not in seen_items:
seen_items[item] = True
unique_items.append(item)
return unique_items

def _find_dependency_by_coord(coursier_report, dep_coord):
for artifact in coursier_report["dependencies"]:
if artifact["coord"] == dep_coord:
return artifact
return None

# Generate the base `coursier` command depending on the OS, JAVA_HOME or the
# location of `java`.
def _generate_coursier_command(repository_ctx):
Expand Down
7 changes: 7 additions & 0 deletions defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ def artifact(a, repository_name = REPOSITORY_NAME):
def maven_artifact(a):
return artifact(a, repository_name = REPOSITORY_NAME)

def library(a, repository_name = REPOSITORY_NAME):
artifact_obj = _parse_artifact_str(a) if type(a) == "string" else a
return "@%s//:%s" % (repository_name, _escape(artifact_obj["group"] + ":" + artifact_obj["artifact"] + "_lib"))

def maven_library(a):
return library(a, repository_name = REPOSITORY_NAME)

def _escape(string):
return string.replace(".", "_").replace("-", "_").replace(":", "_")

Expand Down