Skip to content
Draft
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 @@ -118,7 +118,7 @@ void generate() throws IOException {

// Merge generated marketplace into existing one
// This ensures existing data is preserved and updated with generated data
existing.merge(generated);
existing.getRoot().merge(generated.getRoot());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkschneider not sure if you wish to do this another way

marketplace = existing;
} else {
getLogger().info("No existing recipes.csv found, creating new file");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
import org.gradle.api.tasks.TaskAction;
import org.gradle.jvm.tasks.Jar;
import org.openrewrite.Validated;
import org.openrewrite.config.ClasspathScanningLoader;
import org.openrewrite.config.Environment;
import org.openrewrite.marketplace.RecipeMarketplace;
import org.openrewrite.marketplace.RecipeMarketplaceCompletenessValidator;
import org.openrewrite.marketplace.RecipeMarketplaceReader;
import org.openrewrite.maven.marketplace.RecipeClassLoader;
import org.openrewrite.maven.marketplace.ResolvedMavenRecipeBundle;
import org.openrewrite.maven.tree.ResolvedGroupArtifactVersion;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Properties;

import static java.util.stream.Collectors.toList;

Expand Down Expand Up @@ -102,22 +102,9 @@ void validate() {
.collect(toList());

// Load environment from JAR
// We create a temporary GAV just for loading the environment
ResolvedGroupArtifactVersion tempGav = new ResolvedGroupArtifactVersion(
null,
"temp",
"temp",
"1.0.0",
null
);
ResolvedMavenRecipeBundle bundle = new ResolvedMavenRecipeBundle(
tempGav,
recipeJarPath,
classpath,
RecipeClassLoader::new,
null
);
Environment jarEnvironment = bundle.getEnvironment();
Environment jarEnvironment = Environment.builder()
.load(new ClasspathScanningLoader(new Properties(), new RecipeClassLoader(recipeJarPath, classpath)))
.build();

// Validate completeness
RecipeMarketplaceCompletenessValidator validator = new RecipeMarketplaceCompletenessValidator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -33,26 +32,27 @@
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SuppressWarnings("ResultOfMethodCallIgnored")
class RecipeMarketplaceCsvGenerateTaskTest {
@TempDir
File projectDir;
Path projectDir;

private File settingsFile;
private File buildFile;
private Path settingsFile;
private Path buildFile;
private Path csvFile;

@BeforeEach
public void setup() {
settingsFile = new File(projectDir, "settings.gradle");
buildFile = new File(projectDir, "build.gradle");
settingsFile = projectDir.resolve("settings.gradle");
buildFile = projectDir.resolve("build.gradle");
csvFile = Path.of(projectDir.toString(), "src", "main", "resources", "META-INF", "rewrite", "recipes.csv");
}

@Test
void generatesRecipeCsvFromJar() throws Exception {
createSimpleRecipeProject();

BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withProjectDir(projectDir.toFile())
.withArguments("recipeCsvGenerate", "--info", "--stacktrace")
.withPluginClasspath()
.withDebug(true)
Expand All @@ -62,7 +62,6 @@ void generatesRecipeCsvFromJar() throws Exception {
assertEquals(SUCCESS, requireNonNull(result.task(":recipeCsvGenerate")).getOutcome());

// Read and verify CSV content
File csvFile = new File(projectDir, "src/main/resources/META-INF/rewrite/recipes.csv");
assertThat(csvFile)
.content()
.contains("name")
Expand All @@ -74,17 +73,16 @@ void mergesWithExistingRecipeCsv() throws Exception {
createSimpleRecipeProject();

// Create existing recipes.csv with custom entry
File csvFile = new File(projectDir, "src/main/resources/META-INF/rewrite/recipes.csv");
csvFile.getParentFile().mkdirs();
Files.writeString(csvFile.toPath(),
Files.createDirectories(csvFile.getParent());
Files.writeString(csvFile,
//language=csv
"""
name,displayName,category1
org.example.CustomRecipe,Custom Recipe,Custom
name,displayName,packageName,ecosystem,category1
org.example.CustomRecipe,Custom Recipe,org.example:example-custom,Maven,Custom
""");

BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withProjectDir(projectDir.toFile())
.withArguments("recipeCsvGenerate", "--info", "--stacktrace")
.withPluginClasspath()
.withDebug(true)
Expand All @@ -105,16 +103,15 @@ void mergesRecipesFromBothSources() throws Exception {
createSimpleRecipeProject();

// Create existing recipes.csv with same recipe but in different category
File csvFile = new File(projectDir, "src/main/resources/META-INF/rewrite/recipes.csv");
csvFile.getParentFile().mkdirs();
Files.writeString(csvFile.toPath(),
Files.createDirectories(csvFile.getParent());
Files.writeString(csvFile,
"""
name,displayName,category1
org.example.TestRecipe,Old Display Name,Old Category
ecosystem,packageName,name,displayName,category1
maven,org.example:test-recipe-project,org.example.TestRecipe,Old Display Name,Old Category
""");

BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withProjectDir(projectDir.toFile())
.withArguments("recipeCsvGenerate", "--info", "--stacktrace")
.withPluginClasspath()
.withDebug(true)
Expand Down Expand Up @@ -154,7 +151,7 @@ void usesNebulaPublicationForGav() throws Exception {
createSimpleRecipeClass();

BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withProjectDir(projectDir.toFile())
.withArguments("recipeCsvGenerate", "--info", "--stacktrace")
.withPluginClasspath()
.withDebug(true)
Expand All @@ -173,13 +170,13 @@ void createsParentDirectoriesIfNeeded() throws Exception {
createSimpleRecipeProject();

// Delete the resources directory if it exists
File resourcesDir = new File(projectDir, "src/main/resources");
if (resourcesDir.exists()) {
deleteDirectory(resourcesDir.toPath());
Path resourcesDir = Path.of(projectDir.toString(), "src", "main", "resources");
if (Files.exists(resourcesDir)) {
deleteDirectory(resourcesDir);
}

BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withProjectDir(projectDir.toFile())
.withArguments("recipeCsvGenerate", "--info", "--stacktrace")
.withPluginClasspath()
.withDebug(true)
Expand All @@ -188,9 +185,8 @@ void createsParentDirectoriesIfNeeded() throws Exception {
assertEquals(SUCCESS, requireNonNull(result.task(":recipeCsvGenerate")).getOutcome());

// Assert parent directories were created
File csvFile = new File(projectDir, "src/main/resources/META-INF/rewrite/recipes.csv");
assertThat(csvFile.getParentFile()).exists().isDirectory();
assertThat(csvFile).exists().isFile();
assertThat(csvFile.getParent()).exists().isDirectory();
assertThat(csvFile).exists().isNotEmptyFile();
}

private void createSimpleRecipeProject() throws IOException {
Expand All @@ -216,11 +212,10 @@ private void createSimpleRecipeProject() throws IOException {
createSimpleRecipeClass();
}

@SuppressWarnings("NullableProblems")
private void createSimpleRecipeClass() throws IOException {
// Use a declarative YAML recipe instead of Java class for simpler testing
File rewriteDir = new File(projectDir, "src/main/resources/META-INF/rewrite");
rewriteDir.mkdirs();
Path rewriteDir = Path.of(projectDir.toString(), "src", "main", "resources", "META-INF", "rewrite");
Files.createDirectories(rewriteDir);

@Language("yaml")
String rewriteYml = """
Expand All @@ -234,12 +229,12 @@ private void createSimpleRecipeClass() throws IOException {
toText: "Hello World"
""";

Files.writeString(new File(rewriteDir, "rewrite.yml").toPath(), rewriteYml);
Files.writeString(rewriteDir.resolve("rewrite.yml"), rewriteYml);
}

private void createGradleBuildFiles(@Language("gradle") String buildFileContent) throws IOException {
Files.writeString(settingsFile.toPath(), "rootProject.name = 'test-recipe-project'");
Files.writeString(buildFile.toPath(), buildFileContent);
Files.writeString(settingsFile, "rootProject.name = 'test-recipe-project'");
Files.writeString(buildFile, buildFileContent);
}

private void deleteDirectory(Path path) throws IOException {
Expand Down
Loading
Loading