| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 import 'dart:async'; | 4 import 'dart:async'; |
| 5 | 5 |
| 6 import 'package:analyzer/analyzer.dart' as analyzer; | 6 import 'package:analyzer/analyzer.dart' as analyzer; |
| 7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:barback/barback.dart'; | 8 import 'package:barback/barback.dart'; |
| 9 | 9 |
| 10 /// Checks to see if the provided AssetId is a Dart file in a directory which | 10 /// Checks to see if the provided AssetId is a Dart file in a directory which |
| 11 /// may contain entry points. | 11 /// may contain entry points. |
| 12 /// | 12 /// |
| 13 /// Directories are considered entry points if they are Dart files located in | 13 /// Directories are considered entry points if they are Dart files located in |
| 14 /// web/, test/, benchmark/ or example/. | 14 /// web/, test/, benchmark/ or example/. |
| 15 bool isPossibleDartEntryId(AssetId id) { | 15 bool isPossibleDartEntryId(AssetId id) { |
| 16 if (id.extension != '.dart') return false; | 16 if (id.extension != '.dart') return false; |
| 17 | 17 |
| 18 return [ | 18 return ['benchmark', 'example', 'test', 'web'] |
| 19 'benchmark', | 19 .any((dir) => id.path.startsWith("$dir/")); |
| 20 'example', | |
| 21 'test', | |
| 22 'web' | |
| 23 ].any((dir) => id.path.startsWith("$dir/")); | |
| 24 } | 20 } |
| 25 | 21 |
| 26 /// Checks to see if the provided Asset is possibly a Dart entry point. | 22 /// Checks to see if the provided Asset is possibly a Dart entry point. |
| 27 /// | 23 /// |
| 28 /// Assets are considered entry points if they pass [isPossibleDartEntryId] and | 24 /// Assets are considered entry points if they pass [isPossibleDartEntryId] and |
| 29 /// have a main() method. | 25 /// have a main() method. |
| 30 /// | 26 /// |
| 31 /// Because this only analyzes the primary asset this may return true for files | 27 /// Because this only analyzes the primary asset this may return true for files |
| 32 /// which are not dart entries if the file does not have a main() but does have | 28 /// which are not dart entries if the file does not have a main() but does have |
| 33 /// parts or exports. | 29 /// parts or exports. |
| 34 Future<bool> isPossibleDartEntry(Asset asset) { | 30 Future<bool> isPossibleDartEntry(Asset asset) { |
| 35 if (!isPossibleDartEntryId(asset.id)) return new Future.value(false); | 31 if (!isPossibleDartEntryId(asset.id)) return new Future.value(false); |
| 36 | 32 |
| 37 return asset.readAsString().then((contents) { | 33 return asset.readAsString().then((contents) { |
| 38 return _couldBeEntrypoint( | 34 return _couldBeEntrypoint( |
| 39 analyzer.parseCompilationUnit(contents, suppressErrors: true)); | 35 analyzer.parseCompilationUnit(contents, suppressErrors: true)); |
| 40 }); | 36 }); |
| 41 } | 37 } |
| 42 | 38 |
| 43 bool _couldBeEntrypoint(CompilationUnit compilationUnit) { | 39 bool _couldBeEntrypoint(CompilationUnit compilationUnit) { |
| 44 // Allow two or fewer arguments so that entrypoints intended for use with | 40 // Allow two or fewer arguments so that entrypoints intended for use with |
| 45 // [spawnUri] get counted. | 41 // [spawnUri] get counted. |
| 46 var hasMain = compilationUnit.declarations.any( | 42 var hasMain = compilationUnit.declarations.any((node) => |
| 47 (node) => node is FunctionDeclaration && | 43 node is FunctionDeclaration && |
| 48 node.name.name == "main" && | 44 node.name.name == "main" && |
| 49 node.functionExpression.parameters.parameters.length <= 2); | 45 node.functionExpression.parameters.parameters.length <= 2); |
| 50 | 46 |
| 51 if (hasMain) return true; | 47 if (hasMain) return true; |
| 52 | 48 |
| 53 // If it has an export or a part, assume the worst- that the main could be | 49 // If it has an export or a part, assume the worst- that the main could be |
| 54 // in there. | 50 // in there. |
| 55 // We avoid loading those since this can be run from isPrimaryAsset calls | 51 // We avoid loading those since this can be run from isPrimaryAsset calls |
| 56 // where we do not have access to other sources. | 52 // where we do not have access to other sources. |
| 57 return compilationUnit.directives | 53 return compilationUnit.directives |
| 58 .any((node) => node is ExportDirective || node is PartDirective); | 54 .any((node) => node is ExportDirective || node is PartDirective); |
| 59 } | 55 } |
| OLD | NEW |