Conquering Code Generation in Dart – Part 1: Write your first builder
Tags: dart, webdev, productivity
Published: 2019-10-06 20:09:31

Code Generation describes the process of automatically generating code that one would otherwise write by hand. In this new series we will be exploring the tooling made available for Code Generation for Dart projects.

This has been one of the least covered and understood topics due to the lack of documentation on it. We will conquer this tooling beast and grow your confidence in its usage.

{% youtube H4HWB2Pmgcw %}

## How to get started

Code Generation is available through a package called [build](https://pub.dev/packages/build). This package exposes a set of interfaces for creating what are known as _Builders_. A `builder` is where you define the business logic to instruct the build process on how to generate code.

Although we encapsulate our logic inside a `Builder` class, we need a way to run this builder...and that's where [**build_runner**](https://pub.dev/packages/build_runner) comes in. It runs the build process based on configuration defined inside a `build.yaml` file. In order for it to understand the `build.yaml` configuration, the [**build_config**](https://pub.dev/packages/build_config) package is used to parse the the instructions defined therein. Therefore the **build_runner** package uses **build_config** behind the scenes.

To create the files for the project use the [**stagehand**](https://creativebracket.com/generate-the-scaffolding-for-your-next-project/) tool to scaffold a console application:

```bash
$ mkdir code_generators && cd code_generators
$ stagehand console-full
```

Open the pubspec.yaml file and update `dependencies` and `dev_dependencies`:

```yaml
dependencies:
  build: ^1.1.6 # <-- Add this line
  path: ^1.6.0

dev_dependencies: 
  build_config: ^0.4.1+1 # <-- And this line
  build_runner: ^1.6.6 # <-- And this line 
  pedantic: ^1.7.0 
  test: ^1.5.0
```
Update all your dependencies by running `pub get`.

## Configuring the `build.yaml` file

The `build.yaml` file contains configuration for **targets**, **builders**, and **post process builders**. The `builders` configuration allows you to register the builders you have implemented, as well as the top-level function to invoke in order to run the build process for the builder in question. You would also set the `build_extensions` key with the same value as was set in the `buildExtensions` member of your `Builder` class the configuration relates to. The `targets` config allows you to configure your builder to run on a subset of files in your project, as well as specify if they are enabled or not.

Here’s a typical example registering our `CopyBuilder` and configuring some targets for it:

```yaml
targets: 
  $default: 
    builders: 
      code_generators|copyBuilder: 
        generate_for: 
         - lib/* 
        enabled: True 

builders: 
  copyBuilder: 
    import: 'package:code_generators/code_generators.dart' 
    builder_factories: ['copyBuilder'] 
    build_extensions: 
      .txt: 
       - .copy.txt 
    build_to: source 
    auto_apply: dependents
```

[Learn more about configuring the build.yaml file](https://github.com/dart-lang/build/blob/master/build_config/README.md)

**Continue the full tutorial in the video above.**

[**Get the source code**](https://github.com/graphicbeacon/codegeneration_with_dart)

## Sharing is caring

If you enjoyed reading this post, please share this through the various social buttons hovering on the left/top side of the screen. Also, check out and [subscribe to my YouTube channel](https://youtube.com/c/CreativeBracket) (_hit the bell icon too_) for videos on Dart.

Watch my [Free Get started with Dart course](https://egghead.io/courses/get-started-with-dart) on Egghead.io and [Subscribe to my email newsletter](http://eepurl.com/gipQBX) to download my Free 35-page eBook titled Get started with Dart and to be notified when new content is released.

**Like, share and [follow me](https://twitter.com/creativ_bracket)** for more content on Dart.

_Originally published at [https://creativebracket.com](https://creativebracket.com/conquering-code-generation-in-dart-1/)._