version.dart 3.17 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7
import 'package:meta/meta.dart';

@immutable
8 9 10 11 12
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';
13
      if (minor != null) {
14
        text = '$text.$minor';
15 16
      }
      if (patch != null) {
17
        text = '$text.$patch';
18
      }
19 20
    }

21
    return Version._(major ?? 0, minor ?? 0, patch ?? 0, text);
22 23 24
  }

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

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

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

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


67
  static Version get unknown => Version(0, 0, 0, text: 'unknown');
68

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

87 88 89
  /// Two [Version]s are equal if their version numbers are. The version text
  /// is ignored.
  @override
90 91 92 93 94
  bool operator ==(Object other) {
    return other is Version
        && other.major == major
        && other.minor == minor
        && other.patch == patch;
95 96 97 98 99 100 101 102 103 104 105 106
  }

  @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) {
107
    if (major != other.major) {
108
      return major.compareTo(other.major);
109 110
    }
    if (minor != other.minor) {
111
      return minor.compareTo(other.minor);
112
    }
113 114 115 116 117 118
    return patch.compareTo(other.patch);
  }

  @override
  String toString() => _text;
}