|
| 1 | +# sbm-openrewrite-launcher |
| 2 | +these components |
| 3 | + |
| 4 | +The `sbm-openrewrite-launcher` module provides these components |
| 5 | + |
| 6 | +- `ProjectScanner` - to scan a project to a list of `Resource`s |
| 7 | +- `RewriteProjectParser` - to parse the list of `Resource`s that belong to the build to OpenRewrite's AST representation |
| 8 | +- `RecipeDiscovery` - to provide access to recipes found at possible locations (Jar, classpath, ...) |
| 9 | +
|
| 10 | +## Parse a project |
| 11 | + |
| 12 | +Use ProjectScanner to scan a given baseDir |
| 13 | +[source,java] |
| 14 | +.... |
| 15 | +Path baseDir = Path.of("...").toAbsolutePath.normalize(); |
| 16 | +List<Resource> resources = projectScanner.scan(baseDir); |
| 17 | +.... |
| 18 | + |
| 19 | +Create ExecutionContext, it will be populated with messages during the parsing. |
| 20 | +These messages are important for other recipes |
| 21 | +[source,java] |
| 22 | +.... |
| 23 | +ExecutionContext ctx = new InMemoryExecutionContext(t -> t.printStackTrace()); |
| 24 | +.... |
| 25 | + |
| 26 | +The RewriteParser parses a given baseDir to ORs AST |
| 27 | +[source,java] |
| 28 | +.... |
| 29 | +RewriteProjectParser parser = new RewriteProjectParser(); |
| 30 | +List<SourceFile> ast = parser.parse(baseDir, resources, ctx); |
| 31 | +.... |
| 32 | + |
| 33 | +## Discover recipes |
| 34 | +[source,java] |
| 35 | +.... |
| 36 | +RecipeDiscovery discovery = new RewriteRecipeDiscovery(); |
| 37 | +List<String> activeRecipes = ... |
| 38 | +List<Recipe> recipes = discovery.discoverFilteredRecipes(activeRecipes, properties); |
| 39 | +.... |
| 40 | + |
| 41 | +## Run recipes |
| 42 | +[source,java] |
| 43 | +.... |
| 44 | +RecipeRun result = recipes.get(0).run(ast, ctx); |
| 45 | +.... |
| 46 | + |
| 47 | + |
| 48 | +## Handle Recipe Result |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +The `OpenRewriteProjectParser` aims to provide the exact same parsing capabilities as the parsing in `rewrite-maven-plugin` and `rewrite-gradle-plugin`. |
| 53 | + |
| 54 | +This allows parsing a given project to it's OpenRewrite AST representation and then provide the AST to OR recipes and visitors to run OpenRewrite recipes without requiring a build plugin. |
| 55 | + |
| 56 | +## Running OpenRewrite recipes |
| 57 | + |
| 58 | +### Scan the codebase |
| 59 | + |
| 60 | +The `RewriteProjectParser` uses Spring's `Resource` abstraction to abstract from file system. |
| 61 | +This allows testing without file access among other things. |
| 62 | +The `ProjectScanner` allows to scan all resources under a given `Path`. |
| 63 | + |
| 64 | +[source,java] |
| 65 | +.... |
| 66 | +@Component |
| 67 | +
|
| 68 | +public class ProjectScannerClient { |
| 69 | + public List<Resource> scan(Path baseDir) { |
| 70 | +
|
| 71 | + } |
| 72 | +} |
| 73 | +@Autowired |
| 74 | +private ProjectScanner scanner; |
| 75 | +
|
| 76 | +List<Resource> resources = scanner.scan(projectRoot); |
| 77 | +
|
| 78 | +.... |
| 79 | + |
| 80 | +### Parse the scanned resources |
| 81 | + |
| 82 | +### Discover recipes |
| 83 | + |
| 84 | +### Run recipe |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +[source,java] |
| 90 | +.... |
| 91 | +import org.openrewrite.InMemoryExecutionContext;import org.openrewrite.LargeSourceSet; |
| 92 | +import org.openrewrite.Recipe; |
| 93 | +import org.openrewrite.internal.InMemoryLargeSourceSet |
| 94 | +import org.springframework.sbm.recipes.RewriteRecipeDiscovery; |
| 95 | +import java.util.List;import java.util.Optional; |
| 96 | +
|
| 97 | +@Component |
| 98 | +@RequiredArgsConstructor |
| 99 | +public class MyTool { |
| 100 | +
|
| 101 | + // The parser is a Spring bean |
| 102 | + private final RewriteProjectParser parser; |
| 103 | + // RecipeDiscovery is a Spring bean |
| 104 | + private final RewriteRecipeDiscovery recipeDiscovery; |
| 105 | + // ProjectScanner is a Spring bean |
| 106 | + private final ProjectScanner projectScanner; |
| 107 | +
|
| 108 | + public RecipeResult runRecipe(Path baseDir, String recipeName) { |
| 109 | + ExecutionContext ctx = new InMemoryExecutionContext(); |
| 110 | + List<Resource> resources = projectScanner.scan(baseDir); |
| 111 | + List<SourceFile> ast = parser.parse(baseDir, resources, ctx); |
| 112 | + Xml.Document rootPom = findRootPom(ast); |
| 113 | + recipeDiscovery.discoverFilteredRecipe(rootPom, recipeName) |
| 114 | + .ifPresent(recipe -> recipe.run(new InMemoryLargeSourceSet(ast), ctx)); |
| 115 | + } |
| 116 | +} |
| 117 | +.... |
| 118 | + |
| 119 | +[plantuml,"class-design","svg"] |
| 120 | +.... |
| 121 | +class RewriteProjectParser { |
| 122 | + parse(..) |
| 123 | +} |
| 124 | +
|
| 125 | +class MavenProjectParser { |
| 126 | +
|
| 127 | +} |
| 128 | +
|
| 129 | +MavenProjectParser ..> BuildFileParser |
| 130 | +BuildFileParser ..> RewriteMavenMojoProjectParser |
| 131 | +
|
| 132 | +class GradleProjectParser { |
| 133 | +} |
| 134 | +.... |
| 135 | + |
| 136 | +Example code showing how to apply OpenRewrite's UpgradeSpringBoot_3_1 recipe |
| 137 | + |
| 138 | +[source, java] |
| 139 | +..... |
| 140 | +import org.openrewrite.*; |
| 141 | +import org.openrewrite.internal.InMemoryLargeSourceSet; |
| 142 | +import org.springframework.beans.factory.annotation.Autowired; |
| 143 | +import org.springframework.boot.CommandLineRunner; |
| 144 | +import org.springframework.boot.SpringApplication; |
| 145 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 146 | +import org.springframework.core.io.Resource; |
| 147 | +import org.springframework.sbm.parsers.ProjectScanner; |
| 148 | +import org.springframework.sbm.parsers.RewriteMavenProjectParser; |
| 149 | +import org.springframework.sbm.parsers.RewriteProjectParsingResult; |
| 150 | +import org.springframework.sbm.recipes.RewriteRecipeDiscovery; |
| 151 | +
|
| 152 | +import java.nio.file.Path; |
| 153 | +import java.util.List; |
| 154 | +import java.util.Set; |
| 155 | +
|
| 156 | +/** |
| 157 | + * @author Fabian Krüger |
| 158 | + */ |
| 159 | +@SpringBootApplication |
| 160 | +public class BootUpgrade implements CommandLineRunner { |
| 161 | + public static void main(String[] args) { |
| 162 | + SpringApplication.run(BootUpgrade.class, args); |
| 163 | + } |
| 164 | +
|
| 165 | + @Autowired |
| 166 | + ProjectScanner scanner; |
| 167 | + @Autowired |
| 168 | + RewriteMavenProjectParser parser; |
| 169 | + @Autowired |
| 170 | + RewriteRecipeDiscovery discovery; |
| 171 | +
|
| 172 | + @Override |
| 173 | + public void run(String... args) throws Exception { |
| 174 | +
|
| 175 | + String path = "demo-spring-song-app"; |
| 176 | + Path baseDir = Path.of(path ).toAbsolutePath().normalize(); |
| 177 | + System.out.println(baseDir); |
| 178 | + if(!baseDir.toFile().exists() || !baseDir.toFile().isDirectory()) { |
| 179 | + throw new IllegalArgumentException("Given path '%s' does not exist or is not a directory.".formatted(path)); |
| 180 | + } |
| 181 | + List<Resource> resources = scanner.scan(baseDir, Set.of("**/.idea/**", "**/.DS_Store", "**/.git/**")); |
| 182 | + ExecutionContext ctx = new InMemoryExecutionContext(t -> {throw new RuntimeException(t);}); |
| 183 | + RewriteProjectParsingResult parsingResult = parser.parse(baseDir/*, resources*/, ctx); |
| 184 | + String recipeName = "org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_1"; |
| 185 | + List<Recipe> recipes = discovery.discoverRecipes(); |
| 186 | + recipes.stream() |
| 187 | + .filter(r -> recipeName.equals(r.getName())) |
| 188 | + .forEach(r -> { |
| 189 | + System.out.println("Applying recipe '%s'".formatted(r.getName())); |
| 190 | + LargeSourceSet lss = new InMemoryLargeSourceSet(parsingResult.sourceFiles()); |
| 191 | + RecipeRun recipeRun = r.run(lss, ctx); |
| 192 | + recipeRun.getChangeset().getAllResults().stream() |
| 193 | + .map(Result::diff) |
| 194 | + .forEach(System.out::println); |
| 195 | + }); |
| 196 | + } |
| 197 | +} |
| 198 | +..... |
0 commit comments