OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015, 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 /// 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.
| |
6 library compiler.tool.code_deps; | |
7 | |
8 import 'dart:convert'; | |
9 import 'dart:io'; | |
10 | |
11 import 'package:compiler/src/info/info.dart'; | |
12 import 'graph.dart'; | |
13 import 'util.dart'; | |
14 | |
15 main(args) { | |
16 if (args.length < 2) { | |
17 print('usage: dart tool/code_deps.dart path-to-info.json <query>'); | |
18 print(' where <query> can be:'); | |
19 print(' - some_path elementA elementB'); | |
20 // TODO(sigmund): add other queries, such as 'all_paths'. | |
21 exit(1); | |
22 } | |
23 | |
24 var json = JSON.decode(new File(args[0]).readAsStringSync()); | |
25 var info = AllInfo.parseFromJson(json); | |
26 var graph = graphFromInfo(info); | |
27 | |
28 var queryName = args[1]; | |
29 if (queryName == 'some_path') { | |
30 if (args.length < 4) { | |
31 print('missing arguments for `some_path`'); | |
32 exit(1); | |
33 } | |
34 var source = | |
35 info.functions.firstWhere(_longNameMatcher(new RegExp(args[2]))); | |
36 var target = | |
37 info.functions.firstWhere(_longNameMatcher(new RegExp(args[3]))); | |
38 print('query: some_path'); | |
39 print('source: ${longName(source)}'); | |
40 print('target: ${longName(target)}'); | |
41 var path = new SomePathQuery(source, target).run(graph); | |
42 if (path.isEmpty) { | |
43 print('result: no path found'); | |
44 } else { | |
45 print('result:'); | |
46 for (int i = 0; i < path.length; i++) { | |
47 print(' $i. ${longName(path[i])}'); | |
48 } | |
49 } | |
50 } else { | |
51 print('unrecognized query: $queryName'); | |
52 } | |
53 } | |
54 | |
55 /// A query supported by this tool. | |
56 abstract class Query { | |
57 run(Graph<Info> graph); | |
58 } | |
59 | |
60 /// Query that searches for a single path between two elements. | |
61 class SomePathQuery { | |
62 /// The info associated with the source element. | |
63 Info source; | |
64 | |
65 /// The info associated with the target element. | |
66 Info target; | |
67 | |
68 SomePathQuery(this.source, this.target); | |
69 | |
70 run(Graph<Info> graph) { | |
71 var seen = new Set(); | |
72 var result = []; | |
73 helper(n) { | |
74 if (!seen.add(n)) return false; | |
75 result.add(n); | |
76 if (identical(n, target)) return true; | |
77 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
| |
78 assert(result.removeLast() == n); | |
79 return false; | |
80 } | |
81 helper(source); | |
82 return result; | |
83 } | |
84 } | |
85 | |
86 _longNameMatcher(RegExp regexp) => (e) => regexp.hasMatch(longName(e)); | |
OLD | NEW |