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 {
  final argMorphemeYaml = argResults.getOptionMorphemeYaml();
  final argFlavor = argResults.getOptionFlavor(defaultTo: '');

  YamlHelper.validateMorphemeYaml(argMorphemeYaml);
  final morphemeYaml = YamlHelper.loadFileYaml(argMorphemeYaml);

  if (morphemeYaml['assets'] == null) {
    StatusHelper.failed('assets not found in $argMorphemeYaml');
  }

  projectName = morphemeYaml.projectName;
  final morphemeAssets = morphemeYaml['assets'] as Map;

  if (argFlavor.isNotEmpty && exists(morphemeAssets.flavorDir)) {
    final pathFlavorDir = join(current, morphemeAssets.flavorDir, argFlavor);
    final pathAssetsDir = join(current, morphemeAssets.assetsDir);

    copyTree(pathFlavorDir, pathAssetsDir, overwrite: true);
  }

  final pathYaml = join(current, morphemeAssets.pubspecDir, 'pubspec.yaml');

  if (!exists(pathYaml)) {
    StatusHelper.failed('$pathYaml not found!');
  }

  final yaml = YamlHelper.loadFileYaml(pathYaml);
  if (!yaml.containsKey('flutter')) {
    StatusHelper.failed('flutter not found in pubspec.yaml');
  }

  final flutter = (yaml['flutter'] is Map) ? yaml['flutter'] as Map : null;
  if (flutter == null || !flutter.containsKey('assets')) {
    StatusHelper.failed('assets not found in pubspec.yaml');
  }

  final pubspecAssets = (yaml['flutter']['assets'] as List)
      .where((element) =>
          !RegExp(r'(0|[1-9]\d*)\.?(0|[1-9]\d*)?\.?(0|[1-9]\d*)?x')
              .hasMatch(element))
      .toList();

  DirectoryHelper.createDir(join(morphemeAssets.outputDir, 'src'));

  createFileAssets(pubspecAssets, morphemeAssets);
  if (morphemeAssets.createLibraryFile) createFileExport(morphemeAssets);

  await ModularHelper.format([morphemeAssets.outputDir]);

  StatusHelper.success('morpheme assets');
}