Skip to content

Commit 85d0994

Browse files
authored
Load dependent recipes in a declarative from an existing RecipeMarketplace (#6584)
1 parent 934b45e commit 85d0994

3 files changed

Lines changed: 39 additions & 5 deletions

File tree

rewrite-core/src/main/java/org/openrewrite/config/YamlResourceLoader.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import org.openrewrite.internal.ObjectMappers;
2525
import org.openrewrite.internal.PropertyPlaceholderHelper;
2626
import org.openrewrite.internal.RecipeLoader;
27+
import org.openrewrite.marketplace.RecipeBundleResolver;
28+
import org.openrewrite.marketplace.RecipeListing;
29+
import org.openrewrite.marketplace.RecipeMarketplace;
2730
import org.openrewrite.style.NamedStyles;
2831
import org.openrewrite.style.Style;
2932
import org.yaml.snakeyaml.LoaderOptions;
@@ -87,6 +90,34 @@ private enum ResourceType {
8790
}
8891
}
8992

93+
public YamlResourceLoader(InputStream yamlInput, URI source, Properties properties, RecipeMarketplace marketplace, Collection<RecipeBundleResolver> resolvers) {
94+
this.source = source;
95+
this.dependencyResourceLoaders = emptyList();
96+
this.mapper = ObjectMappers.propertyBasedMapper(getClass().getClassLoader());
97+
this.recipeLoader = (recipeName, options) -> {
98+
RecipeListing listing = marketplace.findRecipe(recipeName);
99+
if (listing == null) {
100+
throw new IllegalStateException("Unable to find recipe " + recipeName + " listed in " + source);
101+
}
102+
return listing.prepare(resolvers, options == null ? emptyMap() : options);
103+
};
104+
105+
maybeAddKotlinModule(mapper);
106+
107+
try {
108+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
109+
int nRead;
110+
byte[] data = new byte[8192];
111+
while ((nRead = yamlInput.read(data, 0, data.length)) != -1) {
112+
buffer.write(data, 0, nRead);
113+
}
114+
this.yamlSource = propertyPlaceholderHelper.replacePlaceholders(
115+
new String(buffer.toByteArray(), StandardCharsets.UTF_8), properties);
116+
} catch (IOException e) {
117+
throw new UncheckedIOException(e);
118+
}
119+
}
120+
90121
/**
91122
* Load a declarative recipe using the runtime classloader
92123
*

rewrite-core/src/main/java/org/openrewrite/marketplace/YamlRecipeBundleReader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.io.InputStream;
2525
import java.net.URI;
26+
import java.util.Collection;
2627
import java.util.Map;
2728
import java.util.Properties;
2829

@@ -33,8 +34,8 @@ public class YamlRecipeBundleReader implements RecipeBundleReader {
3334
private final @Getter RecipeBundle bundle;
3435
private final YamlResourceLoader yamlLoader;
3536

36-
public YamlRecipeBundleReader(RecipeBundle bundle, InputStream yamlLoader, URI source, Properties properties) {
37-
this.yamlLoader = new YamlResourceLoader(yamlLoader, source, properties);
37+
public YamlRecipeBundleReader(RecipeBundle bundle, InputStream yamlLoader, URI source, Properties properties, RecipeMarketplace marketplace, Collection<RecipeBundleResolver> resolvers) {
38+
this.yamlLoader = new YamlResourceLoader(yamlLoader, source, properties, marketplace, resolvers);
3839
this.bundle = bundle;
3940
}
4041

rewrite-core/src/main/java/org/openrewrite/marketplace/YamlRecipeBundleResolver.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@
1717

1818
import lombok.RequiredArgsConstructor;
1919

20-
import java.io.IOException;
2120
import java.io.InputStream;
2221
import java.net.URI;
2322
import java.nio.file.Files;
2423
import java.nio.file.Path;
2524
import java.nio.file.Paths;
25+
import java.util.Collection;
2626
import java.util.Properties;
2727

2828
@RequiredArgsConstructor
2929
public class YamlRecipeBundleResolver implements RecipeBundleResolver {
3030
private final Properties properties;
31+
private final RecipeMarketplace marketplace;
32+
private final Collection<RecipeBundleResolver> resolvers;
3133

3234
@Override
3335
public String getEcosystem() {
@@ -40,7 +42,7 @@ public RecipeBundleReader resolve(RecipeBundle bundle) {
4042
Path path = Paths.get(bundle.getPackageName());
4143
if (Files.exists(path)) {
4244
try (InputStream is = Files.newInputStream(path)) {
43-
return new YamlRecipeBundleReader(bundle, is, path.toUri(), properties);
45+
return new YamlRecipeBundleReader(bundle, is, path.toUri(), properties, marketplace, resolvers);
4446
}
4547
}
4648
} catch (Exception ignored) {
@@ -49,7 +51,7 @@ public RecipeBundleReader resolve(RecipeBundle bundle) {
4951
try {
5052
URI resource = URI.create(bundle.getPackageName());
5153
try (InputStream is = resource.toURL().openStream()) {
52-
return new YamlRecipeBundleReader(bundle, is, resource, properties);
54+
return new YamlRecipeBundleReader(bundle, is, resource, properties, marketplace, resolvers);
5355
}
5456
} catch (Exception e) {
5557
throw new RuntimeException(e);

0 commit comments

Comments
 (0)