package_map.dart 1.68 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 6 7
import 'dart:typed_data';

import 'package:package_config/package_config.dart';
8

9
import '../base/common.dart';
10
import '../base/file_system.dart';
11
import '../base/logger.dart';
12

13 14
/// Load the package configuration from [file] or throws a [ToolExit]
/// if the operation would fail.
15
///
16
/// If [throwOnError] is false, in the event of an error an empty package
17 18
/// config is returned.
Future<PackageConfig> loadPackageConfigWithLogging(File file, {
19
  required Logger logger,
20
  bool throwOnError = true,
21
}) async {
22
  final FileSystem fileSystem = file.fileSystem;
23 24
  bool didError = false;
  final PackageConfig result = await loadPackageConfigUri(
25
    file.absolute.uri,
26
    loader: (Uri uri) async {
27 28 29 30 31 32 33
      final File configFile = fileSystem.file(uri);
      if (!configFile.existsSync()) {
        return null;
      }
      return Future<Uint8List>.value(configFile.readAsBytesSync());
    },
    onError: (dynamic error) {
34 35 36
      if (!throwOnError) {
        return;
      }
37 38 39 40 41 42 43 44 45
      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);
46
      didError = true;
47
    }
48
  );
49
  if (didError) {
50
    throwToolExit('');
51 52
  }
  return result;
53
}