-
-
Notifications
You must be signed in to change notification settings - Fork 287
Create jdeps output from classpath entries #1376
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
Changes from 3 commits
93fd7a7
3e21d8b
7a0a408
fda5a2f
3a84df4
d54e460
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| load("@rules_java//java:defs.bzl", "java_library") | ||
|
|
||
| java_library( | ||
| name = "jdeps", | ||
| srcs = ["JdepsWriter.java"], | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "@bazel_tools//src/main/protobuf:deps_java_proto", | ||
simuons marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "@com_google_protobuf//:protobuf_java", | ||
| ], | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package io.bazel.rulesscala.jdeps; | ||
|
|
||
| import com.google.devtools.build.lib.view.proto.Deps; | ||
| import java.io.BufferedOutputStream; | ||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
|
|
||
| public class JdepsWriter { | ||
|
||
|
|
||
| public static void write(String jdpesPath, String currentTarget, String[] classpath) | ||
| throws IOException { | ||
|
|
||
| Deps.Dependencies.Builder builder = Deps.Dependencies.newBuilder(); | ||
| builder.setSuccess(true); | ||
| builder.setRuleLabel(currentTarget); | ||
|
|
||
| for (String jar : classpath) { | ||
| Deps.Dependency.Builder dependencyBuilder = Deps.Dependency.newBuilder(); | ||
| dependencyBuilder.setKind(Deps.Dependency.Kind.EXPLICIT); | ||
| dependencyBuilder.setPath(jar); | ||
| builder.addDependency(dependencyBuilder.build()); | ||
| } | ||
|
|
||
| try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(jdpesPath))) { | ||
| outputStream.write(builder.build().toByteArray()); | ||
|
||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| load("@rules_java//java:defs.bzl", "java_test") | ||
|
|
||
| java_test( | ||
| name = "jdeps", | ||
| srcs = ["JdepsWriterTest.java"], | ||
| test_class = "jdeps.JdepsWriterTest", | ||
| deps = [ | ||
| "//src/java/io/bazel/rulesscala/jdeps", | ||
| "@bazel_tools//src/main/protobuf:deps_java_proto", | ||
| "@com_google_protobuf//:protobuf_java", | ||
| "@io_bazel_rules_scala_junit_junit", | ||
| ], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package jdeps; | ||
|
|
||
| import static org.junit.Assert.assertArrayEquals; | ||
|
|
||
| import com.google.devtools.build.lib.view.proto.Deps; | ||
| import com.google.devtools.build.lib.view.proto.Deps.Dependency; | ||
| import io.bazel.rulesscala.jdeps.JdepsWriter; | ||
| import java.io.BufferedInputStream; | ||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import org.junit.Test; | ||
|
|
||
| public class JdepsWriterTest { | ||
|
|
||
| @Test | ||
| public void writesJarListToJdepsFile() throws IOException { | ||
| File jdepsPath = Files | ||
| .createTempDirectory("jdeps-test-tempdir-") | ||
| .resolve("some-target.jdeps") | ||
| .toFile(); | ||
|
|
||
| String[] classpath = {"foo.jar", "baz/zoo.class"}; | ||
|
|
||
| JdepsWriter.write( | ||
| jdepsPath.getPath(), | ||
| "some-target", | ||
| classpath | ||
| ); | ||
|
|
||
| assertArrayEquals(readJarsFromJdepsFile(jdepsPath), classpath); | ||
| } | ||
|
|
||
| private String[] readJarsFromJdepsFile(File jdepsPath) throws IOException { | ||
| Deps.Dependencies dependencies = Deps.Dependencies | ||
| .parseFrom(new BufferedInputStream(new FileInputStream(jdepsPath))); | ||
|
|
||
| return dependencies | ||
| .getDependencyList() | ||
| .stream() | ||
| .map(Dependency::getPath) | ||
| .toArray(String[]::new); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
jdepsPathcan beNone? Maybe there is no need forNoneandnullchecks?