Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<includes> and <excludes> filtering feature for extraDirectories #3173

Merged
merged 19 commits into from
Mar 31, 2021
Merged
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 @@ -33,7 +33,9 @@
import java.nio.file.Paths;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;

Expand All @@ -45,6 +47,8 @@ public class JavaContainerBuilderHelper {
*
* @param sourceDirectory the source extra directory path
* @param targetDirectory the root directory on the container to place the files in
* @param includes the list of glob patterns to include from the source directory
* @param excludes the list of glob patterns to exclude from the source directory
* @param extraDirectoryPermissions map from path on container to file permissions
* @param modificationTimeProvider file modification time provider
* @return a {@link FileEntriesLayer} for adding the extra directory to the container
Expand All @@ -53,47 +57,69 @@ public class JavaContainerBuilderHelper {
public static FileEntriesLayer extraDirectoryLayerConfiguration(
Path sourceDirectory,
AbsoluteUnixPath targetDirectory,
List<String> includes,
List<String> excludes,
Map<String, FilePermissions> extraDirectoryPermissions,
ModificationTimeProvider modificationTimeProvider)
throws IOException {
FileEntriesLayer.Builder builder =
FileEntriesLayer.builder().setName(LayerType.EXTRA_FILES.getName());
Map<PathMatcher, FilePermissions> pathMatchers = new LinkedHashMap<>();
Map<PathMatcher, FilePermissions> permissionsPathMatchers = new LinkedHashMap<>();
for (Map.Entry<String, FilePermissions> entry : extraDirectoryPermissions.entrySet()) {
pathMatchers.put(
permissionsPathMatchers.put(
FileSystems.getDefault().getPathMatcher("glob:" + entry.getKey()), entry.getValue());
}

new DirectoryWalker(sourceDirectory)
.filterRoot()
.walk(
localPath -> {
AbsoluteUnixPath pathOnContainer =
targetDirectory.resolve(sourceDirectory.relativize(localPath));
Instant modificationTime = modificationTimeProvider.get(localPath, pathOnContainer);
FilePermissions permissions =
extraDirectoryPermissions.get(pathOnContainer.toString());
if (permissions == null) {
// Check for matching globs
Path containerPath = Paths.get(pathOnContainer.toString());
for (Map.Entry<PathMatcher, FilePermissions> entry : pathMatchers.entrySet()) {
if (entry.getKey().matches(containerPath)) {
builder.addEntry(
localPath, pathOnContainer, entry.getValue(), modificationTime);
return;
}
}

// Add with default permissions
builder.addEntry(localPath, pathOnContainer, modificationTime);
} else {
// Add with explicit permissions
builder.addEntry(localPath, pathOnContainer, permissions, modificationTime);
}
});
DirectoryWalker walker = new DirectoryWalker(sourceDirectory).filterRoot();
// add exclusion filters
excludes
.stream()
.map(pattern -> FileSystems.getDefault().getPathMatcher("glob:" + pattern))
.forEach(pathMatcher -> walker.filter(path -> !pathMatcher.matches(path)));
// add an inclusion filter
includes
.stream()
.map(pattern -> FileSystems.getDefault().getPathMatcher("glob:" + pattern))
.map(pathMatcher -> (Predicate<Path>) (path -> pathMatcher.matches(path)))
.reduce((matches1, matches2) -> matches1.or(matches2))
.ifPresent(walker::filter);
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved
// walk the source tree and add layer entries
walker.walk(
localPath -> {
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved
AbsoluteUnixPath pathOnContainer =
targetDirectory.resolve(sourceDirectory.relativize(localPath));
Instant modificationTime = modificationTimeProvider.get(localPath, pathOnContainer);
Optional<FilePermissions> permissions =
determinePermissions(
pathOnContainer, extraDirectoryPermissions, permissionsPathMatchers);
if (permissions.isPresent()) {
builder.addEntry(localPath, pathOnContainer, permissions.get(), modificationTime);
} else {
builder.addEntry(localPath, pathOnContainer, modificationTime);
}
});
return builder.build();
}

private static Optional<FilePermissions> determinePermissions(
AbsoluteUnixPath path,
Map<String, FilePermissions> extraDirectoryPermissions,
Map<PathMatcher, FilePermissions> permissionsPathMatchers) {
// The check is only for optimization. (`permissionsPathMatchers` is constructed from the map.)
FilePermissions permissions = extraDirectoryPermissions.get(path.toString());
if (permissions != null) {
return Optional.of(permissions);
}

// Check for matching globs
for (Map.Entry<PathMatcher, FilePermissions> entry : permissionsPathMatchers.entrySet()) {
if (entry.getKey().matches(Paths.get(path.toString()))) {
return Optional.of(entry.getValue());
}
}
return Optional.empty();
}

/**
* Constructs a new {@link JibContainerBuilder} for a WAR project.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ static JibContainerBuilder processCommonConfiguration(
JavaContainerBuilderHelper.extraDirectoryLayerConfiguration(
from,
AbsoluteUnixPath.get(extraDirectory.getInto()),
extraDirectory.getIncludesList(),
extraDirectory.getExcludesList(),
rawConfiguration.getExtraDirectoryPermissions(),
modificationTimeProvider));
}
Expand Down
Loading