MelosWorkspaceConfig.fromYaml constructor

MelosWorkspaceConfig.fromYaml(
  1. Map<Object?, Object?> yaml, {
  2. required String path,
})

Implementation

factory MelosWorkspaceConfig.fromYaml(
  Map<Object?, Object?> yaml, {
  required String path,
}) {
  final name = assertKeyIsA<String>(key: 'name', map: yaml);
  final isValidDartPackageNameRegExp =
      RegExp(r'^[a-z][a-z\d_-]*$', caseSensitive: false);
  if (!isValidDartPackageNameRegExp.hasMatch(name)) {
    throw MelosConfigException(
      'The name $name is not a valid dart package name',
    );
  }

  HostedGitRepository? repository;
  if (yaml.containsKey('repository')) {
    final repositoryYaml = yaml['repository'];

    if (repositoryYaml is Map<Object?, Object?>) {
      final type = assertKeyIsA<String>(
        key: 'type',
        map: repositoryYaml,
        path: 'repository',
      );
      final origin = assertKeyIsA<String>(
        key: 'origin',
        map: repositoryYaml,
        path: 'repository',
      );
      final owner = assertKeyIsA<String>(
        key: 'owner',
        map: repositoryYaml,
        path: 'repository',
      );
      final name = assertKeyIsA<String>(
        key: 'name',
        map: repositoryYaml,
        path: 'repository',
      );

      try {
        repository = parseHostedGitRepositorySpec(type, origin, owner, name);
      } on FormatException catch (e) {
        throw MelosConfigException(e.toString());
      }
    } else if (repositoryYaml is String) {
      Uri repositoryUrl;
      try {
        repositoryUrl = Uri.parse(repositoryYaml);
      } on FormatException catch (e) {
        throw MelosConfigException(
          'The repository URL $repositoryYaml is not a valid URL:\n $e',
        );
      }

      try {
        repository = parseHostedGitRepositoryUrl(repositoryUrl);
      } on FormatException catch (e) {
        throw MelosConfigException(e.toString());
      }
    } else if (repositoryYaml != null) {
      throw MelosConfigException(
        'The repository value must be a string or repository spec',
      );
    }
  }

  final packages = assertListIsA<String>(
    key: 'packages',
    map: yaml,
    isRequired: true,
    assertItemIsA: (index, value) => assertIsA<String>(
      value: value,
      index: index,
      path: 'packages',
    ),
  );
  final ignore = assertListIsA<String>(
    key: 'ignore',
    map: yaml,
    isRequired: false,
    assertItemIsA: (index, value) => assertIsA<String>(
      value: value,
      index: index,
      path: 'ignore',
    ),
  );

  final scriptsMap = assertKeyIsA<Map<Object?, Object?>?>(
    key: 'scripts',
    map: yaml,
  );

  final ideMap = assertKeyIsA<Map<Object?, Object?>?>(
    key: 'ide',
    map: yaml,
  );

  final commandMap = assertKeyIsA<Map<Object?, Object?>?>(
    key: 'command',
    map: yaml,
  );

  final sdkPath = assertKeyIsA<String?>(
    key: 'sdkPath',
    map: yaml,
  );

  return MelosWorkspaceConfig(
    path: path,
    name: name,
    repository: repository,
    sdkPath: sdkPath,
    packages: packages
        .map((package) => createGlob(package, currentDirectoryPath: path))
        .toList(),
    ignore: ignore
        .map((ignore) => createGlob(ignore, currentDirectoryPath: path))
        .toList(),
    scripts: scriptsMap == null
        ? Scripts.empty
        : Scripts.fromYaml(scriptsMap, workspacePath: path),
    ide: ideMap == null ? IDEConfigs.empty : IDEConfigs.fromYaml(ideMap),
    commands: commandMap == null
        ? CommandConfigs.empty
        : CommandConfigs.fromYaml(commandMap, workspacePath: path),
  );
}