PackageCheckoutConfig.fromJson constructor

PackageCheckoutConfig.fromJson(
  1. String key,
  2. dynamic json
)

Implementation

factory PackageCheckoutConfig.fromJson(
  String key,
  dynamic json,
) {
  GitReference? git;
  VersionConstraint? version;
  String? repo;
  CheckoutMode? mode;
  String? source;

  json ??= {};
  if (json is String) {
    version = VersionConstraint.parse(json);
  } else if (json is Map<Object?, Object?>) {
    final gitConfig = json['git'];
    final versionConfig = json['version'];
    final modeConfig = json['mode'];
    if (gitConfig != null) {
      try {
        git = GitReference.fromJson({'git': gitConfig});
      } catch (e) {
        throw StateError(
          'Invalid configuration for package $key: '
          'the git property must match a pubspec git definition',
        );
      }
    }

    if (versionConfig is String) {
      try {
        version = VersionConstraint.parse(versionConfig);
      } catch (e) {
        throw StateError(
          'Invalid configuration for package $key: '
          'the version property must match a pubspec version constraint',
        );
      }
    }
    repo = json['repo'] as String?;
    if (modeConfig is String) {
      mode = CheckoutMode.parse(modeConfig);
    }

    if (json['source'] is String) {
      source = json['source'] as String;
    }
  }

  return PackageCheckoutConfig(
    git: git,
    repo: repo,
    version: version,
    mode: mode,
    source: source,
  );
}