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 again. It 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 the same type of tasks. And these are COMPUTERS !
Today, code generation is the ability to do work in the shortest amount of time . The basic idea is simple: to find patterns in the same type and tedious parts of the code that you have to write again and again, create a tool for generating, run it and see how the magic happens!
In the Android world of development, such tools are well known to every developer. This and 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?
To create a code generation tool, we need the following two packages:
This package provides a convenient API for code generation. This is an abstraction over some low-level Dart packages, such as analyzer and build . Although the use of this package is optional, it can relieve you of many difficulties.
source_gen provides two abstract generator classes that follow the Visitor pattern:
Generator
: When you inherit this class, each element of your code will be visited. Thus, you have complete control over what to do with each node or element visited.GeneratorForAnnotation
: this class is similar to the simple Generator, but when you inherit this class, you also specify an "annotation". Thus, only those nodes that are annotated with this annotation will be visited. The rest of the code will be ignored.You also need to configure the Builder
, which will wrap the generator. There are three options:
partial
piece of code, then you should select SharedPartBuilder
. "part" allows you to split the library into several Dart files. SharedPartBuilder
creates a file with the extension .g.dart
.PartBuilder
if you want to use the "part" approach, but you need more control over the extension of the generated file, for example, .my_file.dart
.LibraryBuilder
.This tool allows you to start the generator at the design stage. It can be called from the constant line:
pub run build_runner <command>
At the place of <command>
can be:
build
: runs generation once.watch
: launches a daemon that monitors changes in files and starts generation when required.serve
: similar to watch, but also run as a development server.test
: starts the generation once, creates a common output directory, and then starts pub run test --precompiled <merged-output-dir>
.In order for source_gen
work, you also need to create the buil.yaml
file, which specifies the details of the code generator configuration.
Code generation is used in many well-known Dart libraries:
The second part will show how to use annotations and code generation to track all the TODO
in the application.
Source: https://habr.com/ru/post/445824/
All Articles