version.dart 4.64 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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import '../base/common.dart';
8
import '../base/io.dart';
9 10 11 12 13
import '../base/os.dart';
import '../base/process.dart';
import '../base/version.dart';
import '../cache.dart';
import '../dart/pub.dart';
14
import '../globals.dart' as globals;
15 16 17 18
import '../runner/flutter_command.dart';
import '../version.dart';

class VersionCommand extends FlutterCommand {
19
  VersionCommand() : super() {
20
    usesPubOption(hide: true);
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    argParser.addFlag('force',
      abbr: 'f',
      help: 'Force switch to older Flutter versions that do not include a version command',
    );
  }

  @override
  final String name = 'version';

  @override
  final String description = 'List or switch flutter versions.';

  // The first version of Flutter which includes the flutter version command. Switching to older
  // versions will require the user to manually upgrade.
  Version minSupportedVersion = Version.parse('1.2.1');

  Future<List<String>> getTags() async {
38 39
    RunResult runResult;
    try {
40
      runResult = await processUtils.run(
41
        <String>['git', 'tag', '-l', 'v*', '--sort=-creatordate'],
42
        throwOnError: true,
43 44 45 46 47 48 49 50 51
        workingDirectory: Cache.flutterRoot,
      );
    } on ProcessException catch (error) {
      throwToolExit(
        'Unable to get the tags. '
        'This might be due to git not being installed or an internal error'
        '\nError: $error.'
      );
    }
52 53 54 55 56 57 58
    return runResult.toString().split('\n');
  }

  @override
  Future<FlutterCommandResult> runCommand() async {
    final List<String> tags = await getTags();
    if (argResults.rest.isEmpty) {
59
      tags.forEach(globals.printStatus);
60 61 62 63
      return const FlutterCommandResult(ExitStatus.success);
    }
    final String version = argResults.rest[0].replaceFirst('v', '');
    if (!tags.contains('v$version')) {
64
      globals.printError('There is no version: $version');
65 66 67 68
    }

    // check min supported version
    final Version targetVersion = Version.parse(version);
69 70 71 72
    if (targetVersion == null) {
      throwToolExit('Failed to parse version "$version"');
    }

73 74
    bool withForce = false;
    if (targetVersion < minSupportedVersion) {
75
      if (!boolArg('force')) {
76
        globals.printError(
77 78 79 80 81 82 83 84 85 86
          'Version command is not supported in $targetVersion and it is supported since version $minSupportedVersion'
          'which means if you switch to version $minSupportedVersion then you can not use version command.'
          'If you really want to switch to version $targetVersion, please use `--force` flag: `flutter version --force $targetVersion`.'
        );
        return const FlutterCommandResult(ExitStatus.success);
      }
      withForce = true;
    }

    try {
87
      await processUtils.run(
88
        <String>['git', 'checkout', 'v$version'],
89
        throwOnError: true,
90
        workingDirectory: Cache.flutterRoot,
91 92 93 94 95 96 97
      );
    } catch (e) {
      throwToolExit('Unable to checkout version branch for version $version.');
    }

    final FlutterVersion flutterVersion = FlutterVersion();

98
    globals.printStatus('Switching Flutter to version ${flutterVersion.frameworkVersion}${withForce ? ' with force' : ''}');
99 100 101 102 103

    // Check for and download any engine and pkg/ updates.
    // We run the 'flutter' shell script re-entrantly here
    // so that it will download the updated Dart and so forth
    // if necessary.
104 105
    globals.printStatus('');
    globals.printStatus('Downloading engine...');
106
    int code = await processUtils.stream(<String>[
107
      globals.fs.path.join('bin', 'flutter'),
108 109 110 111 112 113 114 115
      '--no-color',
      'precache',
    ], workingDirectory: Cache.flutterRoot, allowReentrantFlutter: true);

    if (code != 0) {
      throwToolExit(null, exitCode: code);
    }

116 117
    globals.printStatus('');
    globals.printStatus(flutterVersion.toString());
118 119

    final String projectRoot = findProjectRoot();
120
    if (projectRoot != null && shouldRunPub) {
121
      globals.printStatus('');
122
      await pub.get(
123 124 125
        context: PubContext.pubUpgrade,
        directory: projectRoot,
        upgrade: true,
126
        checkLastModified: false,
127 128 129 130
      );
    }

    // Run a doctor check in case system requirements have changed.
131 132
    globals.printStatus('');
    globals.printStatus('Running flutter doctor...');
133
    code = await processUtils.stream(
134
      <String>[
135
        globals.fs.path.join('bin', 'flutter'),
136 137 138 139 140 141 142 143 144 145 146 147 148
        'doctor',
      ],
      workingDirectory: Cache.flutterRoot,
      allowReentrantFlutter: true,
    );

    if (code != 0) {
      throwToolExit(null, exitCode: code);
    }

    return const FlutterCommandResult(ExitStatus.success);
  }
}