[#5556] Code generator should delete catalog and schema directories when no longer configured

This commit is contained in:
lukaseder 2016-09-22 12:13:42 +02:00
parent 1efb944322
commit cd1b76ad51
2 changed files with 28 additions and 11 deletions

View File

@ -607,18 +607,29 @@ abstract class AbstractGenerator implements Generator {
*/
protected void empty(File file, String suffix, Set<File> keep) {
if (file != null) {
// Just a Murphy's Law safeguard in case a user misconfigures their config...
if (file.getParentFile() == null) {
log.warn("WARNING: Root directory configured for code generation. Not deleting anything from previous generations!");
return;
}
if (file.isDirectory()) {
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
if (children != null)
for (File child : children)
empty(child, suffix, keep);
}
}
} else {
if (file.getName().endsWith(suffix) && !keep.contains(file)) {
File[] childrenAfterDeletion = file.listFiles();
// [#5556] Delete directory if empty after content was removed.
// Useful if a catalog / schema was dropped, or removed from code generation, or renamed
if (childrenAfterDeletion != null && childrenAfterDeletion.length == 0)
file.delete();
}
}
else if (file.getName().endsWith(suffix) && !keep.contains(file)) {
file.delete();
}
}
}

View File

@ -257,7 +257,10 @@ public class JavaGenerator extends AbstractGenerator {
// XXX Generating catalogs
// ----------------------------------------------------------------------
log.info("Generating catalogs", "Total: " + database.getCatalogs().size());
CatalogDefinition last = null;
for (CatalogDefinition catalog : database.getCatalogs()) {
last = catalog;
try {
if (generateCatalogIfEmpty(catalog))
generate(catalog);
@ -268,6 +271,13 @@ public class JavaGenerator extends AbstractGenerator {
throw new GeneratorException("Error generating code for catalog " + catalog, e);
}
}
// [#5556] Clean up common parent directory
if (last != null) {
log.info("Removing excess files");
empty(getStrategy().getFile(last).getParentFile(), (scala ? ".scala" : ".java"), files);
files.clear();
}
}
private boolean generateCatalogIfEmpty(CatalogDefinition catalog) {
@ -449,10 +459,6 @@ public class JavaGenerator extends AbstractGenerator {
log.info("Removing excess files");
empty(getStrategy().getFile(schema).getParentFile(), (scala ? ".scala" : ".java"), files);
files.clear();
// XXX [#651] Refactoring-cursor
watch.splitInfo("Generation finished: " + schema.getQualifiedName());
log.info("");