generateBoilerplate method

void generateBoilerplate({
  1. required String featureName,
  2. required String basePath,
  3. required String templateBasePath,
  4. required Context context,
})

Renders all Mustache templates (model, entity, repository, datasource, usecase, bloc/event/state, or riverpod notifier) into the feature directory.

Each template receives a simple map to keep the generator decoupled from the Mustache engine.

Implementation

void generateBoilerplate({
  required String featureName,
  required String basePath,
  required String templateBasePath,
  required Context context,
}) {
  // Injector
  renderTemplate(
    '$templateBasePath/injector.mustache',
    '${context.projectRoot}/lib/core/di/injector.dart',
    {"projectName": context.projectName},
  );

  // Model
  renderTemplate(
    '$templateBasePath/data/model.mustache',
    '$basePath/data/models/${featureName}_model.dart',
    context.toMap(),
  );

  // Entity
  renderTemplate(
    '$templateBasePath/domain/entity.mustache',
    '$basePath/domain/entities/${featureName}_entity.dart',
    context.toMap(),
  );

  // Repository
  renderTemplate(
    '$templateBasePath/domain/repository.mustache',
    '$basePath/domain/repositories/${featureName}_repository.dart',
    {
      ...context.toMap(),
      "methods": context.methods
          .map((e) => {...e.toMap(), "methodNameLowerCase": e.methodName.camelCaseToSnakeCase()})
          .toList(),
    },
  );

  // Repository Impl
  renderTemplate(
    '$templateBasePath/data/repository_impl.mustache',
    '$basePath/data/repositories/${featureName}_repository_impl.dart',
    {
      ...context.toMap(),
      "methods": context.methods
          .map((e) => {...e.toMap(), "methodNameLowerCase": e.methodName.camelCaseToSnakeCase()})
          .toList(),
    },
  );

  // Remote Datasource
  renderTemplate(
    '$templateBasePath/data/remote_datasource.mustache',
    '$basePath/data/datasources/${featureName}_remote_datasource.dart',
    {
      ...context.toMap(),
      "methods": context.methods
          .map((e) => {...e.toMap(), "methodNameLowerCase": e.methodName.camelCaseToSnakeCase()})
          .toList(),
    },
  );

  // Use Cases
  if (context.generateUseCase) {
    renderTemplate(
      '$templateBasePath/usecase.mustache',
      '${context.projectRoot}/lib/features/shared/usecase/usecase.dart',
      context.toMap(),
    );

    for (var method in context.methods) {
      renderTemplate(
        '$templateBasePath/domain/usecase.mustache',
        '$basePath/domain/usecases/${method.methodName.camelCaseToSnakeCase()}_usecase.dart',
        {
          'name': context.name,
          'isList': context.isList,
          'nameLowerCase': context.nameLowerCase,
          'nameCamelCase': context.nameCamelCase,
          'projectName': context.projectName,
          ...method.toMap(),
        },
      );
    }
  }

  if (context.config.bloc == true) {
    // Bloc
    renderTemplate(
      '$templateBasePath/presentation/bloc/bloc.mustache',
      '$basePath/presentation/bloc/${featureName}_bloc.dart',
      {
        ...context.toMap(),
        "methods": context.methods
            .map(
              (e) => {...e.toMap(), "methodNameLowerCase": e.methodName.camelCaseToSnakeCase()},
            )
            .toList(),
      },
    );

    // Event
    renderTemplate(
      '$templateBasePath/presentation/bloc/event.mustache',
      '$basePath/presentation/bloc/${featureName}_event.dart',
      {
        'name': context.name,
        'projectName': context.projectName,
        "methods": context.methods
            .map(
              (e) => {
                ...e.toMap(),
                "methodNameLowerCase": e.methodName.camelCaseToSnakeCase(),
                'nameLowerCase': context.nameLowerCase,
              },
            )
            .toList(),
      },
    );

    // State
    renderTemplate(
      '$templateBasePath/presentation/bloc/state.mustache',
      '$basePath/presentation/bloc/${featureName}_state.dart',
      {
        'name': context.name,
        'isList': context.isList,
        'nameLowerCase': context.nameLowerCase,
        'nameCamelCase': context.nameCamelCase,
        ...context.toMap(),
      },
    );

    // Screen
    renderTemplate(
      '$templateBasePath/presentation/screen/screen_bloc.mustache',
      '$basePath/presentation/screen/${featureName}_screen.dart',
      context.toMap(),
    );
  }

  if (context.config.riverpod == true) {
    // Notifier
    renderTemplate(
      '$templateBasePath/presentation/riverpod/notifier.mustache',
      '$basePath/presentation/riverpod/${featureName}_notifier.dart',
      {
        ...context.toMap(),
        "methods": context.methods
            .map(
              (e) => {...e.toMap(), "methodNameLowerCase": e.methodName.camelCaseToSnakeCase()},
            )
            .toList(),
      },
    );

    // Screen
    renderTemplate(
      '$templateBasePath/presentation/screen/screen_riverpod.mustache',
      '$basePath/presentation/screen/${featureName}_screen.dart',
      context.toMap(),
    );
  }
}