Skip to content

Commit

Permalink
Bump OpenRewrite (#917)
Browse files Browse the repository at this point in the history
* Add build file for other modules

* Bumped junit-pioneer to 2.1.0

* Bumped openrewrite to 8.4.2

* Tweak build file
  • Loading branch information
fabapp2 committed Sep 14, 2023
1 parent cc02869 commit 3f60688
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 56 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/build-sbm-revamp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Build SBM Legacy
on:
push:
branches:
- "**"
branches-ignore:
- "main"
jobs:
build:
runs-on: ubuntu-latest
steps:

- name: Checkout
uses: actions/checkout@v3

- name: Setup Java
uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: 17
cache: 'maven'

- name: Maven Build
run: mvn -DskipTests --batch-mode clean package
22 changes: 20 additions & 2 deletions sbm-support-rewrite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>3.1.3</spring-boot.version>
<rewrite.version>8.1.6</rewrite.version>
<rewrite.version>8.4.2</rewrite.version>
<rewrite-spring.version>5.0.5</rewrite-spring.version>
<rewrite-maven-plugin.version>5.3.2</rewrite-maven-plugin.version>
<maven.version>3.9.1</maven.version>
Expand All @@ -31,7 +31,7 @@
<plexus-cypher.version>1.8</plexus-cypher.version>
<maven-invoker.version>3.2.0</maven-invoker.version>
<jaxb-api.version>2.3.1</jaxb-api.version>
<junit-pioneer.version>2.0.1</junit-pioneer.version>
<junit-pioneer.version>2.1.0</junit-pioneer.version>
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
<maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
</properties>
Expand Down Expand Up @@ -291,6 +291,24 @@
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.16.0</version>
<!-- <configuration>-->
<!-- <properties>-->
<!-- <property>-->
<!-- <name>manchu.version</name>-->
<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>com.foo.bar</groupId>-->
<!-- <artifactId>manchu-wibble</artifactId>-->
<!-- </dependency>-->
<!-- </dependencies>-->
<!-- </property>-->
<!-- </properties>-->
<!-- </configuration>-->
</plugin>
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2021 - 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sbm.parsers.events;

import org.openrewrite.Parser;
import org.openrewrite.SourceFile;

/**
* @author Fabian Krüger
*/
public record FinishedParsingResourceEvent(Parser.Input input, SourceFile sourceFile){
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2021 - 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.sbm.parsers.events;

/**
* @author Fabian Krüger
*/
public record IntermediateParsingEvent(String stateMessage) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.openrewrite.SourceFile;
import org.openrewrite.tree.ParsingEventListener;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

/**
* Adapter listening to OpenRewrite ParsingEvents and publishing them as Spring application events.
Expand All @@ -34,9 +33,19 @@ public class RewriteParsingEventListenerAdapter implements ParsingEventListener

private final ApplicationEventPublisher eventPublisher;

@Override
public void intermediateMessage(String stateMessage) {
eventPublisher.publishEvent(new IntermediateParsingEvent(stateMessage));
}

@Override
public void startedParsing(Parser.Input input) {
eventPublisher.publishEvent(new StartedParsingResourceEvent(input));
}

@Override
public void parsed(Parser.Input input, SourceFile sourceFile) {
log.debug("Parsed %s to %s".formatted(input.getPath(), sourceFile.getSourcePath()));
eventPublisher.publishEvent(new ParsedResourceEvent(input, sourceFile));
eventPublisher.publishEvent(new FinishedParsingResourceEvent(input, sourceFile));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
package org.springframework.sbm.parsers.events;

import org.openrewrite.Parser;
import org.openrewrite.SourceFile;
import org.springframework.context.ApplicationEvent;

/**
* @author Fabian Krüger
*/
public record ParsedResourceEvent(Parser.Input input, SourceFile sourceFile){
public record StartedParsingResourceEvent(Parser.Input input){
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.springframework.core.io.Resource;
import org.springframework.sbm.boot.autoconfigure.ScannerConfiguration;
import org.springframework.sbm.parsers.events.SuccessfullyParsedProjectEvent;
import org.springframework.sbm.parsers.events.ParsedResourceEvent;
import org.springframework.sbm.parsers.events.FinishedParsingResourceEvent;
import org.springframework.sbm.parsers.events.StartedParsingProjectEvent;

import java.nio.file.Path;
Expand All @@ -51,7 +51,7 @@ public class ParserEventPublicationIntegrationTest {
@Autowired
ParserProperties parserProperties;

private static List<ParsedResourceEvent> capturedEvents = new ArrayList<>();
private static List<FinishedParsingResourceEvent> capturedEvents = new ArrayList<>();
private static StartedParsingProjectEvent startedParsingEvent;
private static SuccessfullyParsedProjectEvent finishedParsingEvent;

Expand Down Expand Up @@ -90,8 +90,8 @@ void shouldPublishParsingEvents() {
static class TestEventListener {


@EventListener(ParsedResourceEvent.class)
public void onEvent(ParsedResourceEvent event) {
@EventListener(FinishedParsingResourceEvent.class)
public void onEvent(FinishedParsingResourceEvent event) {
capturedEvents.add(event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.event.EventListener;
import org.springframework.sbm.parsers.events.ParsedResourceEvent;
import org.springframework.sbm.parsers.events.FinishedParsingResourceEvent;

import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -40,7 +40,7 @@ public class RewriteMavenProjectParserIntegrationTest {
@Autowired
private RewriteMavenProjectParser sut;

private static List<ParsedResourceEvent> capturedEvents = new ArrayList<>();
private static List<FinishedParsingResourceEvent> capturedEvents = new ArrayList<>();

@Test
@ExpectedToFail("Parsing order of pom files is not correct, see https://github.com/openrewrite/rewrite-maven-plugin/pull/601")
Expand Down Expand Up @@ -70,8 +70,8 @@ void testFailingProject() {

@TestConfiguration
static class TestEventListener {
@EventListener(ParsedResourceEvent.class)
public void onEvent(ParsedResourceEvent event) {
@EventListener(FinishedParsingResourceEvent.class)
public void onEvent(FinishedParsingResourceEvent event) {
capturedEvents.add(event);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,18 @@ void parseComplexMavenReactorProject() {
RewriteMavenProjectParser projectParser = sut;
ExecutionContext executionContext = new InMemoryExecutionContext(t -> t.printStackTrace());
List<String> parsedFiles = new ArrayList<>();
ParsingExecutionContextView.view(executionContext).setParsingListener((Parser.Input input, SourceFile sourceFile) -> {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
.withLocale(Locale.US)
.withZone(ZoneId.systemDefault());
String format = dateTimeFormatter.format(Instant.now());
System.out.println("%s: Parsed file: %s".formatted(format, sourceFile.getSourcePath()));
parsedFiles.add(sourceFile.getSourcePath().toString());
ParsingExecutionContextView.view(executionContext).setParsingListener(new ParsingEventListener() {
@Override
public void parsed(Parser.Input input, SourceFile sourceFile) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
.withLocale(Locale.US)
.withZone(ZoneId.systemDefault());
String format = dateTimeFormatter.format(Instant.now());
System.out.println("%s: Parsed file: %s".formatted(format, sourceFile.getSourcePath()));
parsedFiles.add(sourceFile.getSourcePath().toString());
}
});

parserProperties.setIgnoredPathPatterns(Set.of("**/testcode/**", ".rewrite/**", "internal/**"));
RewriteProjectParsingResult parsingResult = projectParser.parse(
projectRoot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,41 +53,41 @@ class RewriteProjectParserTest {

@Language("xml")
String pomXml = """
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>root-project</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>
</project>
""";
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>root-project</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>
</project>
""";

@Language("java")
String javaClass = """
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyMain {
public static void main(String[] args){
SpringApplication.run(MyMain.class, args);
}
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyMain {
public static void main(String[] args){
SpringApplication.run(MyMain.class, args);
}
""";
}
""";

@Test
@DisplayName("Parse simple Maven project")
Expand All @@ -98,7 +98,9 @@ void parseSimpleMavenProject(@TempDir Path tempDir) {
MavenMojoProjectParserFactory mavenMojoProjectParserFactory = new MavenMojoProjectParserFactory(parserProperties);
MavenArtifactCache mavenArtifactCache = new LocalMavenArtifactCache(Paths.get(System.getProperty("user.home"), ".m2", "repository"));
@Nullable MavenSettings mavenSettings = null;
Consumer<Throwable> onError = (t) -> {throw new RuntimeException(t);};
Consumer<Throwable> onError = (t) -> {
throw new RuntimeException(t);
};
MavenMojoProjectParserPrivateMethods mavenMojoParserPrivateMethods = new MavenMojoProjectParserPrivateMethods(mavenMojoProjectParserFactory, new RewriteMavenArtifactDownloader(mavenArtifactCache, mavenSettings, onError));
RewriteProjectParser projectParser = new RewriteProjectParser(
new MavenExecutor(new MavenExecutionRequestFactory(new MavenConfigFileParser()), new MavenPlexusContainer()),
Expand All @@ -115,9 +117,14 @@ void parseSimpleMavenProject(@TempDir Path tempDir) {
);
ExecutionContext executionContext = new InMemoryExecutionContext(t -> t.printStackTrace());
List<String> parsedFiles = new ArrayList<>();
ParsingExecutionContextView.view(executionContext).setParsingListener((Parser.Input input, SourceFile sourceFile) -> {
parsedFiles.add(sourceFile.getSourcePath().toString());
});
ParsingExecutionContextView.view(executionContext).setParsingListener(
new ParsingEventListener() {
@Override
public void parsed(Parser.Input input, SourceFile sourceFile) {
parsedFiles.add(sourceFile.getSourcePath().toString());
}
}
);

List<Resource> resources = List.of(
new DummyResource(basePath.resolve("pom.xml"), pomXml),
Expand Down

0 comments on commit 3f60688

Please sign in to comment.