/*
 * Copyright 2019, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.tools.compiler;

import io.spine.logging.Logging;
import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;

/**
 * Custom {@linkplain java.nio.file.FileVisitor FileVisitor} which recursively deletes
 * the contents of the walked folder.
 */
@SuppressWarnings("RefusedBequest")
// As we define a completely different behavior for the visitor methods.
public class DirectoryCleaner extends SimpleFileVisitor<Path> {

    public static void deleteDirs(List<String> dirs) {
        for (String dirPath : dirs) {
            File file = new File(dirPath);
            if (file.exists() && file.isDirectory()) {
                deleteRecursively(file.toPath());
            } else {
                String msg = "Trying to delete '{}' which is not a directory";
                log().warn(msg, file.getAbsolutePath());
            }
        }
    }

    private static void deleteRecursively(Path path) {
        try {
            SimpleFileVisitor<Path> visitor = new DirectoryCleaner();
            log().debug("Starting to delete the files recursively in {}", path.toString());
            Files.walkFileTree(path, visitor);
        } catch (IOException e) {
            throw new IllegalStateException(
                    "Failed to delete the folder with its contents: " + path, e);
        }
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        logDeletionOf(file);
        Files.delete(file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
        logDeletionOf(file);
        Files.delete(file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
        if (e == null) {
            logDeletionOf(dir);
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        } else {
            throw e;
        }
    }

    private static void logDeletionOf(Path file) {
        log().debug("Deleting file {}", file.toString());
    }

    private static Logger log() {
        return Logging.get(DirectoryCleaner.class);
    }
}

@@ -1825,42 +1825,66 @@
 ile.
-exists() && file.isDirectory
+isDirectory()) %7B%0A                if (file.exists
 ()) %7B%0A
+    
     
@@ -1921,32 +1921,50 @@
 file.toPath());%0A
+                %7D%0A
             %7D el

Fix wrong diagnostics when deleting directories