run method

  1. @override
void run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
void run() async {
  if (argResults?.rest.isEmpty ?? true) {
    StatusHelper.failed(
        'Api name is empty, add a new api with "morpheme api <api-name> -f <feature-name> -p <page-name>"');
  }

  final argMorphemeYaml = argResults.getOptionMorphemeYaml();
  projectName = YamlHelper.loadFileYaml(argMorphemeYaml).projectName;

  final appsName = argResults?['apps-name'] as String?;
  final featureName =
      (argResults?['feature-name'] as String? ?? '').snakeCase;
  final pageName = (argResults?['page-name'] as String? ?? '').snakeCase;
  final bool json2dart = argResults?['json2dart'] ?? false;
  final String method = argResults?['method'] as String? ?? 'post';
  final String? pathUrl = argResults?['path'];
  final String? header = argResults?['header'];
  final bool bodyList = (argResults?['body-list'] ?? false) &&
      !method.toLowerCase().contains('multipart');
  final bool responseList = argResults?['response-list'] ?? false;

  final CacheStrategy? cacheStrategy = argResults?['cache-strategy'] == null
      ? null
      : CacheStrategy.fromString(argResults?['cache-strategy']);
  final int? ttl = int.tryParse(argResults?['ttl'] ?? '');
  final bool? keepExpiredCache = argResults?['keep-expired-cache'] == null
      ? null
      : argResults?['keep-expired-cache'] == 'true';

  String pathFeature = join(current, 'features', featureName);
  if (appsName != null) {
    pathFeature = join(current, 'apps', appsName, 'features', featureName);
  }

  if (!exists(pathFeature)) {
    StatusHelper.failed(
        'Feature with "$featureName" does not exists, create a new feature with "morpheme feature <feature-name>"');
  }

  String pathPage = join(pathFeature, 'lib', pageName);

  if (!exists(pathPage)) {
    StatusHelper.failed(
        'Page with "$pageName" does not exists, create a new page with "morpheme page <page-name> -f <feature-name>"');
  }

  final apiName = (argResults?.rest.first ?? '').snakeCase;

  createLocator(pathPage, pageName, apiName);

  createDataDataSource(
    pathPage,
    appsName ?? '',
    pageName,
    apiName,
    method,
    pathUrl,
    header,
    bodyList,
    responseList,
    cacheStrategy,
    ttl,
    keepExpiredCache,
  );
  if (!json2dart) createDataModelBody(pathPage, pageName, apiName, bodyList);
  if (!json2dart) createDataModelResponse(pathPage, pageName, apiName);
  createDataRepository(pathPage, pageName, apiName, bodyList, responseList);
  if (!json2dart) createDomainEntity(pathPage, pageName, apiName);
  createDomainRepository(pathPage, pageName, apiName, bodyList, responseList);
  createDomainUseCase(pathPage, pageName, apiName, bodyList, responseList);
  createPresentationBloc(pathPage, pageName, apiName, bodyList, responseList);
  if (!json2dart) createMapper(pathPage, pageName, apiName);

  createPostLocator(pathPage, pageName, apiName);

  if (!json2dart) await ModularHelper.format();

  if (!json2dart) {
    StatusHelper.success('generate $apiName in $featureName/$pageName');
  }
}