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.json
with richer information.
- OpenAI API Key: You need a valid
OPENAI_API_KEY
with access to the chosen model (e.g.,gpt-4
orgpt-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
find
orsed
commands.
-
app_explore.sh
: The main entry point. This script:- Prompts for OpenAI API key and model name.
- Runs
generate_summary.sh
to createapp_summary.json
. - Runs
orchestrate_summaries_in_json.sh
to call OpenAI API viasummarize_file.sh
and add detailed summaries toapp_summary.json
.
-
generate_summary.sh
:- Scans the Rails
app/models
,app/controllers
,app/services
, andapp/views
directories. - Finds
.rb
files (for models, controllers, services) and template files (e.g.,.erb
,.haml
,.html
for views). - Extracts a short description from the top of each file.
- Produces
app_summary.json
with 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.json
generated bygenerate_summary.sh
. - Iterates through each file listed in
app_summary.json
. - Calls
summarize_file.sh
to get a detailed summary. - Updates
app_summary.json
by adding adetailed_summary
field for each file.
- Reads
-
Ensure you have
jq
installed:# macOS (Homebrew) brew install jq
On Linux,
jq
is 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/views
are in the expected locations.
-
Run the Main Entry Script:
./app_explore.sh
- If
OPENAI_API_KEY
is not set, it will prompt you to enter one. - It will ask you for your preferred model (default
gpt-4
).
- If
-
Process:
app_explore.sh
runsgenerate_summary.sh
to createapp_summary.json
.- It then runs
orchestrate_summaries_in_json.sh
to add detailed summaries to each file inapp_summary.json
.
-
Result:
- After completion, check the
app_summary.json
file. - Each file entry will now have a
detailed_summary
field 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_summary
isnull
, ensure yourOPENAI_API_KEY
is 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.sh
to debug any API errors:echo "$RESPONSE" >&2
-
Parse Errors in JSON:
Make sure not to modifygenerate_summary.sh
to 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.json
to locate the issue.
-
Additional Directories:
To include more directories (e.g.,app/helpers
), add another call toprocess_directory
or 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.