Skip to content
Merged
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
3 changes: 2 additions & 1 deletion bin/generate-samples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ if [ ! -f "$executable" ]; then
fi

export JAVA_OPTS="${JAVA_OPTS} -ea -server -Duser.timezone=UTC"
export BATCH_OPTS="${BATCH_OPTS:-}"

files=()
args=()
Expand Down Expand Up @@ -61,6 +62,6 @@ else

# shellcheck disable=SC2086
# shellcheck disable=SC2068
java ${JAVA_OPTS} -jar "$executable" batch --includes-base-dir "${root}" --fail-fast -- ${files[@]}
java ${JAVA_OPTS} -jar "$executable" batch ${BATCH_OPTS} --includes-base-dir "${root}" --fail-fast -- ${files[@]}
fi

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.airlift.airline.Arguments;
import io.airlift.airline.Command;
import io.airlift.airline.Option;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConfig;
Expand All @@ -46,6 +47,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -77,6 +79,9 @@ public class GenerateBatch extends OpenApiGeneratorCommand {
@Option(name = {"--fail-fast"}, description = "fail fast on any errors")
private Boolean failFast;

@Option(name = {"--clean"}, description = "clean output of previously written files before generation")
private Boolean clean;

@Option(name = {"--timeout"}, description = "execution timeout (minutes)")
private Integer timeout;

Expand Down Expand Up @@ -149,7 +154,10 @@ public void execute() {
ExecutorService executor = Executors.newFixedThreadPool(numThreads);

// Execute each configurator on a separate pooled thread.
configurators.forEach(configurator -> executor.execute(new GenerationRunner(configurator, rootDir, Boolean.TRUE.equals(failFast))));
configurators.forEach(configurator -> {
GenerationRunner runner = new GenerationRunner(configurator, rootDir, Boolean.TRUE.equals(failFast), Boolean.TRUE.equals(clean));
executor.execute(runner);
});

executor.shutdown();

Expand Down Expand Up @@ -178,11 +186,13 @@ private static class GenerationRunner implements Runnable {
private final CodegenConfigurator configurator;
private final Path rootDir;
private final boolean exitOnError;
private final boolean clean;

private GenerationRunner(CodegenConfigurator configurator, Path rootDir, boolean failFast) {
private GenerationRunner(CodegenConfigurator configurator, Path rootDir, boolean failFast, boolean clean) {
this.configurator = configurator;
this.rootDir = rootDir;
this.exitOnError = failFast;
this.clean = clean;
}

/**
Expand All @@ -198,7 +208,7 @@ private GenerationRunner(CodegenConfigurator configurator, Path rootDir, boolean
*/
@Override
public void run() {
String name = "";
String name = null;
try {
GlobalSettings.reset();

Expand All @@ -210,6 +220,10 @@ public void run() {
Path updated = rootDir.resolve(target);
config.setOutputDir(updated.toString());

if (this.clean) {
cleanPreviousFiles(name, updated);
}

System.out.printf(Locale.ROOT, "[%s] Generating %s (outputs to %s)…%n", Thread.currentThread().getName(), name, updated.toString());

DefaultGenerator defaultGenerator = new DefaultGenerator();
Expand All @@ -234,6 +248,28 @@ public void run() {
GlobalSettings.reset();
}
}

private void cleanPreviousFiles(final String name, Path outDir) throws IOException {
System.out.printf(Locale.ROOT, "[%s] Cleaning previous contents for %s in %s…%n", Thread.currentThread().getName(), name, outDir.toString());
Path filesMeta = Paths.get(outDir.toAbsolutePath().toString(), ".openapi-generator", "FILES");
if (filesMeta.toFile().exists()) {
FileUtils.readLines(filesMeta.toFile(), StandardCharsets.UTF_8).forEach(relativePath -> {
if (!StringUtils.startsWith(relativePath, ".")) {
Path file = outDir.resolve(relativePath).toAbsolutePath();
// hack: disallow directory traversal outside of output directory. we don't want to delete wrong files.
if (file.toString().startsWith(outDir.toAbsolutePath().toString())) {
try {
Files.delete(file);
} catch (Throwable e) {
System.out.printf(Locale.ROOT, "[%s] Generator %s failed to clean file %s…%n", Thread.currentThread().getName(), name, file);
}
}
} else {
System.out.printf(Locale.ROOT, "[%s] Generator %s skip cleaning special filename %s…%n", Thread.currentThread().getName(), name, relativePath);
}
});
}
}
}

static SimpleModule getCustomDeserializationModel(final File includesDir) {
Expand Down