version.dart 3.13 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9
// 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
      if (minor != null) {
11
        text = '$text.$minor';
12 13
      }
      if (patch != null) {
14
        text = '$text.$patch';
15
      }
16 17
    }

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

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

  /// Creates a new [Version] by parsing [text].
  factory Version.parse(String text) {
35
    final Match match = versionPattern.firstMatch(text ?? '');
36
    if (match == null) {
37
      return null;
38 39 40
    }

    try {
41 42 43
      final int major = int.parse(match[1] ?? '0');
      final int minor = int.parse(match[3] ?? '0');
      final int patch = int.parse(match[5] ?? '0');
44
      return Version._(major, minor, patch, text);
45
    } on FormatException {
46
      return null;
47 48 49
    }
  }

50 51 52 53 54
  /// 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;
55
    for (final Version version in versions) {
56 57 58 59 60 61 62 63
      if (primary == null || (version > primary)) {
        primary = version;
      }
    }
    return primary;
  }


64
  static Version get unknown => Version(0, 0, 0, text: 'unknown');
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  /// 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+))?)?');

84 85 86 87
  /// Two [Version]s are equal if their version numbers are. The version text
  /// is ignored.
  @override
  bool operator ==(dynamic other) {
88
    if (other is! Version) {
89
      return false;
90
    }
91 92 93 94 95 96 97 98 99 100 101 102 103
    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) {
104
    if (major != other.major) {
105
      return major.compareTo(other.major);
106 107
    }
    if (minor != other.minor) {
108
      return minor.compareTo(other.minor);
109
    }
110 111 112 113 114 115
    return patch.compareTo(other.patch);
  }

  @override
  String toString() => _text;
}