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

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: Code review changes 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
« no previous file with comments | « lib/src/test_package_config ('k') | pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 [client] is
18 /// passed and an HTTP request is needed, it's used to make that request;
19 /// otherwise, a default client is used.
20 Future<Map<String, Uri>> loadConfigMap(Uri uri, {http.Client client}) async {
21 var resolved = Uri.base.resolveUri(uri);
22
23 var text;
24 if (resolved.scheme == 'http') {
25 text = await (client == null
26 ? http.read(resolved)
27 : client.read(resolved));
28 } else if (resolved.scheme == 'file') {
29 var path = resolved.toFilePath(windows: Platform.isWindows);
30 text = await new File(path).readAsString();
31 } else if (resolved.scheme == 'data') {
32 text = resolved.data.contentAsString();
33 } else if (resolved.scheme == 'package') {
34 return loadConfigMap(await Isolate.resolvePackageUri(uri),
35 client: client);
36 } else {
37 throw new UnsupportedError(
38 'PackageInfo.loadConfig doesn\'t support URI scheme "${uri.scheme}:".');
39 }
40
41 return packages_file.parse(UTF8.encode(text), resolved);
42 }
43
44 /// Converts [uri] to a [Uri] and verifies that it's a valid `package:` URI.
45 ///
46 /// Throws an [ArgumentError] if [uri] isn't a [String] or a [Uri]. [name] is
47 /// used as the argument name in that error.
48 ///
49 /// Throws a [FormatException] if [uri] isn't a `package:` URI or doesn't have
50 /// at least one path segment.
51 Uri asPackageUri(uri, String name) {
52 uri = asUri(uri, name);
53
54 if (uri.scheme != 'package') {
55 throw new FormatException("Can only resolve a package: URI.",
56 uri.toString(), 0);
57 } else if (uri.pathSegments.isEmpty) {
58 throw new FormatException("Expected package name.",
59 uri.toString(), "package:".length);
60 }
61
62 return uri;
63 }
64
65 /// Converts [uri] to a [Uri].
66 ///
67 /// Throws an [ArgumentError] if [uri] isn't a [String] or a [Uri]. [name] is
68 /// used as the argument name in that error.
69 Uri asUri(uri, String name) {
70 if (uri is Uri) return uri;
71 if (uri is String) return Uri.parse(uri);
72
73 throw new ArgumentError.value(uri, name, "Must be a String or a Uri.");
74 }
75
76 /// Returns a copy of [uri] with a trailing slash.
77 ///
78 /// If [uri] already ends in a slash, returns it as-is.
79 Uri ensureTrailingSlash(Uri uri) {
80 if (uri.pathSegments.last.isEmpty) return uri;
81 return uri.replace(pathSegments: uri.pathSegments.toList()..add(""));
82 }
OLDNEW
« no previous file with comments | « lib/src/test_package_config ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698