Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: lib/src/utils.dart

Issue 2132443003: Add package implementation. (Closed) Base URL: git@github.com:dart-lang/package_resolver@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 // TODO(nweiz): Avoid importing dart:io directly when cross-platform libraries
6 // exist.
7 import 'dart:io';
8 import 'dart:isolate';
9 import 'dart:async';
10 import 'dart:convert';
11
12 import 'package:http/http.dart' as http;
13 import 'package:package_config/packages_file.dart' as packages_file;
14
15 /// Loads the configuration map from [uri].
16 ///
17 /// This supports `http`, `file`, `data`, and `package` URIs. If it's passed and
18 /// an HTTP request is needed, [client] is used to make that request.
19 Future<Map<String, Uri>> loadConfigMap(Uri uri, {http.Client client}) async {
20 var resolved = Uri.base.resolveUri(uri);
21
22 var text;
23 if (resolved.scheme == 'http') {
24 text = await (client == null
25 ? http.read(resolved)
26 : client.read(resolved));
27 } else if (resolved.scheme == 'file') {
28 var path = resolved.toFilePath(windows: Platform.isWindows);
29 text = await new File(path).readAsString();
30 } else if (resolved.scheme == 'data') {
31 text = resolved.data.contentAsString();
32 } else if (resolved.scheme == 'package') {
33 return loadConfigMap(await Isolate.resolvePackageUri(uri),
34 client: client);
35 } else {
36 throw new UnsupportedError(
37 'PackageInfo.loadConfig doesn\'t support URI scheme "${uri.scheme}:".');
38 }
39
40 return packages_file.parse(UTF8.encode(text), resolved);
41 }
42
43 /// Converts [uri] to a [Uri] and verifies that it's a valid `package:` URI.
44 ///
45 /// Throws an [ArgumentError] if [uri] isn't a [String] or a [Uri]. [name] is
46 /// used as the argument name in that error.
47 ///
48 /// Throws a [FormatException] if [uri] isn't a `package:` URI or doesn't have
49 /// at least one path segment.
50 Uri asPackageUri(uri, String name) {
51 uri = asUri(uri, name);
52
53 if (uri.scheme != 'package') {
54 throw new FormatException("Can only resolve a package: URI.",
55 uri.toString(), 0);
56 } else if (uri.pathSegments.isEmpty) {
57 throw new FormatException("Expected package name.",
58 uri.toString(), "package:".length);
59 }
60
61 return uri;
62 }
63
64 /// Converts [uri] to a [Uri].
65 ///
66 /// Throws an [ArgumentError] if [uri] isn't a [String] or a [Uri]. [name] is
67 /// used as the argument name in that error.
68 Uri asUri(uri, String name) {
69 if (uri is Uri) return uri;
70 if (uri is String) return Uri.parse(uri);
71
72 throw new ArgumentError.value(uri, name, "Must be a String or a Uri.");
73 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698