pub.dart 2.79 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2016 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 'package:path/path.dart' as path;

10
import '../base/common.dart';
11
import '../base/logger.dart';
12
import '../base/process.dart';
13
import '../cache.dart';
14
import '../globals.dart';
15
import 'sdk.dart';
16

17 18 19 20 21 22
bool _shouldRunPubGet({ File pubSpecYaml, File dotPackages }) {
  if (!dotPackages.existsSync())
    return true;
  DateTime dotPackagesLastModified = dotPackages.lastModifiedSync();
  if (pubSpecYaml.lastModifiedSync().isAfter(dotPackagesLastModified))
    return true;
23
  File flutterToolsStamp = Cache.instance.getStampFileFor('flutter_tools');
24 25 26 27 28
  if (flutterToolsStamp.lastModifiedSync().isAfter(dotPackagesLastModified))
    return true;
  return false;
}

29
Future<Null> pubGet({
30
  String directory,
31 32 33
  bool skipIfAbsent: false,
  bool upgrade: false,
  bool checkLastModified: true
34 35 36 37 38 39 40 41
}) async {
  if (directory == null)
    directory = Directory.current.path;

  File pubSpecYaml = new File(path.join(directory, 'pubspec.yaml'));
  File dotPackages = new File(path.join(directory, '.packages'));

  if (!pubSpecYaml.existsSync()) {
42 43 44
    if (!skipIfAbsent)
      throwToolExit('$directory: no pubspec.yaml found');
    return;
45 46
  }

47
  if (!checkLastModified || _shouldRunPubGet(pubSpecYaml: pubSpecYaml, dotPackages: dotPackages)) {
48
    String command = upgrade ? 'upgrade' : 'get';
49
    Status status = logger.startProgress("Running 'flutter packages $command' in ${path.basename(directory)}...");
50
    int code = await runCommandAndStreamOutput(
51
      <String>[sdkBinaryName('pub'), '--verbosity=warning', command, '--no-packages-dir', '--no-precompile'],
52
      workingDirectory: directory,
53 54
      mapFunction: _filterOverrideWarnings,
      environment: <String, String>{ 'FLUTTER_ROOT': Cache.flutterRoot }
55
    );
Devon Carew's avatar
Devon Carew committed
56
    status.stop();
57
    if (code != 0)
58
      throwToolExit('pub $command failed ($code)', exitCode: code);
59 60
  }

61 62
  if (!dotPackages.existsSync())
    throwToolExit('$directory: pub did not create .packages file');
63

64 65
  if (dotPackages.lastModifiedSync().isBefore(pubSpecYaml.lastModifiedSync()))
    throwToolExit('$directory: pub did not update .packages file (pubspec.yaml file has a newer timestamp)');
66
}
67

68
final RegExp _analyzerWarning = new RegExp(r'^! analyzer [^ ]+ from path \.\./\.\./bin/cache/dart-sdk/lib/analyzer$');
69

70 71 72 73 74
String _filterOverrideWarnings(String message) {
  // This function filters out these two messages:
  //   Warning: You are using these overridden dependencies:
  //   ! analyzer 0.29.0-alpha.0 from path ../../bin/cache/dart-sdk/lib/analyzer
  if (message == 'Warning: You are using these overridden dependencies:')
75
    return null;
76
  if (message.contains(_analyzerWarning))
77
    return null;
78
  return message;
79
}