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

feat: option to add additionalPlainTextMasks #857

Merged
merged 3 commits into from
Sep 11, 2024
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
25 changes: 20 additions & 5 deletions src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +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")
Expand Down Expand Up @@ -103,17 +104,29 @@ protected Set<String> 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<String> plainTextMasks;

/**
* Allows to add additional plain text masks without overriding
* the defaults.
*/
@Nullable
@Parameter(property = "rewrite.additionalPlainTextMasks")
private LinkedHashSet<String> additionalPlainTextMasks;

protected Set<String> getPlainTextMasks() {
Set<String> 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",
Expand Down Expand Up @@ -147,6 +160,8 @@ protected Set<String> getPlainTextMasks() {
"**/*.txt",
"**/*.py"
));
masks.addAll(getCleanedSet(additionalPlainTextMasks));
return unmodifiableSet(masks);
}

@Nullable
Expand Down Expand Up @@ -291,15 +306,15 @@ protected Set<String> getRecipeArtifactCoordinates() {
return computedRecipeArtifactCoordinates;
}

private static Set<String> getCleanedSet(@Nullable Set<String> set) {
private static Set<String> getCleanedSet(@Nullable Set<@Nullable String> set) {
if (set == null) {
return Collections.emptySet();
}
Set<String> cleaned = set.stream()
.filter(Objects::nonNull)
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toCollection(LinkedHashSet::new));
return Collections.unmodifiableSet(cleaned);
.collect(toCollection(LinkedHashSet::new));
return unmodifiableSet(cleaned);
}
}
17 changes: 17 additions & 0 deletions src/test/java/org/openrewrite/maven/RewriteRunIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
findMe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
findMe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
findMe
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>

<groupId>org.openrewrite.maven</groupId>
<artifactId>single_project</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>RewriteRunIT#plaintext_masks</name>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<activeRecipes>
<recipe>com.example.RewriteRunIT.PlainTextMasks</recipe>
</activeRecipes>
<configLocation>
${maven.multiModuleProjectDirectory}/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/plaintext_masks/rewrite.yml
</configLocation>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-static-analysis</artifactId>
<version>1.15.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
type: specs.openrewrite.org/v1beta/recipe
name: com.example.RewriteRunIT.PlainTextMasks

recipeList:
- org.openrewrite.text.FindAndReplace:
find: findMe
replace: replacedWith
regex: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sample;

public class Dummy {
private static final String findMe = "findMe";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
findMe