generate method

String generate(
  1. GeneratorData data
)

This is the main function to generate everything at first variables, types and modes of generation will detected and stored then everything will generate at the first step, classes of Model and Entity will generate (Every class will generate with a Additional name at the end of the name of class Model and Entity) then, mappers will generate for both directions of model to entity and entity to model

In Freezed mode all classes will decorated with @freezed annotation, of course, freezed annotations are custom, we know that in entities will do not need toJson and fromJson functions so, it should be informed to freezed but we have both functions for Models

This code generator also support the post-process-code-generation. So, at first, everything will generate, then freezed builder will call to generate everything with its annotation and code-generator

Implementation

String generate(GeneratorData data) {
  isFreezed = data.isFreezed;
  type = data.annotationType;
  variables = data.variablesList ?? [];

  String generatedClass = '';
  // generatedClass += Prints.variablesList(variables);

  GeneratorLog.info(title: 'Generating Class...');
  generatedClassName = '${data.className?.capitalizeFirst}${type?.name.capitalizeFirst}';
  if (isFreezed == true) {
    bool jsonGeneration = type == AnnotationTypes.model;
    generatedClass += AddCode.addLine('@Freezed(toJson: ${jsonGeneration.toString()}, fromJson: ${jsonGeneration.toString()})');
    generatedClass += AddCode.addLine('class $generatedClassName with _\$$generatedClassName {');
    generatedClass += AddCode.addLine('const factory $generatedClassName ({${_addFields(extended: data.extended)}}) = _$generatedClassName;');
    if (type == AnnotationTypes.model) {
      generatedClass += AddCode.addLine('\nfactory $generatedClassName.fromJson(Map<String, dynamic> json) => _\$${generatedClassName}FromJson(json);\n');
    }
    generatedClass += AddCode.addLine('}\n\n');
  } else {
    GeneratorLog.info(title: 'Adding Fields');
    generatedClass += AddCode.addLine(
        'class $generatedClassName ${data.extended == true ? 'extends ${data.className}' : ''} {${_generateConstructor()}\n${_addFields(extended: data.extended)}\n${type == AnnotationTypes.model ? _generateModelToAndFromJson() : ''}}');
  }
  GeneratorLog.info(title: 'Classes Generation Finished...');
  return generatedClass;
}