dart-generator

手动安装

1- 从代码生成平台可执行文件

dart compile exe main.dart -o generator

这将在 lib 文件夹中生成一个新的生成器文件。

2- 在您的 .bash_profile.zshrc 文件中创建别名

alias generator="/Users/userName/packages/generator/lib/generator"

现在您可以直接从终端使用命令行

用法

1- 生成工厂

generator make factory --name UserFactory

这将在 data/factories 目录中生成 user_factory.dart 文件

import 'package:famcare/auth/models/user.dart';
import 'package:famcare/auth/models/address.dart';
import 'package:famcare/auth/models/note.dart';

import 'address_factory.dart';
import 'note_factory.dart';

import 'package:faker/faker.dart';

class UserFactory {
  int? id;

  String? name;

  int? age;

  Address? address;

  List<Note>? notes;

  int? _count;

  dynamic create(
      {int? id, String? name, int? age, Address? address, List<Note>? notes}) {
    final mUser = User(
      id: id ?? this.id ?? faker.randomGenerator.integer(2),
      name: name ?? this.name ?? faker.randomGenerator.string(23),
      age: age ?? this.age ?? faker.randomGenerator.integer(2),
      address: address ?? this.address ?? AddressFactory().make(),
      notes: notes ?? this.notes ?? NoteFactory().count(10).make(),
    );
    if (_count != null) {
      return List<User>.filled(_count!, mUser);
    } else {
      return mUser;
    }
  }

  UserFactory state(
      {int? id, String? name, int? age, Address? address, List<Note>? notes}) {
    this.id = id ?? this.id;
    this.name = name ?? this.name;
    this.age = age ?? this.age;
    this.address = address ?? this.address;
    this.notes = notes ?? this.notes;
    return this;
  }

  UserFactory count(int count) {
    _count = count;
    return this;
  }

  UserFactory hasAddress(
      {Address? address,
      int? id,
      Country? country,
      String? address,
      String? street}) {
    if (address != null) {
      this.address = address;
    } else {
      this.address = AddressFactory().create(
        id: id,
        country: country,
        address: address,
        street: street,
      );
    }
    return this;
  }

  UserFactory hasNotes(
      {List<Note>? notes, int? count, String? title, String? content}) {
    if (notes != null) {
      this.notes = notes;
    } else {
      assert(count != null);
      this.notes = NoteFactory().count(count).create(
            title: title,
            content: content,
          );
    }
    return this;
  }
}

2- 生成功能,首先您需要创建一个包含此功能内容的 yaml 文件,并且文件名必须与功能名相同

例如,对于情感追踪功能,我们在情感追踪功能包中创建 emotions_tracker.yaml 文件

name: emotions_tracker


data:
  base_url: ***.***.app
  headers: { Authorization: Bearer ******,
             Content-Type: application/json }
  apis:
    - name: getEmotions
      method: GET
      path: api/v2/emotions
      model: Emotion
      response_model: ListResponse
    - name: addDailyEmotion
      method: POST
      path: api/v2/users/userId/emotions
      body: {emotion_id: 1, diary: hello im sad}
      values: {userId: 4}
      response_model: SuccessResponse
    - name: getCurrentEmotion
      method: GET
      path: api/v2/users/userId/emotions
      values: { userId: 4 }
      model: UserEmotion
      response_model: SingleResponse
    - name: emotionsHistory
      method: GET
      path: api/v2/users/userId/emotions-history
      values: {userId: 4}
      model: UserEmotion
      response_model: ListResponse
  create_repo: true
  events:
    - name: View Daily Emotion
      params: [Placement]
    - name: View Emotions History
      params: []
    - name: View Specific Day Emotional
      params: [Id, Emotion Id,  Emotion Text,  Created At]

presentations:
  - name: DailyEmotionView
    controllers:
      - initial_api: getCurrentEmotion
        apis: [getCurrentEmotion]
    feature_flag:
      key: emotions_feature_flag
    generate_route: false
  - name: FillDailyEmotionPage
    controllers:
      - initial_api: getEmotions
        apis: [addDailyEmotion]
    generate_route: true
  - name: EmotionsHistoryPage
    controller:
      - initial_api: emotionsHistory
        apis: [emotionsHistory]
    generate_route: true

然后输入此命令

generate make feature --name emotionsTracker

这将生成所有数据文件

  • Emotion
  • UserEmotion
  • EmotionsTrackerRemoteDataSource
  • EmotionsTrackerRepository
  • ViewDailyEmotionEvent
  • ViewEmotionsHistoryEvent
  • ViewSpecificDayEmotionalEvent

GitHub

查看 Github