Skip to content

Commit

Permalink
CreateTextFile should override files of different type (#3682)
Browse files Browse the repository at this point in the history
* CreateTextFile should override files of different type

* Address review comments to override any file
  • Loading branch information
timtebeek authored Nov 13, 2023
1 parent 42ebe52 commit 368b253
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public TreeVisitor<?, ExecutionContext> 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;
Expand All @@ -81,7 +81,7 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {

@Override
public Collection<SourceFile> 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());
Expand All @@ -92,13 +92,27 @@ public Collection<SourceFile> generate(AtomicBoolean shouldCreate, ExecutionCont
@Override
public TreeVisitor<?, ExecutionContext> getVisitor(AtomicBoolean created) {
Path path = Paths.get(relativeFileName);
return new PlainTextVisitor<ExecutionContext>() {
return new TreeVisitor<SourceFile, ExecutionContext>() {
@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 SourceFile visit(@Nullable Tree tree, ExecutionContext executionContext) {
SourceFile sourceFile = (SourceFile) requireNonNull(tree);
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 text;
return sourceFile;
}
};
}
Expand Down
2 changes: 2 additions & 0 deletions rewrite-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
)
);
}
}

0 comments on commit 368b253

Please sign in to comment.