Skip to content

Commit

Permalink
Account for a second common resource filtering pattern (#4666)
Browse files Browse the repository at this point in the history
* Account for a second common resource filtering pattern

* Drop JetBrains annotations
  • Loading branch information
timtebeek authored Nov 13, 2024
1 parent 87f83de commit 9f62e3d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.ScanningRecipe;
import org.openrewrite.SourceFile;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.*;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.maven.internal.MavenPomDownloader;
import org.openrewrite.maven.tree.MavenResolutionResult;
Expand All @@ -38,12 +33,7 @@

import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -76,14 +66,14 @@ public Map<String, Set<MavenResolutionResult>> getFilteredResourceUsages() {
Map<String, Set<MavenResolutionResult>> result = new HashMap<>();
filteredResourcePathsToDeclaringPoms.forEach((filteredResourcePath, mrr) ->
nonPomPathsToUsages.forEach((usagePath, properties) -> {
if (usagePath.startsWith(filteredResourcePath)) {
properties.forEach(property -> {
result.putIfAbsent(property, new HashSet<>());
result.get(property).add(mrr);
});
}
}
));
if (usagePath.startsWith(filteredResourcePath)) {
properties.forEach(property -> {
result.putIfAbsent(property, new HashSet<>());
result.get(property).add(mrr);
});
}
}
));
return result;
}
}
Expand All @@ -99,15 +89,10 @@ private String getPropertyPattern() {

@Override
public TreeVisitor<?, ExecutionContext> getScanner(RemoveUnusedProperties.Accumulator acc) {
Pattern propertyMatcher = Pattern.compile(getPropertyPattern());
Pattern propertyUsageMatcher = Pattern.compile("[^$]*\\$\\{(" + propertyMatcher.pattern() + ")}[^$]*");

MavenIsoVisitor<ExecutionContext> findPomUsagesVisitor =
new FindPomUsagesVisitor(propertyUsageMatcher, acc);
MavenIsoVisitor<ExecutionContext> findFilteredResourcePathsVisitor =
new FindFilteredResourcePathsVisitor(acc);
PlainTextVisitor<ExecutionContext> findResourceUsagesVisitor =
new FindResourceUsagesVisitor(propertyUsageMatcher, acc);
String patternOrDefault = getPropertyPattern();
MavenIsoVisitor<ExecutionContext> findPomUsagesVisitor = new FindPomUsagesVisitor(dollarPropertyMatcher(patternOrDefault), acc);
MavenIsoVisitor<ExecutionContext> findFilteredResourcePathsVisitor = new FindFilteredResourcePathsVisitor(acc);
PlainTextVisitor<ExecutionContext> findResourceUsagesVisitor = new FindResourceUsagesVisitor(patternOrDefault, acc);

return new TreeVisitor<Tree, ExecutionContext>() {
@Override
Expand All @@ -126,6 +111,14 @@ public TreeVisitor<?, ExecutionContext> getScanner(RemoveUnusedProperties.Accumu
};
}

private static Pattern dollarPropertyMatcher(String patternOrDefault) {
return Pattern.compile("[^$]*\\$\\{(" + patternOrDefault + ")}[^$]*");
}

private static Pattern atPropertyMatcher(String patternOrDefault) {
return Pattern.compile("@(" + patternOrDefault + ")@");
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor(RemoveUnusedProperties.Accumulator acc) {
Pattern propertyMatcher = Pattern.compile(getPropertyPattern());
Expand Down Expand Up @@ -182,7 +175,7 @@ private boolean isAncestor(MavenResolutionResult project, ResolvedGroupArtifactV
}

private boolean parentHasProperty(MavenResolutionResult resolutionResult, String propertyName,
ExecutionContext ctx) {
ExecutionContext ctx) {
MavenPomDownloader downloader = new MavenPomDownloader(resolutionResult.getProjectPoms(), ctx,
resolutionResult.getMavenSettings(), resolutionResult.getActiveProfiles());
try {
Expand Down Expand Up @@ -239,11 +232,12 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (resourceMatcher.matches(getCursor())) {
String directory = tag.getChildValue("directory").orElse(null);
if (tag.getChildValue("filtering").map(Boolean::valueOf).orElse(false)
&& directory != null) {
&& directory != null) {
Path path = getCursor().firstEnclosingOrThrow(SourceFile.class).getSourcePath();
try {
acc.filteredResourcePathsToDeclaringPoms.put(path.getParent().resolve(directory), getResolutionResult());
} catch (InvalidPathException ignored) {} // fail quietly
} catch (InvalidPathException ignored) {
} // fail quietly
}
return tag;
} else {
Expand All @@ -253,17 +247,24 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
}

private static class FindResourceUsagesVisitor extends PlainTextVisitor<ExecutionContext> {
private final Pattern propertyUsageMatcher;
private final Pattern dollarMatcher;
private final Pattern atMatcher;
private final Accumulator acc;

public FindResourceUsagesVisitor(Pattern propertyUsageMatcher, Accumulator acc) {
this.propertyUsageMatcher = propertyUsageMatcher;
public FindResourceUsagesVisitor(String pattern, Accumulator acc) {
this.dollarMatcher = dollarPropertyMatcher(pattern);
this.atMatcher = atPropertyMatcher(pattern);
this.acc = acc;
}

@Override
public PlainText visitText(PlainText text, ExecutionContext ctx) {
Matcher matcher = propertyUsageMatcher.matcher(text.getText());
Matcher matcher = dollarMatcher.matcher(text.getText());
while (matcher.find()) {
acc.nonPomPathsToUsages.putIfAbsent(text.getSourcePath(), new HashSet<>());
acc.nonPomPathsToUsages.get(text.getSourcePath()).add(matcher.group(1));
}
matcher = atMatcher.matcher(text.getText());
while (matcher.find()) {
acc.nonPomPathsToUsages.putIfAbsent(text.getSourcePath(), new HashSet<>());
acc.nonPomPathsToUsages.get(text.getSourcePath()).add(matcher.group(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.openrewrite.maven;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

Expand Down Expand Up @@ -388,8 +390,12 @@ void keepsPropertyUsedByChild() {
);
}

@Test
void keepsPropertyUsedByFilteredResource() {
@ParameterizedTest
@ValueSource(strings = {
"Hello ${a}",
"Hello @a@"
})
void keepsPropertyUsedByFilteredResource(String text) {
rewriteRun(
mavenProject("my-project",
pomXml(
Expand All @@ -416,7 +422,8 @@ void keepsPropertyUsedByFilteredResource() {
</build>
</project>
""", """
""",
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
Expand All @@ -441,7 +448,7 @@ void keepsPropertyUsedByFilteredResource() {
"""
),
srcMainResources(
text("Hello ${a}")
text(text)
)
)
);
Expand Down

0 comments on commit 9f62e3d

Please sign in to comment.