package ca.mestevens.unity;

import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.List;

import static org.twdata.maven.mojoexecutor.MojoExecutor.*;

@Mojo(name = "unity-copy-test-dependencies", requiresDependencyResolution = ResolutionScope.TEST)
public class CopyTestDependenciesMojo extends AbstractMojo {

	@Component
	private MavenProject mavenProject;

	@Component
	private MavenSession mavenSession;

	@Component
	private BuildPluginManager pluginManager;

	@Parameter(property = "project.build.directory", required = true, readonly = true)
	private String projectBuildDirectory;

	@Parameter(property = "unity.plugins.directory", defaultValue = "Assets/Editor/Plugins", required = true, readonly = true)
	private String testPluginsDirectory;

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {

		executeMojo(
				plugin(
						groupId("org.apache.maven.plugins"),
						artifactId("maven-dependency-plugin"),
						version("2.0")
				),
				goal("copy-dependencies"),
				configuration(
						element(name("outputDirectory"), "${project.build.directory}/test-dependency"),
						element(name("excludeScope"), "compile")
				),
				executionEnvironment(
						mavenProject, mavenSession, pluginManager));

		final File testDependencyDirectoryFile = new File(String.format("%s/test-dependency", this.projectBuildDirectory));
		final File testPluginsDirectoryFile = new File(String.format("%s/%s", this.mavenProject.getBasedir(), this.testPluginsDirectory));

		try {
			final FileFilter fileFilter = new WildcardFileFilter("*.dll");
			final List<File> testDependencies = Lists.newArrayList(testDependencyDirectoryFile.listFiles(fileFilter));
			for (final File dependency : testDependencies) {
				this.getLog().info(String.format("Copying [%s] to [%s]", dependency.getName(), this.testPluginsDirectory));
				FileUtils.copyFileToDirectory(dependency, testPluginsDirectoryFile);
			}
		} catch (final IOException e) {
			this.getLog().error("Failed to copy dependencies", e);
			throw new MojoFailureException("Failed to copy dependencies", e);
		}

	}

}

@@ -2196,16 +2196,135 @@
 ory));%0A%0A
+%09%09if (!testDependencyDirectoryFile.exists()) %7B%0A%09%09%09this.getLog().info(%22No test dependencies detected%22);%0A%09%09%09return;%0A%09%09%7D%0A%0A
 %09%09try %7B%0A

Fix null pointer exception when there are no test dependencies