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

fix: CreateYaml does not work the same as CreateTextFile with precondition #4360

Merged
merged 6 commits into from
Jul 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public TreeVisitor<?, ExecutionContext> getScanner(AtomicBoolean shouldCreate) {
@Override
public Collection<SourceFile> generate(AtomicBoolean shouldCreate, ExecutionContext ctx) {
if (shouldCreate.get()) {
return YamlParser.builder().build().parse("")
return YamlParser.builder().build().parse(getYamlContents(ctx))
.map(brandNewFile -> (SourceFile) brandNewFile.withSourcePath(Paths.get(relativeFileName)))
.collect(Collectors.toList());
}
Expand All @@ -98,13 +98,12 @@ public Collection<SourceFile> generate(AtomicBoolean shouldCreate, ExecutionCont
public TreeVisitor<?, ExecutionContext> getVisitor(AtomicBoolean created) {
Path path = Paths.get(relativeFileName);
return new YamlVisitor<ExecutionContext>() {

@Override
public Yaml visitDocuments(Yaml.Documents documents, ExecutionContext ctx) {
if ((created.get() || Boolean.TRUE.equals(overwriteExisting)) && path.equals(documents.getSourcePath())) {
@Language("yml") String yamlContents = fileContents;
if (yamlContents == null && fileContentsUrl != null) {
yamlContents = Remote.builder(path, URI.create(fileContentsUrl)).build().printAll(ctx);
}
if (Boolean.TRUE.equals(overwriteExisting) && path.equals(documents.getSourcePath())) {
@Language("yml")
String yamlContents = getYamlContents(ctx);
if (StringUtils.isBlank(yamlContents)) {
return documents.withDocuments(emptyList());
}
Expand All @@ -125,4 +124,13 @@ public Yaml visitDocuments(Yaml.Documents documents, ExecutionContext ctx) {
}
};
}

@Language("yml")
private String getYamlContents(ExecutionContext ctx) {
@Language("yml") String yamlContents = fileContents;
if (yamlContents == null && fileContentsUrl != null) {
yamlContents = Remote.builder(Paths.get(relativeFileName), URI.create(fileContentsUrl)).build().printAll(ctx);
}
return yamlContents == null ? "" : yamlContents;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void shouldAddAnotherFile() {
rewriteRun(
spec -> spec.recipe(new CreateYamlFile(
"test/test-file-2.yaml",
null,
"",
null,
true
)),
Expand Down Expand Up @@ -208,4 +208,65 @@ void shouldUseFileContentsWhenContentsAndContentsUrlNotNull() {
)
);
}

@Test
void shouldCreateYamlFromYamlRecipe() {
rewriteRun(spec -> spec.recipeFromYaml("""
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.CreateYamlPrecondition
displayName: Create yaml file with precondition
description: Create a yaml file with a precondition.
recipeList:
- org.openrewrite.yaml.CreateYamlFile:
relativeFileName: created.yml
overwriteExisting: false
fileContents: |
content: yes
""", "org.openrewrite.CreateYamlPrecondition"),
yaml(
"""
foo: bar
""", spec -> spec.path("precondition.yml")),
yaml(
null,
"""
content: yes
""",
spec -> spec.path("created.yml")
)
);
}

@Test
void shouldCreateYamlFromYamlRecipeWithPrecondition() {
rewriteRun(spec -> spec.recipeFromYaml("""
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.CreateYamlPrecondition
displayName: Create yaml file with precondition
description: Create a yaml file with a precondition.
preconditions:
- org.openrewrite.FindSourceFiles:
filePattern: "**/precondition.yml"
recipeList:
- org.openrewrite.yaml.CreateYamlFile:
relativeFileName: created.yml
overwriteExisting: false
fileContents: |
content: yes
""", "org.openrewrite.CreateYamlPrecondition"),
yaml(
"""
foo: bar
""", spec -> spec.path("precondition.yml")),
yaml(
null,
"""
content: yes
""",
spec -> spec.path("created.yml")
)
);
}
}
Loading