Skip to content

Commit

Permalink
remove shared variables
Browse files Browse the repository at this point in the history
  • Loading branch information
nitschSB authored May 23, 2024
1 parent 73ac3df commit 63839c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,8 @@
@Singleton
public class JibLayerFilterExtension implements JibMavenPluginExtension<Configuration> {

private Map<PathMatcher, String> pathMatchers = new LinkedHashMap<>();

@VisibleForTesting @Inject ProjectDependenciesResolver dependencyResolver;

// (layer name, layer builder) map for new layers of configured <toLayer>
@VisibleForTesting Map<String, FileEntriesLayer.Builder> newToLayers = new LinkedHashMap<>();

@Override
public Optional<Class<Configuration>> getExtraConfigType() {
return Optional.of(Configuration.class);
Expand All @@ -80,7 +75,12 @@ public ContainerBuildPlan extendContainerBuildPlan(
return buildPlan;
}

preparePathMatchersAndLayerBuilders(buildPlan, config.get());
Map<PathMatcher, String> pathMatchers = new LinkedHashMap<>();

// (layer name, layer builder) map for new layers of configured <toLayer>
Map<String, FileEntriesLayer.Builder> newToLayers = new LinkedHashMap<>();

preparePathMatchersAndLayerBuilders(buildPlan, config.get(), pathMatchers);

ContainerBuildPlan.Builder newPlanBuilder = buildPlan.toBuilder();
newPlanBuilder.setLayers(Collections.emptyList());
Expand All @@ -92,7 +92,7 @@ public ContainerBuildPlan extendContainerBuildPlan(
List<FileEntry> filesToKeep = new ArrayList<>();

for (FileEntry entry : layer.getEntries()) {
Optional<String> finalLayerName = determineFinalLayerName(entry, layer.getName());
Optional<String> finalLayerName = determineFinalLayerName(entry, layer.getName(), pathMatchers);
// Either keep, move, or delete this FileEntry.
if (finalLayerName.isPresent()) {
if (finalLayerName.get().equals(layer.getName())) {
Expand Down Expand Up @@ -235,11 +235,14 @@ private void logMissingParentDependencies(
}

private void preparePathMatchersAndLayerBuilders(
ContainerBuildPlan buildPlan, Configuration config) throws JibPluginExtensionException {
ContainerBuildPlan buildPlan,
Configuration config,
Map<PathMatcher, String> pathMatchers,
Map<String, FileEntriesLayer.Builder> newToLayers)
throws JibPluginExtensionException {
List<String> originalLayerNames =
buildPlan.getLayers().stream().map(LayerObject::getName).collect(Collectors.toList());

newToLayers.clear(); // ensure empty (in case previously built module already populated it)
for (Configuration.Filter filter : config.getFilters()) {
String toLayerName = filter.getToLayer();
if (!toLayerName.isEmpty() && originalLayerNames.contains(toLayerName)) {
Expand Down Expand Up @@ -271,7 +274,8 @@ private void preparePathMatchersAndLayerBuilders(
* @return final layer name into which {@code fileEntry} should move. May be same as {@code
* originalLayerName}. {@link Optional#empty()} indicates deletion.
*/
private Optional<String> determineFinalLayerName(FileEntry fileEntry, String originalLayerName) {
private Optional<String> determineFinalLayerName(
FileEntry fileEntry, String originalLayerName, Map<PathMatcher, String> pathMatchers) {
Optional<String> finalLayerName = Optional.of(originalLayerName);

for (Map.Entry<PathMatcher, String> mapEntry : pathMatchers.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,6 @@ public void testExtendContainerBuildPlan_sameLayerNameInMultipleFilters()
ContainerBuildPlan newPlan =
extension.extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);

ArrayList<String> layerNames = new ArrayList<>(extension.newToLayers.keySet());
assertEquals(Arrays.asList("foo", "same layer name", "bar", "baz"), layerNames);

assertEquals(4, newPlan.getLayers().size());
FileEntriesLayer newLayer1 = (FileEntriesLayer) newPlan.getLayers().get(0);
FileEntriesLayer newLayer2 = (FileEntriesLayer) newPlan.getLayers().get(1);
Expand Down

0 comments on commit 63839c9

Please sign in to comment.