version.dart 3.36 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
class Version implements Comparable<Version> {
  /// Creates a new [Version] object.
10
  factory Version(int? major, int? minor, int? patch, {String? text}) {
11 12
    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 25 26
  /// Public constant constructor when all fields are non-null, without default value fallbacks.
  const Version.withText(this.major, this.minor, this.patch, this._text);

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

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

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

56 57 58
  /// Returns the primary version out of a list of candidates.
  ///
  /// This is the highest-numbered stable version.
59 60
  static Version? primary(List<Version> versions) {
    Version? primary;
61
    for (final Version version in versions) {
62 63 64 65 66 67 68 69
      if (primary == null || (version > primary)) {
        primary = version;
      }
    }
    return primary;
  }


70
  static Version get unknown => Version(0, 0, 0, text: 'unknown');
71

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

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

  @override
101
  int get hashCode => Object.hash(major, minor, patch);
102 103 104 105 106 107 108 109

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

  @override
  String toString() => _text;
}