update_versions.dart 6.9 KB
Newer Older
1 2 3 4 5 6
// 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.

// Updates the version numbers of the Flutter repo.
// Only tested on Linux.
7 8
//
// See: https://github.com/flutter/flutter/wiki/Release-process
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

import 'dart:io';

import 'package:args/args.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;

const String kIncrement = 'increment';
const String kBrokeSdk = 'broke-sdk';
const String kBrokeFramework = 'broke-framework';
const String kBrokeTest = 'broke-test';
const String kBrokeDriver = 'broke-driver';
const String kMarkRelease = 'release';
const String kHelp = 'help';

const String kYamlVersionPrefix = 'version: ';
const String kDev = '-dev';

27 28
enum VersionKind { dev, release }

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
void main(List<String> args) {
  // If we're run from the `tools` dir, set the cwd to the repo root.
  if (path.basename(Directory.current.path) == 'tools')
    Directory.current = Directory.current.parent.parent;

  final ArgParser argParser = new ArgParser();
  argParser.addFlag(kIncrement, defaultsTo: false, help: 'Increment all the version numbers. Cannot be specified with --$kMarkRelease or with any --broke-* commands.');
  argParser.addFlag(kBrokeSdk, defaultsTo: false, negatable: false, help: 'Increment the Flutter SDK version number to indicate that there has been a breaking change to the SDK (for example, to the command line options).');
  argParser.addFlag(kBrokeFramework, defaultsTo: false, negatable: false, help: 'Increment the "flutter" package version number to indicate that there has been a breaking change to the Flutter framework.');
  argParser.addFlag(kBrokeTest, defaultsTo: false, negatable: false, help: 'Increment the "flutter_test" package version number to indicate that there has been a breaking change to the test API framework.');
  argParser.addFlag(kBrokeDriver, defaultsTo: false, negatable: false, help: 'Increment the "flutter_driver" package version number to indicate that there has been a breaking change to the driver API framework.');
  argParser.addFlag(kMarkRelease, defaultsTo: false, help: 'Remove "-dev" from each version number. This is used when releasing. When not present, "-dev" is added to each version number. Cannot be specified with --$kIncrement or with any --broke-* commands.');
  argParser.addFlag(kHelp, negatable: false, help: 'Show this help message.');
  final ArgResults argResults = argParser.parse(args);

  final bool increment = argResults[kIncrement];
  final bool brokeSdk = argResults[kBrokeSdk];
  final bool brokeFramework = argResults[kBrokeFramework];
  final bool brokeTest = argResults[kBrokeTest];
  final bool brokeDriver = argResults[kBrokeDriver];
  final bool brokeAnything = brokeSdk || brokeFramework || brokeTest || brokeDriver;
50
  final VersionKind level = argResults[kMarkRelease] ? VersionKind.release : VersionKind.dev;
51 52 53 54 55 56 57 58
  final bool help = argResults[kHelp];

  if (help) {
    print('update_versions.dart - update version numbers of Flutter packages and SDK');
    print(argParser.usage);
    exit(0);
  }

59
  final bool release = level == VersionKind.release;
60 61 62 63 64 65 66 67 68 69 70 71 72
  if ((brokeAnything && release) || (brokeAnything && increment) || (release && increment)) {
    print('You can either increment all the version numbers (--$kIncrement), indicate that some packages have had breaking changes (--broke-*), or switch to release mode (--$kMarkRelease).');
    print('You cannot combine these, however.');
    exit(1);
  }

  final RawVersion sdk = new RawVersion('VERSION');
  final PubSpecVersion framework = new PubSpecVersion('packages/flutter/pubspec.yaml');
  final PubSpecVersion test = new PubSpecVersion('packages/flutter_test/pubspec.yaml');
  final PubSpecVersion driver = new PubSpecVersion('packages/flutter_driver/pubspec.yaml');

  if (increment || brokeAnything)
    sdk.increment(brokeAnything);
73
  sdk.setMode(level);
74 75 76

  if (increment || brokeFramework)
    framework.increment(brokeFramework);
77
  framework.setMode(level);
78 79 80

  if (increment || brokeTest)
    test.increment(brokeTest);
81
  test.setMode(level);
82 83 84

  if (increment || brokeDriver)
    driver.increment(brokeDriver);
85
  driver.setMode(level);
86 87 88 89 90 91 92 93 94 95

  sdk.write();
  framework.write();
  test.write();
  driver.write();

  print('Flutter SDK is now at version: $sdk');
  print('flutter package is now at version: $framework');
  print('flutter_test package is now at version: $test');
  print('flutter_driver package is now at version: $driver');
96 97 98 99 100 101

  if (release) {
    print('\nDuring the tagging step in the instructions, the commands will be:');
    print('git tag $sdk');
    print('git push upstream $sdk');
  }
102 103 104 105 106 107 108 109 110 111 112
}

abstract class Version {
  Version() {
    read();
  }

  @protected
  final List<int> version = <int>[];

  @protected
113
  VersionKind level;
114 115 116 117 118 119 120 121

  @protected
  bool dirty = false;

  @protected
  void read();

  void interpret(String value) {
122 123
    level = value.endsWith(kDev) ? VersionKind.dev : VersionKind.release;
    if (level == VersionKind.dev)
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
      value = value.substring(0, value.length - kDev.length);
    version.addAll(value.split('.').map<int>(int.parse));
  }

  void increment(bool breaking) {
    assert(version.length == 3);
    if (breaking) {
      version[1] += 1;
      version[2] = 0;
    } else {
      version[2] += 1;
    }
    dirty = true;
  }

139 140 141
  void setMode(VersionKind value) {
    if (value != level) {
      level = value;
142 143 144 145 146 147 148
      dirty = true;
    }
  }

  void write();

  @override
149
  String toString() => version.join('.') + (level == VersionKind.dev ? kDev : '');
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 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 193 194 195 196 197 198 199 200 201 202 203 204 205
}

class PubSpecVersion extends Version {
  PubSpecVersion(this.path);

  final String path;

  @override
  void read() {
    final List<String> lines = new File(path).readAsLinesSync();
    final String versionLine = lines.where((String line) => line.startsWith(kYamlVersionPrefix)).single;
    interpret(versionLine.substring(kYamlVersionPrefix.length));
  }

  @override
  void write() {
    if (!dirty)
      return;
    final List<String> lines = new File(path).readAsLinesSync();
    for (int index = 0; index < lines.length; index += 1) {
      final String line = lines[index];
      if (line.startsWith(kYamlVersionPrefix)) {
        lines[index] = '$kYamlVersionPrefix$this';
        break;
      }
    }
    new File(path).writeAsStringSync(lines.join('\n') + '\n');
  }
}

class RawVersion extends Version {
  RawVersion(this.path);

  final String path;

  @override
  void read() {
    final List<String> lines = new File(path).readAsLinesSync();
    interpret(lines.where((String line) => line.isNotEmpty && !line.startsWith('#')).single);
  }

  @override
  void write() {
    if (!dirty)
      return;
    final List<String> lines = new File(path).readAsLinesSync();
    for (int index = 0; index < lines.length; index += 1) {
      final String line = lines[index];
      if (line.isNotEmpty && !line.startsWith('#')) {
        lines[index] = '$this';
        break;
      }
    }
    new File(path).writeAsStringSync(lines.join('\n') + '\n');
  }
}