OLD | NEW |
1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
2 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 import 'package:analyzer/src/generated/ast.dart'; | 8 import 'package:analyzer/src/generated/ast.dart'; |
9 import 'package:analyzer/src/generated/element.dart'; | 9 import 'package:analyzer/src/generated/element.dart'; |
10 import 'package:analyzer/src/generated/engine.dart'; | 10 import 'package:analyzer/src/generated/engine.dart'; |
11 import 'package:analyzer/src/generated/java_io.dart'; | 11 import 'package:analyzer/src/generated/java_io.dart'; |
12 import 'package:analyzer/src/generated/sdk.dart' show DartSdk; | 12 import 'package:analyzer/src/generated/sdk.dart' show DartSdk; |
13 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; | 13 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; |
| 14 import 'package:analyzer/src/generated/source.dart'; |
14 import 'package:analyzer/src/generated/source_io.dart'; | 15 import 'package:analyzer/src/generated/source_io.dart'; |
15 | 16 |
16 main(List<String> args) { | 17 const _usage = |
| 18 'Usage: resolve_driver <path_to_sdk> <file_to_resolve> [<packages_root>]'; |
| 19 |
| 20 void main(List<String> args) { |
17 print('working dir ${new File('.').resolveSymbolicLinksSync()}'); | 21 print('working dir ${new File('.').resolveSymbolicLinksSync()}'); |
18 | 22 |
19 if (args.length != 2) { | 23 if (args.length < 2 || args.length > 3) { |
20 print('Usage: resolve_driver [path_to_sdk] [file_to_resolve]'); | 24 print(_usage); |
21 exit(0); | 25 exit(0); |
22 } | 26 } |
23 | 27 |
| 28 String packageRoot; |
| 29 if (args.length == 3) { |
| 30 packageRoot = args[2]; |
| 31 } |
| 32 |
24 JavaSystemIO.setProperty("com.google.dart.sdk", args[0]); | 33 JavaSystemIO.setProperty("com.google.dart.sdk", args[0]); |
25 DartSdk sdk = DirectoryBasedDartSdk.defaultSdk; | 34 DartSdk sdk = DirectoryBasedDartSdk.defaultSdk; |
26 | 35 |
27 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext(); | 36 var resolvers = [new DartUriResolver(sdk), new FileUriResolver()]; |
28 context.sourceFactory = | 37 |
29 new SourceFactory([new DartUriResolver(sdk), new FileUriResolver()]); | 38 if (packageRoot != null) { |
| 39 var packageDirectory = new JavaFile(packageRoot); |
| 40 resolvers.add(new PackageUriResolver([packageDirectory])); |
| 41 } |
| 42 |
| 43 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext() |
| 44 ..sourceFactory = new SourceFactory(resolvers); |
| 45 |
30 Source source = new FileBasedSource.con1(new JavaFile(args[1])); | 46 Source source = new FileBasedSource.con1(new JavaFile(args[1])); |
31 // | 47 ChangeSet changeSet = new ChangeSet()..addedSource(source); |
32 ChangeSet changeSet = new ChangeSet(); | |
33 changeSet.addedSource(source); | |
34 context.applyChanges(changeSet); | 48 context.applyChanges(changeSet); |
35 LibraryElement libElement = context.computeLibraryElement(source); | 49 LibraryElement libElement = context.computeLibraryElement(source); |
36 print("libElement: $libElement"); | 50 print("libElement: $libElement"); |
37 | 51 |
38 CompilationUnit resolvedUnit = | 52 CompilationUnit resolvedUnit = |
39 context.resolveCompilationUnit(source, libElement); | 53 context.resolveCompilationUnit(source, libElement); |
40 var visitor = new _ASTVisitor(); | 54 var visitor = new _ASTVisitor(); |
41 resolvedUnit.accept(visitor); | 55 resolvedUnit.accept(visitor); |
42 } | 56 } |
43 | 57 |
44 class _ASTVisitor extends GeneralizingAstVisitor { | 58 class _ASTVisitor extends GeneralizingAstVisitor { |
45 visitNode(AstNode node) { | 59 visitNode(AstNode node) { |
46 String text = '${node.runtimeType} : <"$node">'; | 60 var lines = <String>['${node.runtimeType} : <"$node">']; |
47 if (node is SimpleIdentifier) { | 61 if (node is SimpleIdentifier) { |
48 Element element = node.staticElement; | 62 Element element = node.staticElement; |
49 if (element != null) { | 63 if (element != null) { |
50 text += " element: ${element.runtimeType}"; | 64 lines.add(' element: ${element.runtimeType}'); |
51 LibraryElement library = element.library; | 65 LibraryElement library = element.library; |
52 if (library != null) { | 66 if (library != null) { |
53 text += | 67 var fullName = |
54 " from ${element.library.definingCompilationUnit.source.fullName}"
; | 68 element.library.definingCompilationUnit.source.fullName; |
| 69 lines.add(" from $fullName"); |
55 } | 70 } |
56 } | 71 } |
57 } | 72 } |
58 print(text); | 73 print(lines.join('\n')); |
59 return super.visitNode(node); | 74 return super.visitNode(node); |
60 } | 75 } |
61 } | 76 } |
OLD | NEW |