Line data Source code
1 : // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 : // for details. All rights reserved. Use of this source code is governed by a
3 : // BSD-style license that can be found in the LICENSE file.
4 :
5 : import 'dart:async';
6 : import 'dart:isolate';
7 :
8 : import 'package:path/path.dart' as p;
9 :
10 : import 'package_config_resolver.dart';
11 : import 'package_resolver.dart';
12 : import 'package_root_resolver.dart';
13 : import 'sync_package_resolver.dart';
14 : import 'utils.dart';
15 :
16 : /// The package resolution strategy used by the current isolate.
17 : class CurrentIsolateResolver implements PackageResolver {
18 : Future<Map<String, Uri>> get packageConfigMap async {
19 0 : if (_packageConfigMap != null) return _packageConfigMap;
20 :
21 0 : var url = await Isolate.packageConfig;
22 : if (url == null) return null;
23 :
24 0 : return await loadConfigMap(url);
25 0 : }
26 : Map<String, Uri> _packageConfigMap;
27 :
28 0 : Future<Uri> get packageConfigUri => Isolate.packageConfig;
29 :
30 0 : Future<Uri> get packageRoot => Isolate.packageRoot;
31 :
32 : Future<SyncPackageResolver> get asSync async {
33 0 : var root = await packageRoot;
34 0 : if (root != null) return new PackageRootResolver(root);
35 :
36 0 : var map = await packageConfigMap;
37 :
38 : // It's hard to imagine how there would be no package resolution strategy
39 : // for an Isolate that can load the package_resolver package, but it's easy
40 : // to handle that case so we do.
41 0 : if (map == null) return SyncPackageResolver.none;
42 :
43 0 : return new PackageConfigResolver(map, uri: await packageConfigUri);
44 0 : }
45 :
46 : Future<String> get processArgument async {
47 0 : var configUri = await packageConfigUri;
48 0 : if (configUri != null) return "--packages=$configUri";
49 :
50 0 : var root = await packageRoot;
51 0 : if (root != null) return "--package-root=$root";
52 :
53 : return null;
54 0 : }
55 :
56 : Future<Uri> resolveUri(packageUri) =>
57 0 : Isolate.resolvePackageUri(asPackageUri(packageUri, "packageUri"));
58 :
59 : Future<Uri> urlFor(String package, [String path]) =>
60 0 : Isolate.resolvePackageUri(Uri.parse("package:$package/${path ?? ''}"));
61 :
62 0 : Future<Uri> packageUriFor(url) async => (await asSync).packageUriFor(url);
63 :
64 : Future<String> packagePath(String package) async {
65 0 : var root = await packageRoot;
66 0 : if (root != null) return new PackageRootResolver(root).packagePath(package);
67 :
68 0 : return p.dirname(p.fromUri(await urlFor(package)));
69 0 : }
70 : }
|