Skip to content

Commit

Permalink
Drop HasSourcePath in favor of FindSourceFiles (#3758)
Browse files Browse the repository at this point in the history
* Drop HasSourcePath in favor of FindSourceFiles

* Demonstrate feature from HasSourcePath not present in FindSourceFiles

* Add to PathUtilsTest

* Gradle wrapper 8.5

* Only document limitation for now

* Restore match when pattern is empty
  • Loading branch information
timtebeek authored Dec 4, 2023
1 parent b066fa0 commit 9055f92
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 204 deletions.
95 changes: 0 additions & 95 deletions rewrite-core/src/main/java/org/openrewrite/HasSourcePath.java

This file was deleted.

7 changes: 4 additions & 3 deletions rewrite-core/src/main/java/org/openrewrite/PathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import java.util.List;

public class PathUtils {
private PathUtils() {}
private PathUtils() {
}

private static final char UNIX_SEPARATOR = '/';

Expand All @@ -43,7 +44,7 @@ public static boolean equalIgnoringSeparators(Path a, Path b) {
/**
* Compare two strings representing file paths, returning true if they indicate the same path regardless of separators
*/
public static boolean equalIgnoringSeparators(String a , String b) {
public static boolean equalIgnoringSeparators(String a, String b) {
return separatorsToSystem(a).equals(separatorsToSystem(b));
}

Expand Down Expand Up @@ -127,7 +128,7 @@ private static boolean matchesGlob(String pattern, String path) {
return false;
}
if (pattIdxEnd == (pattTokens.length - 1)
&& (isFileSeparator(pattern.charAt(pattern.length() - 1)) ^ isFileSeparator(path.charAt(path.length() - 1)))) {
&& (isFileSeparator(pattern.charAt(pattern.length() - 1)) ^ isFileSeparator(path.charAt(path.length() - 1)))) {
return false;
}
pattIdxEnd--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String getDisplayName() {
@Override
public String getDescription() {
return "Completely replaces the contents of the text file with other text. " +
"Use together with a `HasSourcePath` precondition to limit which files are changed.";
"Use together with a `FindSourceFiles` precondition to limit which files are changed.";
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion rewrite-core/src/main/java/org/openrewrite/text/Find.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (filePattern != null) {
//noinspection unchecked
TreeVisitor<?, ExecutionContext> check = Preconditions.or(Arrays.stream(filePattern.split(";"))
.map(HasSourcePath::new)
.map(FindSourceFiles::new)
.map(Recipe::getVisitor)
.toArray(TreeVisitor[]::new));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if(filePattern != null) {
//noinspection unchecked
TreeVisitor<?, ExecutionContext> check = Preconditions.or(Arrays.stream(filePattern.split(";"))
.map(HasSourcePath::new)
.map(FindSourceFiles::new)
.map(Recipe::getVisitor)
.toArray(TreeVisitor[]::new));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (filePattern != null) {
//noinspection unchecked
TreeVisitor<?, ExecutionContext> check = Preconditions.or(Arrays.stream(filePattern.split(";"))
.map(HasSourcePath::new)
.map(FindSourceFiles::new)
.map(Recipe::getVisitor)
.toArray(TreeVisitor[]::new));

Expand Down
13 changes: 13 additions & 0 deletions rewrite-core/src/test/java/org/openrewrite/PathUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.nio.file.Path;
Expand Down Expand Up @@ -98,6 +99,18 @@ void globMatching() {
assertThat(matchesGlob(path("a-test/test.txt"), "**/*-test/*-test/test.txt")).isFalse();
}

@Test
@Issue("https://github.com/openrewrite/rewrite/pull/3758")
@Disabled("{} syntax not supported yet")
void eitherOr() {
// matches with {}'s, used in for instance `"**/{application,application-*,bootstrap,bootstrap-*}.{yml,yaml}"`
assertThat(matchesGlob(path("test/"), "test/{foo,bar}")).isFalse();
assertThat(matchesGlob(path("test/quz"), "test/{foo,bar}")).isFalse();
assertThat(matchesGlob(path("test/foo"), "test/{foo,bar}")).isTrue();
assertThat(matchesGlob(path("test/foo"), "test/{f*,bar}")).isTrue();
assertThat(matchesGlob(path("test/bar"), "test/{foo,bar}")).isTrue();
}

private static Path path(String path) {
return Paths.get(path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ public String getDisplayName() {
@Override
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
if (fileMatcher != null) {
return new HasSourcePath(fileMatcher).getVisitor();
return new FindSourceFiles(fileMatcher).getVisitor();
} else {
return null;
}
Expand Down Expand Up @@ -633,7 +633,7 @@ public String getDisplayName() {
@Override
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
if (fileMatcher != null) {
return new HasSourcePath(fileMatcher).getVisitor();
return new FindSourceFiles(fileMatcher).getVisitor();
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand Down Expand Up @@ -120,4 +121,23 @@ void findDotfiles() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/pull/3758")
@Disabled("{} syntax not supported yet")
void eitherOr() {
rewriteRun(
spec -> spec.recipe(new FindSourceFiles("**/*.{md,txt}")),
text(
"hello world!",
"~~>hello world!",
spec -> spec.path("a/b/hello.md")
),
text(
"hello world!",
"~~>hello world!",
spec -> spec.path("a/c/hello.txt")
)
);
}
}
97 changes: 0 additions & 97 deletions rewrite-test/src/test/java/org/openrewrite/HasSourcePathTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(fileMatcher != null ? new HasSourcePath(fileMatcher) : new HasSourcePath(""), new XmlIsoVisitor<ExecutionContext>() {
return Preconditions.check(fileMatcher != null ? new FindSourceFiles(fileMatcher) : new FindSourceFiles("**"), new XmlIsoVisitor<ExecutionContext>() {
private final XPathMatcher xPathMatcher = new XPathMatcher(xPath);

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.ExecutionContext;
import org.openrewrite.HasSourcePath;
import org.openrewrite.FindSourceFiles;
import org.openrewrite.Preconditions;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.yaml.tree.Yaml;
Expand Down Expand Up @@ -89,7 +89,7 @@ void insertCopyValueAndRemoveSource() {
void changeOnlyMatchingFile() {
rewriteRun(
spec -> spec.recipe(
toRecipe(() -> Preconditions.check(new HasSourcePath("**/a.yml"), new YamlIsoVisitor<>() {
toRecipe(() -> Preconditions.check(new FindSourceFiles("**/a.yml"), new YamlIsoVisitor<>() {
@Override
public Yaml.Documents visitDocuments(Yaml.Documents documents, ExecutionContext executionContext) {
doAfterVisit(new CopyValue(".source", ".destination").getVisitor());
Expand Down

0 comments on commit 9055f92

Please sign in to comment.