diff --git a/habushu-maven-plugin/src/main/java/org/technologybrewery/habushu/StageDependenciesMojo.java b/habushu-maven-plugin/src/main/java/org/technologybrewery/habushu/StageDependenciesMojo.java index 43fc512..62a2be0 100644 --- a/habushu-maven-plugin/src/main/java/org/technologybrewery/habushu/StageDependenciesMojo.java +++ b/habushu-maven-plugin/src/main/java/org/technologybrewery/habushu/StageDependenciesMojo.java @@ -1,15 +1,15 @@ package org.technologybrewery.habushu; import org.apache.commons.io.FileUtils; -import org.apache.maven.Maven; +import org.apache.commons.io.filefilter.FileFileFilter; +import org.apache.commons.io.filefilter.FileFilterUtils; +import org.apache.commons.io.filefilter.IOFileFilter; import org.apache.maven.execution.MavenSession; -import org.apache.maven.model.Dependency; 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.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,7 +38,8 @@ public class StageDependenciesMojo extends AbstractHabushuMojo { @Component protected MavenSession session; - protected File anchorDirectory; + protected File anchorSourceDirectory; + protected File anchorOutputDirectory; protected final String HABUSHU = "habushu"; @@ -53,12 +54,55 @@ protected void setSession(MavenSession session) { @Override protected void doExecute() throws MojoExecutionException, MojoFailureException { Set habushuProjects = getHabushuProjects(); - this.anchorDirectory = calculateAnchorDirectory(habushuProjects); - // copy relevant(anchorDirectory, habushuProjects) + this.anchorSourceDirectory = calculateAnchorDirectory(habushuProjects); + try { + copyRelevant(anchorSourceDirectory, habushuProjects); + } catch (Exception e) { + throw new MojoExecutionException(""); + } String s = ""; s += "asdf"; } + private void copyRelevant(File anchorDirectory, Set habushuProjects) throws IOException { + File destDir = new File(getSession().getCurrentProject().getBuild().getDirectory() + "/venv-support"); + setAnchorOutputDirectory(destDir); + + Set habushuDirsFilters = new HashSet<>(); + for (MavenProject project : habushuProjects) { + habushuDirsFilters.add(FileFilterUtils.nameFileFilter(project.getFile().getParentFile().getName())); + } + + + + + // Define what to include + IOFileFilter whiteListFilter = FileFilterUtils.or( + FileFilterUtils.nameFileFilter("poetry.lock"), + FileFilterUtils.nameFileFilter("pyproject.toml"), + FileFilterUtils.nameFileFilter("README.md") + ); + + IOFileFilter whiteListDirFilter = FileFilterUtils.or( + habushuDirsFilters.toArray(new IOFileFilter[0]) + ); + + + // Get the files to be copied + Collection filesToCopy = FileUtils.listFilesAndDirs(anchorDirectory, FileFilterUtils.trueFileFilter(), whiteListDirFilter); + + // Copy the filtered files and directories + for (File file : filesToCopy) { + File destFile = new File(destDir, anchorDirectory.toURI().relativize(file.toURI()).getPath()); + if (file.isDirectory()) { + FileUtils.forceMkdir(destFile); + } else { + FileUtils.copyFile(file, destFile); + } + } + + } + protected File calculateAnchorDirectory(Set habushuProjects) { ArrayList habushuDirectories = new ArrayList<>(); for (MavenProject project : habushuProjects) { @@ -85,7 +129,7 @@ private void mockSuceess() { + "/default-single-monorepo-dep/test-monorepo/extensions/extensions-monorepo-dep-consuming-application" + "/target/venv-support/test-monorepo" ); - setAnchorDirectory(destination); + setAnchorSourceDirectory(destination); try { FileUtils.copyDirectory(source, destination); } catch (IOException e) { @@ -101,11 +145,19 @@ public void execute() throws MojoExecutionException, MojoFailureException { doExecute(); } - public File getAnchorDirectory() { - return anchorDirectory; + public File getAnchorSourceDirectory() { + return anchorSourceDirectory; + } + + protected void setAnchorSourceDirectory(File anchorSourceDirectory) { + this.anchorSourceDirectory = anchorSourceDirectory; + } + + public File getAnchorOutputDirectory() { + return anchorOutputDirectory; } - protected void setAnchorDirectory(File anchorDirectory) { - this.anchorDirectory = anchorDirectory; + protected void setAnchorOutputDirectory(File anchorOutputDirectory) { + this.anchorOutputDirectory = anchorOutputDirectory; } } diff --git a/habushu-maven-plugin/src/test/java/org/technologybrewery/habushu/StageDependenciesMojoTestCase.java b/habushu-maven-plugin/src/test/java/org/technologybrewery/habushu/StageDependenciesMojoTestCase.java index 1793279..7db4e68 100644 --- a/habushu-maven-plugin/src/test/java/org/technologybrewery/habushu/StageDependenciesMojoTestCase.java +++ b/habushu-maven-plugin/src/test/java/org/technologybrewery/habushu/StageDependenciesMojoTestCase.java @@ -22,6 +22,7 @@ import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; /** * Wraps the default behavior provided by the Maven plugin testing harness through {@link AbstractMojoTestCase} to @@ -34,6 +35,8 @@ public class StageDependenciesMojoTestCase extends AbstractMojoTestCase { private static final Logger logger = LoggerFactory.getLogger(StageDependenciesMojoTestCase.class); + private List mavenProjectFiles = new ArrayList<>(); + public void configurePluginTestHarness() throws Exception { super.setUp(); } @@ -94,26 +97,32 @@ public MavenSession newDefaultMavenSession() { @Override protected MavenSession newMavenSession(MavenProject project) { MavenSession session = newDefaultMavenSession(); + ArrayList mavenProjects = new ArrayList<>(); - MavenProject depXProject; - MavenProject depYProject; - MavenProject depZProject; try { ProjectBuilder projectBuilder = lookup(ProjectBuilder.class); - File depX = new File("src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/extensions/extensions-python-dep-X/pom.xml"); - File depY = new File("src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-python-dep-Y/pom.xml"); - File depZ = new File("src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-sub/foundation-sub-python-dep-Z/pom.xml"); - depXProject = projectBuilder.build(depX, session.getProjectBuildingRequest()).getProject(); - depYProject = projectBuilder.build(depY, session.getProjectBuildingRequest()).getProject(); - depZProject = projectBuilder.build(depZ, session.getProjectBuildingRequest()).getProject(); + + for(File mavenProjectFile: this.mavenProjectFiles) { + mavenProjects.add(projectBuilder.build(mavenProjectFile, session.getProjectBuildingRequest()).getProject()); + } } catch (Exception e) { throw new RuntimeException(e); } - session.setProjects(Arrays.asList(project, depXProject, depYProject, depZProject)); + + session.setProjects(mavenProjects); session.setCurrentProject(project); + return session; } + public void addMavenProjectFile(File mavenProjectFile) { + this.mavenProjectFiles.add(mavenProjectFile); + } + + public void clearMavenProjectFiles() { + this.mavenProjectFiles = new ArrayList<>(); + } + /** * Overloads super's {@link #lookupConfiguredMojo(MavenProject, String)} * to ingest the given {@code pom.xml} {@link File} instead of a {@link MavenProject}. @@ -150,4 +159,6 @@ public Mojo lookupConfiguredMojo(File pom, String goal) throws Exception { return mojo; } + + } diff --git a/habushu-maven-plugin/src/test/java/org/technologybrewery/habushu/StageDependenciesSteps.java b/habushu-maven-plugin/src/test/java/org/technologybrewery/habushu/StageDependenciesSteps.java index 324c344..00a4f3c 100644 --- a/habushu-maven-plugin/src/test/java/org/technologybrewery/habushu/StageDependenciesSteps.java +++ b/habushu-maven-plugin/src/test/java/org/technologybrewery/habushu/StageDependenciesSteps.java @@ -13,6 +13,7 @@ import java.io.*; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -41,12 +42,17 @@ public void configureMavenTestSession() throws Exception { @After("@stageDependencies") public void tearDownMavenPluginTestHarness() throws Exception { + mojoTestCase.clearMavenProjectFiles(); mojoTestCase.tearDownPluginTestHarness(); } @Given("a single Habushu-type dependency") public void a_single_habushu_type_dependency() throws Exception { mavenProjectPath = targetDefaultSingleMonorepoDepPath + "/extensions/extensions-monorepo-dep-consuming-application"; + + mojoTestCase.addMavenProjectFile(new File("src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/extensions/extensions-python-dep-X/pom.xml")); + mojoTestCase.addMavenProjectFile(new File("src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-python-dep-Y/pom.xml")); + mojo = (StageDependenciesMojo) mojoTestCase.lookupConfiguredMojo( new File(mavenProjectPath, POM_FILE), "stage-dependencies" ); @@ -63,7 +69,7 @@ public void the_source_files_of_the_dependency_and_transitive_habushu_type_depen expectedHabushuDirsNames.add("extensions/extensions-python-dep-X"); expectedHabushuDirsNames.add("foundation/foundation-python-dep-Y"); expectedHabushuDirsNames.add("foundation/foundation-sub/foundation-sub-python-dep-Z"); - assertStaged(new File(testDefaultSingleMonorepoDepPath), mojo.getAnchorDirectory(), expectedHabushuDirsNames); + assertStaged(new File(testDefaultSingleMonorepoDepPath), mojo.getAnchorOutputDirectory(), expectedHabushuDirsNames); } @Given("no Habushu-type dependencies") @@ -99,54 +105,41 @@ private boolean isNonExistentOrEmptyDir(File dir) { private void assertStaged(File expectedRoot, File actual, Set expectedHabushuDirNames) { try { // find the subdirectory of the source that matches the stage anchor of the mojo - File expected = getMatchingDir(actual.getName(), expectedRoot); + // File expected = getMatchingDir(actual.getName(), expectedRoot); // relativize - Map expectedFiles = getRelativizedContentsMap(expected); - Map actualFiles = getRelativizedContentsMap(actual); + Set actualFiles = getRelativizedPaths(actual); - boolean stagingAccurate = true; - Assertions.assertTrue(actualFiles.keySet().containsAll(expectedHabushuDirNames), - "The expected set of Habushu-type dependencies were not staged."); - for (Map.Entry entry : actualFiles.entrySet()) { - String relativePath = entry.getKey(); - File actualFile = entry.getValue(); - File expectedFile = expectedFiles.get(relativePath); + assertFile(actualFiles, "test-monorepo/extensions/extensions-python-dep-X/src/python_dep_x/python_dep_x.py"); + assertFile(actualFiles, "test-monorepo/extensions/extensions-python-dep-X/pyproject.toml"); + assertFile(actualFiles, "test-monorepo/extensions/extensions-python-dep-X/README.md"); + assertFile(actualFiles, "test-monorepo/foundation/foundation-python-dep-Y/src/python_dep_x/python_dep_y.py"); + assertFile(actualFiles, "test-monorepo/foundation/foundation-python-dep-Y/pyproject.toml"); + assertFile(actualFiles, "test-monorepo/foundation/foundation-python-dep-Y/README.md"); - if (expectedFile == null) { - stagingAccurate = false; - } - - if ((expectedFile != null && expectedFile.isFile()) && (actualFile != null && actualFile.isFile())) { - if (!filesHaveSameContent(expectedFile, actualFile)) { - stagingAccurate = false; - } - } - - } - Assertions.assertTrue(stagingAccurate, - "Expected file contents to be the same, but they were not."); } catch (Exception e) { throw new RuntimeException(); } } + private static void assertFile(Set actualFiles, String path) { + Assertions.assertTrue(actualFiles.contains(Paths.get(path)), "Could not find: " + path); + } + private boolean filesHaveSameContent(File expectedFile, File actualFile) throws IOException { InputStream expectedStream = new FileInputStream(expectedFile); InputStream actualStream = new FileInputStream(actualFile); return IOUtils.contentEquals(expectedStream, actualStream); } - private Map getRelativizedContentsMap(File rootDir) throws IOException { + private Set getRelativizedPaths(File rootDir) throws IOException { try (Stream paths = Files.walk(rootDir.toPath())) { return paths - .filter(path -> Files.isRegularFile(path) || Files.isDirectory(path)) - .collect(Collectors.toMap( - path -> rootDir.toPath().relativize(path).toString(), - Path::toFile - )); + .filter(Files::isRegularFile) + .map(rootDir.toPath()::relativize) + .collect(Collectors.toSet()); } } diff --git a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/extensions/extensions-python-dep-X/README.md b/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/extensions/extensions-python-dep-X/README.md new file mode 100644 index 0000000..e69de29 diff --git a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/extensions/extensions-python-dep-X/other/unneeded-file.txt b/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/extensions/extensions-python-dep-X/other/unneeded-file.txt new file mode 100644 index 0000000..e69de29 diff --git a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-python-dep-Y/README.md b/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-python-dep-Y/README.md new file mode 100644 index 0000000..e69de29 diff --git a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-python-dep-Y/pom.xml b/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-python-dep-Y/pom.xml index 35c3f29..a428d4a 100644 --- a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-python-dep-Y/pom.xml +++ b/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-python-dep-Y/pom.xml @@ -27,13 +27,6 @@ habushu-maven-plugin 2.15.0 - - - org.test.monorepo - foundation-sub-python-dep-Z - ${project.version} - habushu - diff --git a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-sub/foundation-sub-python-dep-Z/pom.xml b/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-sub/foundation-sub-python-dep-Z/pom.xml deleted file mode 100644 index 566f9f4..0000000 --- a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-sub/foundation-sub-python-dep-Z/pom.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - 4.0.0 - - Foundation::Sub::Python Dependency Z - org.test.monorepo - foundation-sub-python-dep-Z - 1.0.0-SNAPSHOT - - habushu - - - - - org.technologybrewery.habushu - habushu-maven-plugin - 2.15.0 - true - - - - - - - org.technologybrewery.habushu - habushu-maven-plugin - 2.15.0 - - - - \ No newline at end of file diff --git a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-sub/foundation-sub-python-dep-Z/pyproject.toml b/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-sub/foundation-sub-python-dep-Z/pyproject.toml deleted file mode 100644 index f017125..0000000 --- a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-sub/foundation-sub-python-dep-Z/pyproject.toml +++ /dev/null @@ -1,23 +0,0 @@ -[tool.poetry] -name = "extensions-python-dep-Z" -version = "1.0.0.dev" -description = "description" -authors = ["author"] -packages = [ - {include = "python_dep_z", from = "src"}, -] - -[tool.poetry.dependencies] -python = ">=3.11.4, <4" - -[tool.poetry.group.dev.dependencies] -black = ">=22.1.0" -behave = ">=1.2.6" -nose = ">=1.3.7" -pylint = "^3.2.3" - - - -[build-system] -requires = ["poetry-core>=1.7.0"] -build-backend = "poetry.core.masonry.api" \ No newline at end of file diff --git a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-sub/foundation-sub-python-dep-Z/src/python_dep_Z/python_dep_z.py b/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-sub/foundation-sub-python-dep-Z/src/python_dep_Z/python_dep_z.py deleted file mode 100644 index fdffa2a..0000000 --- a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-sub/foundation-sub-python-dep-Z/src/python_dep_Z/python_dep_z.py +++ /dev/null @@ -1 +0,0 @@ -# placeholder diff --git a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-sub/pom.xml b/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-sub/pom.xml deleted file mode 100644 index 8a0aed9..0000000 --- a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/foundation-sub/pom.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - 4.0.0 - - Foundation::Sub - org.test.monorepo - foundation-sub - 1.0.0-SNAPSHOT - - pom - - - foundation-sub-python-dep-Z - - - \ No newline at end of file diff --git a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/pom.xml b/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/pom.xml index 0231f8e..c6e39f3 100644 --- a/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/pom.xml +++ b/habushu-maven-plugin/src/test/resources/stage-dependencies/default-single-monorepo-dep/test-monorepo/foundation/pom.xml @@ -12,7 +12,6 @@ foundation-python-dep-Y - foundation-sub \ No newline at end of file