# Conquering Code Generation in Dart (Video Series)

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 in 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.

%[https://youtu.be/H4HWB2Pmgcw] 

[Watch Part 1 on YouTube](https://youtu.be/H4HWB2Pmgcw)

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

## **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 this `build.yaml` configuration, the [build\_config](https://pub.dev/packages/build_config) package is used to parse 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**](http://localhost:8080/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
  path: ^1.6.0
  markdown: ^2.0.3

dev_dependencies:
  build_config: ^0.4.1+1
  build_runner: ^1.6.6
  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 a target 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)

%[https://youtu.be/HnXdsXh_Jdc] 

[Watch Part 2 on YouTube](https://youtu.be/HnXdsXh_Jdc)

%[https://youtu.be/xRJHKb3rekQ] 

[Watch Part 3 on YouTube](https://youtu.be/xRJHKb3rekQ)

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

## Further reading

* [Dart source\_gen package](https://github.com/dart-lang/source_gen)
    
* [Dart build packages repository](https://github.com/dart-lang/build)
