Skip to content

Commit

Permalink
TechnologyBrewery#146 potential way of filtering and copying
Browse files Browse the repository at this point in the history
  • Loading branch information
Cho-William committed Jun 21, 2024
1 parent c17f4a6 commit c963ee1
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 132 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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";

Expand All @@ -53,12 +54,55 @@ protected void setSession(MavenSession session) {
@Override
protected void doExecute() throws MojoExecutionException, MojoFailureException {
Set<MavenProject> 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<MavenProject> habushuProjects) throws IOException {
File destDir = new File(getSession().getCurrentProject().getBuild().getDirectory() + "/venv-support");
setAnchorOutputDirectory(destDir);

Set<IOFileFilter> 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<File> 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<MavenProject> habushuProjects) {
ArrayList<String> habushuDirectories = new ArrayList<>();
for (MavenProject project : habushuProjects) {
Expand All @@ -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) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,6 +35,8 @@ public class StageDependenciesMojoTestCase extends AbstractMojoTestCase {

private static final Logger logger = LoggerFactory.getLogger(StageDependenciesMojoTestCase.class);

private List<File> mavenProjectFiles = new ArrayList<>();

public void configurePluginTestHarness() throws Exception {
super.setUp();
}
Expand Down Expand Up @@ -94,26 +97,32 @@ public MavenSession newDefaultMavenSession() {
@Override
protected MavenSession newMavenSession(MavenProject project) {
MavenSession session = newDefaultMavenSession();
ArrayList<MavenProject> 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}.
Expand Down Expand Up @@ -150,4 +159,6 @@ public Mojo lookupConfiguredMojo(File pom, String goal) throws Exception {
return mojo;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"
);
Expand All @@ -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")
Expand Down Expand Up @@ -99,54 +105,41 @@ private boolean isNonExistentOrEmptyDir(File dir) {
private void assertStaged(File expectedRoot, File actual, Set<String> 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<String, File> expectedFiles = getRelativizedContentsMap(expected);
Map<String, File> actualFiles = getRelativizedContentsMap(actual);
Set<Path> 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<String, File> 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<Path> 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<String, File> getRelativizedContentsMap(File rootDir) throws IOException {
private Set<Path> getRelativizedPaths(File rootDir) throws IOException {
try (Stream<Path> 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());
}
}

Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@
<artifactId>habushu-maven-plugin</artifactId>
<version>2.15.0</version>
</dependency>

<dependency>
<groupId>org.test.monorepo</groupId>
<artifactId>foundation-sub-python-dep-Z</artifactId>
<version>${project.version}</version>
<type>habushu</type>
</dependency>
</dependencies>


Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

<modules>
<module>foundation-python-dep-Y</module>
<module>foundation-sub</module>
</modules>

</project>

0 comments on commit c963ee1

Please sign in to comment.