Script.fromYaml constructor

Script.fromYaml(
  1. Object yaml, {
  2. required String name,
  3. required String workspacePath,
})

Implementation

factory Script.fromYaml(
  Object yaml, {
  required String name,
  required String workspacePath,
}) {
  final scriptPath = 'scripts/$name';
  String? run;
  String? description;
  var env = <String, String>{};
  final List<String> steps;
  PackageFilters? packageFilters;
  ExecOptions? exec;

  if (yaml is String) {
    run = yaml;
    steps = [];
    return Script(
      name: name,
      run: run,
      steps: steps,
      description: description,
      env: env,
      packageFilters: packageFilters,
      exec: exec,
    );
  }

  if (yaml is! Map<Object?, Object?>) {
    throw MelosConfigException('Unsupported value for script $name');
  }

  final execYaml = yaml['exec'];
  if (execYaml is String) {
    if (yaml['run'] is String) {
      throw MelosConfigException(
        'The script $name specifies a command in both "run" and "exec". '
        'Remove one of them.',
      );
    }
    run = execYaml;
    exec = const ExecOptions();
  } else {
    final execMap = assertKeyIsA<Map<Object?, Object?>?>(
      key: 'exec',
      map: yaml,
      path: scriptPath,
    );

    exec = execMap != null
        ? execOptionsFromYaml(execMap, scriptName: name)
        : null;
  }

  final stepsList = yaml['steps'];
  steps = stepsList is List && stepsList.isNotEmpty
      ? assertListIsA<String>(
          key: 'steps',
          map: yaml,
          isRequired: false,
          assertItemIsA: (index, value) {
            return assertIsA<String>(
              value: value,
              index: index,
              path: scriptPath,
            );
          },
        )
      : [];

  final runYaml = yaml['run'];
  if (runYaml is String && runYaml.isNotEmpty) {
    run = execYaml is String
        ? execYaml
        : assertKeyIsA<String>(
            key: 'run',
            map: yaml,
            path: scriptPath,
          );
  }

  description = assertKeyIsA<String?>(
    key: 'description',
    map: yaml,
    path: scriptPath,
  );
  final envMap = assertKeyIsA<Map<Object?, Object?>?>(
    key: 'env',
    map: yaml,
    path: scriptPath,
  );

  env = <String, String>{
    if (envMap != null)
      for (final entry in envMap.entries)
        assertIsA<String>(
          value: entry.key,
          key: 'env',
          path: scriptPath,
        ): entry.value.toString(),
  };

  final packageFiltersMap = assertKeyIsA<Map<Object?, Object?>?>(
    key: 'packageFilters',
    map: yaml,
    path: scriptPath,
  );

  packageFilters = packageFiltersMap == null
      ? null
      : PackageFilters.fromYaml(
          packageFiltersMap,
          path: 'scripts/$name/packageFilters',
          workspacePath: workspacePath,
        );

  return Script(
    name: name,
    run: run,
    steps: steps,
    description: description,
    env: env,
    packageFilters: packageFilters,
    exec: exec,
  );
}