Skip to content

Commit

Permalink
Merge pull request #89 from menny/exports-generation-type
Browse files Browse the repository at this point in the history
Allow exports-type on artifacts
  • Loading branch information
menny committed Jun 6, 2021
2 parents 64f6748 + 174901c commit 0f282f9
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public class CommandLineOptions {
@Parameter(
names = {"--exports_generation"},
splitter = NoSplitter.class,
description = "Where to generate exports: inherit, all, requested_deps, none.",
description = "Where to generate exports: all, requested_deps, none. Note: Can not be inherit for this call.",
required = true)
ExportsGenerationType exports_generation;

Expand Down
38 changes: 25 additions & 13 deletions resolver/src/main/java/net/evendanan/bazel/mvn/Merger.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.evendanan.bazel.mvn.api.Target;
import net.evendanan.bazel.mvn.api.TargetsBuilder;
import net.evendanan.bazel.mvn.api.model.Dependency;
import net.evendanan.bazel.mvn.api.model.ExportsGenerationType;
import net.evendanan.bazel.mvn.api.model.MavenCoordinate;
import net.evendanan.bazel.mvn.api.model.Resolution;
import net.evendanan.bazel.mvn.api.model.ResolutionOutput;
Expand All @@ -29,6 +30,8 @@
import net.evendanan.bazel.mvn.merger.TestOnlyMarker;
import net.evendanan.timing.ProgressTimer;

import org.apache.commons.lang3.tuple.Pair;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -100,6 +103,10 @@ public static void main(String[] args) throws Exception {
optionParser.usage();
return;
}
if (options.exports_generation.equals(ExportsGenerationType.inherit)) {
optionParser.usage();
return;
}

final DependencyTools dependencyTools;
if (options.rule_prefix.isEmpty()) {
Expand Down Expand Up @@ -297,19 +304,24 @@ private void writeResults(
final Set<MavenCoordinate> rootDependencies = resolutions.stream().map(r -> r.resolution().rootDependency()).collect(Collectors.toSet());

final Set<MavenCoordinate> exportsForDependency =
resolvedDependencies.stream()
.filter(d -> {
switch (options.exports_generation) {
case all:
return true;
case requested_deps:
return rootDependencies.contains(d.mavenCoordinate());
default:
return false;
}
})
.map(Dependency::mavenCoordinate)
.collect(Collectors.toSet());
resolutions.stream()
.map(r -> Pair.of(r.exportsGenerationType(), r.resolution().allResolvedDependencies()))
.flatMap(p -> p.getRight().stream().map(d -> Pair.of(d.mavenCoordinate(), p.getKey())))
.map(p -> Pair.of(p.getLeft(), p.getRight().equals(ExportsGenerationType.inherit)? options.exports_generation : p.getRight()))
.collect(Collectors.toMap(Pair::getKey, Pair::getValue, ExportsGenerationType::prioritizeType))
.entrySet().stream()
.filter(p -> {
switch (p.getValue()) {
case all:
return true;
case requested_deps:
return rootDependencies.contains(p.getKey());
default:
return false;
}
})
.map(Map.Entry::getKey)
.collect(Collectors.toSet());

resolvedDependencies =
resolvedDependencies.stream()
Expand Down
9 changes: 8 additions & 1 deletion resolver/src/main/java/net/evendanan/bazel/mvn/Resolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.google.common.base.Strings;
import com.google.devtools.bazel.workspace.maven.adapter.MigrationToolingGraphResolver;
import net.evendanan.bazel.mvn.api.GraphResolver;
import net.evendanan.bazel.mvn.api.model.ExportsGenerationType;
import net.evendanan.bazel.mvn.api.model.Resolution;
import net.evendanan.bazel.mvn.api.model.ResolutionOutput;
import net.evendanan.bazel.mvn.api.model.TargetType;
Expand Down Expand Up @@ -67,7 +68,7 @@ private void writeResults(Options options, Resolution resolution) throws Excepti
}

try (final FileWriter writer = new FileWriter(outputFile, Charsets.UTF_8, false)) {
new Serialization().serialize(ResolutionOutput.create(options.type, options.test_only, resolution), writer);
new Serialization().serialize(ResolutionOutput.create(options.type, options.exportsGenerationType, options.test_only, resolution), writer);
}
}

Expand All @@ -88,6 +89,12 @@ public static class Options {
required = true)
TargetType type;

@Parameter(
names = {"--exports_generation"},
splitter = NoSplitter.class,
description = "Type of exports generation: inherit, all, requested_deps, none.",
required = true)
ExportsGenerationType exportsGenerationType;

@Parameter(
names = {"--test_only"},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package net.evendanan.bazel.mvn.api.model;

public enum ExportsGenerationType {
/**
* USe default exports-generation. This can only be used in `artifact`.
*/
inherit,

/**
* Generate exports for all targets.
*/
Expand All @@ -15,5 +20,15 @@ public enum ExportsGenerationType {
/**
* Never generate exports.
*/
none
none;

public static ExportsGenerationType prioritizeType(ExportsGenerationType type1, ExportsGenerationType type2) {
if (type1.equals(ExportsGenerationType.inherit))
throw new IllegalArgumentException("type1 is inherit, which is not supported");
if (type2.equals(ExportsGenerationType.inherit))
throw new IllegalArgumentException("type2 is inherit, which is not supported");

final int highest = Math.min(type1.ordinal(), type2.ordinal());
return ExportsGenerationType.values()[highest];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
public abstract class ResolutionOutput {
public static ResolutionOutput create(
TargetType type,
ExportsGenerationType exportsGenerationType,
boolean testOnly,
Resolution resolution) {
return new AutoValue_ResolutionOutput(type, testOnly, resolution);
return new AutoValue_ResolutionOutput(type, exportsGenerationType, testOnly, resolution);
}

public abstract TargetType targetType();

public abstract ExportsGenerationType exportsGenerationType();

public abstract boolean testOnly();

public abstract Resolution resolution();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public ResolutionOutput deserialize(

return ResolutionOutput.create(
context.deserialize(jsonObject.get("t"), TargetType.class),
context.deserialize(jsonObject.get("e"), ExportsGenerationType.class),
context.deserialize(jsonObject.get("to"), boolean.class),
context.deserialize(jsonObject.get("r"), Resolution.class));
}
Expand All @@ -59,6 +60,8 @@ public JsonElement serialize(

jsonObject.add(
"t", context.serialize(resolution.targetType(), TargetType.class));
jsonObject.add(
"e", context.serialize(resolution.exportsGenerationType(), ExportsGenerationType.class));
jsonObject.add(
"to", context.serialize(resolution.testOnly(), boolean.class));
jsonObject.add(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.evendanan.bazel.mvn.api.serialization;

import net.evendanan.bazel.mvn.api.model.Dependency;
import net.evendanan.bazel.mvn.api.model.ExportsGenerationType;
import net.evendanan.bazel.mvn.api.model.License;
import net.evendanan.bazel.mvn.api.model.MavenCoordinate;
import net.evendanan.bazel.mvn.api.model.Resolution;
Expand Down Expand Up @@ -77,6 +78,7 @@ public void testHappyPath() {
final ResolutionOutput resolutionOutput =
ResolutionOutput.create(
TargetType.auto,
ExportsGenerationType.inherit,
false,
resolution);

Expand All @@ -87,6 +89,7 @@ public void testHappyPath() {

final ResolutionOutput actualOutput = serialization.deserialize(new StringReader(builder.toString()));
Assert.assertEquals(resolutionOutput.targetType(), actualOutput.targetType());
Assert.assertEquals(resolutionOutput.exportsGenerationType(), actualOutput.exportsGenerationType());
Assert.assertEquals(resolutionOutput.testOnly(), actualOutput.testOnly());

final Resolution actual = actualOutput.resolution();
Expand Down
5 changes: 4 additions & 1 deletion rules/maven_deps/mabel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def _impl_resolver(ctx):
[
"--artifact={}".format(ctx.attr.coordinate),
"--type={}".format(ctx.attr.type),
"--exports_generation={}".format(ctx.attr.exports_generation_type),
"--output_file={}".format(output_file.path),
"--debug_logs={}".format(ctx.attr.debug_logs).lower(),
"--test_only={}".format(ctx.attr.test_only).lower(),
Expand All @@ -34,6 +35,7 @@ _mabel_maven_dependency_graph_resolving_rule = rule(
attrs = {
"coordinate": attr.string(mandatory = True, doc = "Maven coordinate in the form of `group-id:artifact-id:version`."),
"debug_logs": attr.bool(default = False, doc = "If set to True, will print out debug logs while resolving dependencies. Default is False.", mandatory = False),
"exports_generation_type": attr.string(mandatory = True, default = "inherit", values = ["inherit", "all", "requested_deps", "none"], doc = "For which targets should we generate exports attribute."),
"maven_exclude_deps": attr.string_list(allow_empty = True, default = [], doc = "List of Maven dependencies which should not be resolved. You can omit the `version` or both `artifact-id:version`."),
"repositories": attr.string_list(allow_empty = False, default = DEFAULT_MAVEN_SERVERS, doc = "List of URLs that point to Maven servers. Defaut is Maven-Central."),
"test_only": attr.bool(default = False, doc = "Should this artifact be marked as test_only. Default is False.", mandatory = False),
Expand All @@ -45,7 +47,7 @@ _mabel_maven_dependency_graph_resolving_rule = rule(
)

# buildifier: disable=unnamed-macro
def artifact(coordinate, maven_exclude_deps = [], repositories = DEFAULT_MAVEN_SERVERS, debug_logs = False, type = "inherit", exports_generation = "inherit", test_only = False):
def artifact(coordinate, maven_exclude_deps = [], repositories = DEFAULT_MAVEN_SERVERS, debug_logs = False, type = "inherit", exports_generation_type = "inherit", test_only = False):
rule_name = "_mabel_maven_dependency_graph_resolving_{}".format(coordinate.replace(":", "__").replace("-", "_").replace(".", "_"))

# different targets may use the same artifact
Expand All @@ -57,6 +59,7 @@ def artifact(coordinate, maven_exclude_deps = [], repositories = DEFAULT_MAVEN_S
maven_exclude_deps = maven_exclude_deps,
repositories = repositories,
test_only = test_only,
exports_generation_type = exports_generation_type,
debug_logs = debug_logs,
visibility = ["//visibility:private"],
)
Expand Down

0 comments on commit 0f282f9

Please sign in to comment.