From 292d7c7f17b49671f9a813ac8c8c0d8c735f240c Mon Sep 17 00:00:00 2001 From: dpozinen Date: Thu, 5 Sep 2024 20:41:46 +0200 Subject: [PATCH 1/2] feat: option to add `additionalPlainTextMasks` --- .../maven/ConfigurableRewriteMojo.java | 19 ++++++++- .../org/openrewrite/maven/RewriteRunIT.java | 17 ++++++++ .../RewriteRunIT/plaintext_masks/.in-root | 1 + .../plaintext_masks/from-default-list.py | 1 + .../plaintext_masks/in-root.ignored | 1 + .../RewriteRunIT/plaintext_masks/pom.xml | 42 +++++++++++++++++++ .../RewriteRunIT/plaintext_masks/rewrite.yml | 9 ++++ .../src/main/java/sample/Dummy.java | 5 +++ .../src/main/java/sample/in-src.ext | 1 + 9 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/.in-root create mode 100644 src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/from-default-list.py create mode 100644 src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/in-root.ignored create mode 100644 src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/pom.xml create mode 100644 src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/rewrite.yml create mode 100644 src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/Dummy.java create mode 100644 src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/in-src.ext diff --git a/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java b/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java index bf94a39e..f6166f0a 100644 --- a/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java +++ b/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java @@ -38,6 +38,7 @@ import java.util.stream.Collectors; import static java.util.Collections.emptyMap; +import static java.util.Collections.unmodifiableSet; import static org.openrewrite.java.style.CheckstyleConfigLoader.loadCheckstyleConfig; @SuppressWarnings("FieldMayBeFinal") @@ -103,17 +104,29 @@ protected Set getExclusions() { return getCleanedSet(exclusions); } + /** + * Override default plain text masks. If this is specified, + * {@code rewrite.additionalPlainTextMasks} will have no effect. + */ @Nullable @Parameter(property = "rewrite.plainTextMasks") private LinkedHashSet plainTextMasks; + /** + * Allows to add additional plain text masks without overriding + * the defaults. + */ + @Nullable + @Parameter(property = "rewrite.additionalPlainTextMasks") + private LinkedHashSet additionalPlainTextMasks; + protected Set getPlainTextMasks() { Set masks = getCleanedSet(plainTextMasks); if (!masks.isEmpty()) { return masks; } //If not defined, use a default set of masks - return new HashSet<>(Arrays.asList( + masks = new HashSet<>(Arrays.asList( "**/*.adoc", "**/*.aj", "**/*.bash", @@ -147,6 +160,8 @@ protected Set getPlainTextMasks() { "**/*.txt", "**/*.py" )); + masks.addAll(getCleanedSet(additionalPlainTextMasks)); + return unmodifiableSet(masks); } @Nullable @@ -300,6 +315,6 @@ private static Set getCleanedSet(@Nullable Set set) { .map(String::trim) .filter(s -> !s.isEmpty()) .collect(Collectors.toCollection(LinkedHashSet::new)); - return Collections.unmodifiableSet(cleaned); + return unmodifiableSet(cleaned); } } diff --git a/src/test/java/org/openrewrite/maven/RewriteRunIT.java b/src/test/java/org/openrewrite/maven/RewriteRunIT.java index 8d8ff285..b31b9533 100644 --- a/src/test/java/org/openrewrite/maven/RewriteRunIT.java +++ b/src/test/java/org/openrewrite/maven/RewriteRunIT.java @@ -109,4 +109,21 @@ void java_compiler_plugin_project(MavenExecutionResult result) { .filteredOn(line -> line.contains("Changes have been made")) .hasSize(1); } + + @MavenTest + @SystemProperty(value = "rewrite.additionalPlainTextMasks", content = "**/*.ext,**/.in-root") + void plaintext_masks(MavenExecutionResult result) { + assertThat(result) + .isSuccessful() + .out() + .warn() + .contains( + "Changes have been made to target/maven-it/org/openrewrite/maven/RewriteRunIT/plaintext_masks/project/src/main/java/sample/in-src.ext by:", + "Changes have been made to target/maven-it/org/openrewrite/maven/RewriteRunIT/plaintext_masks/project/.in-root by:", + "Changes have been made to target/maven-it/org/openrewrite/maven/RewriteRunIT/plaintext_masks/project/from-default-list.py by:", + "Changes have been made to target/maven-it/org/openrewrite/maven/RewriteRunIT/plaintext_masks/project/src/main/java/sample/Dummy.java by:" + ) + .doesNotContain("in-root.ignored"); + } + } diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/.in-root b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/.in-root new file mode 100644 index 00000000..9d591209 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/.in-root @@ -0,0 +1 @@ +shouldBeChanged \ No newline at end of file diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/from-default-list.py b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/from-default-list.py new file mode 100644 index 00000000..9d591209 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/from-default-list.py @@ -0,0 +1 @@ +shouldBeChanged \ No newline at end of file diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/in-root.ignored b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/in-root.ignored new file mode 100644 index 00000000..9d591209 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/in-root.ignored @@ -0,0 +1 @@ +shouldBeChanged \ No newline at end of file diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/pom.xml b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/pom.xml new file mode 100644 index 00000000..7e404eb2 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/pom.xml @@ -0,0 +1,42 @@ + + 4.0.0 + + org.openrewrite.maven + single_project + 1.0 + jar + RewriteRunIT#plaintext_masks + + + 1.8 + 1.8 + UTF-8 + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + com.example.RewriteRunIT.PlainTextMasks + + + ${maven.multiModuleProjectDirectory}/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/rewrite.yml + + + + + org.openrewrite.recipe + rewrite-static-analysis + 1.0.4 + + + + + + diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/rewrite.yml b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/rewrite.yml new file mode 100644 index 00000000..c6ad0d91 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/rewrite.yml @@ -0,0 +1,9 @@ +--- +type: specs.openrewrite.org/v1beta/recipe +name: com.example.RewriteRunIT.PlainTextMasks + +recipeList: + - org.openrewrite.text.FindAndReplace: + find: shouldBeChanged + replace: to this + regex: false diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/Dummy.java b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/Dummy.java new file mode 100644 index 00000000..aa4f715e --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/Dummy.java @@ -0,0 +1,5 @@ +package sample; + +public class Dummy { + private static final String shouldBeChanged = "shouldBeChanged"; +} diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/in-src.ext b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/in-src.ext new file mode 100644 index 00000000..9d591209 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/in-src.ext @@ -0,0 +1 @@ +shouldBeChanged \ No newline at end of file From dbe01b8d1074f466e3e52a53def663ce64c8f988 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 11 Sep 2024 11:14:31 +0200 Subject: [PATCH 2/2] Minor polish to prevent confusion in ignored file --- .../java/org/openrewrite/maven/ConfigurableRewriteMojo.java | 6 +++--- .../openrewrite/maven/RewriteRunIT/plaintext_masks/.in-root | 2 +- .../maven/RewriteRunIT/plaintext_masks/from-default-list.py | 2 +- .../maven/RewriteRunIT/plaintext_masks/in-root.ignored | 2 +- .../openrewrite/maven/RewriteRunIT/plaintext_masks/pom.xml | 2 +- .../maven/RewriteRunIT/plaintext_masks/rewrite.yml | 4 ++-- .../plaintext_masks/src/main/java/sample/Dummy.java | 2 +- .../plaintext_masks/src/main/java/sample/in-src.ext | 2 +- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java b/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java index f6166f0a..b723b610 100644 --- a/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java +++ b/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java @@ -35,10 +35,10 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; -import java.util.stream.Collectors; import static java.util.Collections.emptyMap; import static java.util.Collections.unmodifiableSet; +import static java.util.stream.Collectors.toCollection; import static org.openrewrite.java.style.CheckstyleConfigLoader.loadCheckstyleConfig; @SuppressWarnings("FieldMayBeFinal") @@ -306,7 +306,7 @@ protected Set getRecipeArtifactCoordinates() { return computedRecipeArtifactCoordinates; } - private static Set getCleanedSet(@Nullable Set set) { + private static Set getCleanedSet(@Nullable Set<@Nullable String> set) { if (set == null) { return Collections.emptySet(); } @@ -314,7 +314,7 @@ private static Set getCleanedSet(@Nullable Set set) { .filter(Objects::nonNull) .map(String::trim) .filter(s -> !s.isEmpty()) - .collect(Collectors.toCollection(LinkedHashSet::new)); + .collect(toCollection(LinkedHashSet::new)); return unmodifiableSet(cleaned); } } diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/.in-root b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/.in-root index 9d591209..719fefef 100644 --- a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/.in-root +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/.in-root @@ -1 +1 @@ -shouldBeChanged \ No newline at end of file +findMe \ No newline at end of file diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/from-default-list.py b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/from-default-list.py index 9d591209..719fefef 100644 --- a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/from-default-list.py +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/from-default-list.py @@ -1 +1 @@ -shouldBeChanged \ No newline at end of file +findMe \ No newline at end of file diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/in-root.ignored b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/in-root.ignored index 9d591209..719fefef 100644 --- a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/in-root.ignored +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/in-root.ignored @@ -1 +1 @@ -shouldBeChanged \ No newline at end of file +findMe \ No newline at end of file diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/pom.xml b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/pom.xml index 7e404eb2..995b7dc8 100644 --- a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/pom.xml +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/pom.xml @@ -33,7 +33,7 @@ org.openrewrite.recipe rewrite-static-analysis - 1.0.4 + 1.15.0 diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/rewrite.yml b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/rewrite.yml index c6ad0d91..4a465325 100644 --- a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/rewrite.yml +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/rewrite.yml @@ -4,6 +4,6 @@ name: com.example.RewriteRunIT.PlainTextMasks recipeList: - org.openrewrite.text.FindAndReplace: - find: shouldBeChanged - replace: to this + find: findMe + replace: replacedWith regex: false diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/Dummy.java b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/Dummy.java index aa4f715e..e999b318 100644 --- a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/Dummy.java +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/Dummy.java @@ -1,5 +1,5 @@ package sample; public class Dummy { - private static final String shouldBeChanged = "shouldBeChanged"; + private static final String findMe = "findMe"; } diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/in-src.ext b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/in-src.ext index 9d591209..719fefef 100644 --- a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/in-src.ext +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/src/main/java/sample/in-src.ext @@ -1 +1 @@ -shouldBeChanged \ No newline at end of file +findMe \ No newline at end of file