package_map.dart 2.03 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:async';
6 7
import 'dart:typed_data';

8
import 'package:meta/meta.dart';
9
import 'package:package_config/package_config.dart';
10

11
import '../base/common.dart';
12
import '../base/file_system.dart';
13
import '../base/logger.dart';
14

15
const String kPackagesFileName = '.packages';
16

17
// No touching!
18
String get globalPackagesPath => _globalPackagesPath ?? kPackagesFileName;
19

20 21 22
set globalPackagesPath(String value) {
  _globalPackagesPath = value;
}
23

24 25 26 27 28 29
bool get isUsingCustomPackagesPath => _globalPackagesPath != null;

String _globalPackagesPath;

/// Load the package configuration from [file] or throws a [ToolExit]
/// if the operation would fail.
30 31 32 33
///
/// If [nonFatal] is true, in the event of an error an empty package
/// config is returned.
Future<PackageConfig> loadPackageConfigWithLogging(File file, {
34
  @required Logger logger,
35
  bool throwOnError = true,
36
}) async {
37
  final FileSystem fileSystem = file.fileSystem;
38 39
  bool didError = false;
  final PackageConfig result = await loadPackageConfigUri(
40 41 42 43 44 45 46 47 48
    file.absolute.uri,
    loader: (Uri uri) {
      final File configFile = fileSystem.file(uri);
      if (!configFile.existsSync()) {
        return null;
      }
      return Future<Uint8List>.value(configFile.readAsBytesSync());
    },
    onError: (dynamic error) {
49 50 51
      if (!throwOnError) {
        return;
      }
52 53 54 55 56 57 58 59 60
      logger.printTrace(error.toString());
      String message = '${file.path} does not exist.';
      final String pubspecPath = fileSystem.path.absolute(fileSystem.path.dirname(file.path), 'pubspec.yaml');
      if (fileSystem.isFileSync(pubspecPath)) {
        message += '\nDid you run "flutter pub get" in this directory?';
      } else {
        message += '\nDid you run this command from the same directory as your pubspec.yaml file?';
      }
      logger.printError(message);
61
      didError = true;
62
    }
63
  );
64 65 66 67
  if (didError) {
    throwToolExit(null);
  }
  return result;
68
}