This set of scripts helps you:
- Scan a Ruby on Rails application to identify key files (models, controllers, services, views).
- Generate a summary JSON file (
app_summary.json) that lists all discovered files along with a brief description. - Use OpenAI's API to generate detailed summaries of each file and augment
app_summary.jsonwith richer information.
- OpenAI API Key: You need a valid
OPENAI_API_KEYwith access to the chosen model (e.g.,gpt-4orgpt-3.5-turbo). - jq: A command-line JSON processor. On macOS, install with
brew install jq. - Bash Shell: Tested on Linux and macOS. If on macOS, you may need to install GNU utilities if you make changes to
findorsedcommands.
-
app_explore.sh: The main entry point. This script:- Prompts for OpenAI API key and model name.
- Runs
generate_summary.shto createapp_summary.json. - Runs
orchestrate_summaries_in_json.shto call OpenAI API viasummarize_file.shand add detailed summaries toapp_summary.json.
-
generate_summary.sh:- Scans the Rails
app/models,app/controllers,app/services, andapp/viewsdirectories. - Finds
.rbfiles (for models, controllers, services) and template files (e.g.,.erb,.haml,.htmlfor views). - Extracts a short description from the top of each file.
- Produces
app_summary.jsonwith an array of files and their basic info.
- Scans the Rails
-
summarize_file.sh:- Takes a file path as input.
- Sends the file’s content to the OpenAI API (using the model specified in
OPENAI_MODEL). - Returns a short, AI-generated summary of the file’s purpose and main functions.
-
orchestrate_summaries_in_json.sh:- Reads
app_summary.jsongenerated bygenerate_summary.sh. - Iterates through each file listed in
app_summary.json. - Calls
summarize_file.shto get a detailed summary. - Updates
app_summary.jsonby adding adetailed_summaryfield for each file.
- Reads
-
Ensure you have
jqinstalled:# macOS (Homebrew) brew install jqOn Linux,
jqis often available in package managers:# Debian/Ubuntu sudo apt-get install jq -
Set Executable Permissions:
chmod +x app_explore.sh chmod +x generate_summary.sh chmod +x summarize_file.sh chmod +x orchestrate_summaries_in_json.sh
-
Prepare Your Rails App Directory: Run these scripts at the root of your Rails application so that
app/models,app/controllers,app/services, andapp/viewsare in the expected locations.
-
Run the Main Entry Script:
./app_explore.sh
- If
OPENAI_API_KEYis not set, it will prompt you to enter one. - It will ask you for your preferred model (default
gpt-4).
- If
-
Process:
app_explore.shrunsgenerate_summary.shto createapp_summary.json.- It then runs
orchestrate_summaries_in_json.shto add detailed summaries to each file inapp_summary.json.
-
Result:
- After completion, check the
app_summary.jsonfile. - Each file entry will now have a
detailed_summaryfield with the AI-generated description.
- After completion, check the
-
Empty
app_summary.json:
Check if your Rails directories (app/models,app/controllers,app/services,app/views) contain files. If empty, no entries will be generated. -
Invalid Model or API Key:
Ifdetailed_summaryisnull, ensure yourOPENAI_API_KEYis valid and that the model you chose (e.g.,gpt-4) is accessible to your API key.
You can also print the raw response insummarize_file.shto debug any API errors:echo "$RESPONSE" >&2
-
Parse Errors in JSON:
Make sure not to modifygenerate_summary.shto add trailing commas or incomplete braces. The provided version ensures well-formed JSON. If you run into JSON parse errors, validate the file withjq . app_summary.jsonto locate the issue.
-
Additional Directories:
To include more directories (e.g.,app/helpers), add another call toprocess_directoryor create a similar function if needed. -
Adjusting Prompt or Temperature:
Insummarize_file.sh, you can tweak themax_tokens,temperature, or prompt to get different styles or lengths of summaries.
These scripts streamline the process of generating an overview and detailed summaries of a Rails codebase using the OpenAI API. Just run app_explore.sh, follow the prompts, and review the enriched app_summary.json file.