forked from bazel-contrib/rules_jvm_external
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for resources in javadoc jars and make variable substitut…
…ion in javadoc arguments (bazel-contrib#1238)
- Loading branch information
Showing
9 changed files
with
177 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
tests/com/github/bazelbuild/rules_jvm_external/ZipUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.github.bazelbuild.rules_jvm_external; | ||
|
||
import com.github.bazelbuild.rules_jvm_external.zip.StableZipEntry; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.io.ByteStreams; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipInputStream; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
public class ZipUtils { | ||
public static void createJar(Path outputTo, Map<String, String> pathToContents) throws IOException { | ||
try (OutputStream os = Files.newOutputStream(outputTo); | ||
ZipOutputStream zos = new ZipOutputStream(os)) { | ||
|
||
for (Map.Entry<String, String> entry : pathToContents.entrySet()) { | ||
ZipEntry ze = new StableZipEntry(entry.getKey()); | ||
zos.putNextEntry(ze); | ||
if (!ze.isDirectory()) { | ||
zos.write(entry.getValue().getBytes(UTF_8)); | ||
} | ||
zos.closeEntry(); | ||
} | ||
} | ||
} | ||
|
||
public static Map<String, String> readJar(Path jar) throws IOException { | ||
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); | ||
|
||
try (InputStream is = Files.newInputStream(jar); | ||
ZipInputStream zis = new ZipInputStream(is)) { | ||
|
||
for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) { | ||
if (entry.isDirectory()) { | ||
continue; | ||
} | ||
|
||
builder.put(entry.getName(), new String(ByteStreams.toByteArray(zis), UTF_8)); | ||
} | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
tests/com/github/bazelbuild/rules_jvm_external/javadoc/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
load("//:defs.bzl", "artifact") | ||
|
||
java_test( | ||
name = "ResourceTest", | ||
srcs = ["ResourceTest.java"], | ||
test_class = "com.github.bazelbuild.rules_jvm_external.javadoc.ResourceTest", | ||
deps = [ | ||
"//private/tools/java/com/github/bazelbuild/rules_jvm_external", | ||
"//private/tools/java/com/github/bazelbuild/rules_jvm_external/javadoc", | ||
"//tests/com/github/bazelbuild/rules_jvm_external:zip_utils", | ||
artifact( | ||
"junit:junit", | ||
repository_name = "regression_testing_coursier", | ||
), | ||
artifact("com.google.guava:guava"), | ||
], | ||
) |
52 changes: 52 additions & 0 deletions
52
tests/com/github/bazelbuild/rules_jvm_external/javadoc/ResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.github.bazelbuild.rules_jvm_external.javadoc; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static com.github.bazelbuild.rules_jvm_external.ZipUtils.createJar; | ||
import static com.github.bazelbuild.rules_jvm_external.ZipUtils.readJar; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ResourceTest { | ||
@Rule | ||
public TemporaryFolder temp = new TemporaryFolder(); | ||
|
||
@Test | ||
public void shouldIncludeResourceFiles() throws Exception { | ||
Path inputJar = temp.newFile("in.jar").toPath(); | ||
Path outputJar = temp.newFile("out.jar").toPath(); | ||
Path elementList = temp.newFile("element-list").toPath(); | ||
// deleting the file since JavadocJarMaker fails on existing files, we just need to supply the path. | ||
elementList.toFile().delete(); | ||
|
||
Path license = temp.newFile("LICENSE").toPath(); | ||
Files.write(license, List.of("Apache License 2.0"), UTF_8); | ||
|
||
createJar( | ||
inputJar, | ||
ImmutableMap.of("com/example/Main.java", "public class Main { public static void main(String[] args) {} }") | ||
); | ||
|
||
JavadocJarMaker.main(new String[]{ | ||
"--resources", | ||
license.toAbsolutePath().toString(), | ||
"--in", | ||
inputJar.toAbsolutePath().toString(), | ||
"--out", | ||
outputJar.toAbsolutePath().toString(), | ||
"--element-list", | ||
elementList.toAbsolutePath().toString() | ||
}); | ||
|
||
Map<String, String> contents = readJar(outputJar); | ||
assertEquals("Apache License 2.0".strip(), contents.get("LICENSE").strip()); | ||
} | ||
} |