-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Example app running Spring Boot Upgrade
- Loading branch information
Showing
7 changed files
with
204 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
sbm-support-rewrite/src/test/java/org/springframework/sbm/example/BootUpgradeExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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.example; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.SourceFile; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.sbm.parsers.ProjectScanner; | ||
import org.springframework.sbm.parsers.RewriteProjectParser; | ||
import org.springframework.sbm.parsers.RewriteProjectParsingResult; | ||
import org.springframework.sbm.project.resource.ProjectResourceSet; | ||
import org.springframework.sbm.project.resource.ProjectResourceSetFactory; | ||
import org.springframework.sbm.project.resource.ProjectResourceSetSerializer; | ||
import org.springframework.sbm.recipes.RewriteRecipeDiscovery; | ||
import org.springframework.sbm.test.util.GitTestHelper; | ||
|
||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
import static java.util.stream.Collectors.joining; | ||
|
||
@SpringBootApplication | ||
public class BootUpgradeExample implements CommandLineRunner { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(BootUpgradeExample.class, args); | ||
} | ||
|
||
@Autowired | ||
ProjectScanner scanner; | ||
@Autowired | ||
RewriteProjectParser parser; | ||
@Autowired | ||
RewriteRecipeDiscovery discovery; | ||
@Autowired | ||
ProjectResourceSetSerializer serializer; | ||
@Autowired | ||
ProjectResourceSetFactory factory; | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
|
||
// Clone Spring PetClinic using Boot 2.7 | ||
String tmpDir = System.getProperty("java.io.tmpdir"); | ||
String target = tmpDir + "/cloned"; | ||
FileUtils.forceDelete(Path.of(target).toFile()); | ||
Path baseDir = GitTestHelper.cloneProjectCommit("https://github.com/spring-projects/spring-petclinic.git", target, "9ecdc1111e3da388a750ace41a125287d9620534"); | ||
|
||
// parse | ||
RewriteProjectParsingResult parsingResult = parser.parse(baseDir); | ||
List<SourceFile> sourceFiles = parsingResult.sourceFiles(); | ||
|
||
// create ProjectResourceSet | ||
ProjectResourceSet projectResourceSet = factory.create(baseDir, sourceFiles); | ||
|
||
// find recipe | ||
String recipeName = "org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_1"; | ||
List<Recipe> recipes = discovery.discoverRecipes(); | ||
System.out.println("Discovered %d recipes.".formatted(recipes.size())); | ||
Recipe recipe = recipes.stream().filter(r -> r.getName().equals(recipeName)).findFirst().get(); | ||
|
||
// apply recipe | ||
System.out.println("Apply recipe '%s'".formatted(recipe.getName())); | ||
projectResourceSet.apply(recipe); | ||
|
||
// write changes to fs | ||
serializer.writeChanges(projectResourceSet); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
sbm-support-rewrite/src/test/java/org/springframework/sbm/test/util/GitTestHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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.test.util; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.openrewrite.shaded.jgit.api.Git; | ||
import org.openrewrite.shaded.jgit.api.errors.GitAPIException; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* @author Fabian Krüger | ||
*/ | ||
public class GitTestHelper { | ||
|
||
public static Path cloneProjectCommit(String url, String target, String commitHash) { | ||
return checkout(url, target, commitHash); | ||
} | ||
|
||
public static Path cloneProjectTag(String url, String target, String tag) { | ||
String ref = "refs/tags/" + tag; | ||
return checkout(url, target, ref); | ||
} | ||
|
||
@NotNull | ||
private static Path checkout(String url, String target, String ref) { | ||
try { | ||
File directory = Path.of(target).toFile(); | ||
if (!directory.exists()) { | ||
FileUtils.forceMkdir(directory.toPath().toAbsolutePath().normalize().toFile()); | ||
} | ||
Git git = Git.cloneRepository() | ||
.setDirectory(directory) | ||
.setURI(url) | ||
.call(); | ||
|
||
git.checkout() | ||
.setName(ref) | ||
.call(); | ||
|
||
return git.getRepository().getDirectory().toPath().getParent(); | ||
} catch (GitAPIException e) { | ||
throw new RuntimeException(e); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |