MelosWorkspaceConfig.fromYaml constructor

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

Implementation

factory MelosWorkspaceConfig.fromYaml(
  Map<Object?, Object?> pubspecYaml, {
  required String path,
}) {
  final name = assertKeyIsA<String>(key: 'name', map: pubspecYaml);
  if (!isValidPubPackageName(name)) {
    throw MelosConfigException(
      'The name $name is not a valid pub package name.',
    );
  }

  final melosYaml = pubspecYaml['melos'] as Map<Object?, Object?>? ?? {};

  HostedGitRepository? repository;
  if (melosYaml.containsKey('repository')) {
    final repositoryYaml = melosYaml['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 repositoryName = assertKeyIsA<String>(
        key: 'name',
        map: repositoryYaml,
        path: 'repository',
      );

      try {
        repository = parseHostedGitRepositorySpec(
          type,
          origin,
          owner,
          repositoryName,
        );
      } on FormatException catch (e) {
        throw MelosConfigException(e.toString());
      }
    } else if (repositoryYaml is String) {
      repository = _urlToRepository(repositoryYaml);
    } else if (repositoryYaml != null) {
      throw MelosConfigException(
        'The repository value must be a string or repository spec',
      );
    }
  } else if (pubspecYaml.containsKey('repository')) {
    final repositoryYaml = pubspecYaml['repository'];
    if (repositoryYaml is String) {
      repository = _urlToRepository(repositoryYaml);
    }
  }

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

  final categories = assertMapIsA<String, List<String>>(
    key: 'categories',
    map: melosYaml,
    isRequired: false,
    assertKey: (value) => assertIsA<String>(
      value: value,
    ),
    assertValue: (key, value) => assertListIsA<String>(
      key: key!,
      map: (melosYaml['categories'] ?? {}) as Map<Object?, Object?>,
      isRequired: false,
      assertItemIsA: (index, value) => assertIsA<String>(
        value: value,
        index: index,
      ),
    ),
  );

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

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

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

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

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

  final useRootAsPackage =
      assertKeyIsA<bool?>(
        key: 'useRootAsPackage',
        map: melosYaml,
      ) ??
      false;

  final discoverNestedWorkspaces =
      assertKeyIsA<bool?>(
        key: 'discoverNestedWorkspaces',
        map: melosYaml,
      ) ??
      false;

  final pubConfig = melosYaml.containsKey('pub')
      ? PubClientConfig.fromYaml(melosYaml['pub'])
      : const PubClientConfig();

  return MelosWorkspaceConfig(
    path: path,
    name: name,
    repository: repository,
    pub: pubConfig,
    sdkPath: sdkPath,
    categories: categories.map(
      (key, value) => MapEntry(
        key,
        value.map(Glob.new).toList(),
      ),
    ),
    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,
            repositoryIsConfigured: repository != null,
          ),
    useRootAsPackage: useRootAsPackage,
    discoverNestedWorkspaces: discoverNestedWorkspaces,
  );
}