Skip to content

Commit

Permalink
Merge pull request #447 from ascopes/feature/GH-438-inline-archive-ex…
Browse files Browse the repository at this point in the history
…tractor

GH-438: Inline the proto archive extractor
  • Loading branch information
ascopes authored Nov 9, 2024
2 parents a130f0d + 0fc687f commit 09f5229
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 128 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import static java.util.function.Predicate.not;

import io.github.ascopes.protobufmavenplugin.generation.TemporarySpace;
import io.github.ascopes.protobufmavenplugin.utils.ConcurrentExecutor;
import io.github.ascopes.protobufmavenplugin.utils.Digests;
import io.github.ascopes.protobufmavenplugin.utils.FileUtils;
import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -51,15 +53,15 @@ public final class ProtoSourceResolver {
private static final Logger log = LoggerFactory.getLogger(ProtoSourceResolver.class);

private final ConcurrentExecutor concurrentExecutor;
private final ProtoArchiveExtractor protoArchiveExtractor;
private final TemporarySpace temporarySpace;

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

public Collection<ProtoFileListing> createProtoFileListings(
Expand Down Expand Up @@ -89,30 +91,13 @@ public Optional<ProtoFileListing> createProtoFileListing(
return Optional.empty();
}

if (Files.isRegularFile(rootPath)) {
return createProtoFileListingForArchive(rootPath, filter);
}

try (var stream = Files.walk(rootPath)) {
return stream
.filter(filePath -> filter.matches(rootPath, filePath))
.peek(protoFile -> log.trace("Found proto file in root {}: {}", rootPath, protoFile))
.collect(Collectors.collectingAndThen(
// Terminal operation, means we do not return a closed stream
// by mistake.
Collectors.toCollection(LinkedHashSet::new),
Optional::of
))
.filter(not(Collection::isEmpty))
.map(protoFiles -> ImmutableProtoFileListing
.builder()
.addAllProtoFiles(protoFiles)
.protoFilesRoot(rootPath)
.build());
}
return Files.isRegularFile(rootPath)
? createProtoFileListingForFile(rootPath, filter)
: createProtoFileListingForDirectory(rootPath, filter);
}

private Optional<ProtoFileListing> createProtoFileListingForArchive(

private Optional<ProtoFileListing> createProtoFileListingForFile(
Path rootPath,
ProtoFileFilter filter
) throws IOException {
Expand All @@ -125,7 +110,7 @@ private Optional<ProtoFileListing> createProtoFileListingForArchive(
// GH-327: We filter out non-zip archives to prevent vague errors if
// users include non-zip dependencies such as POMs, which cannot be extracted.
if (fileExtension.filter(ZIP_FILE_EXTENSIONS::contains).isPresent()) {
return protoArchiveExtractor.extractProtoFiles(rootPath, filter);
return createProtoFileListingForArchive(rootPath, filter);
}

if (fileExtension.filter(POM_FILE_EXTENSIONS::contains).isPresent()) {
Expand All @@ -136,4 +121,65 @@ private Optional<ProtoFileListing> createProtoFileListingForArchive(
log.warn("Ignoring unknown archive type at {}", rootPath);
return Optional.empty();
}

private Optional<ProtoFileListing> createProtoFileListingForArchive(
Path rootPath,
ProtoFileFilter filter
) throws IOException {
try (var vfs = FileUtils.openZipAsFileSystem(rootPath)) {
var vfsRoot = vfs.getRootDirectories().iterator().next();
var sourceFiles = createProtoFileListing(vfsRoot, filter);

if (sourceFiles.isEmpty()) {
return Optional.empty();
}

var extractionRoot = getArchiveExtractionRoot().resolve(generateUniqueName(rootPath));
var targetFiles = FileUtils.rebaseFileTree(
vfsRoot,
extractionRoot,
sourceFiles.get().getProtoFiles().stream()
);

var listing = ImmutableProtoFileListing
.builder()
.addAllProtoFiles(targetFiles)
.protoFilesRoot(extractionRoot)
.build();

return Optional.of(listing);
}
}

private Optional<ProtoFileListing> createProtoFileListingForDirectory(
Path rootPath,
ProtoFileFilter filter
) throws IOException {
try (var stream = Files.walk(rootPath)) {
return stream
.filter(filePath -> filter.matches(rootPath, filePath))
.peek(protoFile -> log.trace("Found proto file in root {}: {}", rootPath, protoFile))
.collect(Collectors.collectingAndThen(
// Terminal operation, means we do not return a closed stream
// by mistake.
Collectors.toCollection(LinkedHashSet::new),
Optional::of
))
.filter(not(Collection::isEmpty))
.map(protoFiles -> ImmutableProtoFileListing
.builder()
.addAllProtoFiles(protoFiles)
.protoFilesRoot(rootPath)
.build());
}
}

private Path getArchiveExtractionRoot() {
return temporarySpace.createTemporarySpace("archives");
}

private String generateUniqueName(Path path) {
var digest = Digests.sha1(path.toAbsolutePath().toString());
return FileUtils.getFileNameWithoutExtension(path) + "-" + digest;
}
}

0 comments on commit 09f5229

Please sign in to comment.