forked from loafle/openapi-generator-original
[batch] Option to clean files before generation (#7465)
This commit is contained in:
parent
1716ee3154
commit
6e937ba9f9
@ -11,6 +11,7 @@ if [ ! -f "$executable" ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
export JAVA_OPTS="${JAVA_OPTS} -ea -server -Duser.timezone=UTC"
|
export JAVA_OPTS="${JAVA_OPTS} -ea -server -Duser.timezone=UTC"
|
||||||
|
export BATCH_OPTS="${BATCH_OPTS:-}"
|
||||||
|
|
||||||
files=()
|
files=()
|
||||||
args=()
|
args=()
|
||||||
@ -61,6 +62,6 @@ else
|
|||||||
|
|
||||||
# shellcheck disable=SC2086
|
# shellcheck disable=SC2086
|
||||||
# shellcheck disable=SC2068
|
# 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
|
fi
|
||||||
|
|
||||||
|
@ -34,6 +34,7 @@ import com.fasterxml.jackson.databind.util.TokenBuffer;
|
|||||||
import io.airlift.airline.Arguments;
|
import io.airlift.airline.Arguments;
|
||||||
import io.airlift.airline.Command;
|
import io.airlift.airline.Command;
|
||||||
import io.airlift.airline.Option;
|
import io.airlift.airline.Option;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.openapitools.codegen.ClientOptInput;
|
import org.openapitools.codegen.ClientOptInput;
|
||||||
import org.openapitools.codegen.CodegenConfig;
|
import org.openapitools.codegen.CodegenConfig;
|
||||||
@ -46,6 +47,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
@ -77,6 +79,9 @@ public class GenerateBatch extends OpenApiGeneratorCommand {
|
|||||||
@Option(name = {"--fail-fast"}, description = "fail fast on any errors")
|
@Option(name = {"--fail-fast"}, description = "fail fast on any errors")
|
||||||
private Boolean failFast;
|
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)")
|
@Option(name = {"--timeout"}, description = "execution timeout (minutes)")
|
||||||
private Integer timeout;
|
private Integer timeout;
|
||||||
|
|
||||||
@ -149,7 +154,10 @@ public class GenerateBatch extends OpenApiGeneratorCommand {
|
|||||||
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
|
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
|
||||||
|
|
||||||
// Execute each configurator on a separate pooled thread.
|
// 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();
|
executor.shutdown();
|
||||||
|
|
||||||
@ -178,11 +186,13 @@ public class GenerateBatch extends OpenApiGeneratorCommand {
|
|||||||
private final CodegenConfigurator configurator;
|
private final CodegenConfigurator configurator;
|
||||||
private final Path rootDir;
|
private final Path rootDir;
|
||||||
private final boolean exitOnError;
|
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.configurator = configurator;
|
||||||
this.rootDir = rootDir;
|
this.rootDir = rootDir;
|
||||||
this.exitOnError = failFast;
|
this.exitOnError = failFast;
|
||||||
|
this.clean = clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -198,7 +208,7 @@ public class GenerateBatch extends OpenApiGeneratorCommand {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
String name = "";
|
String name = null;
|
||||||
try {
|
try {
|
||||||
GlobalSettings.reset();
|
GlobalSettings.reset();
|
||||||
|
|
||||||
@ -210,6 +220,10 @@ public class GenerateBatch extends OpenApiGeneratorCommand {
|
|||||||
Path updated = rootDir.resolve(target);
|
Path updated = rootDir.resolve(target);
|
||||||
config.setOutputDir(updated.toString());
|
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());
|
System.out.printf(Locale.ROOT, "[%s] Generating %s (outputs to %s)…%n", Thread.currentThread().getName(), name, updated.toString());
|
||||||
|
|
||||||
DefaultGenerator defaultGenerator = new DefaultGenerator();
|
DefaultGenerator defaultGenerator = new DefaultGenerator();
|
||||||
@ -234,6 +248,28 @@ public class GenerateBatch extends OpenApiGeneratorCommand {
|
|||||||
GlobalSettings.reset();
|
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) {
|
static SimpleModule getCustomDeserializationModel(final File includesDir) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user