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 c926ab1859c..4d9309a8597 100644 --- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/CreateYamlFile.java +++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/CreateYamlFile.java @@ -21,8 +21,10 @@ import org.openrewrite.*; import org.openrewrite.internal.StringUtils; import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.remote.Remote; import org.openrewrite.yaml.tree.Yaml; +import java.net.URI; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collection; @@ -49,6 +51,13 @@ public class CreateYamlFile extends ScanningRecipe { @Nullable String fileContents; + @Option(displayName = "File contents URL", + description = "URL to file containing text content for the file. Use either `fileContents` or `fileContentsUrl` option.", + example = "http://foo.bar/baz.yaml", + required = false) + @Nullable + String fileContentsUrl; + @Option(displayName = "Overwrite existing file", description = "If there is an existing file, should it be overwritten.", required = false) @@ -92,14 +101,18 @@ public TreeVisitor getVisitor(AtomicBoolean created) { @Override public Yaml visitDocuments(Yaml.Documents documents, ExecutionContext ctx) { if ((created.get() || Boolean.TRUE.equals(overwriteExisting)) && path.equals(documents.getSourcePath())) { - if (StringUtils.isBlank(fileContents)) { + @Language("yml") String yamlContents = fileContents; + if (yamlContents == null && fileContentsUrl != null) { + yamlContents = Remote.builder(path, URI.create(fileContentsUrl)).build().printAll(ctx); + } + if (StringUtils.isBlank(yamlContents)) { return documents.withDocuments(emptyList()); } - if (documents.printAll().equals(fileContents)) { + if (documents.printAll().equals(yamlContents)) { return documents; } Optional sourceFiles = YamlParser.builder().build() - .parse(fileContents) + .parse(yamlContents) .findFirst(); if (sourceFiles.isPresent()) { SourceFile sourceFile = sourceFiles.get(); 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 31868ca9692..d78f2cd0e25 100644 --- a/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java +++ b/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java @@ -15,10 +15,27 @@ */ package org.openrewrite.yaml; +import org.intellij.lang.annotations.Language; import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; +import org.openrewrite.HttpSenderExecutionContextView; +import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.remote.RemoteArtifactCache; +import org.openrewrite.remote.RemoteExecutionContextView; +import org.openrewrite.test.MockHttpSender; import org.openrewrite.test.RewriteTest; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.function.Consumer; + import static org.openrewrite.yaml.Assertions.yaml; class CreateYamlFileTest implements RewriteTest { @@ -35,6 +52,7 @@ void hasCreatedFile() { spec -> spec.recipe(new CreateYamlFile( "test/test.yaml", fileContents, + null, null )), yaml( @@ -52,6 +70,7 @@ void hasOverwrittenFile() { spec -> spec.recipe(new CreateYamlFile( "test/test.yaml", "after: true", + null, true )), yaml( @@ -68,6 +87,7 @@ void shouldNotChangeExistingFile() { spec -> spec.recipe(new CreateYamlFile( "test/test.yaml", null, + null, false )), yaml( @@ -85,6 +105,7 @@ void shouldNotChangeExistingFileWhenOverwriteNull() { spec -> spec.recipe(new CreateYamlFile( "test/test.yaml", null, + null, null )), yaml( @@ -100,6 +121,7 @@ void shouldAddAnotherFile() { spec -> spec.recipe(new CreateYamlFile( "test/test-file-2.yaml", null, + null, true )), yaml( @@ -113,4 +135,77 @@ void shouldAddAnotherFile() { ) ); } + + @Test + void shouldDownloadFileContents() { + @Language("yml") + String yamlContent = """ + # This is a comment + foo: x + bar: + z: y + """; + InMemoryExecutionContext ctx = new InMemoryExecutionContext(e -> e.printStackTrace()); + MockHttpSender httpSender = new MockHttpSender(() -> + new ByteArrayInputStream(yamlContent.getBytes())); + HttpSenderExecutionContextView.view(ctx) + .setHttpSender(httpSender) + .setLargeFileHttpSender(httpSender); + RemoteExecutionContextView.view(ctx).setArtifactCache(new RemoteArtifactCache() { + @Override + public @Nullable Path get(URI uri) { + return null; + } + + @Override + public @Nullable Path put(URI uri, InputStream is, Consumer onError) { + try { + var file = File.createTempFile("rewrite", "yaml"); + Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING); + return file.toPath(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + }); + rewriteRun( + spec -> spec + .executionContext(ctx) + .recipeExecutionContext(ctx) + .recipe(new CreateYamlFile( + "test/test.yaml", + null, + "http://fake.url/test.yaml", + true) + ), + yaml( + null, + yamlContent, + spec -> spec.path("test/test.yaml") + ) + ); + } + + @Test + void shouldUseFileContentsWhenContentsAndContentsUrlNotNull() { + @Language("yml") + String fileContents = """ + # This is a comment + x: + y: z + """; + rewriteRun( + spec -> spec.recipe(new CreateYamlFile( + "test/test.yaml", + fileContents, + "http://foo.bar/baz.yaml", + true) + ), + yaml( + null, + fileContents, + spec -> spec.path("test/test.yaml") + ) + ); + } }