Skip to content

Commit

Permalink
@MavenArtifactSource can be gziped resources
Browse files Browse the repository at this point in the history
  • Loading branch information
treblereel committed Oct 12, 2023
1 parent 7678862 commit ea65d33
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@ public interface ClientBundle {
String path();

String copyTo() default "<auto>";

boolean unzip() default false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public String getRepositoryUrl() {
return repositoryUrl;
}

void copyResourceTo(String path, FileObject dst) {
mavenArtifactDownloader.copyResourceTo(path, dst);
void copyResourceTo(String path, FileObject dst, boolean unzip) {
mavenArtifactDownloader.copyResourceTo(path, dst, unzip);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PushbackInputStream;
import java.util.Collections;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.tools.FileObject;
Expand Down Expand Up @@ -58,19 +62,26 @@ class MavenArtifactDownloader {
this.mavenArtifact = mavenArtifact;
}

public void copyResourceTo(String path, FileObject dst) {
void copyResourceTo(String path, FileObject dst, boolean unzip) {
if (artifact == null) {
download();
}
try (ZipFile zipFile = new ZipFile(artifact.getFile())) {
ZipEntry entry = zipFile.getEntry(path);
if (entry != null) {
if (unzip && !checkIfZip(zipFile.getInputStream(entry))) {
throw new GenerationException("Resource with a path " + path + " isn't a GZIP");
}
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(zipFile.getInputStream(entry)));
new BufferedReader(
new InputStreamReader(
unzip
? new GZIPInputStream(zipFile.getInputStream(entry))
: zipFile.getInputStream(entry)));
BufferedWriter bufferedWriter =
new BufferedWriter(new OutputStreamWriter(dst.openOutputStream()))) {

char[] buffer = new char[1024]; // a buffer to hold chunks of characters
char[] buffer = new char[1024];
int charsRead;
while ((charsRead = reader.read(buffer)) != -1) {
bufferedWriter.write(buffer, 0, charsRead);
Expand All @@ -85,6 +96,21 @@ public void copyResourceTo(String path, FileObject dst) {
}
}

private boolean checkIfZip(InputStream inputStream) {
if (!(inputStream instanceof PushbackInputStream)) {
inputStream = new PushbackInputStream(inputStream, 2);
}

byte[] signature = new byte[2];
try {
int bytesRead = inputStream.read(signature);
((PushbackInputStream) inputStream).unread(signature, 0, bytesRead);
return bytesRead == 2 && signature[0] == (byte) 0x1f && signature[1] == (byte) 0x8b;
} catch (IOException e) {
throw new GenerationException(e);
}
}

private void download() {
RepositorySystem system = newRepositorySystem();
RepositorySystemSession session = newSession(system);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@
package org.treblereel.j2cl.processors.generator.resources;

import com.google.common.io.Files;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -103,8 +98,7 @@ private void copyResources(Map<MavenArtifact, Set<ExecutableElement>> mavenArtif
StandardLocation.CLASS_OUTPUT,
"", // no package
copyTo);
mavenArtifactSetEntry.getKey().copyResourceTo(path, resource);
// copy(is, os);
mavenArtifactSetEntry.getKey().copyResourceTo(path, resource, artifactSource.unzip());
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -120,7 +114,7 @@ private void checkPath(String path, String type, ExecutableElement method) {

if (!pattern.matcher(pkg).matches()) {
throw new GenerationException(
"Wring " + type + " at " + method.getEnclosingElement() + "." + method.getSimpleName());
"Wrong " + type + " at " + method.getEnclosingElement() + "." + method.getSimpleName());
}
}

Expand All @@ -145,18 +139,4 @@ private void delete(File dir) {
}
}
}

public void copy(InputStreamReader reader, OutputStream outStream) throws IOException {
try (BufferedReader bufferedReader = new BufferedReader(reader);
BufferedWriter bufferedWriter =
new BufferedWriter(new OutputStreamWriter(outStream, reader.getEncoding()))) {

char[] buffer = new char[1024]; // a buffer to hold chunks of characters
int charsRead;
while ((charsRead = bufferedReader.read(buffer)) != -1) {
bufferedWriter.write(buffer, 0, charsRead);
}
bufferedWriter.flush();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,10 @@ public void testExternalResourceWebJarRename() {
String content = readFileAsString("original_support.js");
assertEquals(content, TextTestResourceImpl.INSTANCE.externalResourceWebJarRename().getText());
}

@Test
public void testExternalResourceWebJarGZIP() {
String content = readFileAsString("bootstrap.min.js.back");
assertEquals(content, TextTestResourceImpl.INSTANCE.externalResourceWebJarGZIP().getText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,13 @@ interface TextTestResource extends ClientBundle {
path = "META-INF/resources/webjars/jquery/3.7.1/src/css/support.js",
copyTo = "org/test/support-old.js.back")
TextResource externalResourceWebJarRenameDashInFile();

@MavenArtifactSource(
group = "org.webjars",
artifact = "bootstrap",
version = "3.4.1",
path = "META-INF/resources/webjars/bootstrap/3.4.1/js/bootstrap.min.js.gz",
copyTo = "org/test/bootstrap.min.js.back",
unzip = true)
TextResource externalResourceWebJarGZIP();
}

Large diffs are not rendered by default.

0 comments on commit ea65d33

Please sign in to comment.