Skip to content

Commit

Permalink
Implement "embulkSystemProperty" to auto-generate "embulk.properties"…
Browse files Browse the repository at this point in the history
… in the Embulk home
  • Loading branch information
dmikurube committed May 31, 2024
1 parent dff9616 commit dfc1d3b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/main/java/org/embulk/gradle/runset/InstallEmbulkRunSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
package org.embulk.gradle.runset;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand All @@ -25,6 +29,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.gradle.api.Action;
import org.gradle.api.IllegalDependencyNotation;
import org.gradle.api.InvalidUserDataException;
Expand All @@ -45,6 +50,7 @@
import org.gradle.api.logging.Logger;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.Copy;
import org.gradle.api.tasks.TaskAction;
import org.gradle.maven.MavenModule;
import org.gradle.maven.MavenPomArtifact;

Expand All @@ -57,6 +63,8 @@ public InstallEmbulkRunSet() {

this.project = this.getProject();
this.logger = this.project.getLogger();
this.embulkSystemProperties = new Properties();
this.embulkSystemPropertiesSource = null;

final ObjectFactory objectFactory = this.project.getObjects();
}
Expand Down Expand Up @@ -134,6 +142,12 @@ public InstallEmbulkRunSet embulkHome(final File dir) {
return this;
}

public InstallEmbulkRunSet embulkSystemProperty(final String key, final String value) {
this.createPropertiesSourceAndSetToCopy();
this.embulkSystemProperties.setProperty(key, value);
return this;
}

@Override
public final Copy into​(final Object destDir) {
throw new InvalidUserDataException("\"into\" is not permitted in InstallEmbulkRunSet. Use \"embulkHome\" instead.");
Expand Down Expand Up @@ -234,11 +248,47 @@ private static Map<Object, Object> castMap(final Map map) {
return (Map<Object, Object>) map;
}

@Override
@TaskAction
protected void copy() {
if (this.embulkSystemPropertiesSource != null) {
try (final OutputStream out = Files.newOutputStream(this.embulkSystemPropertiesSource)) {
this.embulkSystemProperties.store(
out, "Generated by the \"org.embulk.embulk-runset\" Gradle plugin.");
} catch (final IOException ex) {
throw new UncheckedIOException(ex);
}
}
super.copy();
}

private synchronized void createPropertiesSourceAndSetToCopy() {
if (this.embulkSystemPropertiesSource == null) {
final File temporaryDir = this.getTemporaryDir();
final Path path;
try {
path = Files.createTempFile(temporaryDir.toPath(), "embulk.", ".properties");
} catch (final IOException ex) {
throw new UncheckedIOException(ex);
}

this.from(path.toFile(), copySpec -> {
copySpec.rename(path.getFileName().toString(), "embulk.properties");
});

this.embulkSystemPropertiesSource = path;
}
}

// https://github.com/gradle/gradle/blob/v8.7.0/platforms/software/dependency-management/src/main/java/org/gradle/api/internal/notations/DependencyMapNotationConverter.java#L42-L58
private static List<String> ACCEPTABLE_MAP_KEYS =
Arrays.asList("group", "name", "version", "configuration", "ext", "classifier");

private final Logger logger;

private final Project project;

private final Properties embulkSystemProperties;

private Path embulkSystemPropertiesSource;
}
12 changes: 12 additions & 0 deletions src/test/java/org/embulk/gradle/runset/TestEmbulkRunSetPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

import static org.embulk.gradle.runset.Util.prepareProjectDir;
import static org.embulk.gradle.runset.Util.runGradle;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -42,5 +45,14 @@ public FileVisitResult visitFile(final Path file, final BasicFileAttributes attr
return FileVisitResult.CONTINUE;
}
});

final Properties properties = new Properties();
final Path propertiesPath = projectDir.resolve("build/simple/embulk.properties");
Files.copy(propertiesPath, System.out);
try (final InputStream in = Files.newInputStream(propertiesPath)) {
properties.load(in);
}
assertEquals(1, properties.size());
assertEquals("value", properties.getProperty("key"));
}
}
1 change: 1 addition & 0 deletions src/test/resources/simple/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repositories {

installEmbulkRunSet {
embulkHome file("${project.buildDir}/simple")
embulkSystemProperty "key", "value"
artifact "org.embulk:embulk-input-postgresql:0.13.2"
artifact group: "org.embulk", name: "embulk-input-s3", version: "0.6.0"
}

0 comments on commit dfc1d3b

Please sign in to comment.