Skip to content

Commit

Permalink
v1.5.0 (#22)
Browse files Browse the repository at this point in the history
* #20 - Add support for "--platform"

* #21 - Add support for Windows file paths
  • Loading branch information
mfriesen authored Jun 4, 2023
1 parent beac66a commit afaff9d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
}

group 'com.formkiq.gradle'
version '1.4.0'
version '1.5.0'

java {
toolchain {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/formkiq/gradle/GraalvmNativeExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public class GraalvmNativeExtension {
private Property<String> mainClassName;
/** Output File Name. */
private Property<String> outputFileName;
/** Docker Platform. */
private Property<String> platform;
/** Reflection Config File. */
private Property<String> reflectionConfig;
/** Resource Configuration Files. */
Expand All @@ -102,6 +104,7 @@ public class GraalvmNativeExtension {
public GraalvmNativeExtension(final ObjectFactory objects) {
this.javaVersion = objects.property(String.class);
this.imageVersion = objects.property(String.class);
this.platform = objects.property(String.class);
this.mainClassName = objects.property(String.class);
this.reflectionConfig = objects.property(String.class);
this.serializationConfig = objects.property(String.class);
Expand Down Expand Up @@ -235,6 +238,15 @@ public String getOutputFileName() {
return this.outputFileName.getOrNull();
}

/**
* Returns the version of GraalVM Community Edition to download.
*
* @return The version of GraalVM Community Edition to download.
*/
public String getPlatform() {
return this.platform.getOrNull();
}

/**
* Get Reflection Config File.
*
Expand Down Expand Up @@ -686,6 +698,15 @@ public void setOutputFileName(final String name) {
this.outputFileName.set(name);
}

/**
* Set Platform.
*
* @param targetPlatform {@link String}
*/
public void setPlatform(final String targetPlatform) {
this.platform.set(targetPlatform);
}

/**
* Set Reflection Config File.
*
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/com/formkiq/gradle/GraalvmNativeTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,8 @@ private String getArchitecture() {
case "i386":
arch = "386";
break;
case "x86_64":
case "amd64":
arch = "amd64";
break;
default:
arch = "amd64";
break;
}

Expand Down
32 changes: 25 additions & 7 deletions src/main/java/com/formkiq/gradle/internal/DockerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package com.formkiq.gradle.internal;

import static com.formkiq.gradle.internal.Strings.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -54,7 +55,7 @@ public boolean exec(final Project project, final String workingDir, final List<S

if (workingDir != null) {
a.add("--workdir");
a.add(workingDir);
a.add(formatToUnix(workingDir));
}

a.add(this.containerId);
Expand Down Expand Up @@ -108,17 +109,28 @@ public void execute(ExecSpec arg0) {
* Pull Docker Image.
*
* @param project {@link Project}
* @param extension {@link GraalvmNativeExtension}
* @param imageVersion {@link String}
* @param javaVersion {@link String}
* @return boolean
* @throws IOException IOException
*/
private boolean pullImage(final Project project, final String dockerImage) throws IOException {
private boolean pullImage(final Project project, final GraalvmNativeExtension extension,
final String dockerImage) throws IOException {
ExecResult result = project.exec(new Action<ExecSpec>() {
@Override
public void execute(ExecSpec arg0) {
arg0.setCommandLine("docker");
arg0.args(Arrays.asList("pull", dockerImage));

List<String> args = new ArrayList<>();
args.add("pull");
if (extension.getPlatform() != null) {
args.add("--platform");
args.add(extension.getPlatform());
}

args.add(dockerImage);
arg0.args(args);
}
});
project.getLogger().debug(result.toString());
Expand All @@ -138,7 +150,7 @@ public boolean startImage(final Project project, final GraalvmNativeExtension ex
final List<File> classPaths) throws IOException {

String dockerImage = extension.getDockerImage();
pullImage(project, dockerImage);
pullImage(project, extension, dockerImage);

ByteArrayOutputStream so = new ByteArrayOutputStream();

Expand All @@ -151,11 +163,17 @@ public void execute(ExecSpec arg0) {

List<String> args = new ArrayList<>(Arrays.asList("run", "-d"));

classPaths.forEach(cp -> args.addAll(Arrays.asList("-v", cp + ":" + cp)));
if (extension.getPlatform() != null) {
args.add("--platform");
args.add(extension.getPlatform());
}

classPaths.forEach(
cp -> args.addAll(Arrays.asList("-v", formatToUnix(cp) + ":" + formatToUnix(cp))));

if (extension.getReflectionConfig() != null) {
args.addAll(Arrays.asList("-v",
extension.getReflectionConfig() + ":" + extension.getReflectionConfig()));
args.addAll(Arrays.asList("-v", formatToUnix(extension.getReflectionConfig()) + ":"
+ formatToUnix(extension.getReflectionConfig())));
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package com.formkiq.gradle.internal;

import static com.formkiq.gradle.internal.Strings.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -93,7 +94,7 @@ private String buildClassPathString(final Project project) {
files.add(Path.of(project.getBuildDir().getAbsolutePath(), GRAALVM_JAVA_MAIN).toFile());
addClasspaths(files);

return files.stream().map(File::getAbsolutePath)
return files.stream().map(File::getAbsolutePath).map(s -> formatToUnix(s))
.collect(Collectors.joining(OperatingSystem.current().isWindows() ? ";" : ":"));
}

Expand Down Expand Up @@ -196,7 +197,8 @@ private List<String> getBuildGraalvmImageArguments(final Project project, final

String reflectConfig = this.extension.getReflectionConfig();
if (reflectConfig != null) {
addStringArgument(args, reflectConfig, "-H:ReflectionConfigurationFiles=" + reflectConfig);
addStringArgument(args, reflectConfig,
"-H:ReflectionConfigurationFiles=" + formatToUnix(reflectConfig));
}

String serializationConfig = this.extension.getSerializationConfig();
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/formkiq/gradle/internal/Strings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.formkiq.gradle.internal;

import java.io.File;
import org.gradle.internal.os.OperatingSystem;

/**
*
* {@link String} utilities.
*
*/
public class Strings {

/**
* Format {@link File} in Unix format.
*
* @param file {@link File}
* @return {@link String}
*/
public static String formatToUnix(final File file) {
String path = file.getAbsolutePath();

return formatToUnix(path);
}

/**
* Format {@link String} in Unix format.
*
* @param path {@link String}
* @return {@link String}
*/
public static String formatToUnix(final String path) {
String s = path;
if (OperatingSystem.current().isWindows()) {
s = s.replace("\\", "/").replace(":", "");
if (!s.startsWith("/")) {
s = "/" + s;
}
}

return s;
}
}

0 comments on commit afaff9d

Please sign in to comment.