Skip to content

Commit

Permalink
recipes now working.
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly committed Nov 12, 2024
1 parent b9e37c6 commit 75dd250
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
package dev.morphia.rewrite.recipes.test;

import org.jetbrains.annotations.NotNull;

public abstract class Morphia2RewriteTest extends MorphiaRewriteTest {
@Override
protected @NotNull String findMorphiaCore() {
var core = runtimeClasspath.stream()
.filter(uri -> {
String string = uri.toString();
return string.contains("morphia") && string.contains("core");
})
.findFirst().orElseThrow().toString();

final String artifact = core.contains("morphia-core") ? "morphia-core" : "morphia/core";
return artifact;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import java.io.File;
import java.net.URI;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import org.jetbrains.annotations.NotNull;
import org.openrewrite.Recipe;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaParser.Builder;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

Expand All @@ -16,31 +18,38 @@
public abstract class MorphiaRewriteTest implements RewriteTest {
protected List<URI> runtimeClasspath = new ClassGraph().disableNestedJarScanning().getClasspathURIs();

public String[] classpath() {
List<String> classpath = findMongoArtifacts();

classpath.add(findMorphiaCore());
return classpath.toArray(new String[0]);
}

@Override
public void defaults(RecipeSpec spec) {
// spec.recipe(getRecipe())
// .parser(JavaParser.fromJavaVersion()
// .classpath(classpath()));

Builder<? extends JavaParser, ?> builder = JavaParser.fromJavaVersion()
.addClasspathEntry(Path.of(findMorphiaCore()));
findMongoDependencies().stream()
.map(Path::of)
.forEach(builder::addClasspathEntry);
spec.recipe(getRecipe())
.parser(JavaParser.fromJavaVersion()
.classpath(classpath()));
.parser(builder);
}

@NotNull
protected List<String> findMongoArtifacts() {
protected List<String> findMongoDependencies() {
List<String> classpath = runtimeClasspath.stream()
.filter(uri -> uri.toString().contains("mongodb") || uri.toString().contains("bson"))
.map(uri -> new File(uri).getAbsolutePath()/* .getName().replaceAll("-[0-9].*", "") */)
.map(uri -> new File(uri).getAbsolutePath())
.collect(ArrayList::new, List::add, List::addAll);
return classpath;
}

@NotNull
protected abstract String findMorphiaCore();
protected @NotNull String findMorphiaCore() {
var core = runtimeClasspath.stream()
.filter(uri -> uri.toString().contains("morphia-core"))
.map(uri -> new File(uri).getAbsolutePath())
.findFirst().orElseThrow().toString();

return core;
}

@NotNull
protected abstract Recipe getRecipe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import static org.openrewrite.java.Assertions.java;

public class PipelineRewriteStage1Test extends Morphia2RewriteTest {
public class PipelineRewriteStage1Test extends MorphiaRewriteTest {

@Override
@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,24 @@ public void update(Aggregation<?> aggregation) {
"""));
}

@Override
protected @NotNull String findMorphiaCore() {
return classesFolder;
}

public String[] classpath() {
return findMongoArtifacts().toArray(new String[0]);
}
/*
* @Override
* protected @NotNull String findMorphiaCore() {
* return classesFolder;
* }
*/

/*
* public String[] classpath() {
* return findMongoArtifacts().toArray(new String[0]);
* }
*/

@Override
public void defaults(RecipeSpec spec) {
Builder<? extends JavaParser, ?> builder = JavaParser.fromJavaVersion()
.addClasspathEntry(Path.of(classesFolder));
findMongoArtifacts().stream().map(Path::of)
findMongoDependencies().stream().map(Path::of)
.forEach(builder::addClasspathEntry);
spec.recipe(getRecipe())
.parser(builder);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
package dev.morphia.rewrite.recipes.test;

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import dev.morphia.rewrite.recipes.UnwrapFieldExpressions;

import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.openrewrite.Recipe;

import io.github.classgraph.ClassGraph;

import static org.openrewrite.java.Assertions.java;

public class UnwrapFieldTest extends Morphia2RewriteTest {

@Override
public String[] classpath() {
List<URI> runtimeClasspath = new ClassGraph().disableNestedJarScanning().getClasspathURIs();
List<String> classpath = runtimeClasspath.stream()
.filter(uri -> {
String string = uri.toString();
return string.contains("mongodb") || string.contains("bson");
})
.map(uri -> {
return new File(uri).getName().replaceAll("-[0-9].*", "");
})
.collect(ArrayList::new, List::add, List::addAll);
var core = runtimeClasspath.stream()
.filter(uri -> {
String string = uri.toString();
return string.contains("morphia") && string.contains("core");
})
.findFirst().orElseThrow().toString();

final String artifact = core.contains("morphia-core") ? "morphia-core" : "morphia/core";

classpath.add(artifact);
return classpath.toArray(new String[0]);
}
public class UnwrapFieldTest extends MorphiaRewriteTest {

/*
* @Override
* public String[] classpath() {
* List<URI> runtimeClasspath = new ClassGraph().disableNestedJarScanning().getClasspathURIs();
* List<String> classpath = runtimeClasspath.stream()
* .filter(uri -> {
* String string = uri.toString();
* return string.contains("mongodb") || string.contains("bson");
* })
* .map(uri -> {
* return new File(uri).getName().replaceAll("-[0-9].*", "");
* })
* .collect(ArrayList::new, List::add, List::addAll);
* var core = runtimeClasspath.stream()
* .filter(uri -> {
* String string = uri.toString();
* return string.contains("morphia") && string.contains("core");
* })
* .findFirst().orElseThrow().toString();
*
* final String artifact = core.contains("morphia-core") ? "morphia-core" : "morphia/core";
*
* classpath.add(artifact);
* return classpath.toArray(new String[0]);
* }
*/

@Override
@NotNull
Expand Down

0 comments on commit 75dd250

Please sign in to comment.