/*
 * Copyright 2020, 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.gradle.testing;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

import static com.google.common.base.Preconditions.checkState;
import static java.nio.file.Files.exists;

/**
 * Finds a root of a project by presence of the {@link #VERSION_GRADLE_NAME version.gradle.kts}
 * file.
 *
 * <p>Starts from the current directory, climbing up, until the file is found. By convention
 * a project should have only one {@link #VERSION_GRADLE_NAME version.gradle.kts} file, which is
 * placed in the root directory of the project.
 */
enum ProjectRoot {

    INSTANCE;

    private static final String VERSION_GRADLE_NAME = "version.gradle.kts";

    static ProjectRoot instance() {
        return INSTANCE;
    }

    /**
     * Obtains a root directory of the project.
     *
     * @throws IllegalStateException
     *         if the {@link #VERSION_GRADLE_NAME version.gradle.kts} file is not found
     */
    Path toPath() {
        Path workingFolderPath = Paths.get(".")
                                      .toAbsolutePath();
        Path extGradleDirPath = workingFolderPath;
        while (extGradleDirPath != null
                && !exists(extGradleDirPath.resolve(VERSION_GRADLE_NAME))) {
            extGradleDirPath = extGradleDirPath.getParent();
        }
        checkState(extGradleDirPath != null,
                   "%s file not found in %s or parent directories.",
                   VERSION_GRADLE_NAME,
                   workingFolderPath);
        return extGradleDirPath;
    }

    /**
     * Obtains root directory of the project.
     *
     * @throws IllegalStateException
     *         if the {@link #VERSION_GRADLE_NAME version.gradle.kts} file is not found
     * @see #toPath()
     */
    File toFile() {
        return toPath().toFile();
    }
}

@@ -1295,37 +1295,81 @@
  #VERSION_GRADLE
-_NAME
+ version.gradle%7D or%0A * %7B@link #VERSION_GRADLE_KTS
  version.gradle.
@@ -1372,19 +1372,16 @@
 dle.kts%7D
-%0A *
  file.%0A 
@@ -1513,55 +1513,15 @@
 one 
-%7B@link #VERSION_GRADLE_NAME version.gradle.kts%7D
+version
  fil
@@ -1531,19 +1531,16 @@
 which is
-%0A *
  placed 
@@ -1555,32 +1555,35 @@
 oot directory of
+%0A *
  the project.%0A *
@@ -1665,21 +1665,87 @@
 N_GRADLE
-_NAME
+ = %22version.gradle%22;%0A    private static final String VERSION_GRADLE_KTS
  = %22vers
@@ -1966,37 +1966,32 @@
  #VERSION_GRADLE
-_NAME
  version.gradle.
@@ -2240,81 +2240,45 @@
 null
-%0A                && !exists(extGradleDirPath.resolve(VERSION_GRADLE_NAME)
+ && hasVersionGradle(extGradleDirPath
 )) %7B
@@ -2418,19 +2418,29 @@
    %22
-%25s file not
+Neither %60%25s%60 nor %60%25s%60
  fou
@@ -2445,18 +2445,20 @@
 ound in 
+%60
 %25s
+%60
  or pare
@@ -2508,21 +2508,55 @@
 N_GRADLE
-_NAME
+,%0A                   VERSION_GRADLE_KTS
 ,%0A      
@@ -2620,32 +2620,223 @@
 DirPath;%0A    %7D%0A%0A
+    private static boolean hasVersionGradle(Path directory) %7B%0A        return exists(directory.resolve(VERSION_GRADLE)) %7C%7C%0A               exists(directory.resolve(VERSION_GRADLE_KTS));%0A    %7D%0A%0A
     /**%0A     * O
@@ -2965,13 +2965,8 @@
 ADLE
-_NAME
  ver

Allow using Groovy for `version.gradle` in test projects