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