Chromium Code Reviews| OLD | NEW |
|---|---|
| (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. | |
| 6 library code_transformers.src.assets; | |
| 7 | |
| 8 import 'dart:async'; | |
| 9 import 'dart:math' show min, max; | |
| 10 | |
| 11 import 'package:analyzer/analyzer.dart' as analyzer; | |
| 12 import 'package:analyzer/src/generated/ast.dart'; | |
| 13 import 'package:barback/barback.dart'; | |
| 14 import 'package:path/path.dart' as path; | |
| 15 import 'package:source_maps/span.dart' show SourceFile, Span; | |
| 16 | |
| 17 /// Create an [AssetId] for a [url] seen in the [source] asset. By default this | |
| 18 /// is used to resolve relative urls that occur in HTML assets, including | |
| 19 /// cross-package urls of the form "packages/foo/bar.html". Dart "package:" | |
| 20 /// urls are not resolved unless [source] is Dart file (has a .dart extension). | |
| 21 // TODO(sigmund): delete once this is part of barback (dartbug.com/12610) | |
| 22 AssetId uriToAssetId(AssetId source, String url, TransformLogger logger, | |
|
Siggi Cherem (dart-lang)
2014/03/18 16:42:16
let's split this function out to a separate public
blois
2014/03/18 17:39:07
Done.
| |
| 23 Span span, {bool allowAbsolute: false}) { | |
| 24 if (url == null || url == '') return null; | |
| 25 var uri = Uri.parse(url); | |
| 26 var urlBuilder = path.url; | |
| 27 if (uri.host != '' || uri.scheme != '' || urlBuilder.isAbsolute(url)) { | |
| 28 if (source.extension == '.dart' && uri.scheme == 'package') { | |
| 29 var index = uri.path.indexOf('/'); | |
| 30 if (index != -1) { | |
| 31 return new AssetId(uri.path.substring(0, index), | |
| 32 'lib${uri.path.substring(index)}'); | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 if (!allowAbsolute) { | |
| 37 logger.error('absolute paths not allowed: "$url"', span: span); | |
| 38 } | |
| 39 return null; | |
| 40 } | |
| 41 | |
| 42 var targetPath = urlBuilder.normalize( | |
| 43 urlBuilder.join(urlBuilder.dirname(source.path), url)); | |
| 44 var segments = urlBuilder.split(targetPath); | |
| 45 var sourceSegments = urlBuilder.split(source.path); | |
| 46 assert (sourceSegments.length > 0); | |
| 47 var topFolder = sourceSegments[0]; | |
| 48 var entryFolder = topFolder != 'lib' && topFolder != 'asset'; | |
| 49 | |
| 50 // Find the first 'packages/' or 'assets/' segment: | |
| 51 var packagesIndex = segments.indexOf('packages'); | |
| 52 var assetsIndex = segments.indexOf('assets'); | |
| 53 var index = (packagesIndex >= 0 && assetsIndex >= 0) | |
| 54 ? min(packagesIndex, assetsIndex) | |
| 55 : max(packagesIndex, assetsIndex); | |
| 56 if (index > -1) { | |
| 57 if (entryFolder) { | |
| 58 // URLs of the form "packages/foo/bar" seen under entry folders (like | |
| 59 // web/, test/, example/, etc) are resolved as an asset in another | |
| 60 // package. 'packages' can be used anywhere, there is no need to walk up | |
| 61 // where the entrypoint file was. | |
| 62 return _extractOtherPackageId(index, segments, logger, span); | |
| 63 } else if (index == 1 && segments[0] == '..') { | |
| 64 // Relative URLs of the form "../../packages/foo/bar" in an asset under | |
| 65 // lib/ or asset/ are also resolved as an asset in another package, but we | |
| 66 // check that the relative path goes all the way out where the packages | |
| 67 // folder lives (otherwise the app would not work in Dartium). Since | |
| 68 // [targetPath] has been normalized, "packages" or "assets" should be at | |
| 69 // index 1. | |
| 70 return _extractOtherPackageId(1, segments, logger, span); | |
| 71 } else { | |
| 72 var prefix = segments[index]; | |
| 73 var fixedSegments = []; | |
| 74 fixedSegments.addAll(sourceSegments.map((_) => '..')); | |
| 75 fixedSegments.addAll(segments.sublist(index)); | |
| 76 var fixedUrl = urlBuilder.joinAll(fixedSegments); | |
| 77 logger.error('Invalid url to reach to another package: $url. Path ' | |
| 78 'reaching to other packages must first reach up all the ' | |
| 79 'way to the $prefix folder. For example, try changing the url above ' | |
| 80 'to: $fixedUrl', span: span); | |
| 81 return null; | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 // Otherwise, resolve as a path in the same package. | |
| 86 return new AssetId(source.package, targetPath); | |
| 87 } | |
| 88 | |
| 89 AssetId _extractOtherPackageId(int index, List segments, | |
| 90 TransformLogger logger, Span span) { | |
| 91 if (index >= segments.length) return null; | |
| 92 var prefix = segments[index]; | |
| 93 if (prefix != 'packages' && prefix != 'assets') return null; | |
| 94 var folder = prefix == 'packages' ? 'lib' : 'asset'; | |
| 95 if (segments.length < index + 3) { | |
| 96 logger.error("incomplete $prefix/ path. It should have at least 3 " | |
| 97 "segments $prefix/name/path-from-name's-$folder-dir", span: span); | |
| 98 return null; | |
| 99 } | |
| 100 return new AssetId(segments[index + 1], | |
| 101 path.url.join(folder, path.url.joinAll(segments.sublist(index + 2)))); | |
| 102 } | |
| 103 | |
| 104 | |
| 105 /// Checks to see if the provided Asset is a Dart entry point. | |
| 106 /// | |
| 107 /// Assets are considered entry points if they are Dart files located in | |
| 108 /// web/, test/, benchmark/ or example/ and have a main() function. | |
| 109 /// | |
| 110 /// Because this only analyzes the primary asset this may return true for files | |
| 111 /// which are not dart entries if the file does not have a main() but does have | |
| 112 /// parts or exports. | |
| 113 Future<bool> isPossibleDartEntry(Asset asset) { | |
| 114 if (asset.id.extension != '.dart') return new Future.value(false); | |
| 115 | |
| 116 if (!['benchmark', 'example', 'test', 'web'] | |
| 117 .any((dir) => asset.id.path.startsWith("$dir/"))) { | |
| 118 return new Future.value(false); | |
| 119 } | |
| 120 return asset.readAsString().then((contents) { | |
| 121 return _isEntrypoint(analyzer.parseCompilationUnit(contents)); | |
| 122 }); | |
| 123 } | |
| 124 | |
| 125 bool _isEntrypoint(CompilationUnit compilationUnit) { | |
|
Siggi Cherem (dart-lang)
2014/03/18 16:42:16
nit: rename to _couldBeEntryPoint or _mayBeEntryPo
blois
2014/03/18 17:39:07
Done.
| |
| 126 return compilationUnit.declarations.any((node) { | |
| 127 // Allow two or fewer arguments so that entrypoints intended for use with | |
| 128 // [spawnUri] get counted. | |
| 129 return node is FunctionDeclaration && node.name.name == "main" && | |
|
Siggi Cherem (dart-lang)
2014/03/18 16:42:16
Use =>? (maybe moving the comment outside)?
// Al
blois
2014/03/18 17:39:07
Done.
| |
| 130 node.functionExpression.parameters.parameters.length <= 2; | |
| 131 }) || compilationUnit.directives.any((node) { | |
|
Siggi Cherem (dart-lang)
2014/03/18 16:42:16
the mix of multi-line closures with || is not that
blois
2014/03/18 17:39:07
Done.
| |
| 132 return node is ExportDirective || node is PartDirective; | |
|
Siggi Cherem (dart-lang)
2014/03/18 16:42:16
use => ?, for example:
bool hasPartOrExport = uni
blois
2014/03/18 17:39:07
Done.
| |
| 133 }); | |
| 134 } | |
| OLD | NEW |