| 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 library analyzer_cli.src.boot_loader; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:isolate'; | |
| 9 | |
| 10 import 'package:analyzer/file_system/file_system.dart'; | |
| 11 import 'package:analyzer/file_system/physical_file_system.dart'; | |
| 12 import 'package:analyzer/source/analysis_options_provider.dart'; | |
| 13 import 'package:analyzer/src/context/context.dart'; | |
| 14 import 'package:analyzer/src/generated/engine.dart' as engine; | |
| 15 import 'package:analyzer/src/plugin/plugin_configuration.dart'; | |
| 16 import 'package:analyzer_cli/src/driver.dart'; | |
| 17 import 'package:analyzer_cli/src/options.dart'; | |
| 18 import 'package:source_span/source_span.dart'; | |
| 19 import 'package:yaml/src/yaml_node.dart'; | |
| 20 | |
| 21 const _analyzerPackageName = 'analyzer'; | |
| 22 | |
| 23 /// Return non-null if there is a validation issue with this plugin. | |
| 24 String validate(PluginInfo plugin) { | |
| 25 var missing = <String>[]; | |
| 26 if (plugin.className == null) { | |
| 27 missing.add('class name'); | |
| 28 } | |
| 29 if (plugin.libraryUri == null) { | |
| 30 missing.add('library uri'); | |
| 31 } | |
| 32 if (missing.isEmpty) { | |
| 33 // All good. | |
| 34 return null; | |
| 35 } | |
| 36 return 'Plugin ${plugin.name} skipped, config missing: ${missing.join(", ")}'; | |
| 37 } | |
| 38 | |
| 39 List<PluginInfo> _validate(Iterable<PluginInfo> plugins) { | |
| 40 List<PluginInfo> validated = <PluginInfo>[]; | |
| 41 plugins.forEach((PluginInfo plugin) { | |
| 42 String validation = validate(plugin); | |
| 43 if (validation != null) { | |
| 44 errorSink.writeln(validation); | |
| 45 } else { | |
| 46 validated.add(plugin); | |
| 47 } | |
| 48 }); | |
| 49 return validated; | |
| 50 } | |
| 51 | |
| 52 /// Source code assembler. | |
| 53 class Assembler { | |
| 54 /// Plugins to configure. | |
| 55 final Iterable<PluginInfo> plugins; | |
| 56 | |
| 57 /// Create an assembler for the given plugin [config]. | |
| 58 Assembler(this.plugins); | |
| 59 | |
| 60 /// A string enumerating required package `import`s. | |
| 61 String get enumerateImports => | |
| 62 plugins.map((PluginInfo p) => "import '${p.libraryUri}';").join('\n'); | |
| 63 | |
| 64 /// A string listing initialized plugin instances. | |
| 65 String get pluginList => | |
| 66 plugins.map((PluginInfo p) => 'new ${p.className}()').join(', '); | |
| 67 | |
| 68 /// Create a file containing a `main()` suitable for loading in spawned | |
| 69 /// isolate. | |
| 70 String createMain() => _generateMain(); | |
| 71 | |
| 72 String _generateMain() => """ | |
| 73 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 74 // for details. All rights reserved. Use of this source code is governed by a | |
| 75 // BSD-style license that can be found in the LICENSE file. | |
| 76 | |
| 77 // This code was auto-generated, is not intended to be edited, and is subject to | |
| 78 // significant change. Please see the README file for more information. | |
| 79 | |
| 80 import 'package:analyzer_cli/src/driver.dart'; | |
| 81 | |
| 82 $enumerateImports | |
| 83 | |
| 84 void main(List<String> args) { | |
| 85 var starter = new Driver(); | |
| 86 starter.userDefinedPlugins = [$pluginList]; | |
| 87 starter.start(args); | |
| 88 } | |
| 89 """; | |
| 90 } | |
| 91 | |
| 92 /// Given environment information extracted from command-line `args`, creates a | |
| 93 /// a loadable analyzer "image". | |
| 94 class BootLoader { | |
| 95 /// Emits an error message to [errorSink] if plugin config can't be read. | |
| 96 static final ErrorHandler _pluginConfigErrorHandler = (Exception e) { | |
| 97 String details; | |
| 98 if (e is PluginConfigFormatException) { | |
| 99 details = e.message; | |
| 100 var node = e.yamlNode; | |
| 101 if (node is YamlNode) { | |
| 102 SourceLocation location = node.span.start; | |
| 103 details += ' (line ${location.line}, column ${location.column})'; | |
| 104 } | |
| 105 } else { | |
| 106 details = e.toString(); | |
| 107 } | |
| 108 | |
| 109 errorSink.writeln('Plugin configuration skipped: $details'); | |
| 110 }; | |
| 111 | |
| 112 /// Reads plugin config info from the analysis options file. | |
| 113 PluginConfigOptionsProcessor _pluginOptionsProcessor = | |
| 114 new PluginConfigOptionsProcessor(_pluginConfigErrorHandler); | |
| 115 | |
| 116 /// Create a loadable analyzer image configured with plugins derived from | |
| 117 /// the given analyzer command-line `args`. | |
| 118 Image createImage(List<String> args) { | |
| 119 // Parse commandline options. | |
| 120 CommandLineOptions options = CommandLineOptions.parse(args); | |
| 121 | |
| 122 // Process analysis options file (and notify all interested parties). | |
| 123 _processAnalysisOptions(options); | |
| 124 | |
| 125 // TODO(pquitslund): Pass in .packages info | |
| 126 return new Image(_pluginOptionsProcessor.config, | |
| 127 args: args, packageRootPath: options.packageRootPath); | |
| 128 } | |
| 129 | |
| 130 File _getOptionsFile( | |
| 131 CommandLineOptions options, ResourceProvider resourceProvider) { | |
| 132 String analysisOptionsFile = options.analysisOptionsFile; | |
| 133 if (analysisOptionsFile != null) { | |
| 134 return resourceProvider.getFile(analysisOptionsFile); | |
| 135 } | |
| 136 File file = | |
| 137 resourceProvider.getFile(engine.AnalysisEngine.ANALYSIS_OPTIONS_FILE); | |
| 138 if (!file.exists) { | |
| 139 file = resourceProvider | |
| 140 .getFile(engine.AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); | |
| 141 } | |
| 142 return file; | |
| 143 } | |
| 144 | |
| 145 void _processAnalysisOptions(CommandLineOptions commandLineOptions) { | |
| 146 // Determine options file path. | |
| 147 try { | |
| 148 File file = _getOptionsFile( | |
| 149 commandLineOptions, PhysicalResourceProvider.INSTANCE); | |
| 150 AnalysisOptionsProvider analysisOptionsProvider = | |
| 151 new AnalysisOptionsProvider(); | |
| 152 Map<String, YamlNode> options = | |
| 153 analysisOptionsProvider.getOptionsFromFile(file); | |
| 154 //TODO(pq): thread in proper context. | |
| 155 var temporaryContext = new AnalysisContextImpl(); | |
| 156 _pluginOptionsProcessor.optionsProcessed(temporaryContext, options); | |
| 157 } on Exception catch (e) { | |
| 158 _pluginOptionsProcessor.onError(e); | |
| 159 } | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 /// A loadable "image" of a a configured analyzer instance. | |
| 164 class Image { | |
| 165 /// (Optional) package root path. | |
| 166 final String packageRootPath; | |
| 167 | |
| 168 /// (Optional) package map. | |
| 169 final Map<String, Uri> packages; | |
| 170 | |
| 171 /// (Optional) args to be passed on to the loaded main. | |
| 172 final List<String> args; | |
| 173 | |
| 174 /// Plugin configuration. | |
| 175 final PluginConfig config; | |
| 176 | |
| 177 /// Create an image with the given [config] and optionally [packages], | |
| 178 /// [packageRootPath], and command line [args]. | |
| 179 Image(this.config, {this.packages, this.packageRootPath, this.args}); | |
| 180 | |
| 181 /// Load this image. | |
| 182 /// | |
| 183 /// Loading an image consists in assembling an analyzer `main()`, configured | |
| 184 /// to include the appropriate analyzer plugins as specified in | |
| 185 /// `.analyzer_options` which is then run in a spawned isolate. | |
| 186 Future load() { | |
| 187 List<PluginInfo> plugins = _validate(config.plugins); | |
| 188 String mainSource = new Assembler(plugins).createMain(); | |
| 189 | |
| 190 Completer completer = new Completer(); | |
| 191 ReceivePort exitListener = new ReceivePort(); | |
| 192 exitListener.listen((data) { | |
| 193 completer.complete(); | |
| 194 exitListener.close(); | |
| 195 }); | |
| 196 | |
| 197 Uri uri = | |
| 198 Uri.parse('data:application/dart;charset=utf-8,${Uri.encodeComponent( | |
| 199 mainSource)}'); | |
| 200 | |
| 201 // TODO(pquitslund): update once .packages are supported. | |
| 202 String packageRoot = | |
| 203 packageRootPath != null ? packageRootPath : './packages'; | |
| 204 Uri packageUri = new Uri.file(packageRoot); | |
| 205 | |
| 206 Isolate.spawnUri(uri, args, null /* msg */, | |
| 207 packageRoot: packageUri, onExit: exitListener.sendPort); | |
| 208 | |
| 209 return completer.future; | |
| 210 } | |
| 211 } | |
| OLD | NEW |