From 5f743cc23dd81b906e55e276aedf65e299c959cf Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 12 Nov 2023 22:17:16 +0100 Subject: [PATCH 1/2] CreateTextFile should override files of different type --- .../org/openrewrite/text/CreateTextFile.java | 19 ++++++++---- rewrite-test/build.gradle.kts | 2 ++ .../openrewrite/text/CreateTextFileTest.java | 29 +++++++++++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/rewrite-core/src/main/java/org/openrewrite/text/CreateTextFile.java b/rewrite-core/src/main/java/org/openrewrite/text/CreateTextFile.java index 2e3ecba140b..1a4b5b831db 100644 --- a/rewrite-core/src/main/java/org/openrewrite/text/CreateTextFile.java +++ b/rewrite-core/src/main/java/org/openrewrite/text/CreateTextFile.java @@ -18,7 +18,10 @@ import lombok.EqualsAndHashCode; import lombok.Value; import org.openrewrite.*; +import org.openrewrite.binary.Binary; import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.quark.Quark; +import org.openrewrite.remote.Remote; import java.nio.file.Path; import java.nio.file.Paths; @@ -81,7 +84,7 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { @Override public Collection generate(AtomicBoolean shouldCreate, ExecutionContext ctx) { - if(shouldCreate.get()) { + if (shouldCreate.get()) { return PlainTextParser.builder().build().parse(fileContents) .map(brandNewFile -> (SourceFile) brandNewFile.withSourcePath(Paths.get(relativeFileName))) .collect(Collectors.toList()); @@ -92,13 +95,17 @@ public Collection generate(AtomicBoolean shouldCreate, ExecutionCont @Override public TreeVisitor getVisitor(AtomicBoolean created) { Path path = Paths.get(relativeFileName); - return new PlainTextVisitor() { + return new TreeVisitor() { @Override - public PlainText visitText(PlainText text, ExecutionContext ctx) { - if ((created.get() || Boolean.TRUE.equals(overwriteExisting)) && path.toString().equals(text.getSourcePath().toString())) { - return text.withText(fileContents); + public @Nullable SourceFile visit(@Nullable Tree tree, ExecutionContext executionContext) { + SourceFile sourceFile = (SourceFile) requireNonNull(tree); + if (sourceFile instanceof Quark || sourceFile instanceof Remote || sourceFile instanceof Binary) { + return sourceFile; + } + if ((created.get() || Boolean.TRUE.equals(overwriteExisting)) && path.toString().equals(sourceFile.getSourcePath().toString())) { + return PlainTextParser.convert(sourceFile).withText(fileContents); } - return text; + return sourceFile; } }; } diff --git a/rewrite-test/build.gradle.kts b/rewrite-test/build.gradle.kts index e7f48222158..dc57aa0ed8a 100644 --- a/rewrite-test/build.gradle.kts +++ b/rewrite-test/build.gradle.kts @@ -12,4 +12,6 @@ dependencies { implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-csv") implementation("org.slf4j:slf4j-api:1.7.36") implementation("org.eclipse.jgit:org.eclipse.jgit:5.13.+") + + testImplementation(project(":rewrite-groovy")) } diff --git a/rewrite-test/src/test/java/org/openrewrite/text/CreateTextFileTest.java b/rewrite-test/src/test/java/org/openrewrite/text/CreateTextFileTest.java index 4205b722046..a3dfe9a3b08 100644 --- a/rewrite-test/src/test/java/org/openrewrite/text/CreateTextFileTest.java +++ b/rewrite-test/src/test/java/org/openrewrite/text/CreateTextFileTest.java @@ -17,8 +17,10 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; +import org.openrewrite.Issue; import org.openrewrite.test.RewriteTest; +import static org.openrewrite.groovy.Assertions.groovy; import static org.openrewrite.test.SourceSpecs.text; class CreateTextFileTest implements RewriteTest { @@ -97,4 +99,31 @@ void shouldAddAnotherFile() { ) ); } + + @Test + @Issue("https://github.com/openrewrite/rewrite-jenkins/issues/52") + void shouldOverrideDifferentSourceFileType() { + String after = """ + /* + See the documentation for more options: + https://github.com/jenkins-infra/pipeline-library/ + */ + buildPlugin( + useContainerAgent: true, // Set to `false` if you need to use Docker for containerized tests + configurations: [ + [platform: 'linux', jdk: 21], + [platform: 'windows', jdk: 17], + ])"""; + rewriteRun( + spec -> spec.recipe(new CreateTextFile(after, "Jenkinsfile", true)), + groovy( + """ + #!groovy + buildPlugin() + """, + after, + spec -> spec.path("Jenkinsfile") + ) + ); + } } From 9f3fe7618559141498570b845e22d9209c2fc1e1 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 13 Nov 2023 10:08:57 +0100 Subject: [PATCH 2/2] Address review comments to override any file --- .../org/openrewrite/text/CreateTextFile.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/rewrite-core/src/main/java/org/openrewrite/text/CreateTextFile.java b/rewrite-core/src/main/java/org/openrewrite/text/CreateTextFile.java index 1a4b5b831db..f218d03aba9 100644 --- a/rewrite-core/src/main/java/org/openrewrite/text/CreateTextFile.java +++ b/rewrite-core/src/main/java/org/openrewrite/text/CreateTextFile.java @@ -18,10 +18,7 @@ import lombok.EqualsAndHashCode; import lombok.Value; import org.openrewrite.*; -import org.openrewrite.binary.Binary; import org.openrewrite.internal.lang.Nullable; -import org.openrewrite.quark.Quark; -import org.openrewrite.remote.Remote; import java.nio.file.Path; import java.nio.file.Paths; @@ -74,7 +71,7 @@ public TreeVisitor getScanner(AtomicBoolean shouldCreate) { @Override public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { SourceFile sourceFile = (SourceFile) requireNonNull(tree); - if (path.toString().equals(sourceFile.getSourcePath().toString())) { + if (path.equals(sourceFile.getSourcePath())) { shouldCreate.set(false); } return sourceFile; @@ -97,13 +94,23 @@ public TreeVisitor getVisitor(AtomicBoolean created) { Path path = Paths.get(relativeFileName); return new TreeVisitor() { @Override - public @Nullable SourceFile visit(@Nullable Tree tree, ExecutionContext executionContext) { + public SourceFile visit(@Nullable Tree tree, ExecutionContext executionContext) { SourceFile sourceFile = (SourceFile) requireNonNull(tree); - if (sourceFile instanceof Quark || sourceFile instanceof Remote || sourceFile instanceof Binary) { - return sourceFile; - } - if ((created.get() || Boolean.TRUE.equals(overwriteExisting)) && path.toString().equals(sourceFile.getSourcePath().toString())) { - return PlainTextParser.convert(sourceFile).withText(fileContents); + if ((created.get() || Boolean.TRUE.equals(overwriteExisting)) && path.equals(sourceFile.getSourcePath())) { + if (sourceFile instanceof PlainText) { + return ((PlainText) sourceFile).withText(fileContents); + } + PlainText plainText = PlainText.builder() + .id(sourceFile.getId()) + .sourcePath(sourceFile.getSourcePath()) + .fileAttributes(sourceFile.getFileAttributes()) + .charsetBomMarked(sourceFile.isCharsetBomMarked()) + .text(fileContents) + .build(); + if (sourceFile.getCharset() != null) { + return plainText.withCharset(sourceFile.getCharset()); + } + return plainText; } return sourceFile; }