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

5 6 7
import 'globals.dart' show ConductorException, releaseCandidateBranchRegex;

import 'proto/conductor_state.pbenum.dart';
8

9 10 11 12 13 14
/// Possible string formats that `flutter --version` can return.
enum VersionType {
  /// A stable flutter release.
  ///
  /// Example: '1.2.3'
  stable,
15

16 17 18 19
  /// A pre-stable flutter release.
  ///
  /// Example: '1.2.3-4.5.pre'
  development,
20

21 22 23 24 25 26
  /// A master channel flutter version.
  ///
  /// Example: '1.2.3-4.0.pre.10'
  ///
  /// The last number is the number of commits past the last tagged version.
  latest,
27 28 29 30

  /// A master channel flutter version from git describe.
  ///
  /// Example: '1.2.3-4.0.pre-10-gabc123'.
31
  /// Example: '1.2.3-10-gabc123'.
32
  gitDescribe,
33 34 35 36 37 38
}

final Map<VersionType, RegExp> versionPatterns = <VersionType, RegExp>{
  VersionType.stable: RegExp(r'^(\d+)\.(\d+)\.(\d+)$'),
  VersionType.development: RegExp(r'^(\d+)\.(\d+)\.(\d+)-(\d+)\.(\d+)\.pre$'),
  VersionType.latest: RegExp(r'^(\d+)\.(\d+)\.(\d+)-(\d+)\.(\d+)\.pre\.(\d+)$'),
39
  VersionType.gitDescribe: RegExp(r'^(\d+)\.(\d+)\.(\d+)-((\d+)\.(\d+)\.pre-)?(\d+)-g[a-f0-9]+$'),
40 41 42 43
};

class Version {
  Version({
44 45 46
    required this.x,
    required this.y,
    required this.z,
47 48 49
    this.m,
    this.n,
    this.commits,
50
    required this.type,
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  }) {
    switch (type) {
      case VersionType.stable:
        assert(m == null);
        assert(n == null);
        assert(commits == null);
        break;
      case VersionType.development:
        assert(m != null);
        assert(n != null);
        assert(commits == null);
        break;
      case VersionType.latest:
        assert(m != null);
        assert(n != null);
        assert(commits != null);
        break;
68
      case VersionType.gitDescribe:
69 70
        assert(commits != null);
        break;
71 72 73 74 75 76 77 78 79 80 81 82 83
    }
  }

  /// Create a new [Version] from a version string.
  ///
  /// It is expected that [versionString] will be generated by
  /// `flutter --version` and match one of `stablePattern`, `developmentPattern`
  /// and `latestPattern`.
  factory Version.fromString(String versionString) {
    assert(versionString != null);

    versionString = versionString.trim();
    // stable tag
84
    Match? match = versionPatterns[VersionType.stable]!.firstMatch(versionString);
85 86
    if (match != null) {
      // parse stable
87 88 89 90
      final List<int> parts = match
          .groups(<int>[1, 2, 3])
          .map((String? s) => int.parse(s!))
          .toList();
91 92 93 94 95 96 97 98
      return Version(
        x: parts[0],
        y: parts[1],
        z: parts[2],
        type: VersionType.stable,
      );
    }
    // development tag
99
    match = versionPatterns[VersionType.development]!.firstMatch(versionString);
100 101 102
    if (match != null) {
      // parse development
      final List<int> parts =
103
          match.groups(<int>[1, 2, 3, 4, 5]).map((String? s) => int.parse(s!)).toList();
104 105 106 107 108 109 110 111 112 113
      return Version(
        x: parts[0],
        y: parts[1],
        z: parts[2],
        m: parts[3],
        n: parts[4],
        type: VersionType.development,
      );
    }
    // latest tag
114
    match = versionPatterns[VersionType.latest]!.firstMatch(versionString);
115 116
    if (match != null) {
      // parse latest
117 118 119 120 121
      final List<int> parts = match.groups(
        <int>[1, 2, 3, 4, 5, 6],
      ).map(
        (String? s) => int.parse(s!),
      ).toList();
122 123 124 125 126 127 128 129 130 131
      return Version(
        x: parts[0],
        y: parts[1],
        z: parts[2],
        m: parts[3],
        n: parts[4],
        commits: parts[5],
        type: VersionType.latest,
      );
    }
132 133 134
    match = versionPatterns[VersionType.gitDescribe]!.firstMatch(versionString);
    if (match != null) {
      // parse latest
135 136 137 138 139 140
      final int x = int.parse(match.group(1)!);
      final int y = int.parse(match.group(2)!);
      final int z = int.parse(match.group(3)!);
      final int? m = int.tryParse(match.group(5) ?? '');
      final int? n = int.tryParse(match.group(6) ?? '');
      final int commits = int.parse(match.group(7)!);
141
      return Version(
142 143 144 145 146 147 148
        x: x,
        y: y,
        z: z,
        m: m,
        n: n,
        commits: commits,
        type: VersionType.gitDescribe,
149 150
      );
    }
151 152 153 154 155 156 157 158
    throw Exception('${versionString.trim()} cannot be parsed');
  }

  // Returns a new version with the given [increment] part incremented.
  // NOTE new version must be of same type as previousVersion.
  factory Version.increment(
    Version previousVersion,
    String increment, {
159
    VersionType? nextVersionType,
160 161 162 163
  }) {
    final int nextX = previousVersion.x;
    int nextY = previousVersion.y;
    int nextZ = previousVersion.z;
164 165
    int? nextM = previousVersion.m;
    int? nextN = previousVersion.n;
166
    if (nextVersionType == null) {
167
      if (previousVersion.type == VersionType.latest || previousVersion.type == VersionType.gitDescribe) {
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        nextVersionType = VersionType.development;
      } else {
        nextVersionType = previousVersion.type;
      }
    }

    switch (increment) {
      case 'x':
        // This was probably a mistake.
        throw Exception('Incrementing x is not supported by this tool.');
      case 'y':
        // Dev release following a beta release.
        nextY += 1;
        nextZ = 0;
        if (previousVersion.type != VersionType.stable) {
          nextM = 0;
          nextN = 0;
        }
        break;
      case 'z':
        // Hotfix to stable release.
        assert(previousVersion.type == VersionType.stable);
        nextZ += 1;
        break;
      case 'm':
193
        assert(false, "Do not increment 'm' via Version.increment, use instead Version.fromCandidateBranch()");
194 195 196
        break;
      case 'n':
        // Hotfix to internal roll.
197
        nextN = nextN! + 1;
198 199 200 201 202 203 204 205 206 207 208 209 210 211
        break;
      default:
        throw Exception('Unknown increment level $increment.');
    }
    return Version(
      x: nextX,
      y: nextY,
      z: nextZ,
      m: nextM,
      n: nextN,
      type: nextVersionType,
    );
  }

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  factory Version.fromCandidateBranch(String branchName) {
    // Regular dev release.
    final RegExp pattern = RegExp(r'flutter-(\d+)\.(\d+)-candidate.(\d+)');
    final RegExpMatch? match = pattern.firstMatch(branchName);
    late final int x;
    late final int y;
    late final int m;
    try {
      x = int.parse(match!.group(1)!);
      y = int.parse(match.group(2)!);
      m = int.parse(match.group(3)!);
    } on Exception {
      throw ConductorException('branch named $branchName not recognized as a valid candidate branch');
    }

    return Version(
      type: VersionType.development,
      x: x,
      y: y,
      z: 0,
      m: m,
      n: 0,
    );
  }

237 238 239 240 241 242 243
  /// Major version.
  final int x;

  /// Zero-indexed count of beta releases after a major release.
  final int y;

  /// Number of hotfix releases after a stable release.
244 245
  ///
  /// For non-stable releases, this will be 0.
246 247 248
  final int z;

  /// Zero-indexed count of dev releases after a beta release.
249 250
  ///
  /// For stable releases, this will be null.
251
  final int? m;
252 253

  /// Number of hotfixes required to make a dev release.
254 255
  ///
  /// For stable releases, this will be null.
256
  final int? n;
257 258

  /// Number of commits past last tagged dev release.
259
  final int? commits;
260 261 262

  final VersionType type;

263 264 265 266
  /// Validate that the parsed version is valid.
  ///
  /// Will throw a [ConductorException] if the version is not possible given the
  /// [candidateBranch] and [incrementLetter].
267
  void ensureValid(String candidateBranch, ReleaseType releaseType) {
268 269 270 271 272 273 274 275 276 277 278 279 280
    final RegExpMatch? branchMatch = releaseCandidateBranchRegex.firstMatch(candidateBranch);
    if (branchMatch == null) {
      throw ConductorException(
        'Candidate branch $candidateBranch does not match the pattern '
        '${releaseCandidateBranchRegex.pattern}',
      );
    }

    // These groups are required in the pattern, so these match groups should
    // not be null
    final String branchX = branchMatch.group(1)!;
    if (x != int.tryParse(branchX)) {
      throw ConductorException(
281
        'Parsed version $this has a different x value than candidate '
282 283 284 285 286 287
        'branch $candidateBranch',
      );
    }
    final String branchY = branchMatch.group(2)!;
    if (y != int.tryParse(branchY)) {
      throw ConductorException(
288
        'Parsed version $this has a different y value than candidate '
289 290 291 292 293
        'branch $candidateBranch',
      );
    }

    // stable type versions don't have an m field set
294
    if (type != VersionType.stable && releaseType != ReleaseType.STABLE_HOTFIX && releaseType != ReleaseType.STABLE_INITIAL) {
295 296 297
      final String branchM = branchMatch.group(3)!;
      if (m != int.tryParse(branchM)) {
        throw ConductorException(
298
          'Parsed version $this has a different m value than candidate '
299
          'branch $candidateBranch with type $type',
300 301 302 303 304
        );
      }
    }
  }

305 306 307 308 309 310 311 312 313
  @override
  String toString() {
    switch (type) {
      case VersionType.stable:
        return '$x.$y.$z';
      case VersionType.development:
        return '$x.$y.$z-$m.$n.pre';
      case VersionType.latest:
        return '$x.$y.$z-$m.$n.pre.$commits';
314 315
      case VersionType.gitDescribe:
        return '$x.$y.$z-$m.$n.pre.$commits';
316 317 318
    }
  }
}