Code Generation in Dart. Part 1. Basics

Original author: Jorge Coca
  • Transfer

It is known that it is very good for a programmer to be lazy , because doing more with less is the key to progress . Nobody likes to do the same thing over and over. This is tiring, boring, and not at all creative. Repeating the same action, we often make mistakes, but, fortunately, there are those who are really good and effective in performing similar tasks. And these are COMPUTERS !


https://yougottobekidding.wordpress.com/2012/02/18/geeks-and-repetitive-tasks/


Today, code generation is the ability to do the job in the shortest amount of time . The basic idea is simple: to find a pattern in the same and tedious sections of code that have to be written over and over again, create a tool for generation, run it and see how the magic happens!


In the world of Android development, such tools are well known to every developer. This is Retrofit, and Dagger, and Room. What about Dart? And no less important question: what do we need to create our own tools for code generation?


Dart and code generation: affordable tools


To create a tool for code generation, we need the following two packages:



source_gen


This package provides a convenient API for generating code. This is an abstraction of some low-level Dart packages, such as analyzer and build . Although the use of this package is optional, it can save you from many difficulties.


source_gen provides two abstract generator classes that follow the Visitor pattern :


  • Generator: when inheriting this class, every element of your code will be visited. Thus, you have full control over what to do with each visited node or element.
  • GeneratorForAnnotation: This class is similar to a simple Generator, but when you inherit from this class, you also specify "annotation". Thus, only nodes that are annotated with this annotation will be visited. The rest of the code will be ignored.

It is also necessary to configure Builderwhich will be a wrapper over the generator. There are three options:


  • If you want to write a partialpiece of code, you should choose SharedPartBuilder. "part" allows you to split the library into several Dart files. SharedPartBuildercreates a file with the extension .g.dart.
  • You can also use PartBuilderif you want to use a "part" approach, but you need more control over the generated file extension, for example .my_file.dart.
  • If you want to write a standalone library that can be imported, use LibraryBuilder.

build_runner


This tool allows you to run the generator at the development stage. It can be called from the command line:


pub run build_runner 

In place may be:


  • build: starts the generation once.
  • watch: Launches a daemon that tracks changes in files and starts generating when needed.
  • serve: similar to watch, but also starts as a development server.
  • test: starts the generation once, creates a common output directory and then starts .pub run test --precompiled

To source_genearn money, you also need to create a file buil.yamlin which the details of the configuration of the code generator are indicated.


Using Code Generation in Dart


Code generation is used in many well-known libraries for Dart:



Further...


The second part will show how to use annotations and code generation to track everyone TODOin the application.


Also popular now: