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

Unified Diff: pkg/code_transformers/lib/src/entry_point.dart

Issue 196943024: Adding some asset-related utilities to code_transformers (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: pkg/code_transformers/lib/src/entry_point.dart
diff --git a/pkg/code_transformers/lib/src/entry_point.dart b/pkg/code_transformers/lib/src/entry_point.dart
new file mode 100644
index 0000000000000000000000000000000000000000..4c6a62ae773767fa547ec3235f5d034949a74c64
--- /dev/null
+++ b/pkg/code_transformers/lib/src/entry_point.dart
@@ -0,0 +1,46 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+import 'dart:async';
+
+import 'package:analyzer/analyzer.dart' as analyzer;
+import 'package:analyzer/src/generated/ast.dart';
+import 'package:barback/barback.dart';
+
+/// Checks to see if the provided Asset is a Dart entry point.
+///
+/// Assets are considered entry points if they are Dart files located in
+/// web/, test/, benchmark/ or example/ and have a main() function.
+///
+/// Because this only analyzes the primary asset this may return true for files
+/// which are not dart entries if the file does not have a main() but does have
+/// parts or exports.
+Future<bool> isPossibleDartEntry(Asset asset) {
+ if (asset.id.extension != '.dart') return new Future.value(false);
+
+ if (!['benchmark', 'example', 'test', 'web']
+ .any((dir) => asset.id.path.startsWith("$dir/"))) {
+ return new Future.value(false);
+ }
+ return asset.readAsString().then((contents) {
+ return _couldBeEntrypoint(analyzer.parseCompilationUnit(contents));
+ });
+}
+
+bool _couldBeEntrypoint(CompilationUnit compilationUnit) {
+ // Allow two or fewer arguments so that entrypoints intended for use with
+ // [spawnUri] get counted.
+ var hasMain = compilationUnit.declarations.any((node) =>
+ node is FunctionDeclaration &&
+ node.name.name == "main" &&
+ node.functionExpression.parameters.parameters.length <= 2);
+
+ if (hasMain) return true;
+
+ // If it has an export or a part, assume the worst- that the main could be
+ // in there.
+ // We avoid loading those since this can be run from isPrimaryAsset calls
+ // where we do not have access to other sources.
+ return compilationUnit.directives.any((node) =>
+ node is ExportDirective || node is PartDirective);
+}

Powered by Google App Engine
This is Rietveld 408576698