// 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;
        return mvnRun("releaser:next", buildNumber, arguments);
    }

    public TestProject commitRandomFile(String module) throws IOException, GitAPIException {
        File moduleDir = new File(localDir, module);
        if (!moduleDir.isDirectory()) {
            throw new RuntimeException("Could not find " + moduleDir.getCanonicalPath());
        }
        File random = new File(moduleDir, UUID.randomUUID() + ".txt");
        random.createNewFile();
        String modulePath = module.equals(".") ? "" : module + "/";
        local.add().addFilepattern(modulePath + random.getName()).call();
        local.commit().setMessage("Commit " + commitCounter.getAndIncrement() + ": adding " + random.getName()).call();
        return this;
    }

    public void pushIt() throws GitAPIException {
        local.push().call();
    }

    private List<String> mvnRun(String goal, String buildNumber, String[] arguments) {
        String[] args = new String[arguments.length + 2];
        args[0] = "-DbuildNumber=" + buildNumber;
        System.arraycopy(arguments, 0, args, 1, arguments.length);
        args[args.length-1] = goal;
        return mvnRunner.runMaven(localDir, args);
    }

    private static TestProject project(String name) {
        try {
// 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;

        return mvnRun("releaser:next", buildNumber, arguments);
    }

    public TestProject commitRandomFile(String module) throws IOException, GitAPIException {
        File moduleDir = new File(localDir, module);
        if (!moduleDir.isDirectory()) {
            throw new RuntimeException("Could not find " + moduleDir.getCanonicalPath());
        }
        File random = new File(moduleDir, UUID.randomUUID() + ".txt");
        random.createNewFile();
        String modulePath = module.equals(".") ? "" : module + "/";
        local.add().addFilepattern(modulePath + random.getName()).call();
        local.commit().setMessage("Commit " + commitCounter.getAndIncrement() + ": adding " + random.getName()).call();
        return this;
    }

    public void pushIt() throws GitAPIException {
        local.push().call();
    }

    private List<String> mvnRun(String goal, String buildNumber, String[] arguments) {
        String[] args = new String[arguments.length + 2];
        args[0] = "-DbuildNumber=" + buildNumber;
        System.arraycopy(arguments, 0, args, 1, arguments.length);
        args[args.length-1] = goal;
        return mvnRunner.runMaven(localDir, args);
    }

    private static TestProject project(String name) {
        try {
            File originDir = copyTestProjectToTemporaryLocation(name);