OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import 'dart:async'; | |
6 import 'dart:io'; | |
7 | |
8 import 'package:analysis_server/src/analysis_manager.dart'; | |
9 import 'package:args/args.dart'; | |
10 | |
11 /** | |
12 * Start analysis server as a separate process and use the websocket protocol | |
13 * to analyze the application specified on the command line. | |
14 */ | |
15 void main(List<String> args) { | |
16 new _DartDependencyAnalyzer(args).run().catchError((error, stack) { | |
17 print('Analysis failed: $error'); | |
18 if (stack != null) { | |
19 print(stack); | |
20 } | |
21 }); | |
22 } | |
23 | |
24 /** | |
25 * Instances of [_DartDependencyAnalyzer] launch an analysis server and use | |
26 * that server to analyze the dependencies of an application. | |
27 */ | |
28 class _DartDependencyAnalyzer { | |
29 /** | |
30 * The name of the application that is used to start the dependency analyzer. | |
31 */ | |
32 static const BINARY_NAME = 'dartdeps'; | |
33 | |
34 /** | |
35 * The name of the option used to specify the Dart SDK. | |
36 */ | |
37 static const String DART_SDK_OPTION = 'dart-sdk'; | |
38 | |
39 /** | |
40 * The name of the option used to print usage information. | |
41 */ | |
42 static const String HELP_OPTION = 'help'; | |
43 | |
44 /** | |
45 * The name of the option used to specify an already running server. | |
46 */ | |
47 static const String SERVER_OPTION = 'server'; | |
48 | |
49 /** | |
50 * The command line arguments. | |
51 */ | |
52 final List<String> args; | |
53 | |
54 /** | |
55 * The path to the Dart SDK used during analysis. | |
56 */ | |
57 String sdkPath; | |
58 | |
59 /** | |
60 * The manager for the analysis server. | |
61 */ | |
62 AnalysisManager manager; | |
63 | |
64 _DartDependencyAnalyzer(this.args); | |
65 | |
66 /** | |
67 * Use the given manager to perform the analysis. | |
68 */ | |
69 void analyze(AnalysisManager manager) { | |
70 if (manager == null) { | |
71 return; | |
72 } | |
73 this.manager = manager; | |
74 print('Analyzing...'); | |
75 } | |
76 | |
77 /** | |
78 * Print information about how to use the server. | |
79 */ | |
80 void printUsage(ArgParser parser) { | |
81 print('Usage: $BINARY_NAME [flags] <application_directory>'); | |
82 print(''); | |
83 print('Supported flags are:'); | |
84 print(parser.usage); | |
85 } | |
86 | |
87 /** | |
88 * Parse the command line arguments to determine the application to be | |
89 * analyzed, then launch and manage an analysis server to do the work. | |
90 */ | |
91 Future run() { | |
92 return new Future(start).then(analyze).whenComplete(stop); | |
93 } | |
94 | |
95 /** | |
96 * Parse the command line arguments to determine the application to be | |
97 * analyzed, then launch an analysis server. | |
98 * Return `null` if the command line arguments are invalid. | |
99 */ | |
100 Future<AnalysisManager> start() { | |
101 ArgParser parser = new ArgParser(); | |
102 parser.addOption(DART_SDK_OPTION, help: '[sdkPath] path to Dart SDK'); | |
103 parser.addFlag( | |
104 HELP_OPTION, | |
105 help: 'print this help message without starting analysis', | |
106 defaultsTo: false, | |
107 negatable: false); | |
108 parser.addOption( | |
109 SERVER_OPTION, | |
110 help: '[serverUrl] use an analysis server thats already running'); | |
111 | |
112 // Parse arguments | |
113 ArgResults results; | |
114 try { | |
115 results = parser.parse(args); | |
116 } on FormatException catch (e) { | |
117 print(e.message); | |
118 print(''); | |
119 printUsage(parser); | |
120 exitCode = 1; | |
121 return null; | |
122 } | |
123 if (results[HELP_OPTION]) { | |
124 printUsage(parser); | |
125 return null; | |
126 } | |
127 sdkPath = results[DART_SDK_OPTION]; | |
128 if (sdkPath is! String) { | |
129 print('Missing path to Dart SDK'); | |
130 printUsage(parser); | |
131 return null; | |
132 } | |
133 Directory sdkDir = new Directory(sdkPath); | |
134 if (!sdkDir.existsSync()) { | |
135 print('Specified Dart SDK does not exist: $sdkPath'); | |
136 printUsage(parser); | |
137 return null; | |
138 } | |
139 if (results.rest.length == 0) { | |
140 printUsage(parser); | |
141 exitCode = 1; | |
142 return null; | |
143 } | |
144 Directory appDir = new Directory(results.rest[0]); | |
145 if (!appDir.existsSync()) { | |
146 print('Specified application directory does not exist: $appDir'); | |
147 print(''); | |
148 printUsage(parser); | |
149 exitCode = 1; | |
150 return null; | |
151 } | |
152 if (results.rest.length > 1) { | |
153 print('Unexpected arguments after $appDir'); | |
154 print(''); | |
155 printUsage(parser); | |
156 exitCode = 1; | |
157 return null; | |
158 } | |
159 | |
160 // Connect to an already running analysis server | |
161 String serverUrl = results[SERVER_OPTION]; | |
162 if (serverUrl != null) { | |
163 return AnalysisManager.connect(serverUrl); | |
164 } | |
165 | |
166 // Launch and connect to a new analysis server | |
167 // Assume that the analysis server entry point is in the same directory | |
168 StringBuffer path = new StringBuffer(); | |
169 path.write(FileSystemEntity.parentOf(Platform.script.toFilePath())); | |
170 path.write(Platform.pathSeparator); | |
171 path.write('server.dart'); | |
172 return AnalysisManager.start(path.toString()); | |
173 } | |
174 | |
175 /** | |
176 * Stop the analysis server. | |
177 */ | |
178 void stop() { | |
179 if (manager != null) { | |
180 manager.stop(); | |
181 } | |
182 } | |
183 } | |
OLD | NEW |