Skip to content

Commit

Permalink
Address review comments to override any file
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Nov 13, 2023
1 parent 5f743cc commit 9f3fe76
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions rewrite-core/src/main/java/org/openrewrite/text/CreateTextFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,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 @@ -97,13 +94,23 @@ public TreeVisitor<?, ExecutionContext> getVisitor(AtomicBoolean created) {
Path path = Paths.get(relativeFileName);
return new TreeVisitor<SourceFile, ExecutionContext>() {
@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;
}
Expand Down

0 comments on commit 9f3fe76

Please sign in to comment.