Skip to content

Commit

Permalink
Merge branch 'master' into tylerbertrand/develocity-local-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly authored Nov 12, 2024
2 parents 1f6c045 + 0d06326 commit 16705cb
Show file tree
Hide file tree
Showing 16 changed files with 300 additions and 96 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ on:
jobs:
Build:
uses: evanchooly/workflows/.github/workflows/pull-request.yml@master
secrets:
DEVELOCITY_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }}
with:
java: 17
2 changes: 1 addition & 1 deletion build-plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<dependency>
<groupId>com.squareup</groupId>
<artifactId>kotlinpoet-jvm</artifactId>
<version>1.18.1</version>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public interface Aggregation<T> {

/**
* Execute the aggregation. This form and {@link #execute(AggregationOptions)} should be used for pipelines with $out and $merge
* that do no expect any results to be returned.
* that do not expect any results to be returned.
*
* @see #execute(AggregationOptions)
*/
Expand Down
6 changes: 3 additions & 3 deletions critter/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@
<dependency>
<groupId>com.google.devtools.ksp</groupId>
<artifactId>symbol-processing-api</artifactId>
<version>2.0.21-1.0.25</version>
<version>2.0.21-1.0.27</version>
</dependency>
<dependency>
<groupId>com.google.devtools.ksp</groupId>
<artifactId>symbol-processing-common-deps</artifactId>
<version>2.0.21-1.0.25</version>
<version>2.0.21-1.0.27</version>
</dependency>
<dependency>
<groupId>com.google.devtools.ksp</groupId>
<artifactId>symbol-processing-aa-embeddable</artifactId>
<version>2.0.21-1.0.25</version>
<version>2.0.21-1.0.27</version>
</dependency>
<dependency>
<groupId>org.jboss.forge.roaster</groupId>
Expand Down
13 changes: 10 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.compiler.jvmTarget>17</kotlin.compiler.jvmTarget>
<asm.version>9.7.1</asm.version>
<ksp.version>2.0.21-1.0.25</ksp.version>
<ksp.version>2.0.21-1.0.27</ksp.version>
</properties>

<build>
Expand Down Expand Up @@ -115,6 +115,13 @@
</analysisConfigurationFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>1C</forkCount>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
Expand Down Expand Up @@ -210,7 +217,7 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<version>1.20.2</version>
<version>1.20.3</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -310,7 +317,7 @@
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.15.5</version>
<version>1.15.10</version>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
Expand Down
6 changes: 5 additions & 1 deletion rewrite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-recipe-bom</artifactId>
<version>2.21.0</version>
<version>2.21.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -87,6 +87,10 @@
<version>${morphia.prior.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
package dev.morphia.rewrite.recipes;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;

import org.jetbrains.annotations.NotNull;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.J.MethodInvocation;

public class PipelineRewrite extends Recipe {
public class PipelineRewriteStage1 extends Recipe {

static final String AGGREGATION = "dev.morphia.aggregation.Aggregation";
static final MethodMatcher pipeline = new MethodMatcher(PipelineRewrite.AGGREGATION + " pipeline(..)");
static final MethodMatcher pipeline = new MethodMatcher(PipelineRewriteStage1.AGGREGATION + " pipeline(..)");
static final List<MethodMatcher> matchers = List.of(
new MethodMatcher(AGGREGATION + " addFields(dev.morphia.aggregation.stages.AddFields)"),
new MethodMatcher(AGGREGATION + " autoBucket(dev.morphia.aggregation.stages.AutoBucket)"),
Expand Down Expand Up @@ -73,11 +66,6 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {

@Override
public MethodInvocation visitMethodInvocation(MethodInvocation methodInvocation, @NotNull ExecutionContext context) {
return working(methodInvocation, context);
// return notWorking(methodInvocation, context);
}

public MethodInvocation working(MethodInvocation methodInvocation, @NotNull ExecutionContext context) {
if (matchers.stream().anyMatch(matcher -> matcher.matches(methodInvocation))) {
return super.visitMethodInvocation(methodInvocation
.withName(methodInvocation.getName().withSimpleName("pipeline")),
Expand All @@ -87,40 +75,6 @@ public MethodInvocation working(MethodInvocation methodInvocation, @NotNull Exec
}
}

public MethodInvocation notWorking(MethodInvocation methodInvocation, @NotNull ExecutionContext context) {
if (matchers.stream().anyMatch(matcher -> matcher.matches(methodInvocation))) {
MethodInvocation mi = methodInvocation;
List<Expression> arguments = new ArrayList<>();
Expression expression = mi;
while (expression instanceof MethodInvocation invocation) {
arguments.add(invocation.getArguments().get(0));
mi = invocation;
expression = mi.getSelect();
}
Collections.reverse(arguments);

return applyTemplate(expression, arguments, expression);
} else {
return super.visitMethodInvocation(methodInvocation, context);

}
}

private J.MethodInvocation applyTemplate(Expression expression, List<Expression> arguments, Expression target) {
String code = buildTemplate(expression, arguments);
return JavaTemplate.builder(code)
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion())
.build()
.apply(getCursor(), target.getCoordinates().replace());
}

private String buildTemplate(Expression toReplace, List<Expression> arguments) {
StringJoiner joiner = new StringJoiner(",\n\t", toReplace + ".pipeline(", ")");
arguments.forEach(argument -> joiner.add(argument.toString()));

return joiner.toString();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package dev.morphia.rewrite.recipes;

import java.util.ArrayList;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J.MethodInvocation;

public class PipelineRewriteStage2 extends Recipe {

private static final String AGGREGATION = "dev.morphia.aggregation.Aggregation";

private static final MethodMatcher PIPELINE = new MethodMatcher(PipelineRewriteStage2.AGGREGATION + " pipeline(..)");

@Override
public String getDisplayName() {
return "Aggregation pipeline rewrite";
}

@Override
public String getDescription() {
return "Rewrites an aggregation from using stage-named methods to using pipeline(Stage...).";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>(PIPELINE), new JavaIsoVisitor<>() {

public MethodInvocation visitMethodInvocation(MethodInvocation method, ExecutionContext ignored) {
if (!PIPELINE.matches(method.getSelect())) {
return method;
}

var updated = method;
while (PIPELINE.matches(updated.getSelect()) && PIPELINE.matches(((MethodInvocation) updated.getSelect()).getSelect())) {
var select = updated.getSelect();
MethodInvocation invocation = (MethodInvocation) select;
if (PIPELINE.matches(invocation.getSelect())) {
MethodInvocation parent = (MethodInvocation) invocation.getSelect();
var args = new ArrayList<>(parent.getArguments());
args.addAll(invocation.getArguments());
updated = updated.withSelect(((MethodInvocation) invocation.getSelect()).withArguments(args));
}
}
return updated;
}

});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PipelineVisitor extends JavaIsoVisitor<ExecutionContext> {

@Override
public MethodInvocation visitMethodInvocation(MethodInvocation methodInvocation, @NotNull ExecutionContext context) {
if (PipelineRewrite.pipeline.matches(methodInvocation)) {
if (PipelineRewriteStage1.pipeline.matches(methodInvocation)) {
Expression select = methodInvocation.getSelect();
System.out.println("\n\nselect = " + select);
System.out.println("select.getSideEffects() = " + select.getSideEffects());
Expand Down
6 changes: 2 additions & 4 deletions rewrite/src/main/resources/META-INF/rewrite/morphia3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ name: dev.morphia.UpgradeToMorphia30
displayName: Upgrade to Morphia 3.0
description: Adopt new dependencies and breaking changes in moving to Morphia 3.
recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: dev.morphia.aggregation.stages.Out to(..)
newMethodName: out
- dev.morphia.rewrite.recipes.PipelineRewriteStage1:
matchOverrides: "true"
ignoreDefinition: "false"
- dev.morphia.rewrite.recipes.PipelineRewrite:
- dev.morphia.rewrite.recipes.PipelineRewriteStage2:
matchOverrides: "true"
ignoreDefinition: "false"
- org.openrewrite.java.ChangeType:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dev.morphia.rewrite.recipes.test;

public abstract class Morphia2RewriteTest extends MorphiaRewriteTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,53 @@

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;

import io.github.classgraph.ClassGraph;

public abstract class MorphiaRewriteTest implements RewriteTest {
public static final String ARTIFACT;

public static final List<String> classpath;

static {
List<URI> runtimeClasspath = new ClassGraph().disableNestedJarScanning().getClasspathURIs();
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();
if (core.contains("morphia-core")) {
ARTIFACT = "morphia-core";
} else {
ARTIFACT = "morphia/core";
}

classpath.add(ARTIFACT);
}
protected List<URI> runtimeClasspath = new ClassGraph().disableNestedJarScanning().getClasspathURIs();

@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.toArray(new String[0])));
.parser(builder);
}

@NotNull
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())
.collect(ArrayList::new, List::add, List::addAll);
return classpath;
}

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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package dev.morphia.rewrite.recipes.test;

import dev.morphia.rewrite.recipes.PipelineRewrite;
import dev.morphia.rewrite.recipes.PipelineRewriteStage1;

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

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

public class PipelineRewriteTest extends MorphiaRewriteTest {
public class PipelineRewriteStage1Test extends MorphiaRewriteTest {

@Override
@NotNull
protected Recipe getRecipe() {
return new PipelineRewrite();
return new PipelineRewriteStage1();
}

@Test
Expand Down
Loading

0 comments on commit 16705cb

Please sign in to comment.