package_map.dart 1.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:io';

import 'package:package_config/packages_file.dart' as packages_file;
import 'package:path/path.dart' as path;

10
const String kPackagesFileName = '.packages';
11 12 13 14 15 16 17 18 19

Map<String, Uri> _parse(String packagesPath) {
  List<int> source = new File(packagesPath).readAsBytesSync();
  return packages_file.parse(source, new Uri.file(packagesPath));
}

class PackageMap {
  PackageMap(this.packagesPath);

20 21 22 23 24 25
  static String get globalPackagesPath => _globalPackagesPath ?? kPackagesFileName;

  static set globalPackagesPath(String value) {
    _globalPackagesPath = value;
  }

26 27
  static bool get isUsingCustomPackagesPath => _globalPackagesPath != null;

28 29
  static String _globalPackagesPath;

30 31 32
  final String packagesPath;

  Map<String, Uri> get map {
Ian Hickson's avatar
Ian Hickson committed
33
    _map ??= _parse(packagesPath);
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    return _map;
  }
  Map<String, Uri> _map;

  String checkValid() {
    if (FileSystemEntity.isFileSync(packagesPath))
      return null;
    String message = '$packagesPath does not exist.';
    String pubspecPath = path.absolute(path.dirname(packagesPath), 'pubspec.yaml');
    if (FileSystemEntity.isFileSync(pubspecPath))
      message += '\nDid you run `pub get` in this directory?';
    else
      message += '\nDid you run this command from the same directory as your pubspec.yaml file?';
    return message;
  }
}