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

Side by Side Diff: code_transformers/lib/assets.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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 | « cli_util/pubspec.yaml ('k') | code_transformers/lib/benchmarks.dart » ('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) 2014, 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 /// Common methods used by transfomers for dealing with asset IDs.
6 library code_transformers.assets;
7
8 import 'dart:math' show min, max;
9
10 import 'package:barback/barback.dart';
11 import 'package:path/path.dart' as path;
12 import 'package:source_span/source_span.dart';
13
14 import 'messages/build_logger.dart';
15 import 'src/messages.dart';
16
17 /// Create an [AssetId] for a [url] seen in the [source] asset.
18 ///
19 /// By default this is used to resolve relative urls that occur in HTML assets,
20 /// including cross-package urls of the form "packages/foo/bar.html". Dart
21 /// "package:" urls are not resolved unless [source] is Dart file (has a .dart
22 /// extension).
23 ///
24 /// This function returns null if [url] can't be resolved. We log a warning
25 /// message in [logger] if we know the reason why resolution failed. This
26 /// happens, for example, when using incorrect paths to reach into another
27 /// package, or when [errorsOnAbsolute] is true and the url seems to be
28 /// absolute. If [span] is not `null` it is used to provide context for any
29 /// warning message(s) generated.
30 // TODO(sigmund): delete once this is part of barback (dartbug.com/12610)
31 AssetId uriToAssetId(
32 AssetId source, String url, TransformLogger logger, SourceSpan span,
33 {bool errorOnAbsolute: true}) {
34 if (url == null || url == '') return null;
35 var uri = Uri.parse(url);
36 var urlBuilder = path.url;
37 if (uri.host != '' || uri.scheme != '' || urlBuilder.isAbsolute(url)) {
38 if (source.extension == '.dart' && uri.scheme == 'package') {
39 var index = uri.path.indexOf('/');
40 if (index != -1) {
41 return new AssetId(
42 uri.path.substring(0, index), 'lib${uri.path.substring(index)}');
43 }
44 }
45
46 if (errorOnAbsolute) {
47 var msg = NO_ABSOLUTE_PATHS.create({'url': url});
48 logger.warning(logger is BuildLogger ? msg : msg.snippet, span: span);
49 }
50 return null;
51 }
52
53 var targetPath = urlBuilder
54 .normalize(urlBuilder.join(urlBuilder.dirname(source.path), url));
55 var segments = urlBuilder.split(targetPath);
56 var sourceSegments = urlBuilder.split(source.path);
57 assert(sourceSegments.length > 0);
58 var topFolder = sourceSegments[0];
59 var entryFolder = topFolder != 'lib' && topFolder != 'asset';
60
61 // Find the first 'packages/' or 'assets/' segment:
62 var packagesIndex = segments.indexOf('packages');
63 var assetsIndex = segments.indexOf('assets');
64 var index = (packagesIndex >= 0 && assetsIndex >= 0)
65 ? min(packagesIndex, assetsIndex)
66 : max(packagesIndex, assetsIndex);
67 if (index > -1) {
68 if (entryFolder) {
69 // URLs of the form "packages/foo/bar" seen under entry folders (like
70 // web/, test/, example/, etc) are resolved as an asset in another
71 // package. 'packages' can be used anywhere, there is no need to walk up
72 // where the entrypoint file was.
73 // TODO(sigmund): this needs to change: Only resolve when index == 1 &&
74 // topFolder == segment[0], otherwise give a warning (dartbug.com/17596).
75 return _extractOtherPackageId(index, segments, logger, span);
76 } else if (index == 1 && segments[0] == '..') {
77 // Relative URLs of the form "../../packages/foo/bar" in an asset under
78 // lib/ or asset/ are also resolved as an asset in another package, but we
79 // check that the relative path goes all the way out where the packages
80 // folder lives (otherwise the app would not work in Dartium). Since
81 // [targetPath] has been normalized, "packages" or "assets" should be at
82 // index 1.
83 return _extractOtherPackageId(1, segments, logger, span);
84 } else {
85 var prefix = segments[index];
86 var fixedSegments = [];
87 fixedSegments.addAll(sourceSegments.map((_) => '..'));
88 fixedSegments.addAll(segments.sublist(index));
89 var fixedUrl = urlBuilder.joinAll(fixedSegments);
90 var msg = INVALID_URL_TO_OTHER_PACKAGE
91 .create({'url': url, 'prefix': prefix, 'fixedUrl': fixedUrl});
92 logger.warning(logger is BuildLogger ? msg : msg.snippet, span: span);
93 return null;
94 }
95 }
96
97 // Otherwise, resolve as a path in the same package.
98 return new AssetId(source.package, targetPath);
99 }
100
101 AssetId _extractOtherPackageId(
102 int index, List segments, TransformLogger logger, SourceSpan span) {
103 if (index >= segments.length) return null;
104 var prefix = segments[index];
105 if (prefix != 'packages' && prefix != 'assets') return null;
106 var folder = prefix == 'packages' ? 'lib' : 'asset';
107 if (segments.length < index + 3) {
108 var msg = INVALID_PREFIX_PATH.create({'prefix': prefix, 'folder': folder});
109 logger.warning(logger is BuildLogger ? msg : msg.snippet, span: span);
110 return null;
111 }
112 return new AssetId(segments[index + 1],
113 path.url.join(folder, path.url.joinAll(segments.sublist(index + 2))));
114 }
115
116 /// Gets a URI which would be appropriate for importing the file represented by
117 /// [assetId].
118 ///
119 /// This function returns null if we cannot determine a uri for [assetId].
120 ///
121 /// Note that [assetId] may represent a non-importable file such as a part.
122 String assetIdToUri(AssetId assetId,
123 {TransformLogger logger, SourceSpan span, AssetId from}) {
124 if (!assetId.path.startsWith('lib/')) {
125 // Cannot do absolute imports of non lib-based assets.
126 if (from == null) {
127 if (logger != null) {
128 var msg = UNSPECIFIED_FROM_IN_NON_LIB_ASSET.create({'id': '$assetId'});
129 logger.warning(logger is BuildLogger ? msg : msg.snippet, span: span);
130 }
131 return null;
132 }
133
134 if (assetId.package != from.package) {
135 if (logger != null) {
136 var msg = IMPORT_FROM_DIFFERENT_PACKAGE
137 .create({'toId': '$assetId', 'fromId': '$from'});
138 logger.warning(logger is BuildLogger ? msg : msg.snippet, span: span);
139 }
140 return null;
141 }
142 return new Uri(
143 path: path.url.relative(assetId.path, from: path.url.dirname(from.pa th)))
144 .toString();
145 }
146
147 return Uri
148 .parse('package:${assetId.package}/${assetId.path.substring(4)}')
149 .toString();
150 }
OLDNEW
« no previous file with comments | « cli_util/pubspec.yaml ('k') | code_transformers/lib/benchmarks.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698