VersionConfig.parse constructor

VersionConfig.parse(
  1. dynamic raw
)

Implementation

factory VersionConfig.parse(dynamic raw) {
  if (raw is! Map) {
    throw ArgumentError("'version' must be a mapping.");
  }
  final map = Map<String, dynamic>.from(raw);
  final rawCode = map['code'];
  if (rawCode == null || rawCode.toString().trim().isEmpty) {
    throw ArgumentError(
      "version.code is required (pubspec, increment, git-commits, "
      'timestamp, or a literal number).',
    );
  }
  final code = rawCode.toString().trim();
  final source = switch (code.toLowerCase()) {
    'pubspec' => VersionCodeSource.pubspec,
    'increment' => VersionCodeSource.increment,
    'git-commits' => VersionCodeSource.gitCommits,
    'timestamp' => VersionCodeSource.timestamp,
    _ when int.tryParse(code) != null => VersionCodeSource.literal,
    _ => throw ArgumentError(
        "version.code must be pubspec, increment, git-commits, timestamp, "
        "or a literal number; got '$code'.",
      ),
  };

  final writeBack = _bool(map['write-back'], 'version.write-back');
  if (source == VersionCodeSource.literal && int.parse(code) < 1) {
    throw ArgumentError('version.code literal must be greater than zero.');
  }
  if (writeBack && source == VersionCodeSource.gitCommits) {
    // This is valid, but calling it out through the model keeps the parser
    // strict while allowing teams that deliberately mirror Git in pubspec.
  }
  return VersionConfig(
    name: map['name']?.toString(),
    code: code,
    codeSource: source,
    writeBack: writeBack,
  );
}