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

CreateTextFile should override files of different type #3682

Merged
merged 2 commits into from
Nov 13, 2023
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
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"))
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
}
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")
)
);
}
}
Loading