parse static method

Version parse(
  1. String? versionString
)

Creates a Version instance from a string.

The string must conform to the specification at http://semver.org/ Throws FormatException if the string is empty or does not conform to the spec.

Implementation

static Version parse(String? versionString) {
  if (versionString?.trim().isEmpty ?? true) {
    throw FormatException("Cannot parse empty string into version");
  }
  if (!_versionRegex.hasMatch(versionString!)) {
    throw FormatException("Not a properly formatted version string");
  }
  final m = _versionRegex.firstMatch(versionString);
  final String version = m!.group(1)!;

  int? major, minor, patch;
  final List<String> parts = version.split(".");
  major = int.parse(parts[0]);
  if (parts.length > 1) {
    minor = int.parse(parts[1]);
    if (parts.length > 2) {
      patch = int.parse(parts[2]);
    }
  }

  final String preReleaseString = m.group(3) ?? "";
  List<String> preReleaseList = <String>[];
  if (preReleaseString.trim().isNotEmpty) {
    preReleaseList = preReleaseString.split(".");
  }
  final String build = m.group(5) ?? "";

  return Version(major, minor ?? 0, patch ?? 0,
      build: build, preRelease: preReleaseList);
}