Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -43,10 +40,21 @@ public Stream<Module> stream() {
}

public Module getRootModule() {
// 1st try: module explicitly marked as having a root build file
return modules.stream()
.filter(m -> m.getBuildFile().isRootBuildFile())
.findFirst()
.orElseThrow(() -> new RootBuildFileNotFoundException("Module with root build file is missing"));
// 2nd try (fallback): no module marked as root → choose the one
// whose build file path is closest to the project root
.orElseGet(() -> modules.stream()
.min(Comparator.comparingInt(m -> pathDepth(m.getBuildFile())))
.orElseThrow(() -> new RootBuildFileNotFoundException("Module with root build file is missing")));
}

private int pathDepth(BuildFile buildFile) {
Path path = buildFile.getSourcePath();
// if for some reason there is no path, treat it as "very deep"
return (path == null) ? Integer.MAX_VALUE : path.getNameCount();
}

public List<Module> list() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,42 @@
*/
package org.springframework.sbm.build.api;

import org.jetbrains.annotations.NotNull;
import org.springframework.sbm.project.resource.ProjectResourceSet;
import org.springframework.sbm.project.resource.filter.ProjectResourceFinder;

import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class RootBuildFileFilter implements ProjectResourceFinder<BuildFile> {

@Override
public BuildFile apply(ProjectResourceSet projectResourceSet) {
return projectResourceSet.stream()
public BuildFile apply(@NotNull ProjectResourceSet projectResourceSet) {
// collect all build files (pom.xml, etc.)
List<BuildFile> buildFiles = projectResourceSet.stream()
.filter(pr -> BuildFile.class.isAssignableFrom(pr.getClass()))
.map(BuildFile.class::cast)
.filter(bf -> bf.isRootBuildFile())
.collect(Collectors.toList());

if (buildFiles.isEmpty()) {
throw new RootBuildFileNotFoundException("Could not find any BuildFile in project.");
}

// 1st try: existing logic – respect explicit isRootBuildFile flag
return buildFiles.stream()
.filter(BuildFile::isRootBuildFile)
.findFirst()
.orElseThrow(() -> new RootBuildFileNotFoundException("Could not find BuildFile for root module."));
// 2nd try (fallback): no explicit root → choose the build file
// whose source path is closest to the project root (smallest depth)
.orElseGet(() -> buildFiles.stream()
.min(Comparator.comparingInt(bf -> pathDepth(bf.getSourcePath())))
.orElseThrow(() -> new RootBuildFileNotFoundException("Could not find BuildFile for root module.")));
}

private int pathDepth(Path path) {
// defensive: null check, though OpenRewrite usually always has a path
return (path == null) ? Integer.MAX_VALUE : path.getNameCount();
}
}
}
Loading