Files
gh-sjungling-claude-plugins…/skills/recipe-writer/examples/example-declarative-migration.yml
2025-11-30 08:57:41 +08:00

258 lines
8.3 KiB
YAML

---
# ==============================================================================
# Example: Framework Migration Recipe
# ==============================================================================
# This example demonstrates a realistic declarative recipe for migrating
# from one version of a framework to another, combining multiple recipes
# with configuration.
#
# Save this file in: src/main/resources/META-INF/rewrite/
type: specs.openrewrite.org/v1beta/recipe
name: com.yourorg.MigrateToFrameworkX
displayName: Migrate to Framework X 2.0
description: |
Migrates applications from Framework X 1.x to 2.0. This recipe performs the following steps:
- Updates dependency versions
- Migrates renamed packages and types
- Updates deprecated API usage
- Applies code formatting
tags:
- framework-x
- migration
- upgrade
estimatedEffortPerOccurrence: PT15M
recipeList:
# Step 1: Update dependencies
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: com.example.frameworkx
oldArtifactId: frameworkx-core
newGroupId: com.example.frameworkx
newArtifactId: frameworkx-core
newVersion: 2.0.x
- org.openrewrite.java.dependencies.AddDependency:
groupId: com.example.frameworkx
artifactId: frameworkx-new-module
version: 2.0.x
onlyIfUsing: com.example.frameworkx.NewFeature
configuration: implementation
# Step 2: Package and type migrations
- org.openrewrite.java.ChangePackage:
oldPackageName: com.example.frameworkx.old
newPackageName: com.example.frameworkx.v2
recursive: true
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: com.example.frameworkx.OldConfig
newFullyQualifiedTypeName: com.example.frameworkx.v2.NewConfig
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: com.example.frameworkx.OldClient
newFullyQualifiedTypeName: com.example.frameworkx.v2.Client
# Step 3: Method migrations
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.example.frameworkx.v2.Client execute(..)
newMethodName: run
# Step 4: Apply custom recipes (if you have any)
# - com.yourorg.CustomFrameworkXMigration
# Step 5: Format the code
- org.openrewrite.java.format.AutoFormat
---
# ==============================================================================
# Example: Security Fixes Recipe
# ==============================================================================
# This recipe applies common security fixes across a codebase
type: specs.openrewrite.org/v1beta/recipe
name: com.yourorg.ApplySecurityFixes
displayName: Apply security best practices
description: Applies a collection of security-related fixes and improvements to the codebase.
tags:
- security
- SAST
recipeList:
# Use secure random number generation
- org.openrewrite.java.security.SecureRandom
# Fix SQL injection vulnerabilities
- org.openrewrite.java.security.UseFileCreateTempFile
# Apply static analysis security fixes
- org.openrewrite.staticanalysis.CommonStaticAnalysis
# Remove unused imports
- org.openrewrite.java.format.RemoveUnusedImports
---
# ==============================================================================
# Example: Testing Framework Migration
# ==============================================================================
# Migrates from JUnit 4 to JUnit 5
type: specs.openrewrite.org/v1beta/recipe
name: com.yourorg.MigrateToJUnit5
displayName: Migrate to JUnit 5
description: Migrates JUnit 4 tests to JUnit 5 (Jupiter).
tags:
- junit
- testing
- junit5
preconditions:
- org.openrewrite.java.search.UsesType:
fullyQualifiedTypeName: org.junit.Test
recipeList:
# Update dependencies
- org.openrewrite.java.dependencies.RemoveDependency:
groupId: junit
artifactId: junit
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.junit.jupiter
artifactId: junit-jupiter
version: 5.9.x
onlyIfUsing: org.junit.Test
configuration: testImplementation
# Migrate annotations and assertions
- org.openrewrite.java.testing.junit5.JUnit4to5Migration
# Format
- org.openrewrite.java.format.AutoFormat
---
# ==============================================================================
# Example: Code Quality Improvements
# ==============================================================================
# A collection of code quality improvements
type: specs.openrewrite.org/v1beta/recipe
name: com.yourorg.ImproveCodeQuality
displayName: Improve code quality
description: Applies a comprehensive set of code quality improvements.
tags:
- code-quality
- refactoring
- best-practices
recipeList:
# Static analysis
- org.openrewrite.staticanalysis.CommonStaticAnalysis
# Finalize local variables that aren't reassigned
- org.openrewrite.staticanalysis.FinalizeLocalVariables
# Add missing @Override annotations
- org.openrewrite.staticanalysis.MissingOverrideAnnotation
# Simplify boolean expressions
- org.openrewrite.staticanalysis.SimplifyBooleanExpression
# Remove unnecessary null checks
- org.openrewrite.staticanalysis.UnnecessaryNullCheckBeforeInstanceOf
# Format code
- org.openrewrite.java.format.AutoFormat
---
# ==============================================================================
# Example: Simple Type Replacement
# ==============================================================================
# A focused recipe that just replaces one type with another
type: specs.openrewrite.org/v1beta/recipe
name: com.yourorg.ReplaceStringUtils
displayName: Replace Apache Commons StringUtils with Spring StringUtils
description: Replaces usage of Apache Commons StringUtils with Spring Framework's StringUtils.
recipeList:
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.commons.lang3.StringUtils
newFullyQualifiedTypeName: org.springframework.util.StringUtils
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.springframework
artifactId: spring-core
version: 6.0.x
onlyIfUsing: org.springframework.util.StringUtils
configuration: implementation
---
# ==============================================================================
# TEST EXAMPLE
# ==============================================================================
# Here's how to test a declarative recipe:
#
# ```java
# package com.yourorg;
#
# import org.junit.jupiter.api.Test;
# import org.openrewrite.java.JavaParser;
# import org.openrewrite.test.RecipeSpec;
# import org.openrewrite.test.RewriteTest;
#
# import static org.openrewrite.java.Assertions.java;
#
# class MigrateToFrameworkXTest implements RewriteTest {
#
# @Override
# public void defaults(RecipeSpec spec) {
# spec
# // Load the recipe from resources
# .recipeFromResources("com.yourorg.MigrateToFrameworkX")
# // Add dependencies needed to compile the "before" code
# .parser(JavaParser.fromJavaVersion()
# .classpath("frameworkx-core-1.0"));
# }
#
# @Test
# void migratesFrameworkXCode() {
# rewriteRun(
# java(
# """
# package com.example;
#
# import com.example.frameworkx.old.OldConfig;
#
# class Example {
# OldConfig config;
# }
# """,
# """
# package com.example;
#
# import com.example.frameworkx.v2.NewConfig;
#
# class Example {
# NewConfig config;
# }
# """
# )
# );
# }
# }
# ```
# ==============================================================================
# TIPS FOR DECLARATIVE RECIPES
# ==============================================================================
# 1. Keep them focused - one migration or fix per recipe
# 2. Use meaningful names that describe what the recipe does
# 3. Document the purpose and steps in the description
# 4. Add tags for searchability
# 5. Use preconditions to avoid running on irrelevant files
# 6. Order matters - recipes run sequentially
# 7. Consider adding AutoFormat at the end
# 8. Test each recipe thoroughly
# 9. Wrap string values in quotes if they contain special characters
# 10. Use estimatedEffortPerOccurrence to help users understand the impact