Skip to content

docs: fixes homepage + many guides #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions docs/cli/reset.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: "Reset Command"
sidebarTitle: "reset"
icon: "rotate-left"
iconType: "solid"
---

The `reset` command performs a hard reset of your git repository while carefully preserving all files in the `.codegen` directory. This is useful for undoing codemod changes while keeping your codemod implementations intact.

## Usage

```bash
codegen reset
```

## What it Does

When you run `codegen reset`, it:

1. Backs up all files in `.codegen` directory, preserving their content and staged/unstaged status
2. Performs a hard reset (`git reset --hard HEAD`) on the repository
3. Restores all `.codegen` files to their previous state, including their git staging status
4. Removes untracked files (except those in `.codegen`)

<Note>
This is more sophisticated than `git checkout .` as it:
- Preserves both staged and unstaged changes in `.codegen`
- Handles deleted files correctly
- Removes untracked files (like `git clean`) while protecting `.codegen`
</Note>

## Examples

Reset after testing a codemod:
```bash
# Run your codemod
codegen run organize-imports

# If the changes aren't what you wanted
codegen reset # Reverts changes but keeps your codemod implementation
```


<Warning>
This command performs a hard reset and removes untracked files. Make sure you have committed any changes you want to keep outside of `.codegen`.
</Warning>
Binary file added docs/images/set-interpreter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/system-prompt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 8 additions & 12 deletions docs/introduction/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ icon: "bolt"
iconType: "solid"
---

Follow our step-by-step tutorial to get up and running with Codegen.
A quick tour of Codegen in a Jupyter notebook.

## Installation

We recommend using [uv](https://github.com/astral-sh/uv) to install Codegen's CLI globally.

First, install `uv` if you haven't already:

```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
```

Then install Codegen:
Install [`codegen` on pypi](https://pypi.org/project/codegen/) via [uv](https://github.com/astral-sh/uv):

```bash
uv tool install codegen
Expand All @@ -28,17 +20,21 @@ uv tool install codegen
keeping its dependencies isolated.
</Note>


## Quick Start with Jupyter

The [codgen notebook](/cli/notebook) command creates a virtual environment and opens a jupyter notebook for quick prototyping. This is often the fastest way to get up and running.
The [codgen notebook](/cli/notebook) command creates a virtual environment and opens a Jupyter notebook for quick prototyping. This is often the fastest way to get up and running.

<Tip>
Prefer working in your IDE? See [IDE Usage](/introduction/ide-usage)
</Tip>


```bash
# Navigate to your repository
cd path/to/git/repository

# Initialize codegen and launch Jupyter
codegen init
codegen notebook
```

Expand Down
145 changes: 145 additions & 0 deletions docs/introduction/ide-usage.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
---
title: "Using Codegen in Your IDE"
sidebarTitle: "IDE Usage"
icon: "window"
iconType: "solid"
---

Get up and running with Codegen programs in IDEs like VSCode, Cursor and PyCharm.

<Tip>Make sure to [install and initialize](/introduction/installation) Codegen with `codegen init`</Tip>

## Configuring your IDE Interpreter

Codegen creates a custom Python environment in `.codegen/.venv`. Configure your IDE to use this environment for the best development experience.

<AccordionGroup>
<Accordion title="VSCode, Cursor and Windsurf" icon="window-maximize">
1. Open the Command Palette (Cmd/Ctrl + Shift + P)
2. Type "Python: Select Interpreter"
<img src="/images/set-interpreter.png" />
3. Choose "Enter interpreter path"
4. Navigate to and select:
```bash
.codegen/.venv/bin/python
```

Alternatively, create a `.vscode/settings.json`:
```json
{
"python.defaultInterpreterPath": "${workspaceFolder}/.codegen/.venv/bin/python",
"python.analysis.extraPaths": [
"${workspaceFolder}/.codegen/.venv/lib/python3.11/site-packages"
]
}
```
</Accordion>

<Accordion title="PyCharm" icon="python">
1. Open PyCharm Settings/Preferences
2. Navigate to "Project > Python Interpreter"
3. Click the gear icon ⚙️ and select "Add"
4. Choose "Existing Environment"
5. Set interpreter path to:
```bash
.codegen/.venv/bin/python
```

For better type hints:
1. Right-click on `.codegen/.venv`
2. Mark Directory as > Sources Root
</Accordion>

<Accordion title="Troubleshooting" icon="wrench">
Common issues and solutions:

- **Missing Completions**: Make sure the interpreter is correctly set and the environment has `codegen` installed
- **Import Errors**: Verify the environment is activated and Python path includes the virtual environment
- **Wrong Python Version**: Check that you're using Python 3.11 or higher in `.codegen/.venv`

Run this to verify your environment:
```bash
.codegen/.venv/bin/python -c "import codegen; print(codegen.__version__)"
```
</Accordion>
</AccordionGroup>


## Create a New Codemod

Generate the boilerplate for a new code manipulation program using [codegen create](/cli/create):

```bash
codegen create organize-types \
-d "Move all TypeScript types to \
into a centralized types.ts file"
```

<Tip>
Passing in `-d --description` will get an LLM expert to compose an initial version for you. This requires a Github account registered on [codegen.sh](https://codegen.sh)
</Tip>

This will:
1. Create a new codemod in `.codegen/codemods/organize_types/`
2. Generate a custom `system-prompt.txt` based on your task
3. Set up the basic structure for your program

<Note>
The generated codemod includes type hints and docstrings, making it easy to get IDE autocompletion and documentation.
</Note>

## Iterating with Chat Assistants

When you do `codegen init`, you will receive a [system prompt optimized for AI consumption](/introduction/work-with-ai) at `.codegen/codegen-system-prompt.txt`.

If you reference this file in "chat" sessions with Copilot, Cursor, Cody, etc., the assistant will become fluent in Codegen.

<Frame>
<img src="/images/system-prompt.png" />
Collaborating with Cursor's assistant and the Codegen system prompt
</Frame>

In addition, when you [create](/cli/create) a codemod with "-d", Codegen generates an optimized system prompt in `.codegen/codemods/{name}/{name}-system-prompt.txt`. This prompt contains:
- Relevant Codegen API documentation
- Examples of relevant transformations
- Context about your specific task

<Tip>
You can also drag and drop the system prompt ([available here](/introduction/work-with-ai))file directly into chat windows like ChatGPT or Claude for standalone help.
</Tip>

## Running and Testing Codemods

```bash
# Run => write changes to disk
codegen run organize-types

# Reset changes on disk
codegen reset
```

<Tip>You can also run the program directly via `.codegen/.venv/bin/python path/to/codemod.py` or via your editor's debugger</Tip>

## Viewing Changes

We recommend viewing changes in your IDE's native diff editor.


## What's Next

<CardGroup cols={2}>
<Card
title="Explore Tutorials"
icon="graduation-cap"
href="/tutorials/at-a-glance"
>
See real-world examples of codemods in action.
</Card>
<Card
title="Codegen Guides"
icon="book"
href="/building-with-codegen/at-a-glance"
>
Learn about Codegen's core concepts and features
</Card>
</CardGroup>
111 changes: 111 additions & 0 deletions docs/introduction/installation.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
---
title: "Installation"
sidebarTitle: "Installation"
icon: "download"
iconType: "solid"
---

Install and set up Codegen in your development environment.

## Prerequisites

We recommend using [uv](https://github.com/astral-sh/uv) for installation. If you haven't installed `uv` yet:
```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
```

## Installing Codegen

Install the Codegen CLI globally:

```bash
uv tool install codegen
```


<Note>
This makes the `codegen` command available globally in your terminal, while keeping its dependencies isolated.
</Note>

## Quick Start

Let's walk through a simple example of using Codegen in a project:

1. Navigate to your repository:
```bash
cd path/to/your/project
```

2. Initialize Codegen in your project:
```bash
codegen init
```

This creates a `.codegen/` directory with:
```bash
.codegen/
├── .venv/ # Python virtual environment (gitignored)
├── config.toml # Project configuration
├── codemods/ # Your codemod implementations
├── jupyter/ # Jupyter notebooks for exploration
└── codegen-system-prompt.txt # AI system prompt
```

3. Create your first codemod with [codegen create](/cli/create):
```bash
codegen create organize-imports \
-d "Sort and organize imports according to PEP8"
```
<Note>
The `-d` flag in `codegen create` generates an AI-powered implementation. This requires a Github account registered on [codegen.sh](https://codegen.sh)
</Note>



4. Run your codemod with [codegen run](/cli/run):
```bash
codegen run organize-imports
```

5. Reset any filesystem changes (excluding `.codegen/*`) with [codegen reset](/cli/reset):
```bash
codegen reset
```

## Next Steps

<CardGroup cols={2}>
<Card
title="IDE Integration"
icon="window"
href="/introduction/ide-usage"
>
Learn how to use Codegen effectively in VSCode, Cursor, and other IDEs.
</Card>
<Card
title="Tutorials"
icon="graduation-cap"
href="/tutorials/at-a-glance"
>
Follow step-by-step tutorials for common code transformation tasks.
</Card>
<Card
title="Work with AI"
icon="microchip"
href="/introduction/work-with-ai"
>
Leverage AI assistants like Copilot, Cursor and Devin
</Card>
<Card
title="Guides"
icon="hammer"
href="/building-with-codegen/at-a-glance"
>
Learn more about building with Codegen
</Card>

</CardGroup>

<Note>
For more help, join our [community Slack](/introduction/community) or check the [FAQ](/introduction/faq).
</Note>
11 changes: 10 additions & 1 deletion docs/introduction/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ for function in codebase.functions:
if not function.usages:
# Auto-handles references and imports to maintain correctness
function.remove()

# Fast, in-memory code index
codebase.commit()
`

export const code = `def foo():
Expand All @@ -35,7 +38,7 @@ def baz():

<iframe
width="100%"
height="320px"
height="370px"
scrolling="no"
src={`https://codegen.sh/embedded/codemod/?code=${encodeURIComponent(
metaCode
Expand All @@ -46,6 +49,12 @@ def baz():
className="rounded-xl"
></iframe>

<Note>
Codegen handles complex refactors while maintaining correctness, enabling a broad set of advanced code manipulation programs.
</Note>

<Tip>Codegen works with both Python and Typescript/JSX codebases. Learn more about language support [here](/building-with-codegen/language-support).</Tip>

## Installation

```bash
Expand Down
Loading