// Path: src/main/java/com/github/danielflower/mavenplugins/release/FileUtils.java
// public static String pathOf(File file) {
//     String path;
//     try {
//         path = file.getCanonicalPath();
//     } catch (IOException e1) {
//         path = file.getAbsolutePath();
//     }
//     return path;
// }
// 
// Path: src/test/java/scaffolding/Photocopier.java
// public static File copyTestProjectToTemporaryLocation(String moduleName) throws IOException {
//     File source = new File("test-projects", moduleName);
//     if (!source.isDirectory()) {
//         source = new File(FilenameUtils.separatorsToSystem("../test-projects/" + moduleName));
//     }
//     if (!source.isDirectory()) {
//         throw new RuntimeException("Could not find module " + moduleName);
//     }
// 
//     File target = folderForSampleProject(moduleName);
//     FileUtils.copyDirectory(source, target);
//     return target;
// }


import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.InitCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import static com.github.danielflower.mavenplugins.release.FileUtils.pathOf;
import static scaffolding.Photocopier.copyTestProjectToTemporaryLocation;
                .setDirectory(localDir)
                .setURI(originDir.toURI().toString())
                .call();

            return new TestProject(originDir, origin, localDir, local);
        } catch (Exception e) {
            throw new RuntimeException("Error while creating copies of the test project", e);
        }
    }

    public static void performPomSubstitution(File sourceDir) throws IOException {
        File pom = new File(sourceDir, "pom.xml");
        if (pom.exists()) {
            String xml = FileUtils.readFileToString(pom, "UTF-8");
            if (xml.contains("${scm.url}")) {
                xml = xml.replace("${scm.url}", dirToGitScmReference(sourceDir));
            }
            xml = xml.replace("${current.plugin.version}", PLUGIN_VERSION_FOR_TESTS);
            FileUtils.writeStringToFile(pom, xml, "UTF-8");
        }
        for (File child : sourceDir.listFiles((FileFilter) FileFilterUtils.directoryFileFilter())) {
            performPomSubstitution(child);
        }
    }

    public static TestProject localPluginProject() {
        return project("local-plugin");
    }

    public static String dirToGitScmReference(File sourceDir) {
// Path: src/main/java/com/github/danielflower/mavenplugins/release/FileUtils.java
// public static String pathOf(File file) {
//     String path;
//     try {
//         path = file.getCanonicalPath();
//     } catch (IOException e1) {
//         path = file.getAbsolutePath();
//     }
//     return path;
// }
// 
// Path: src/test/java/scaffolding/Photocopier.java
// public static File copyTestProjectToTemporaryLocation(String moduleName) throws IOException {
//     File source = new File("test-projects", moduleName);
//     if (!source.isDirectory()) {
//         source = new File(FilenameUtils.separatorsToSystem("../test-projects/" + moduleName));
//     }
//     if (!source.isDirectory()) {
//         throw new RuntimeException("Could not find module " + moduleName);
//     }
// 
//     File target = folderForSampleProject(moduleName);
//     FileUtils.copyDirectory(source, target);
//     return target;
// }


// Path: src/test/java/scaffolding/TestProject.java
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.InitCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import static com.github.danielflower.mavenplugins.release.FileUtils.pathOf;
import static scaffolding.Photocopier.copyTestProjectToTemporaryLocation;

                .setDirectory(localDir)
                .setURI(originDir.toURI().toString())
                .call();

            return new TestProject(originDir, origin, localDir, local);
        } catch (Exception e) {
            throw new RuntimeException("Error while creating copies of the test project", e);
        }
    }

    public static void performPomSubstitution(File sourceDir) throws IOException {
        File pom = new File(sourceDir, "pom.xml");
        if (pom.exists()) {
            String xml = FileUtils.readFileToString(pom, "UTF-8");
            if (xml.contains("${scm.url}")) {
                xml = xml.replace("${scm.url}", dirToGitScmReference(sourceDir));
            }
            xml = xml.replace("${current.plugin.version}", PLUGIN_VERSION_FOR_TESTS);
            FileUtils.writeStringToFile(pom, xml, "UTF-8");
        }
        for (File child : sourceDir.listFiles((FileFilter) FileFilterUtils.directoryFileFilter())) {
            performPomSubstitution(child);
        }
    }

    public static TestProject localPluginProject() {
        return project("local-plugin");
    }

    public static String dirToGitScmReference(File sourceDir) {
        return "scm:git:file://localhost/" + pathOf(sourceDir).replace('\\', '/').toLowerCase();