Skip to content
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

Configuration Cache Support #450

Draft
wants to merge 4 commits into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 95 additions & 65 deletions src/main/java/io/github/fvarrui/javapackager/gradle/CreateTarball.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.github.fvarrui.javapackager.gradle;

import java.io.File;
import java.util.UUID;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

import org.gradle.api.tasks.bundling.Compression;
import org.gradle.api.tasks.bundling.Tar;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;

import io.github.fvarrui.javapackager.model.Platform;
import io.github.fvarrui.javapackager.packagers.ArtifactGenerator;
import io.github.fvarrui.javapackager.packagers.Context;
import io.github.fvarrui.javapackager.packagers.MacPackager;
import io.github.fvarrui.javapackager.packagers.Packager;

Expand Down Expand Up @@ -42,69 +44,97 @@ protected File doApply(Packager packager) throws Exception {
String format = ".tar.gz";
File tarFile = new File(outputDirectory, finalName + format);

Tar tarTask = createTarTask();
tarTask.setProperty("archiveFileName", tarFile.getName());
tarTask.setProperty("destinationDirectory", outputDirectory);
tarTask.setCompression(Compression.GZIP);

// if zipball is for windows platform
if (Platform.windows.equals(platform)) {

tarTask.from(appFolder.getParentFile(), copySpec -> {
copySpec.include(appFolder.getName() + "/**");
});

}

// if zipball is for linux platform
else if (Platform.linux.equals(platform)) {

tarTask.from(appFolder.getParentFile(), copySpec -> {
copySpec.include(appFolder.getName() + "/**");
copySpec.exclude(appFolder.getName() + "/" + executable.getName());
copySpec.exclude(appFolder.getName() + "/" + jreDirectoryName + "/bin/*");
copySpec.exclude(appFolder.getName() + "/scripts/*");
});
tarTask.from(appFolder.getParentFile(), copySpec -> {
copySpec.include(appFolder.getName() + "/" + executable.getName());
copySpec.include(appFolder.getName() + "/" + jreDirectoryName + "/bin/*");
copySpec.include(appFolder.getName() + "/scripts/*");
copySpec.setFileMode(0755);
});

}

// if zipball is for macos platform
else if (Platform.mac.equals(platform)) {

MacPackager macPackager = (MacPackager) packager;
File appFile = macPackager.getAppFile();

tarTask.from(appFolder, copySpec -> {
copySpec.include(appFile.getName() + "/**");
copySpec.exclude(appFile.getName() + "/Contents/MacOS/" + executable.getName());
copySpec.exclude(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub");
copySpec.exclude(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/*");
copySpec.exclude(appFile.getName() + "/Contents/Resources/scripts/*");

});
tarTask.from(appFolder, copySpec -> {
copySpec.include(appFile.getName() + "/Contents/MacOS/" + executable.getName());
copySpec.include(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub");
copySpec.include(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/*");
copySpec.include(appFile.getName() + "/Contents/Resources/scripts/*");
copySpec.setFileMode(0755);
});

try (OutputStream fos = Files.newOutputStream(tarFile.toPath());
BufferedOutputStream bos = new BufferedOutputStream(fos);
GzipCompressorOutputStream gcos = new GzipCompressorOutputStream(bos);
TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gcos)) {

if (Platform.windows.equals(platform)) {
Path basePath = appFolder.getParentFile().toPath();
try (Stream<Path> fileStream = Files.walk(appFolder.toPath())) {
fileStream.forEach(path -> {
if (path.equals(tarFile.toPath())) {
return;
}
File file = path.toFile();
if (file.isFile()) {
try {
TarArchiveEntry entry = new TarArchiveEntry(path.toFile(), basePath.relativize(path).toString());
tarOut.putArchiveEntry(entry);
Files.copy(path, tarOut);
tarOut.closeArchiveEntry();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
});
}
} else if (Platform.linux.equals(platform)) {
Path appPath = appFolder.getParentFile().toPath();
try (Stream<Path> fileStream = Files.walk(appPath)) {
fileStream.forEach(path -> {
if (path.equals(tarFile.toPath())) {
return;
}
try {
String relativePath = appPath.relativize(path).toString();
if (path.toFile().isFile()) {
if (!(relativePath.equals(executable.getName())
|| relativePath.startsWith(jreDirectoryName + "/bin/")
|| relativePath.startsWith("scripts/"))) {
TarArchiveEntry entry = new TarArchiveEntry(path.toFile(), relativePath);
if (relativePath.equals(executable.getName())
|| relativePath.startsWith(jreDirectoryName + "/bin/")
|| relativePath.startsWith("scripts/")) {
entry.setMode(0755);
}
tarOut.putArchiveEntry(entry);
Files.copy(path, tarOut);
tarOut.closeArchiveEntry();
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
} else if (Platform.mac.equals(platform)) {
MacPackager macPackager = (MacPackager) packager;
File appFile = macPackager.getAppFile();

Path appPath = appFolder.toPath();
try (Stream<Path> fileStream = Files.walk(appFolder.toPath())) {
fileStream.forEach(path -> {
if (path.equals(tarFile.toPath())) {
return;
}
try {
String relativePath = appPath.relativize(path).toString();
if (path.toFile().isFile()) {
if (!(relativePath.startsWith(appFile.getName() + "/Contents/MacOS/" + executable.getName())
|| relativePath.startsWith(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub")
|| relativePath.startsWith(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/")
|| relativePath.startsWith(appFile.getName() + "/Contents/Resources/scripts/"))) {
TarArchiveEntry entry = new TarArchiveEntry(path.toFile(), relativePath);
if (relativePath.equals(executable.getName())
|| relativePath.startsWith(jreDirectoryName + "/bin/")
|| relativePath.startsWith("scripts/")) {
entry.setMode(0755);
}
tarOut.putArchiveEntry(entry);
Files.copy(path, tarOut);
tarOut.closeArchiveEntry();
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
}
}

tarTask.getActions().forEach(action -> action.execute(tarTask));

return tarFile;
}

private Tar createTarTask() {
return Context.getGradleContext().getProject().getTasks().create("createTarball_" + UUID.randomUUID(), Tar.class);
}

}
155 changes: 93 additions & 62 deletions src/main/java/io/github/fvarrui/javapackager/gradle/CreateZipball.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.github.fvarrui.javapackager.gradle;

import java.io.File;
import java.util.UUID;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

import org.gradle.api.tasks.bundling.Zip;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;

import io.github.fvarrui.javapackager.model.Platform;
import io.github.fvarrui.javapackager.packagers.ArtifactGenerator;
import io.github.fvarrui.javapackager.packagers.Context;
import io.github.fvarrui.javapackager.packagers.MacPackager;
import io.github.fvarrui.javapackager.packagers.Packager;

Expand Down Expand Up @@ -39,67 +41,96 @@ protected File doApply(Packager packager) throws Exception {
String zipFileName = packager.getZipballName() != null ? packager.getZipballName() : name + "-" + version + "-" + platform + ".zip";
File zipFile = new File(outputDirectory, zipFileName);

Zip zipTask = createZipTask();
zipTask.setProperty("archiveFileName", zipFile.getName());
zipTask.setProperty("destinationDirectory", outputDirectory);

// if zipball is for windows platform
if (Platform.windows.equals(platform)) {

zipTask.from(appFolder.getParentFile(), copySpec -> {
copySpec.include(appFolder.getName() + "/**");
});

}

// if zipball is for linux platform
else if (Platform.linux.equals(platform)) {

zipTask.from(appFolder.getParentFile(), copySpec -> {
copySpec.include(appFolder.getName() + "/**");
copySpec.exclude(appFolder.getName() + "/" + executable.getName());
copySpec.exclude(appFolder.getName() + "/" + jreDirectoryName + "/bin/*");
copySpec.exclude(appFolder.getName() + "/scripts/*");
});
zipTask.from(appFolder.getParentFile(), copySpec -> {
copySpec.include(appFolder.getName() + "/" + executable.getName());
copySpec.include(appFolder.getName() + "/" + jreDirectoryName + "/bin/*");
copySpec.include(appFolder.getName() + "/scripts/*");
copySpec.setFileMode(0755);
});

}

// if zipball is for macos platform
else if (Platform.mac.equals(platform)) {

MacPackager macPackager = (MacPackager) packager;
File appFile = macPackager.getAppFile();

zipTask.from(appFolder, copySpec -> {
copySpec.include(appFile.getName() + "/**");
copySpec.exclude(appFile.getName() + "/Contents/MacOS/" + executable.getName());
copySpec.exclude(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub");
copySpec.exclude(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/*");
copySpec.exclude(appFile.getName() + "/Contents/Resources/scripts/*");
});
zipTask.from(appFolder, copySpec -> {
copySpec.include(appFile.getName() + "/Contents/MacOS/" + executable.getName());
copySpec.include(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub");
copySpec.include(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/*");
copySpec.include(appFile.getName() + "/Contents/Resources/scripts/*");
copySpec.setFileMode(0755);
});

try (OutputStream fos = Files.newOutputStream(zipFile.toPath());
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(bos)) {

if (Platform.windows.equals(platform)) {
Path basePath = appFolder.getParentFile().toPath();
try (Stream<Path> fileStream = Files.walk(appFolder.toPath())) {
fileStream.forEach(path -> {
if (path.equals(zipFile.toPath())) {
return;
}
File file = path.toFile();
if (file.isFile()) {
try {
ZipArchiveEntry entry = new ZipArchiveEntry(path.toFile(), basePath.relativize(path).toString());
zipOut.putArchiveEntry(entry);
Files.copy(path, zipOut);
zipOut.closeArchiveEntry();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
});
}
} else if (Platform.linux.equals(platform)) {
Path appPath = appFolder.getParentFile().toPath();
try (Stream<Path> fileStream = Files.walk(appPath)) {
fileStream.forEach(path -> {
if (path.equals(zipFile.toPath())) {
return;
}
try {
String relativePath = appPath.relativize(path).toString();
if (path.toFile().isFile()) {
if (!(relativePath.equals(executable.getName())
|| relativePath.startsWith(jreDirectoryName + "/bin/")
|| relativePath.startsWith("scripts/"))) {
ZipArchiveEntry entry = new ZipArchiveEntry(path.toFile(), relativePath);
if (relativePath.equals(executable.getName())
|| relativePath.startsWith(jreDirectoryName + "/bin/")
|| relativePath.startsWith("scripts/")) {
entry.setUnixMode(0755);
}
zipOut.putArchiveEntry(entry);
Files.copy(path, zipOut);
zipOut.closeArchiveEntry();
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
} else if (Platform.mac.equals(platform)) {
MacPackager macPackager = (MacPackager) packager;
File appFile = macPackager.getAppFile();

Path appPath = appFolder.toPath();
try (Stream<Path> fileStream = Files.walk(appFolder.toPath())) {
fileStream.forEach(path -> {
if (path.equals(zipFile.toPath())) {
return;
}
try {
String relativePath = appPath.relativize(path).toString();
if (path.toFile().isFile()) {
if (!(relativePath.startsWith(appFile.getName() + "/Contents/MacOS/" + executable.getName())
|| relativePath.startsWith(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub")
|| relativePath.startsWith(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/")
|| relativePath.startsWith(appFile.getName() + "/Contents/Resources/scripts/"))) {
ZipArchiveEntry entry = new ZipArchiveEntry(path.toFile(), relativePath);
if (relativePath.equals(executable.getName())
|| relativePath.startsWith(jreDirectoryName + "/bin/")
|| relativePath.startsWith("scripts/")) {
entry.setUnixMode(0755);
}
zipOut.putArchiveEntry(entry);
Files.copy(path, zipOut);
zipOut.closeArchiveEntry();
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
}
}

zipTask.getActions().forEach(action -> action.execute(zipTask));

return zipFile;
}

private Zip createZipTask() {
return Context.getGradleContext().getProject().getTasks().create("createZipball_" + UUID.randomUUID(), Zip.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ public class GradleContext extends Context<Logger> {
private Project project;
private Launch4jLibraryTask libraryTask;
private DuplicatesStrategy duplicatesStrategy;
private PackagePluginExtension packagePluginExtension;
private String defaultVersion;

public GradleContext(Project project) {
super();
this.project = project;
defaultVersion = project.getVersion().toString();
}

public Logger getLogger() {
Expand Down Expand Up @@ -132,4 +135,19 @@ public File createWindowsExe(WindowsPackager packager) throws Exception {
return null;
}

public PackagePluginExtension getPackagePluginExtension() {
return packagePluginExtension;
}

public void setPackagePluginExtension(PackagePluginExtension packagePluginExtension) {
this.packagePluginExtension = packagePluginExtension;
}

public String getDefaultVersion() {
return defaultVersion;
}

public void setDefaultVersion(String defaultVersion) {
this.defaultVersion = defaultVersion;
}
}
Loading