pub.dart 2.01 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

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

27 28
Future<int> pubGet({
  String directory,
29 30 31
  bool skipIfAbsent: false,
  bool upgrade: false,
  bool checkLastModified: true
32 33 34 35 36 37 38 39 40 41 42 43 44 45
}) 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;
  }

46
  if (!checkLastModified || _shouldRunPubGet(pubSpecYaml: pubSpecYaml, dotPackages: dotPackages)) {
47
    String command = upgrade ? 'upgrade' : 'get';
48
    Status status = logger.startProgress("Running 'pub $command' in ${path.basename(directory)}...");
49
    int code = await runCommandAndStreamOutput(
50
      <String>[sdkBinaryName('pub'), '--verbosity=warning', command, '--no-package-symlinks', '--no-precompile'],
51 52
      workingDirectory: directory
    );
53
    status.stop(showElapsedTime: true);
54 55 56 57
    if (code != 0)
      return code;
  }

58
  if (dotPackages.existsSync() && dotPackages.lastModifiedSync().isAfter(pubSpecYaml.lastModifiedSync()))
59 60
    return 0;

61
  printError('$directory: pubspec.yaml and .packages are in an inconsistent state');
62 63
  return 1;
}