This project is a Scala-based application that utilizes AI to generate enhanced ScalaDocs and project documentation for Scala source files. The tool reads Scala source code, processes it via the OpenAI API, and outputs detailed, high-quality documentation. It also offers utilities to generate a structured README.md
summarizing the entire project.
-
Generate ScalaDocs:
- Enriches individual or all Scala source files in a project with detailed ScalaDocs, comments, and explanations in a meaningful, AI-generated format.
-
Generate README.md:
- Summarizes the project in a professional README file, analyzing the provided Scala files and extracting key features, usage, and high-level details.
-
Reliable File Processing:
- Supports concurrent processing of Scala files using FS2 and Cats Effect for efficient handling of large projects.
-
Error Logging:
- Provides meaningful error logs for any issues encountered during processing using Typelevel
log4cats
.
- Provides meaningful error logs for any issues encountered during processing using Typelevel
-
Full CLI Support:
- Offers an interactive command-line interface for input and task selection.
Clone this repository on your local machine:
git clone https://github.com/khanr1/scala-ai-doc.git
cd scala-ai-doc
Ensure you have the following dependencies installed on your system:
- Scala 3.x
- sbt (Scala Build Tool)
- Java 8 or higher
Install necessary libraries via SBT:
sbt update
To run the application, execute:
sbt run
The application will present a CLI menu to select the following tasks:
-
Generate ScalaDocs
- Enter the path to the directory where the Scala source files are located. The application will process the files and generate enriched ScalaDocs.
-
Generate README.md
- Point the application to the root directory of your Scala project. The tool will analyze the project's code and create a detailed
README.md
.
- Point the application to the root directory of your Scala project. The tool will analyze the project's code and create a detailed
-
Exit
- Terminates the application.
The entry point for the application.
- Displays a text-based menu.
- Handles user interaction for generating documentation and selecting directories.
Highlights:
- Menu-driven interface: The application navigates through options with options to generate ScalaDocs, generate a
README
, or exit the application. - Customizable Paths: Accepts input paths for target directories and files.
A central component of this project, it defines the logic for:
- Generating ScalaDoc for a single file.
- Generating ScalaDoc for all files in a directory.
- Creating a project-wide summary in
README.md
.
Key Features:
- AI-based documentation generation using the OpenAI API.
- Concurrency control to streamline processing of multiple files with FS2.
- Adaptive handling of errors and warnings during file updates.
A utility for reading, validating, and processing Scala files.
- Reads
.scala
files using streaming to optimize memory usage. - Filters and validates paths for true
.scala
files to avoid errors. - Works recursively through a directory tree to find and process all Scala files.
Error Handling:
Custom exceptions such as InvalidPathError
and DirectoryError
are raised for invalid file paths or unexpected directory structures. These enhance debugging and ensure safer file processing.
- The
OpenAIConfig
handles API key management required for external API integrations. - You must configure an OpenAI API key in the form of environment variables or application configuration.
-
Input:
- A directory path containing
.scala
files or the root directory of your Scala project. - User input via a CLI interface for menu options and paths.
- A directory path containing
-
Output:
- Updated
.scala
files enriched with detailed and standardized ScalaDocs. - A
README.md
file placed in the root directory of the project.
- Updated
The Main
object provides the application backbone:
def menuLoop: IO[Unit] = {
for {
_ <- showMenu
choice <- getChoice
_ <- choice match {
case 1 => for { path <- getPath; _ <- generateScalaDocs(path) *> menuLoop } yield ()
case 2 => for { path <- getPath; _ <- generateReadME(path) *> menuLoop } yield ()
case 3 => IO.println("Goodbye!")
case _ => IO.println("Invalid input. Try again.") *> menuLoop
}
} yield ()
}
Features:
- Dynamic Path Selection: Prompts the user to enter a directory or file path.
- Activity Loop: Repeats until the user selects the "exit" option.
The ScalaDocGenerator
trait defines:
def generateScalaDoc(path: Path, content: FileContent): fs2.Stream[F, Nothing]
def generateReadMe(path: Path): fs2.Stream[F, Nothing]
def generateAllScalaDoc(path: Path): fs2.Stream[F, Nothing]
The make
factory method creates an implementation that uses OpenAI for generating documentation:
- Enriches code comments via an AI model.
- Writes enhanced documentation back to the original source files.
The FileProcessor
tracks and reads all .scala
files within directories:
def readAllScalaFiles(path: Path): fs2.Stream[F, (Path, FileContent)] = ...
It uses FS2 to handle files in parallel, ensuring efficient processing while managing memory constraints.
The project leverages OpenAI APIs via the scala-openai
library to achieve the following:
- Analyze and interpret Scala source code.
- Generate ScalaDocs with enriched comments and meaningful documentation.
- Compose a structured and informative project README.
The interaction flow involves generating a natural language prompt based on the input Scala code, submitting it to OpenAI, and receiving the processed response as Scala documentation or summarized project metadata.
- Scala 3.x: Leverages modern features like opaque types and extension methods.
- FS2: Functional streaming for efficient file processing.
- Cats Effect: Provides effectful and concurrent abstractions.
- log4cats: Logging utilities (SLF4J Logger).
- OpenAI API: Cloud-based NLP services to generate documentation and summaries.
- Clone the repository.
- Submit PRs for improvements or new feature suggestions.
- Ensure proper scaladoc-style comments for your contributions.
- Typelevel Libraries: Cats Effect, FS2.
- OpenAI: NLP-based documentation generation.
- Contributors: Special thanks to Raphael Khan for creating this tool.