Index: pkg/compiler/tool/code_deps.dart |
diff --git a/pkg/compiler/tool/code_deps.dart b/pkg/compiler/tool/code_deps.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..80e2f0df6686f01f5403e2ea207c204b51e5b363 |
--- /dev/null |
+++ b/pkg/compiler/tool/code_deps.dart |
@@ -0,0 +1,86 @@ |
+// Copyright (c) 2015, 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. |
+ |
+/// Command-line tool to query for code dependncies. |
Harry Terkelsen
2015/08/11 22:57:42
dependencies
Siggi Cherem (dart-lang)
2015/08/12 00:06:01
Done.
|
+library compiler.tool.code_deps; |
+ |
+import 'dart:convert'; |
+import 'dart:io'; |
+ |
+import 'package:compiler/src/info/info.dart'; |
+import 'graph.dart'; |
+import 'util.dart'; |
+ |
+main(args) { |
+ if (args.length < 2) { |
+ print('usage: dart tool/code_deps.dart path-to-info.json <query>'); |
+ print(' where <query> can be:'); |
+ print(' - some_path elementA elementB'); |
+ // TODO(sigmund): add other queries, such as 'all_paths'. |
+ exit(1); |
+ } |
+ |
+ var json = JSON.decode(new File(args[0]).readAsStringSync()); |
+ var info = AllInfo.parseFromJson(json); |
+ var graph = graphFromInfo(info); |
+ |
+ var queryName = args[1]; |
+ if (queryName == 'some_path') { |
+ if (args.length < 4) { |
+ print('missing arguments for `some_path`'); |
+ exit(1); |
+ } |
+ var source = |
+ info.functions.firstWhere(_longNameMatcher(new RegExp(args[2]))); |
+ var target = |
+ info.functions.firstWhere(_longNameMatcher(new RegExp(args[3]))); |
+ print('query: some_path'); |
+ print('source: ${longName(source)}'); |
+ print('target: ${longName(target)}'); |
+ var path = new SomePathQuery(source, target).run(graph); |
+ if (path.isEmpty) { |
+ print('result: no path found'); |
+ } else { |
+ print('result:'); |
+ for (int i = 0; i < path.length; i++) { |
+ print(' $i. ${longName(path[i])}'); |
+ } |
+ } |
+ } else { |
+ print('unrecognized query: $queryName'); |
+ } |
+} |
+ |
+/// A query supported by this tool. |
+abstract class Query { |
+ run(Graph<Info> graph); |
+} |
+ |
+/// Query that searches for a single path between two elements. |
+class SomePathQuery { |
+ /// The info associated with the source element. |
+ Info source; |
+ |
+ /// The info associated with the target element. |
+ Info target; |
+ |
+ SomePathQuery(this.source, this.target); |
+ |
+ run(Graph<Info> graph) { |
+ var seen = new Set(); |
+ var result = []; |
+ helper(n) { |
+ if (!seen.add(n)) return false; |
+ result.add(n); |
+ if (identical(n, target)) return true; |
+ if (graph.targetsOf(n).any(helper)) return true; |
Harry Terkelsen
2015/08/11 22:57:42
maybe more useful to do a BFS instead to find the
Siggi Cherem (dart-lang)
2015/08/12 00:06:01
Great idea! Updated. I tried it on an example in a
|
+ assert(result.removeLast() == n); |
+ return false; |
+ } |
+ helper(source); |
+ return result; |
+ } |
+} |
+ |
+_longNameMatcher(RegExp regexp) => (e) => regexp.hasMatch(longName(e)); |