패키지, 저장소 및 앱 카테고리 검색

패키지 상세v0.12.0

super_annotations

일반 코드와 자연스럽게 함께 코드 생성 함수를 작성하세요. 동일한 파일 또는 프로젝트 내에서 사용자 정의 주석을 정의하고 사용하세요.

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
989
좋아요
52
Pub 점수
160

패키지 설치

flutter pub add super_annotations

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

Super Annotations는 Flutter/Dart 패키지로, 개발자가 일반 코드와 함께 코드 생성 로직을 직접 작성할 수 있게 해줍니다. 동일한 파일이나 프로젝트 내에서 사용자 정의 애너테이션을 정의하고 사용할 수 있어 코드 생성기 제작을 간소화합니다. 애너테이션 정의를 사용과 통합함으로써 반복 코드를 줄이고 유지보수성을 향상시킵니다. 이 패키지는 여러 플랫폼을 지원하며, 사용자 정의 코드 생성 워크플로우가 필요한 프로젝트에 적합합니다.

사용 사례

  • 사용자 정의 코드 생성기 만들기
  • 데이터 클래스 내 봄보일러플레이트 간소화
  • 직렬화 코드 생성
  • 도메인 전용 코드 생성 구축
  • 기존 코드베이스와 주석 통합하기

주요 기능

  • 사용과 함께 정의된 주석
  • 최소한의 보일러플레이트
  • 다중 플랫폼 코드 생성을 지원합니다
  • 일반 Dart 코드와의 원활한 통합
  • 사용자 정의 생성기 로직을 위한 확장 가능

적합한 대상

  • 사용자 정의 코드 생성기를 개발하는 개발자
  • 주석 기반 코드 생성을 사용하는 팀
  • 정결하고 유지보수 가능한 코드 생성이 필요한 프로젝트
  • JSON 직렬화를 사용하는 Dart 개발자
  • 코드 생성 워크플로에서 복잡성을 줄이려는 사람

고려 사항

  • 코드 생성 개념에 대한 이해가 필요합니다
  • 빌드 시간을 약간 증가시킬 수 있습니다
  • 일관된 코드 생성 요구 사항을 가진 프로젝트에 가장 적합
  • 간단하고 단발성 변환에 적합하지 않음
  • 빌드 러너의 올바른 설정에 의존합니다

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

Super Annotations

Hassle-free static metaprogramming as you dream it. Use code generation and custom annotations with ease.

  • Write your code generation functions naturally alongside your normal code.
  • Define and use custom annotations in the same file or project.

For the first time, this makes code generation applicable for all kinds of projects. No complex setup, no experience in writing builders needed.

Outline

Get Started

First, add super_annotations as a dependency, and build_runner as a dev_dependency.

dart pub add super_annotations
dart pub add build_runner --dev

Next, define your custom annotation like this:

import 'package:super_annotations/super_annotations.dart';

/// Choose a name and extend [ClassAnnotation]
class MyAnnotation extends ClassAnnotation {
  /// You need a const constructor to be usable as an annotation
  const MyAnnotation();

  /// You have to implement the [apply] method, which will be
  /// executed during the build phase
  /// @param target: A formal description of the annotated class, e.g. its name and fields
  /// @param output: The output that will be generated as part of the build phase
  @override
  void apply(Class target, LibraryBuilder output) {
    // Your custom implementation here
  }
}

The target parameter will hold all the information about the annotated class, and the library parameter can be used to produce your code generation output in a formal way.

Access information about the class using target.name, target.fields and so on. Add code to the generation output using: library.body.add(...). You can add declarations like Class(...), Extension(...), Mixin(...) etc, or use raw strings with Code('...').

Finally annotate the library directive with @CodeGen() for each library that you want to activate code generation for. A new .g.dart file will be generated alongside each of the annotated libraries.

@CodeGen()
library main;

After that, use your custom annotation in your library, or any of it's imported libraries, as you like:

@MyAnnotation()
class MyClass {
  MyClass(this.data);

  final String data;
}

Finally, run pub run build_runner build to run code generation once or pub run build_runner watch to automatically rebuild on each save.

Head over to the example to view the full code

Generating code

This package leverages the code_builder package to easily specify your generation outputs, whether it's classes, mixins, extensions or other code. This enables you to just care about the semantics of your generation output, while we take care of generating the correct syntax as well as formatting the generated code.

To define your generation outputs, modify the provided LibraryBuilder inside your annotations apply() method or a generation hook. The most used way is to modify the library using output.body.add() or output.body.addAll(). For a list of all supported declarations, check out the api reference of the code_builder package.

Available Annotations

Currently there are three types of annotation classes available.

  • ClassAnnotation: Extend this class to create a custom annotation for class declarations
  • EnumAnnotation: Extend this class to create a custom annotation for enum declarations
  • FunctionAnnotation: Extend this class to create a custom annotation for top-level functions

Have a look at this example to see them in action.

Generation hooks

With custom annotations, you have the possibility to generate code for each annotated class. Besides this you might want to do other tasks during code-generation, such as add imports to your generated library.

To do this, use generation hooks. These are just top-level or static functions, which are passed to the @CodeGen() annotation:

@CodeGen(
  runBefore: [myFunction],
  runAfter: [myOtherFunction],
)
library main;

As the names suggest, the annotated function will the be run before everything else, or after everything else.

Generation targets

By default, there is a single generation target set to .g.dart, however you can set different targets by passing them to the @CodeGen() annotation:

@CodeGen(
  targets: ['client', 'server']
)
library main;

Using this configuration, two files .client.dart and .server.dart would be generated instead of the .g.dart file. Your annotations will be called once for each target. In your annotations code, you can use CodeGen.currentTarget to get the current target:

class MyAnnotation extends ClassAnnotation {
  const MyAnnotation();

  @override
  void apply(Class target, LibraryBuilder output) {
    if (CodeGen.currentTarget == 'client') {
      output.body.add(Code('// GENERATED CLIENT LIBRARY'));
    } else if (CodeGen.currentTarget == 'server') {
      output.body.add(Code('// GENERATED SERVER LIBRARY'));
    }
  }
}

Due to the limitations of build_runner you cannot specify arbitrary targets, but must choose from a predefined set of supported targets. Supported by default are: g, super, client, server, freezed, json, data, mapper, gen, def, types, api, schema, db, query, part and meta.

If you need more targets, you have to use a build.yaml file and specify add the targets in the builder options:

global_options:
  super_annotations:
    options:
      targets: [ mytarget ]
When it fails

There exists a common misuse, that lead to a failure of the code generation when executing build_runner build.

You have to make sure that the code executed during the code generation phase can be compiled. Therefore it is important that the libraries that contain your annotations are syntactically correct and can be compiled, otherwise code generation will fail.

However it is common that your target code - the classes using your annotations - is not compilable until the code generation is complete, for example when you are referring to mixins or classes that are later part of the generated code.

In those cases you have to place your annotations in separate files and import them from your target code. Make sure that your dart files containing the annotations are compilable even when the target files are not, especially that they do not import any code with errors. The builder is then smart enough to only import the needed files with the annotations.

Debugging

Steps for Android Studio / IntelliJ IDEA

Since we use build_runner to execute our annotation code, it is not straight forward to debug our code. To enable debugging, you need to manually add the script that pub run build_runner build calls to your run configurations. This script is inside the .dart_tool folder at the root of your project (after running manually once). In your IDE add a new run configuration for a dart command line app with the following values:

  • Dart file: <project_root>/.dart_tool/build/entrypoint/build.dart
  • Program arguments: build --delete-conflicting-outputs
  • Working directory: <project_root>

Now you can set breakpoints in your annotations and debug them by selecting the created run config and clicking the Debug button.

Mastering Annotations

With super_annotations, your build and runtime environment share the same codebase. This enables a few unique perks, that you wouldn't normally get with normal code generation.

Annotation parameters

Since your annotations are just a normal class, you can define fields that are then used as annotation parameters.

Look at the following example:

@MyAnnotation("abc", 42)
class MyClass {}

class MyAnnotation extends ClassAnnotation {

  const MyAnnotation(this.id, this.myValue);

  final String id;
  final int myValue;

  @override
  void apply(Class target, LibraryBuilder output) {
    // read on
  }
}

In the apply method, you can now access this.id and this.myValue, which will hold the appropriate values from the actual annotation. When you use your annotation on multiple classes, the fields will always have the correct value matching the currently analyzed class.

Resolved Annotations

On top of annotation parameters, you can even access non-functional annotations (that are not super annotations) with ease:

@MyAnnotation()
class MyClass {
  @MyOtherAnnotation("important_label")
  void doSomething() {}
}

/// Just a regular annotation, nothing 'super'
class MyOtherAnnotation {
  final String label;
  const MyOtherAnnotation(this.label);
}

/// The real deal
class MyAnnotation extends ClassAnnotation {
  const MyAnnotation();

  @override
  void apply(Class target, LibraryBuilder output) {
    var element = target.methods.first;
    // use [resolvedAnnotations] on any element (e.g. method) to get the actual annotation objects
    var firstAnnotation = element.resolvedAnnotations.first;
    if (firstAnnotation is MyOtherAnnotation) {
      // do something with [firstAnnotation.label]
    }

    // use [resolvedAnnotationsOfType] when you are expecting a specific annotation
    var allMethodAnnotations = element.resolvedAnnotationsOfType<MyOtherAnnotation>();
  }
}

Combine these two methods and bring your code generation game to a whole new level.

Examples

We prepared a few examples, that showcase different things that you can do with this package.

  • Part-of strategy: This example demonstrates a strategy using part_of directives in order to extend existing declarations or add new ones.

  • Inherit strategy: This example demonstrates a strategy using inheritance in order to modify existing class declarations.

  • Json serialization: This example shows how to generate json serialization code, which is probably the most common use-case for code generation. It is inspired by and mimics the basic behavior of json_serializable

  • Data classes: This example shows how to generate utility methods for data classes. This will generate copyWith and toString methods for each annotated class.

  • Sealed classes / Freezed: This example shows how to generate sealed classes / union types. It is inspired by and mimics the basic behavior of freezed

  • Resolved annotations: This example demonstrates how to use resolved annotations.

How does it work?

I plan to publish a detailed article on the inner workings of this package, but here is a short rundown:

  1. The package defines a builder that automatically runs on all libraries annotated with @CodeGen()
  2. It identifies custom annotations and analyzes the annotated classes
  3. It produces a new *.runner.g.dart file, that:
    • contains the analyzed classes using the code_builder syntax
    • calls the custom annotations write methods
    • calls additional runBefore or runAfter methods
    • builds and returns the created library code
  4. It spawns the runner as a new isolate and receives the generation results via inter-process communication
  5. It kills the isolate and deletes the *.runner.g.dart file
  6. It writes the generation results to a *.g.dart file as the build output
Bonus: Why super?

Custom annotations are not only super great, but also super in terms of programming. As you would define superclasses with superconstructors, this package defines structures above annotations.

Or in other words: While other code generation packages use code that generates other code, this package uses code that generates other code that generates other code.

So super.

카테고리

의존성

의존성 목록 6 개

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.