Skip to content

Commit

Permalink
GH-438: Rename source management components to be more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
ascopes committed Nov 9, 2024
1 parent 1ef2c21 commit de27eda
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import io.github.ascopes.protobufmavenplugin.protoc.ArgLineBuilder;
import io.github.ascopes.protobufmavenplugin.protoc.CommandLineExecutor;
import io.github.ascopes.protobufmavenplugin.protoc.ProtocResolver;
import io.github.ascopes.protobufmavenplugin.sources.ProtoFileFilter;
import io.github.ascopes.protobufmavenplugin.sources.ProtoFileListing;
import io.github.ascopes.protobufmavenplugin.sources.ProtoSourceResolver;
import io.github.ascopes.protobufmavenplugin.sources.SourceGlobFilter;
import io.github.ascopes.protobufmavenplugin.sources.SourceListing;
import io.github.ascopes.protobufmavenplugin.sources.SourceResolver;
import io.github.ascopes.protobufmavenplugin.utils.FileUtils;
import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -61,7 +61,7 @@ public final class SourceCodeGenerator {
private final ProtocResolver protocResolver;
private final BinaryPluginResolver binaryPluginResolver;
private final JvmPluginResolver jvmPluginResolver;
private final ProtoSourceResolver protoListingResolver;
private final SourceResolver protoListingResolver;
private final CommandLineExecutor commandLineExecutor;

@Inject
Expand All @@ -71,7 +71,7 @@ public SourceCodeGenerator(
ProtocResolver protocResolver,
BinaryPluginResolver binaryPluginResolver,
JvmPluginResolver jvmPluginResolver,
ProtoSourceResolver protoListingResolver,
SourceResolver protoListingResolver,
CommandLineExecutor commandLineExecutor
) {
this.mavenSession = mavenSession;
Expand Down Expand Up @@ -120,7 +120,7 @@ public boolean generate(GenerationRequest request) throws ResolutionException, I
.fatalWarnings(request.isFatalWarnings())
.importPaths(
importPaths.stream()
.map(ProtoFileListing::getProtoFilesRoot)
.map(SourceListing::getProtoFilesRoot)
.collect(Collectors.toCollection(LinkedHashSet::new))
);

Expand All @@ -136,7 +136,7 @@ public boolean generate(GenerationRequest request) throws ResolutionException, I

var sourceFiles = sourcePaths
.stream()
.map(ProtoFileListing::getProtoFiles)
.map(SourceListing::getProtoFiles)
.flatMap(Collection::stream)
.collect(Collectors.toCollection(LinkedHashSet::new));

Expand Down Expand Up @@ -181,8 +181,8 @@ private Collection<ResolvedProtocPlugin> discoverPlugins(
.collect(Collectors.toUnmodifiableList());
}

private Collection<ProtoFileListing> discoverImportPaths(
Collection<ProtoFileListing> sourcePathListings,
private Collection<SourceListing> discoverImportPaths(
Collection<SourceListing> sourcePathListings,
GenerationRequest request
) throws ResolutionException {
var artifactPaths = artifactPathResolver.resolveDependencies(
Expand All @@ -193,7 +193,7 @@ private Collection<ProtoFileListing> discoverImportPaths(
request.isFailOnInvalidDependencies()
);

var filter = new ProtoFileFilter();
var filter = new SourceGlobFilter();

var importPathListings = protoListingResolver.createProtoFileListings(
concat(request.getImportPaths(), artifactPaths),
Expand All @@ -208,12 +208,12 @@ private Collection<ProtoFileListing> discoverImportPaths(
.collect(Collectors.toUnmodifiableList());
}

private Collection<ProtoFileListing> discoverCompilableSources(
private Collection<SourceListing> discoverCompilableSources(
GenerationRequest request
) throws ResolutionException {
log.debug("Discovering all compilable protobuf source files");

var filter = new ProtoFileFilter(request.getIncludes(), request.getExcludes());
var filter = new SourceGlobFilter(request.getIncludes(), request.getExcludes());

var sourcePathsListings = protoListingResolver.createProtoFileListings(
request.getSourceRoots(),
Expand Down Expand Up @@ -287,7 +287,7 @@ private void registerSourceRoots(GenerationRequest request) {

private void embedSourcesInClassOutputs(
SourceRootRegistrar registrar,
Collection<ProtoFileListing> listings
Collection<SourceListing> listings
) throws ResolutionException {
for (var listing : listings) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.github.ascopes.protobufmavenplugin.generation;

import io.github.ascopes.protobufmavenplugin.sources.ProtoFileListing;
import io.github.ascopes.protobufmavenplugin.sources.SourceListing;
import io.github.ascopes.protobufmavenplugin.utils.FileUtils;
import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -71,7 +71,7 @@ public void registerSourceRoot(MavenSession session, Path path) {
sourceRootRegistrar.accept(session.getCurrentProject(), path.toString());
}

public void embedListing(MavenSession session, ProtoFileListing listing) throws IOException {
public void embedListing(MavenSession session, SourceListing listing) throws IOException {
log.info("Embedding sources from {} in {} class outputs", listing.getProtoFilesRoot(), this);
var targetDirectory = classOutputDirectoryGetter.andThen(Path::of)
.apply(session.getCurrentProject().getBuild());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@
import java.util.stream.Collectors;

/**
* Filter for files.
* Filter for source files to allow including and excluding based on glob patterns.
*
* @author Ashley Scopes
*/
public final class ProtoFileFilter {
public final class SourceGlobFilter {

private final List<PathMatcher> includes;
private final List<PathMatcher> excludes;

public ProtoFileFilter() {
public SourceGlobFilter() {
this(List.of(), List.of());
}

public ProtoFileFilter(List<String> includes, List<String> excludes) {
public SourceGlobFilter(List<String> includes, List<String> excludes) {
this.includes = compileMatchers(includes);
this.excludes = compileMatchers(excludes);
}
Expand All @@ -48,7 +48,6 @@ public boolean matches(Path relativeRoot, Path file) {
var relativeFile = relativeRoot.relativize(file);

var excluded = !excludes.isEmpty() && excludes.stream().anyMatch(checking(relativeFile));

var notIncluded = !includes.isEmpty() && includes.stream().noneMatch(checking(relativeFile));

if (excluded || notIncluded) {
Expand All @@ -61,16 +60,6 @@ public boolean matches(Path relativeRoot, Path file) {
.isPresent();
}

@Override
public String toString() {
// Used for debug logs only.
return "ProtoFileFilter{"
+ "isProtoFile=true, "
+ "includes=" + includes + ", "
+ "excludes=" + excludes
+ "}";
}

private static List<PathMatcher> compileMatchers(List<String> patterns) {
return patterns.stream()
.map("glob:"::concat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @author Ashley Scopes
*/
@Immutable
public interface ProtoFileListing {
public interface SourceListing {

Path getProtoFilesRoot();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,37 @@
import org.slf4j.LoggerFactory;

/**
* Component that can index and resolve proto files in a file tree.
* Component that can index and resolve protobuf sources in a file tree.
*
* <p>In addition, it can discover proto files within archives. These results will be
* <p>In addition, it can discover sources within archives recursively. These results will be
* extracted to a location within the Maven build directory to enable {@code protoc} and other
* plugins to be able to view them without needing access to the Java NIO file system APIs.
*
* @author Ashley Scopes
*/
@Named
public final class ProtoSourceResolver {
public final class SourceResolver {

private static final Set<String> POM_FILE_EXTENSIONS = Set.of(".pom", ".xml");
private static final Set<String> ZIP_FILE_EXTENSIONS = Set.of(".jar", ".zip");

private static final Logger log = LoggerFactory.getLogger(ProtoSourceResolver.class);
private static final Logger log = LoggerFactory.getLogger(SourceResolver.class);

private final ConcurrentExecutor concurrentExecutor;
private final TemporarySpace temporarySpace;

@Inject
public ProtoSourceResolver(
public SourceResolver(
ConcurrentExecutor concurrentExecutor,
TemporarySpace temporarySpace
) {
this.concurrentExecutor = concurrentExecutor;
this.temporarySpace = temporarySpace;
}

public Collection<ProtoFileListing> createProtoFileListings(
public Collection<SourceListing> createProtoFileListings(
Collection<Path> rootPaths,
ProtoFileFilter filter
SourceGlobFilter filter
) {
return rootPaths
.stream()
Expand All @@ -82,9 +82,9 @@ public Collection<ProtoFileListing> createProtoFileListings(
.collect(Collectors.toUnmodifiableList());
}

public Optional<ProtoFileListing> createProtoFileListing(
public Optional<SourceListing> createProtoFileListing(
Path rootPath,
ProtoFileFilter filter
SourceGlobFilter filter
) throws IOException {
if (!Files.exists(rootPath)) {
log.debug("Skipping lookup in path {} as it does not exist", rootPath);
Expand All @@ -96,10 +96,9 @@ public Optional<ProtoFileListing> createProtoFileListing(
: createProtoFileListingForDirectory(rootPath, filter);
}


private Optional<ProtoFileListing> createProtoFileListingForFile(
private Optional<SourceListing> createProtoFileListingForFile(
Path rootPath,
ProtoFileFilter filter
SourceGlobFilter filter
) throws IOException {
// XXX: we do not convert the extension to lowercase, as there
// is some nuanced logic within the ZipFileSystemProvider that appears
Expand All @@ -122,9 +121,9 @@ private Optional<ProtoFileListing> createProtoFileListingForFile(
return Optional.empty();
}

private Optional<ProtoFileListing> createProtoFileListingForArchive(
private Optional<SourceListing> createProtoFileListingForArchive(
Path rootPath,
ProtoFileFilter filter
SourceGlobFilter filter
) throws IOException {
try (var vfs = FileUtils.openZipAsFileSystem(rootPath)) {
var vfsRoot = vfs.getRootDirectories().iterator().next();
Expand All @@ -141,7 +140,7 @@ private Optional<ProtoFileListing> createProtoFileListingForArchive(
sourceFiles.get().getProtoFiles().stream()
);

var listing = ImmutableProtoFileListing
var listing = ImmutableSourceListing
.builder()
.addAllProtoFiles(targetFiles)
.protoFilesRoot(extractionRoot)
Expand All @@ -151,9 +150,9 @@ private Optional<ProtoFileListing> createProtoFileListingForArchive(
}
}

private Optional<ProtoFileListing> createProtoFileListingForDirectory(
private Optional<SourceListing> createProtoFileListingForDirectory(
Path rootPath,
ProtoFileFilter filter
SourceGlobFilter filter
) throws IOException {
try (var stream = Files.walk(rootPath)) {
return stream
Expand All @@ -166,7 +165,7 @@ private Optional<ProtoFileListing> createProtoFileListingForDirectory(
Optional::of
))
.filter(not(Collection::isEmpty))
.map(protoFiles -> ImmutableProtoFileListing
.map(protoFiles -> ImmutableSourceListing
.builder()
.addAllProtoFiles(protoFiles)
.protoFilesRoot(rootPath)
Expand Down

0 comments on commit de27eda

Please sign in to comment.