https://github.com/spring-projects/spring-rewrite-commons
https://github.com/spring-projects/spring-rewrite-commons
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/spring-projects/spring-rewrite-commons
- Owner: spring-projects
- License: apache-2.0
- Created: 2023-11-14T23:02:37.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-03T17:17:22.000Z (8 months ago)
- Last Synced: 2025-01-29T14:23:04.054Z (4 months ago)
- Language: Java
- Size: 737 KB
- Stars: 11
- Watchers: 5
- Forks: 11
- Open Issues: 26
-
Metadata Files:
- Readme: README.adoc
- Contributing: CONTRIBUTING.adoc
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
= Spring Rewrite Commons
:partials_dir: spring-rewrite-commons-docs/src/main/antora/modules/ROOT/pages/partials
:project-version: 0.1.0-SNAPSHOT
:projectVersion: {project-version}
:docs: https://docs.spring.io/spring-rewrite-commons/docs/current-SNAPSHOT/reference/html/[quote]
____
Spring Rewrite Commons provides a set of components to parse a Java project to https://github.com/openrewrite[OpenRewrite,window=_blank] LST and apply recipes outside a build tool plugin.
____== Get started
=== Add Dependency
**Maven**
[source,xml,indent=0,subs="verbatim,quotes,attributes",role="primary"]
----org.springframwork.rewrite
spring-rewrite-commons-launcher
{project-version}----
**Gradle**
[source,groovy,indent=0,subs="verbatim,quotes,attributes",role="secondary"s]
----
compile 'org.springframework.rewrite:spring-rewrite-commons-launcher:{projectVersion}'
----=== Implement a Recipe Launcher
[source,java]
....
package com.acme.example;import org.openrewrite.Recipe;
import org.openrewrite.SourceFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.rewrite.RewriteProjectParser;
import org.springframework.rewrite.parser.RewriteProjectParsingResult;
import org.springframework.rewrite.resource.ProjectResourceSet;
import org.springframework.rewrite.resource.ProjectResourceSetFactory;
import org.springframework.rewrite.resource.ProjectResourceSetSerializer;
import org.springframework.rewrite.RewriteRecipeDiscovery;
import org.springframework.stereotype.Component;import java.nio.file.Path;
@Component
public class MyMigrationApplication {@Autowired <1>
private RewriteProjectParser parser;@Autowired
private RewriteRecipeDiscovery discovery; <2>@Autowired
private ProjectResourceSetFactory resourceSetFactory;@Autowired
private ProjectResourceSetSerializer serializer;public void migrateToBoot3_1() {
Path baseDir = Path.of("."); <2>
String recipeName = "org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_1";
Recipe boot3Upgrade = discovery.getByName(recipeName); <3>RewriteProjectParsingResult result = parser.parse(baseDir); <4>
List lst = result.sourceFiles(); <5>
ProjectResourceSet resourceSet = resourceSetFactory.create(baseDir, lst); <6>
resourceSet.apply(boot3Upgrade); <7>
serializer.writeChanges(resourceSet); <8>
}
}
....
<1> All components are Spring beans and can be injected as such.
<2> The path of the project that should be migrated.
<3> `RewriteRecipeDiscovery` is used to discover an OpenRewrite recipe by name.
<4> `RewriteProjectParser` parses a given project to OpenRewrite LST.
<5> The result contains the list of ``SourceFile``s (the LST).
<6> `ProjectResourceSetFactory` can be used to create a `ProjectResourceSet`.
<7> The recipe is applied to the `ProjectResourceSet` which wraps the LST.
<8> `ProjectResourceSetSerializer` is used to serialize the changes to disk.== Reference documentation
Find the reference documentation link:{docs}[here].
== Contributing
https://help.github.com/articles/creating-a-pull-request[Pull requests] are welcome. Note, that we expect everyone to follow the https://github.com/spring-projects/.github/blob/main/CODE_OF_CONDUCT.md[code of conduct].
== License
Spring Rewrite Commons is Open Source software released under the
https://www.apache.org/licenses/LICENSE-2.0.html[Apache 2.0 license].