pub.dart 2.45 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/logger.dart';
11
import '../base/process.dart';
12
import '../cache.dart';
13
import '../globals.dart';
14
import 'sdk.dart';
15

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

28 29
Future<int> pubGet({
  String directory,
30 31 32
  bool skipIfAbsent: false,
  bool upgrade: false,
  bool checkLastModified: true
33 34 35 36 37 38 39 40 41 42 43 44 45 46
}) 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()) {
    if (skipIfAbsent)
      return 0;
    printError('$directory: no pubspec.yaml found');
    return 1;
  }

47
  if (!checkLastModified || _shouldRunPubGet(pubSpecYaml: pubSpecYaml, dotPackages: dotPackages)) {
48
    String command = upgrade ? 'upgrade' : 'get';
49
    Status status = logger.startProgress("Running 'pub $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
    );
56
    status.stop(showElapsedTime: true);
57 58 59 60
    if (code != 0)
      return code;
  }

61
  if (dotPackages.existsSync() && dotPackages.lastModifiedSync().isAfter(pubSpecYaml.lastModifiedSync()))
62 63
    return 0;

64
  printError('$directory: pubspec.yaml and .packages are in an inconsistent state');
65 66
  return 1;
}
67 68 69 70 71 72 73 74 75 76 77 78

String _filterOverrideWarnings(String str) {
  // Warning: You are using these overridden dependencies:
  // ! analyzer 0.29.0-alpha.0 from path ../../bin/cache/dart-sdk/lib/analyzer

  if (str.contains('overridden dependencies:'))
    return null;
  if (str.startsWith('! analyzer '))
    return null;

  return str;
}