openrewrite 的rewrite.yml 编写注意事项
1发布给其它project使用,必须存放在META-INF/rewrite下
2Best practices
2.1A file may contain any number of recipes and styles, separated by —.
2.2必须full qualified,即包括完整的namespace/package
2.3自定义recipes不能放在org.openrewrite namespace下
3基本概念
3.1定义recipe和style
4Format
5Preconditions
5.1Preconditions only apply to files that already exist in the source set. They cannot prevent the creation of new files.
5.2Preconditions are used to limit which source files a recipe is allowed to make edits to
5.3可以是检查条件
5.3.1preconditions:
- org.openrewrite.text.Find:
find: 1
5.4常见conditions
5.4.1org.openrewrite.FindSourceFiles - limits the recipe to only run on files whose path matches a glob pattern
5.4.2org.openrewrite.text.Find - limits the recipe to only run on files that contain a given string
5.4.3org.openrewrite.java.search.FindTypes - limits the recipe to run only on source code which contain a given type
5.4.4org.openrewrite.java.search.HasJavaVersion - limits the recipe to run only on Java source code with the specified source or target compatibility versions. Allowing a recipe to be targeted only at Java 8, 11, 17, etc., code.
5.4.5org.openrewrite.java.search.IsLikelyTest - limits the recipe to run only on source code which is likely to be test code.
5.4.6org.openrewrite.java.search.IsLikelyNotTest - limits the recipe to run only on source code which is likely to be production code.
5.5自定义组合-or/and
5.5.1example of or
type: specs.openrewrite.org/v1beta/recipe
name: org.sample.DoSomething
displayName: Do Something
preconditions:- org.sample.FindAnyJson
recipeList:- org.openrewrite.json.ChangeKey:oldKeyPath: $.foonewKey: bar
---
type: specs.openrewrite.org/v1beta/recipe
name: org.sample.FindAnyJson
recipeList:- org.openrewrite.FindSourceFiles:filePattern: "**/my.json"- org.openrewrite.FindSourceFiles:filePattern: "**/your.json"- org.openrewrite.FindSourceFiles:filePattern: "**/our.json"
6Style
6.1type: specs.openrewrite.org/v1beta/style
6.2example
6.2.1—
type: specs.openrewrite.org/v1beta/style
name: com.yourorg.YesTabsNoStarImports
styleConfigs:- org.openrewrite.java.style.TabsAndIndentsStyle:useTabCharacter: true- org.openrewrite.java.style.ImportLayoutStyle:classCountToUseStarImport: 9999
7dependency
7.1注意需要将相关recipe 归属artificial 加入到dependency
7.2example
---
type: specs.openrewrite.org/v1beta/recipe
name: com.radish.gz.MyStaticAnalysis
displayName: Test Common static analysis issues
description: Test of Resolve common static analysis issues (also known as SAST issues).
recipeList:- org.openrewrite.staticanalysis.AbstractClassPublicConstructor- org.openrewrite.staticanalysis.AtomicPrimitiveEqualsUsesGet- org.openrewrite.staticanalysis.BigDecimalDoubleConstructorRecipe- org.openrewrite.staticanalysis.BigDecimalRoundingConstantsToEnums
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-static-analysis</artifactId>
<scope>runtime</scope>
</dependency>
8运行态
8.1运行时候体系为composited recipe
8.2注意composite其实用到很多地方,在配置了多个recipe的运行时候,openrewrite会通过composited 来控制执行次序