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.firstOrNull == 'init') {
    init();
    return;
  }

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

  appsName = argResults?['apps-name'];

  final searchFileJson2Dart = appsName?.isNotEmpty ?? false
      ? '${appsName}_json2dart.yaml'
      : '*json2dart.yaml';

  final workingDirectory = find(
    searchFileJson2Dart,
    workingDirectory: join(current, 'json2dart'),
  ).toList();

  for (var pathJson2Dart in workingDirectory) {
    if (!exists(pathJson2Dart)) {
      StatusHelper.warning(
          'you don\'t have "json2dart.yaml" in $pathJson2Dart');
    }

    final yml = YamlHelper.loadFileYaml(pathJson2Dart);
    Map json2DartMap = Map.from(yml);

    if (json2DartMap['json2dart'] != null) {
      final config = json2DartMap.remove('json2dart');

      if (config['body_format_date_time'] != null) {
        defaultBodyDateFormat =
            ".toFormatDateTimeBody('${config['body_format_date_time']}')";
      }
      if (config['response_format_date_time'] != null) {
        defaultResponseDateFormat =
            ".toFormatDateTimeResponse('${config['response_format_date_time']}')";
      }
      if (config['endpoint'] != null && config['endpoint'] is bool) {
        isEndpoint = config['endpoint'];
      }
      if (config['api'] != null && config['api'] is bool) {
        isApi = config['api'];
      }
      if (config['unit-test'] != null && config['unit-test'] is bool) {
        isUnitTest = config['unit-test'];
      }
      if (config['replace'] != null && config['replace'] is bool) {
        isReplace = config['replace'];
      }
    }

    isEndpoint = (argResults?.arguments.firstWhereOrNull(
                    (element) => element.contains('endpoint')) !=
                null
            ? argResults!['endpoint']
            : null) ??
        isEndpoint ??
        true;

    isApi = (argResults?.arguments
                    .firstWhereOrNull((element) => element.contains('api')) !=
                null
            ? argResults!['api']
            : null) ??
        isApi ??
        true;
    isUnitTest = (argResults?.arguments.firstWhereOrNull(
                    (element) => element.contains('unit-test')) !=
                null
            ? argResults!['unit-test']
            : null) ??
        isUnitTest ??
        false;
    isOnlyUnitTest = (argResults?.arguments.firstWhereOrNull(
                    (element) => element.contains('only-unit-test')) !=
                null
            ? argResults!['only-unit-test']
            : null) ??
        isOnlyUnitTest ??
        false;
    isReplace = (argResults?.arguments.firstWhereOrNull(
                    (element) => element.contains('replace')) !=
                null
            ? argResults!['replace']
            : null) ??
        isReplace ??
        false;
    featureName = argResults?['feature-name'];
    pageName = argResults?['page-name'];

    if (!isOnlyUnitTest && isEndpoint) {
      await 'morpheme endpoint --json2dart'.run;
    }

    if (featureName != null) {
      if (json2DartMap.keys
              .firstWhereOrNull((element) => element == featureName) ==
          null) {
        StatusHelper.warning('$featureName not found in json2dart.yaml');
      } else {
        final lastPathJson2Dart = pathJson2Dart.split(separator).last;

        String featurePath = join(current, 'features', featureName);
        String? appsName = this.appsName;
        if (lastPathJson2Dart.contains('_')) {
          appsName = lastPathJson2Dart.split('_').first;
          featurePath =
              join(current, 'apps', appsName, 'features', featureName);
        }
        await handleFeature(
            featurePath, featureName!, json2DartMap[featureName], appsName);
      }
    } else {
      for (var element in json2DartMap.entries) {
        final featureName = element.key;
        final featureValue = element.value;

        final lastPathJson2Dart = pathJson2Dart.split(separator).last;

        String featurePath = join(current, 'features', featureName);
        String? appsName = this.appsName;
        if (lastPathJson2Dart.contains('_')) {
          appsName = lastPathJson2Dart.split('_').first;
          featurePath =
              join(current, 'apps', appsName, 'features', featureName);
        }
        await handleFeature(featurePath, featureName, featureValue, appsName);
      }
    }
  }

  print('Execute morpheme format................');
  if (format.isNotEmpty) await ModularHelper.format(format);
  print('Execute morpheme fix................');
  if (fix.isNotEmpty) await ModularHelper.fix(fix);
  print('Execute morpheme format................');
  if (format.isNotEmpty) await ModularHelper.format(format);

  StatusHelper.success('morpheme json2dart');
}