Skip to content
Snippets Groups Projects

Entferne OpenAPI-Modul

Merged Martin Vogel requested to merge remove-open-api-module into main
5 files
+ 0
269
Compare changes
  • Side-by-side
  • Inline
Files
5
package de.fitko.fitconnect.api.postprocessing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ModelClassPostProcessor {
private static final Logger logger = LoggerFactory.getLogger(ModelClassPostProcessor.class);
public static void main(final String[] args) {
final ModelClassPostProcessor postProcessor = new ModelClassPostProcessor();
final var dir = args[0]; // -modelPath
postProcessor.readModelsToStrings(dir)
.stream()
.map(ModelClassPostProcessor::replaceContent)
//.peek(pair -> logger.info(pair.content))
.forEach(ModelClassPostProcessor::writeFiles);
}
private static ModelPair replaceContent(final ModelPair pair) {
String content = pair.content;
// Remove all Comments
content = content.replaceAll("\\/\\*([\\S\\s]+?)\\*\\/", "");
content = content.replaceAll("(?s)/\\*.*?\\*/", "");
// Remove unwanted annos
content = content.replaceAll("@ApiModel([\\w]+)((.*))", "");
content = content.replaceAll("@ApiModelProperty([\\w]+)((.*))", "");
content = content.replaceAll("Generated([\\w]+)((.*))", "");
// Replace javax
//content = content.replaceAll("Nonnull([\\w]+)((.*))", "");
//content = content.replaceAll("(@\([\\w]+)\\((.*?)\\)(?:\s|$))", "");
return new ModelPair(pair.originalPath, content);
}
public Set<ModelPair> readModelsToStrings(final String dir) {
return Stream.of(new File(dir).listFiles())
.filter(file -> !file.isDirectory())
.peek(file -> logger.info("Postprocessing model {}", file.getName()))
.map(file -> new ModelPair(file.toPath(), getFileAsString(file)))
.collect(Collectors.toSet());
}
private String getFileAsString(final File file) {
try {
return Files.readString(file.toPath(), StandardCharsets.UTF_8);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
private static void writeFiles(final ModelPair modelPair) {
try {
final File f = new File(modelPair.originalPath.toFile().getAbsolutePath());
final FileWriter fw = new FileWriter( f, false);
fw.write(modelPair.content);
fw.flush();
fw.close();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
private static class ModelPair {
final Path originalPath;
final String content;
ModelPair(final Path originalPath, final String content) {
this.originalPath = originalPath;
this.content = content;
}
@Override
public String toString() {
return content;
}
}
}
Loading