version.dart 3.06 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

class Version implements Comparable<Version> {
  /// Creates a new [Version] object.
  factory Version(int major, int minor, int patch, {String text}) {
    if (text == null) {
      text = major == null ? '0' : '$major';
10 11 12 13
      if (minor != null)
        text = '$text.$minor';
      if (patch != null)
        text = '$text.$patch';
14 15
    }

16
    return Version._(major ?? 0, minor ?? 0, patch ?? 0, text);
17 18 19 20
  }

  Version._(this.major, this.minor, this.patch, this._text) {
    if (major < 0)
21
      throw ArgumentError('Major version must be non-negative.');
22
    if (minor < 0)
23
      throw ArgumentError('Minor version must be non-negative.');
24
    if (patch < 0)
25
      throw ArgumentError('Patch version must be non-negative.');
26 27 28 29
  }

  /// Creates a new [Version] by parsing [text].
  factory Version.parse(String text) {
30
    final Match match = versionPattern.firstMatch(text ?? '');
31
    if (match == null) {
32
      return null;
33 34 35
    }

    try {
36 37 38
      final int major = int.parse(match[1] ?? '0');
      final int minor = int.parse(match[3] ?? '0');
      final int patch = int.parse(match[5] ?? '0');
39
      return Version._(major, minor, patch, text);
40
    } on FormatException {
41
      return null;
42 43 44
    }
  }

45 46 47 48 49 50 51 52 53 54 55 56 57 58
  /// Returns the primary version out of a list of candidates.
  ///
  /// This is the highest-numbered stable version.
  static Version primary(List<Version> versions) {
    Version primary;
    for (Version version in versions) {
      if (primary == null || (version > primary)) {
        primary = version;
      }
    }
    return primary;
  }


59
  static Version get unknown => Version(0, 0, 0, text: 'unknown');
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  /// The major version number: "1" in "1.2.3".
  final int major;

  /// The minor version number: "2" in "1.2.3".
  final int minor;

  /// The patch version number: "3" in "1.2.3".
  final int patch;

  /// The original string representation of the version number.
  ///
  /// This preserves textual artifacts like leading zeros that may be left out
  /// of the parsed version.
  final String _text;

  static final RegExp versionPattern =
      RegExp(r'^(\d+)(\.(\d+)(\.(\d+))?)?');

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  /// Two [Version]s are equal if their version numbers are. The version text
  /// is ignored.
  @override
  bool operator ==(dynamic other) {
    if (other is! Version)
      return false;
    return major == other.major && minor == other.minor && patch == other.patch;
  }

  @override
  int get hashCode => major ^ minor ^ patch;

  bool operator <(Version other) => compareTo(other) < 0;
  bool operator >(Version other) => compareTo(other) > 0;
  bool operator <=(Version other) => compareTo(other) <= 0;
  bool operator >=(Version other) => compareTo(other) >= 0;

  @override
  int compareTo(Version other) {
98 99 100 101
    if (major != other.major)
      return major.compareTo(other.major);
    if (minor != other.minor)
      return minor.compareTo(other.minor);
102 103 104 105 106 107
    return patch.compareTo(other.patch);
  }

  @override
  String toString() => _text;
}