run static method

void run(
  1. List<String> args
)

Main

Implementation

static void run(List<String> args) async {
  String projectRoot = Utils.getProjectRoot();
  if (projectRoot.isEmpty) {
    print('Error: Current location is not the root of the project');
    exitCode = 2;
    return;
  }

  // Read configuration file, if exists
  String configFilePath = '';
  if (Utils.isMustangProject()) {
    configFilePath = p.join(projectRoot, configFile);
  }

  List<String>? customScreenImports;
  String? errorWidget;
  String? progressWidget;
  if (configFilePath.isNotEmpty && File(configFilePath).existsSync()) {
    File configFile = File(configFilePath);
    String rawConfig = await configFile.readAsString();

    dynamic yamlConfig = loadYaml(rawConfig);

    if (yamlConfig[screenKey] != null) {
      YamlList? tempScreenImports = yamlConfig[screenKey][screenImportsKey];
      if (tempScreenImports != null) {
        customScreenImports =
            tempScreenImports.map((e) => e.toString()).toList();
      }
      errorWidget = yamlConfig[screenKey][screenErrorWidgetKey];
      progressWidget = yamlConfig[screenKey][screenProgressWidgetKey];
    }
  }

  ArgParser parser = Args.parser();
  try {
    ArgResults parsedArgs = parser.parse(args);

    if (parsedArgs.arguments.isEmpty) {
      print(parser.usage);
      exitCode = 2;
      return;
    }

    // if arg -a/--aspect exists
    String aspectFile = parsedArgs['aspect'] ?? '';
    if (aspectFile.isNotEmpty) {
      print('Creating aspect file for the screen...');
      await Aspect.create(aspectFile);
    }

    // if arg -s/--screen exists
    String screenDir = parsedArgs['screen'] ?? '';
    if (screenDir.isNotEmpty) {
      print('Creating screen files...');
      screenDir = screenDir.toLowerCase().replaceAll('-', '_');
      await ScreenDirectory.create(screenDir);
      await ScreenState.create(screenDir);
      await ScreenService.create(screenDir);
      await Screen.create(
        screenDir,
        customScreenImports,
        errorWidget,
        progressWidget,
      );
      print('Creating model for the screen...');
      await AppModel.create(screenDir);
    }

    // if arg -m/--model exists
    String modelFile = parsedArgs['model'] ?? '';
    if (modelFile.isNotEmpty) {
      await AppModel.create(modelFile);
    }

    // if arg -d/--clean exists
    bool cleanFlag = parsedArgs['clean'] ?? false;
    if (cleanFlag) {
      Utils.runProcess('dart', [
        'run',
        'build_runner',
        'clean',
      ]);
    }

    // if arg -b/--build exists
    bool buildFlag = parsedArgs['build'] ?? false;
    if (buildFlag) {
      Utils.runProcess('dart', [
        'run',
        'build_runner',
        'build',
        '--delete-conflicting-outputs',
        '--fail-on-severe',
      ]);
      return;
    }

    // if arg -w/--watch exists
    bool watchFlag = parsedArgs['watch'] ?? false;
    if (watchFlag) {
      Utils.runProcess('dart', [
        'run',
        'build_runner',
        'watch',
        '--delete-conflicting-outputs',
      ]);
      return;
    }
  } catch (e) {
    exitCode = 2;
    print(e.toString());
    print(parser.usage);
  }
}