process method

  1. @override
void process(
  1. String dartFile
)
override

Implementation

@override
void process(String dartFile) {
  print('Model: $dartFile');

  var replacer = CodeReplacer(fileReadString(dartFile));

  var code = parseDartFile(dartFile);
  if (code == null) return null;

  var enums = enumGenerator?.names ?? {};
  var classElements = getClasses(code);

  for (var classElement in classElements) {
    var meta = getTag(classElement);
    if (meta != 'model') continue;

    var metaArgs = getTagArgs(classElement);
    var className = classElement.name.lexeme;

    List<ClassMember> fields = classElement.members;

    final processes = <_FieldProcessor>[
      ConstructFields(className: className),
      MapOfFields(className: className, enums: enums),
      JsonOfFields(className: className),
      SerializeFields(enums: enums),
      CloneFields(className: className, enabled: metaArgs.contains('clone')),
      PatchFields(
          className: className, enabled: metaArgs.contains('patchWith')),
    ];

    for (var process in processes) {
      for (var member in fields) {
        if (process.isGenerated(member)) {
          replacer.space(member.offset, member.length);
        }
      }
    }

    var output = '';
    for (var process in processes) {
      output += process.generate(fields);
    }

    replacer.add(classElement.offset + classElement.length - 1, 0, output);
  }

  try {
    var output = formatCode(replacer.process());
    fileWriteString(dartFile, output);
  } catch (e) {
    print(e);
    return;
  }

  _lastGenerated = dartFile;
}