Building with Codegen
Reusable Codemods
Codegen enables you to create reusable code transformations using Python functions decorated with @codegen.function
. These codemods can be shared, versioned, and run by your team.
Creating Codemods
The easiest way to create a new codemod is using the CLI create command:
This creates a new codemod in your .codegen/codemods
directory:
Codemods are stored in .codegen/codemods/name/name.py
and are tracked in Git for easy sharing.
AI-Powered Generation with -d
You can use AI to generate an initial implementation by providing a description:
This will:
- Generate an implementation based on your description
- Create a custom system prompt that you can provide to an IDE chat assistant (learn more about working with AI)
- Place both files in the codemod directory
Running Codemods
Once created, run your codemod using:
The execution flow:
- Codegen parses your codebase into a graph representation
- Your codemod function is executed against this graph
- Changes are tracked and applied to your filesystem
- A diff preview shows what changed
Codemod Structure
A codemod consists of three main parts:
- The
@codegen.function
decorator that names your codemod - A
run
function that takes aCodebase
parameter - Your transformation logic using the Codebase API
Arguments
Codemods can accept arguments using Pydantic models:
Run it with:
Directory Structure
Your codemods live in a dedicated directory structure:
Was this page helpful?