|
| 1 | +> [!TIP] |
| 2 | +> Looking for a way to manage your API contracts effortlessly? |
| 3 | +> Check out our community-driven tools for Odoo and beyond. |
| 4 | +> |
| 5 | +> **[adomi-io](https://github.com/adomi-io)**. |
| 6 | +
|
| 7 | +<p align="center"> |
| 8 | + <img src="docs/static/logo.png" width="240" /> |
| 9 | +</p> |
| 10 | + |
| 11 | +# 📜 Contract Builder - Protobuf 🏗️ |
| 12 | + |
| 13 | +A tool to turn your Protocol Buffer definitions into working code. Drop your `.proto` files in, and the builder automatically generates clients and types for your favorite languages. |
| 14 | + |
| 15 | +Under the hood, this repo uses `protoc` wrapped in Docker, including support for Betterproto, Go, and TypeScript, making it easy to keep your services in sync without manual boilerplate. |
| 16 | + |
| 17 | +## Highlights |
| 18 | + |
| 19 | +- 🌍 Support for **multiple** targets (Python, Betterproto, Go, TypeScript) |
| 20 | +- 📂 **Multi-service support**: Automatically scans the `specs` folder for all your services |
| 21 | +- 🐳 **Fully Dockerized**: No need to install `protoc` or plugins locally |
| 22 | +- 🔄 **Consistent Output**: Ensures your whole team generates the exact same code |
| 23 | +- 🚀 **Fast Iteration**: Quickly design your data models and see the code update instantly |
| 24 | + |
| 25 | +## What you can do |
| 26 | + |
| 27 | +- Keep a single source of truth for your service contracts |
| 28 | +- Generate multiple clients (including Betterproto) in one go |
| 29 | +- Ensure your frontend types always match your backend Protobuf definitions |
| 30 | +- Quickly iterate on service designs and see the code update instantly |
| 31 | + |
| 32 | +To generate clients, just drop your Protobuf files into the `specs` folder and run the builder. |
| 33 | + |
| 34 | +# Getting started |
| 35 | + |
| 36 | +> [!WARNING] |
| 37 | +> This tool is designed to run via Docker. |
| 38 | +> It keeps your environment clean and ensures everyone on your team gets the exact same code output. |
| 39 | +> |
| 40 | +>**[Download Docker Desktop](https://www.docker.com/products/docker-desktop/)** |
| 41 | +
|
| 42 | +# Docker Compose |
| 43 | + |
| 44 | +The easiest way to use this is with `docker-compose`. It mounts your local folders so the generated code appears right on your machine. |
| 45 | + |
| 46 | +See the [docker](./docker) folder for more information. |
| 47 | + |
| 48 | +Copy the files in the [docker](./docker) folder to your project root, or run the commands from within that folder. |
| 49 | + |
| 50 | +**Place your specs** |
| 51 | + |
| 52 | +Put your `.proto` files in subfolders under `specs/` (e.g., `specs/petshop/pet.proto`). The subfolder name determines the output name. |
| 53 | + |
| 54 | +**Run the generator** |
| 55 | + |
| 56 | +```bash |
| 57 | +docker compose up |
| 58 | +``` |
| 59 | + |
| 60 | +**Find your code** |
| 61 | + |
| 62 | +Check the `out/` directory for your generated clients. |
| 63 | + |
| 64 | +# Adding new targets |
| 65 | + |
| 66 | +The logic lives in `src/generate.sh`, which uses the `GENERATORS` environment variable to determine which clients to build. |
| 67 | + |
| 68 | +When using the provided `docker-compose.yml`, it defaults to `python,betterproto,go,typescript`. |
| 69 | + |
| 70 | +## Update docker-compose.yml |
| 71 | + |
| 72 | +Edit the `environment` section in your `docker-compose.yml`: |
| 73 | + |
| 74 | +```yaml |
| 75 | +environment: |
| 76 | + - GENERATORS=python,betterproto,go,typescript |
| 77 | +``` |
| 78 | +
|
| 79 | +## Run with an environment variable |
| 80 | +
|
| 81 | +You can also pass it directly to Docker: |
| 82 | +
|
| 83 | +```bash |
| 84 | +docker run --rm -e GENERATORS="python,go" -v $(pwd)/specs:/local/src -v $(pwd)/out:/local/out contract-builder-protobuf |
| 85 | +``` |
| 86 | + |
| 87 | +### Additional Options |
| 88 | + |
| 89 | +You can pass additional arguments to `protoc` for specific generators using environment variables. This follows the naming convention `GENERATOR_{LANGUAGE}_ARGS`. |
| 90 | + |
| 91 | +By default, the script calls `protoc` with `--{language}_out=...`. Use the `GENERATORS` list to specify the plugin name (e.g., `python_betterproto`, `ts_proto`, or just `go`). |
| 92 | + |
| 93 | +For example, to pass options to the `go` generator or use `betterproto`: |
| 94 | + |
| 95 | +```yaml |
| 96 | +environment: |
| 97 | + - GENERATORS=go,python_betterproto |
| 98 | + - GENERATOR_GO_ARGS=--go-grpc_out=/local/out/petshop/go --go_opt=paths=source_relative |
| 99 | + - GENERATOR_PYTHON_BETTERPROTO_ARGS=--python_betterproto_opt=unwrapped |
| 100 | +``` |
| 101 | +
|
| 102 | +The language name is converted to uppercase, and hyphens are replaced with underscores. |
| 103 | +
|
| 104 | +## Typical data flow |
| 105 | +
|
| 106 | +- Developer updates `specs/petshop/pet.proto` |
| 107 | +- `docker compose up` is triggered |
| 108 | +- The generator container starts, scans `specs/`, and runs `generate.sh` |
| 109 | +- New code is written to `out/petshop/python`, `out/petshop/betterproto`, `out/petshop/go`, etc., depending on your `GENERATORS` setting. |
| 110 | +- Your app uses the updated clients immediately |
| 111 | + |
| 112 | +# Running from source |
| 113 | + |
| 114 | +Clone this repository, and open a terminal in the root directory. |
| 115 | + |
| 116 | +## Build the application |
| 117 | + |
| 118 | +> [!TIP] |
| 119 | +> You can also run `docker compose up --build`. |
| 120 | + |
| 121 | +Run `docker compose build` |
| 122 | + |
| 123 | +## Run the application |
| 124 | + |
| 125 | +Run the application by running |
| 126 | + |
| 127 | +`docker compose up` |
| 128 | + |
| 129 | +If you want to run the application all the time, start it with |
| 130 | + |
| 131 | +`docker compose up -d` |
| 132 | + |
| 133 | +To stop the application, run |
| 134 | + |
| 135 | +`docker compose down` |
| 136 | + |
| 137 | +## About Adomi |
| 138 | + |
| 139 | +Contract Builder is an Adomi project. We build helpful tools for modern development workflows. If you have ideas or run into issues, feel free to open an issue or suggestion. |
0 commit comments