Skip to content

rkam88/codegen-samples

Repository files navigation

1) Overview

codegen-samples is an Android/Kotlin project that demonstrates three practical approaches to generating code mimicking a real codebase:

  • Gradle-based scaffolding — create a module folder (e.g. feature/movies) and let Gradle auto-include it and scaffold the minimal project structure for you on sync.
  • Multi-file templates — commit reusable File Templates to the repo so anyone can create a ready-to-run feature stack from File → New → Feature UI. The template asks for a single file name and uses it as the state file and the prefix for the related classes.
  • IDE plugin integration — this repo also works as a companion for codegen-plugin, which discovers API functions and generates domain models (with safe conflict handling and automatic formatting) into your selected package.

2) Repository layout

  • settings.gradle.kts — drives the Gradle-based scaffolding: auto-discovers subfolders under core/ and feature/, creates missing source sets & a minimal build.gradle.kts when a subfolder is empty, and includes the module on sync.
  • core/ — shared modules used by features (e.g., ui, network, navigation). These are referenced by the scaffolded feature modules.
  • feature/ — create a new folder here (e.g., feature/movies) and sync to scaffold a module with src/{main,test,testFixtures}/kotlin/... plus a ready build.gradle.kts.
  • .idea/fileTemplates/… — project-scoped multi-file templates (committed to VCS). Select via File → New → Feature UI; the prompt’s file name becomes the state file and the prefix for related classes. The template setup is controlled by .idea/file.template.settings.xml.
  • build-logic/ — convention plugins used by scaffolded modules; referenced by the generated build.gradle.kts.

3) Gradle-based generation (module scaffolding)

What it does
The root settings.gradle.kts auto-discovers subfolders under core/ and feature/, and on Gradle sync it will:

  • Create a module skeleton when a subfolder is empty: src/main, src/test, and src/testFixtures with the correct package path.
  • Write a minimal build.gradle.kts pre-wired to shared modules, and include the module in the build.

How to use it

  1. Create a new folder under feature/ (e.g., feature/movies).
  2. Re-sync the project. You’ll get a ready-to-build module with a namespaced package and a starter build.gradle.kts.

Adapt to your codebase
Treat this as a starter. If you want different source sets, package roots, or dependencies for new modules, adjust the logic in settings.gradle.kts to match your conventions. Teams should copy and customize this approach rather than relying on the sample defaults.


4) Multi-file templates

What they are
Project-scoped File Templates that create a small feature stack in one go. They’re committed to VCS so the whole team gets the same templates.

Where they live
Templates are stored under .idea/fileTemplates/…, with their registry/config in .idea/file.template.settings.xml (committed to the repo so they’re shared across machines).

Template name
You’ll find a template called “Feature UI” in the File → New menu.

How to use

  1. In your target module (e.g., a newly scaffolded feature/<name>), open the package where you want files created.
  2. Go to File → New → Feature UI.
  3. When prompted for a file name, enter a short feature name such as Movies.
    • The template uses this as the state file name (e.g., Movies.kt) and as the prefix for the related classes:
      MoviesFragment, MoviesScreen, MoviesViewModel, MoviesUiConverter.

Adapting to your project
Treat these as a starter: copy the files from .idea/fileTemplates/ and the .idea/file.template.settings.xml file into your own project and adjust the generated packages, filenames, and class contents to match your conventions (naming, architecture, dependencies, etc).


5) Using with the Codegen plugin

This repository works as a companion for codegen-plugin.

  • Install & usage: follow the steps in the plugin’s README: https://github.com/rkam88/codegen-plugin
  • Where to run it: in any feature module package inside feature/<name>/src/main/kotlin/....
  • What to try first:
    1. Run the action on MoviesApi.getMovieDetailsV1 to generate the domain models.
    2. Then change to MoviesApi.getMovieDetailsV2 and run it again to see how conflict handling behaves when related files already exist.
  • Output location: the plugin writes generated code, i.e. domain models under a domain/model package path relative to the package you selected.

Adapting to your project
Compared to the previous two approaches, this one is slightly more involved and will require you to make changes to the plugin. Check the readme of codegen-plugin for more instructions on how to get started.


6) License

This project is licensed under the Apache License, Version 2.0. See LICENSE for the full text.

About

codegen-plugin companion repository & other code generation methods

Resources

License

Stars

Watchers

Forks

Languages