Dart-通过注解自动生成代码(SourceGen、BuildRunner)

使用过Dart自动Json序列化的都用过自动生成代码,但是你知道如何自己自定义注解并生成代码吗?

  1. 需要使用 source_gen、build_runner 这两个库;
  2. 定义注解类,配置注解到自动代码的Generator类及Builder类;

综上所述,咱需要构建两个Library库来使用注解自动化代码生成。

Library1: 注解类定义

Library2: 注解类的代码自动生成Generator及Builder

下面我们就说说怎么来做吧!

首先,假如我们需要实现一个自动生成MethodChannel调用Native方法的一个功能,这里我们叫它NativeCall吧。

  1. 新建NativeCall库,编写NativeCall注解的类。

    import 'package:native_call/native_method_call_info.dart';
    
    class NativeCall {
      /// ChannelName
      final String channelName;
      const NativeCall({this.channelName}); //注解类,构造函数必须是Const的
    }
    
  1. 新建NativeCallGen库,编写NativeCallGenerator, NativeCallBuilder,配置builder.yaml,该库需要依赖上面的NativeCall,需要引用第三方的库(即yaml文件中配置:SourceGenBuildRunner)

    dependencies:
      flutter:
        sdk: flutter
      source_gen: ^0.9.6 #需要包含代码自动库
      native_call:  #需要包含注解的库
        path: ../native_call 
    
    dev_dependencies:
      flutter_test:
        sdk: flutter
      build_runner: ^1.10.0  #需要引用该库
    

NativeCallGenerator,就是用来生成代码的,可以通过设置模板代码字符串等来输出。
下面通过解析ClassElement以及其内部的MethodElement来进行解析并得到需要自动化的代码内容字符串。
自定义类需要继承GeneratorForAnnotation类,并实现generateForAnnotationElement方法,该方法内即可编写相应的代码生成逻辑。

library native_call_gen;

import 'package:path/path.dart' as Path;
import 'package:native_call/native_call.dart';
import 'package:source_gen/source_gen.dart';
import 'package:analyzer/dart/element/element.dart';
// ignore: implementation_imports
import 'package:build/src/builder/build_step.dart' show BuildStep;

class NativeCallGenerator extends GeneratorForAnnotation {
  @override
  generateForAnnotatedElement(
      Element element, ConstantReader annotation, BuildStep buildStep) {
    if (element is! ClassElement)
      throw InvalidGenerationSourceError('注解未使用在类上');

    String className = element.displayName;
    String path = buildStep.inputId.path;
    String channelName = annotation.peek('channelName').stringValue;

    StringBuffer sb = StringBuffer();

    (element as ClassElement).methods?.forEach((el) {
      var mName =
          String.fromCharCode(el.displayName.codeUnitAt(0)).toUpperCase() +
              el.displayName.substring(1);
      StringBuffer paramsStr = StringBuffer();
      StringBuffer params = StringBuffer();
      el.parameters?.forEach((param) {
        paramsStr.write('${param.type} ${param.name},');
        params.write('\'${param.name}\':' + param.name + ',');
      });
      sb.writeln('''
      ${el.hasImplicitReturnType == true ? 'Future' : 'void'} \$$className$mName(${paramsStr.toString()}) =>
       _methodChannel.invokeMethod('${el.displayName}', { ${params.toString()} });
      ''');
    });
    return '''
    part of '${Path.basename(buildStep.inputId.path)}';
    
    final MethodChannel _methodChannel = const MethodChannel('$channelName');


    ${sb.toString()}

    

    ''';
  }
}

NativeCallBuilder 生成代码的构造器,到时候build.yaml文件中会用到。

import 'package:native_call_gen/native_call_generator.dart';
import 'package:source_gen/source_gen.dart';
import 'package:build/build.dart';

Builder nativeCallBuilder(BuilderOptions options) =>
    LibraryBuilder(NativeCallGenerator(), generatedExtension: '.nc.g.dart');

build.yaml文件的配置

targets:
  $default: #定义目标库,关键字$default默认为当前库
    builders:
     natice_call_gen|native_call:  
        enabled: true #可选,是否将构建器应用于此目标
     source_gen|combining_builder:
      enabled: true

builders:
  native_call_builder:
    target: ":native_call_gen" #目标库
    import: 'package:native_call_gen/native_call_builder.dart'  #build文件
    builder_factories: ['nativeCallBuilder']
    build_extensions: {'.dart': ['.nc.g.dart']}
    auto_apply: dependents #将此Builder应用于包,直接依赖于公开构建起的包,也可以是root_package
    build_to: source #输出到注解的目标类的代码同目录中,或者输出转到隐藏的构建缓存,不会发布(cache)
    applies_builders: ["source_gen|combining_builder"] #指定是否可以延迟运行构建器
    # 以上参数具体参考 https://github.com/dart-lang/build/blob/master/build_config/README.md
  1. 新建一个测试项目,并引入NativeCallNativeCallGen库,配置如下:

    dependencies:
      flutter:
        sdk: flutter
      cupertino_icons: ^0.1.3
      native_call:  #引入NativeCall
        path: ../native_call/
    
    dev_dependencies:
      flutter_test:
        sdk: flutter
      build_runner: ^1.10.0 #引入builder_runner
      native_call_gen:  #引入NativeCallGen
        path: ../native_call_gen/
    

    测试类:AppNativeUtils

    import 'package:native_call/native_call.dart';
    import 'package:flutter/services.dart';
    
    part 'app_native_utils.nc.g.dart';  //包含该自动生成的part代码
    
    @NativeCall(channelName: 'com.china.mrper/utils/app_utils')  //使用NativeCall注解
    class AppNativeUtils {
      void showToast(String message, [int length]) =>  //定义toast方法
          $AppNativeUtilsShowToast(message, length);
    
      void showAlertDialog(String title, {message: String}) => //定义showAlertDialog的方法
          $AppNativeUtilsShowAlertDialog(title, message);
    }
    
    

    终端执行命令行,如下:**flutter package pub run build_runner build **

    生成如下代码:app_native_utils.nc.g.dart

    // GENERATED CODE - DO NOT MODIFY BY HAND
    
    // **************************************************************************
    // NativeCallGenerator
    // **************************************************************************
    
    part of 'app_native_utils.dart';
    
    final MethodChannel _methodChannel =
        const MethodChannel('com.china.mrper/utils/app_utils');
    
    void $AppNativeUtilsShowToast(
      String message,
      int length,
    ) =>
        _methodChannel.invokeMethod('showToast', {
          'message': message,
          'length': length,
        });
    
    void $AppNativeUtilsShowAlertDialog(
      String title,
      dynamic message,
    ) =>
        _methodChannel.invokeMethod('showAlertDialog', {
          'title': title,
          'message': message,
        });
    
    

至此,所有的流程已经完成,如果你觉得对你有用,请点个赞!
其他:转载请注明出处,感谢!

你可能感兴趣的:(Dart-通过注解自动生成代码(SourceGen、BuildRunner))