diff --git a/rewrite-yaml/src/main/java/org/openrewrite/yaml/CreateYamlFile.java b/rewrite-yaml/src/main/java/org/openrewrite/yaml/CreateYamlFile.java index 4d9309a8597..4ef52c58ddd 100644 --- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/CreateYamlFile.java +++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/CreateYamlFile.java @@ -87,7 +87,7 @@ public TreeVisitor getScanner(AtomicBoolean shouldCreate) { @Override public Collection 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()); } @@ -98,13 +98,12 @@ public Collection generate(AtomicBoolean shouldCreate, ExecutionCont public TreeVisitor getVisitor(AtomicBoolean created) { Path path = Paths.get(relativeFileName); return new YamlVisitor() { + @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()); } @@ -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; + } } diff --git a/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java b/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java index d78f2cd0e25..f590b822b8a 100644 --- a/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java +++ b/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java @@ -120,7 +120,7 @@ void shouldAddAnotherFile() { rewriteRun( spec -> spec.recipe(new CreateYamlFile( "test/test-file-2.yaml", - null, + "", null, true )), @@ -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") + ) + ); + } }