update_packages.dart 1.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright 2015 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.

import 'dart:async';
import 'dart:io';

import '../artifacts.dart';
import '../dart/pub.dart';
import '../globals.dart';
import '../runner/flutter_command.dart';

Future<int> _runPub(Directory directory, { bool upgrade: false }) async {
  int updateCount = 0;
  for (FileSystemEntity dir in directory.listSync()) {
    if (dir is Directory && FileSystemEntity.isFileSync(dir.path + Platform.pathSeparator + 'pubspec.yaml')) {
      updateCount++;
18 19
      // TODO(eseidel): Should this fail immediately if pubGet fails?
      // Currently we're ignoring the return code.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
      await pubGet(directory: dir.path, upgrade: upgrade, checkLastModified: false);
    }
  }
  return updateCount;
}

class UpdatePackagesCommand extends FlutterCommand {
  UpdatePackagesCommand({ hideCommand: false }) : _hideCommand = hideCommand {
    argParser.addFlag(
      'upgrade',
      help: 'Run "pub upgrade" rather than "pub get".',
      defaultsTo: false
    );
  }

  final String name = 'update-packages';
  final String description = 'Update the packages inside the Flutter repo.';

  bool get hidden => _hideCommand;
  final bool _hideCommand;

  bool get requiresProjectRoot => false;

  @override
  Future<int> runInProject() async {
    Stopwatch timer = new Stopwatch()..start();
    int count = 0;
    bool upgrade = argResults['upgrade'];
    count += await _runPub(new Directory("${ArtifactStore.flutterRoot}/packages"), upgrade: upgrade);
    count += await _runPub(new Directory("${ArtifactStore.flutterRoot}/examples"), upgrade: upgrade);
Adam Barth's avatar
Adam Barth committed
50
    count += await _runPub(new Directory("${ArtifactStore.flutterRoot}/dev"), upgrade: upgrade);
51 52 53 54
    printStatus('Ran "pub" $count time${count == 1 ? "" : "s"} in ${timer.elapsedMilliseconds} ms');
    return 0;
  }
}