| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 library analyzer.src.context.context_builder; | 5 library analyzer.src.context.context_builder; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:core' hide Resource; | 8 import 'dart:core' hide Resource; |
| 9 import 'dart:io' as io; | 9 import 'dart:io' as io; |
| 10 | 10 |
| 11 import 'package:analyzer/context/declared_variables.dart'; |
| 11 import 'package:analyzer/file_system/file_system.dart'; | 12 import 'package:analyzer/file_system/file_system.dart'; |
| 12 import 'package:analyzer/plugin/resolver_provider.dart'; | 13 import 'package:analyzer/plugin/resolver_provider.dart'; |
| 13 import 'package:analyzer/source/analysis_options_provider.dart'; | 14 import 'package:analyzer/source/analysis_options_provider.dart'; |
| 14 import 'package:analyzer/source/embedder.dart'; | 15 import 'package:analyzer/source/embedder.dart'; |
| 15 import 'package:analyzer/source/package_map_resolver.dart'; | 16 import 'package:analyzer/source/package_map_resolver.dart'; |
| 16 import 'package:analyzer/source/sdk_ext.dart'; | 17 import 'package:analyzer/source/sdk_ext.dart'; |
| 17 import 'package:analyzer/src/generated/engine.dart'; | 18 import 'package:analyzer/src/generated/engine.dart'; |
| 18 import 'package:analyzer/src/generated/java_io.dart'; | 19 import 'package:analyzer/src/generated/java_io.dart'; |
| 19 import 'package:analyzer/src/generated/sdk.dart'; | 20 import 'package:analyzer/src/generated/sdk.dart'; |
| 20 import 'package:analyzer/src/generated/sdk_io.dart'; | 21 import 'package:analyzer/src/generated/sdk_io.dart'; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 final ContentCache contentCache; | 69 final ContentCache contentCache; |
| 69 | 70 |
| 70 /** | 71 /** |
| 71 * The resolver provider used to create a package: URI resolver, or `null` if | 72 * The resolver provider used to create a package: URI resolver, or `null` if |
| 72 * the normal (Package Specification DEP) lookup mechanism is to be used. | 73 * the normal (Package Specification DEP) lookup mechanism is to be used. |
| 73 */ | 74 */ |
| 74 ResolverProvider packageResolverProvider; | 75 ResolverProvider packageResolverProvider; |
| 75 | 76 |
| 76 /** | 77 /** |
| 77 * The file path of the .packages file that should be used in place of any | 78 * The file path of the .packages file that should be used in place of any |
| 78 * file found using the normal (Package Specification DEP) lookup mechanism. | 79 * file found using the normal (Package Specification DEP) lookup mechanism, |
| 80 * or `null` if the normal lookup mechanism should be used. |
| 79 */ | 81 */ |
| 80 String defaultPackageFilePath; | 82 String defaultPackageFilePath; |
| 81 | 83 |
| 82 /** | 84 /** |
| 83 * The file path of the packages directory that should be used in place of any | 85 * The file path of the packages directory that should be used in place of any |
| 84 * file found using the normal (Package Specification DEP) lookup mechanism. | 86 * file found using the normal (Package Specification DEP) lookup mechanism, |
| 87 * or `null` if the normal lookup mechanism should be used. |
| 85 */ | 88 */ |
| 86 String defaultPackagesDirectoryPath; | 89 String defaultPackagesDirectoryPath; |
| 87 | 90 |
| 88 /** | 91 /** |
| 89 * The file path of the analysis options file that should be used in place of | 92 * The file path of the analysis options file that should be used in place of |
| 90 * any file in the root directory. | 93 * any file in the root directory or a parent of the root directory, or `null` |
| 94 * if the normal lookup mechanism should be used. |
| 91 */ | 95 */ |
| 92 String defaultAnalysisOptionsFilePath; | 96 String defaultAnalysisOptionsFilePath; |
| 93 | 97 |
| 94 /** | 98 /** |
| 95 * The default analysis options that should be used unless some or all of them | 99 * The default analysis options that should be used unless some or all of them |
| 96 * are overridden in the analysis options file. | 100 * are overridden in the analysis options file, or `null` if the default |
| 101 * defaults should be used. |
| 97 */ | 102 */ |
| 98 AnalysisOptions defaultOptions; | 103 AnalysisOptions defaultOptions; |
| 99 | 104 |
| 100 /** | 105 /** |
| 106 * A table mapping variable names to values for the declared variables, or |
| 107 * `null` if no additional variables should be declared. |
| 108 */ |
| 109 Map<String, String> declaredVariables; |
| 110 |
| 111 /** |
| 101 * Initialize a newly created builder to be ready to build a context rooted in | 112 * Initialize a newly created builder to be ready to build a context rooted in |
| 102 * the directory with the given [rootDirectoryPath]. | 113 * the directory with the given [rootDirectoryPath]. |
| 103 */ | 114 */ |
| 104 ContextBuilder(this.resourceProvider, this.sdkManager, this.contentCache); | 115 ContextBuilder(this.resourceProvider, this.sdkManager, this.contentCache); |
| 105 | 116 |
| 106 AnalysisContext buildContext(String rootDirectoryPath) { | 117 /** |
| 118 * Return an analysis context that is configured correctly to analyze code in |
| 119 * the directory with the given [path]. |
| 120 * |
| 121 * *Note:* This method is not yet fully implemented and should not be used. |
| 122 */ |
| 123 AnalysisContext buildContext(String path) { |
| 107 // TODO(brianwilkerson) Split getAnalysisOptions so we can capture the | 124 // TODO(brianwilkerson) Split getAnalysisOptions so we can capture the |
| 108 // option map and use it to run the options processors. | 125 // option map and use it to run the options processors. |
| 109 AnalysisOptions options = getAnalysisOptions(rootDirectoryPath); | 126 AnalysisOptions options = getAnalysisOptions(path); |
| 110 InternalAnalysisContext context = | 127 InternalAnalysisContext context = |
| 111 AnalysisEngine.instance.createAnalysisContext(); | 128 AnalysisEngine.instance.createAnalysisContext(); |
| 112 context.contentCache = contentCache; | 129 context.contentCache = contentCache; |
| 113 context.sourceFactory = createSourceFactory(rootDirectoryPath, options); | 130 context.sourceFactory = createSourceFactory(path, options); |
| 114 context.analysisOptions = options; | 131 context.analysisOptions = options; |
| 115 //_processAnalysisOptions(context, optionMap); | 132 //_processAnalysisOptions(context, optionMap); |
| 133 declareVariables(context); |
| 116 return context; | 134 return context; |
| 117 } | 135 } |
| 118 | 136 |
| 137 Map<String, List<Folder>> convertPackagesToMap(Packages packages) { |
| 138 if (packages == null || packages == Packages.noPackages) { |
| 139 return null; |
| 140 } |
| 141 Map<String, List<Folder>> folderMap = new HashMap<String, List<Folder>>(); |
| 142 packages.asMap().forEach((String packagePath, Uri uri) { |
| 143 String path = resourceProvider.pathContext.fromUri(uri); |
| 144 folderMap[packagePath] = [resourceProvider.getFolder(path)]; |
| 145 }); |
| 146 return folderMap; |
| 147 } |
| 148 |
| 119 // void _processAnalysisOptions( | 149 // void _processAnalysisOptions( |
| 120 // AnalysisContext context, Map<String, YamlNode> optionMap) { | 150 // AnalysisContext context, Map<String, YamlNode> optionMap) { |
| 121 // List<OptionsProcessor> optionsProcessors = | 151 // List<OptionsProcessor> optionsProcessors = |
| 122 // AnalysisEngine.instance.optionsPlugin.optionsProcessors; | 152 // AnalysisEngine.instance.optionsPlugin.optionsProcessors; |
| 123 // try { | 153 // try { |
| 124 // optionsProcessors.forEach( | 154 // optionsProcessors.forEach( |
| 125 // (OptionsProcessor p) => p.optionsProcessed(context, optionMap)); | 155 // (OptionsProcessor p) => p.optionsProcessed(context, optionMap)); |
| 126 // | 156 // |
| 127 // // Fill in lint rule defaults in case lints are enabled and rules are | 157 // // Fill in lint rule defaults in case lints are enabled and rules are |
| 128 // // not specified in an options file. | 158 // // not specified in an options file. |
| 129 // if (context.analysisOptions.lint && !containsLintRuleEntry(optionMap)) { | 159 // if (context.analysisOptions.lint && !containsLintRuleEntry(optionMap)) { |
| 130 // setLints(context, linterPlugin.contributedRules); | 160 // setLints(context, linterPlugin.contributedRules); |
| 131 // } | 161 // } |
| 132 // | 162 // |
| 133 // // Ask engine to further process options. | 163 // // Ask engine to further process options. |
| 134 // if (optionMap != null) { | 164 // if (optionMap != null) { |
| 135 // configureContextOptions(context, optionMap); | 165 // configureContextOptions(context, optionMap); |
| 136 // } | 166 // } |
| 137 // } on Exception catch (e) { | 167 // } on Exception catch (e) { |
| 138 // optionsProcessors.forEach((OptionsProcessor p) => p.onError(e)); | 168 // optionsProcessors.forEach((OptionsProcessor p) => p.onError(e)); |
| 139 // } | 169 // } |
| 140 // } | 170 // } |
| 141 | 171 |
| 142 Map<String, List<Folder>> convertPackagesToMap(Packages packages) { | 172 /** |
| 143 if (packages == null || packages == Packages.noPackages) { | 173 * Return an analysis options object containing the default option values. |
| 144 return null; | 174 */ |
| 175 AnalysisOptions createDefaultOptions() { |
| 176 if (defaultOptions == null) { |
| 177 return new AnalysisOptionsImpl(); |
| 145 } | 178 } |
| 146 Map<String, List<Folder>> folderMap = new HashMap<String, List<Folder>>(); | 179 return new AnalysisOptionsImpl.from(defaultOptions); |
| 147 packages.asMap().forEach((String packagePath, Uri uri) { | |
| 148 String path = resourceProvider.pathContext.fromUri(uri); | |
| 149 folderMap[packagePath] = [resourceProvider.getFolder(path)]; | |
| 150 }); | |
| 151 return folderMap; | |
| 152 } | 180 } |
| 153 | 181 |
| 154 Packages createPackageMap(String rootDirectoryPath) { | 182 Packages createPackageMap(String rootDirectoryPath) { |
| 155 if (defaultPackageFilePath != null) { | 183 if (defaultPackageFilePath != null) { |
| 156 // TODO(brianwilkerson) Figure out why we're going through Uri rather than | 184 // TODO(brianwilkerson) Figure out why we're going through Uri rather than |
| 157 // just creating the file from the path. | 185 // just creating the file from the path. |
| 158 Uri fileUri = new Uri.file(defaultPackageFilePath); | 186 Uri fileUri = new Uri.file(defaultPackageFilePath); |
| 159 io.File configFile = new io.File.fromUri(fileUri).absolute; | 187 io.File configFile = new io.File.fromUri(fileUri).absolute; |
| 160 List<int> bytes = configFile.readAsBytesSync(); | 188 List<int> bytes = configFile.readAsBytesSync(); |
| 161 Map<String, Uri> map = parse(bytes, configFile.uri); | 189 Map<String, Uri> map = parse(bytes, configFile.uri); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 189 List<UriResolver> resolvers = <UriResolver>[]; | 217 List<UriResolver> resolvers = <UriResolver>[]; |
| 190 resolvers.add(new DartUriResolver(findSdk(packageMap, options))); | 218 resolvers.add(new DartUriResolver(findSdk(packageMap, options))); |
| 191 if (packageMap != null) { | 219 if (packageMap != null) { |
| 192 resolvers.add(new PackageMapUriResolver(resourceProvider, packageMap)); | 220 resolvers.add(new PackageMapUriResolver(resourceProvider, packageMap)); |
| 193 } | 221 } |
| 194 resolvers.add(new ResourceUriResolver(resourceProvider)); | 222 resolvers.add(new ResourceUriResolver(resourceProvider)); |
| 195 return new SourceFactory(resolvers); | 223 return new SourceFactory(resolvers); |
| 196 } | 224 } |
| 197 | 225 |
| 198 /** | 226 /** |
| 199 * Use the given [packageMap] and [options] to locate the SDK. | 227 * Add any [declaredVariables] to the list of declared variables used by the |
| 228 * given [context]. |
| 229 */ |
| 230 void declareVariables(InternalAnalysisContext context) { |
| 231 if (declaredVariables != null && declaredVariables.isNotEmpty) { |
| 232 DeclaredVariables contextVariables = context.declaredVariables; |
| 233 declaredVariables.forEach((String variableName, String value) { |
| 234 contextVariables.define(variableName, value); |
| 235 }); |
| 236 } |
| 237 } |
| 238 |
| 239 /** |
| 240 * Return the SDK that should be used to analyze code. Use the given |
| 241 * [packageMap] and [options] to locate the SDK. |
| 200 */ | 242 */ |
| 201 DartSdk findSdk( | 243 DartSdk findSdk( |
| 202 Map<String, List<Folder>> packageMap, AnalysisOptions options) { | 244 Map<String, List<Folder>> packageMap, AnalysisOptions options) { |
| 203 if (packageMap != null) { | 245 if (packageMap != null) { |
| 204 // TODO(brianwilkerson) Fix it so that we don't have to create a resolver | 246 // TODO(brianwilkerson) Fix it so that we don't have to create a resolver |
| 205 // to figure out what the extensions are. | 247 // to figure out what the extensions are. |
| 206 SdkExtUriResolver extResolver = new SdkExtUriResolver(packageMap); | 248 SdkExtUriResolver extResolver = new SdkExtUriResolver(packageMap); |
| 207 List<String> extFilePaths = extResolver.extensionFilePaths; | 249 List<String> extFilePaths = extResolver.extensionFilePaths; |
| 208 EmbedderYamlLocator locator = new EmbedderYamlLocator(packageMap); | 250 EmbedderYamlLocator locator = new EmbedderYamlLocator(packageMap); |
| 209 Map<Folder, YamlMap> embedderYamls = locator.embedderYamls; | 251 Map<Folder, YamlMap> embedderYamls = locator.embedderYamls; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 SdkDescription description = new SdkDescription(<String>[sdkPath], options); | 296 SdkDescription description = new SdkDescription(<String>[sdkPath], options); |
| 255 return sdkManager.getSdk(description, () { | 297 return sdkManager.getSdk(description, () { |
| 256 DirectoryBasedDartSdk sdk = | 298 DirectoryBasedDartSdk sdk = |
| 257 new DirectoryBasedDartSdk(new JavaFile(sdkPath)); | 299 new DirectoryBasedDartSdk(new JavaFile(sdkPath)); |
| 258 sdk.analysisOptions = options; | 300 sdk.analysisOptions = options; |
| 259 sdk.useSummary = sdkManager.canUseSummaries; | 301 sdk.useSummary = sdkManager.canUseSummaries; |
| 260 return sdk; | 302 return sdk; |
| 261 }); | 303 }); |
| 262 } | 304 } |
| 263 | 305 |
| 264 AnalysisOptions getAnalysisOptions(String rootDirectoryPath) { | 306 /** |
| 265 AnalysisOptionsImpl options = new AnalysisOptionsImpl.from(defaultOptions); | 307 * Return the analysis options that should be used when analyzing code in the |
| 266 File optionsFile = getOptionsFile(rootDirectoryPath); | 308 * directory with the given [path]. |
| 309 */ |
| 310 AnalysisOptions getAnalysisOptions(String path) { |
| 311 AnalysisOptionsImpl options = createDefaultOptions(); |
| 312 File optionsFile = getOptionsFile(path); |
| 267 if (optionsFile != null) { | 313 if (optionsFile != null) { |
| 268 Map<String, YamlNode> fileOptions = | 314 Map<String, YamlNode> fileOptions = |
| 269 new AnalysisOptionsProvider().getOptionsFromFile(optionsFile); | 315 new AnalysisOptionsProvider().getOptionsFromFile(optionsFile); |
| 270 applyToAnalysisOptions(options, fileOptions); | 316 applyToAnalysisOptions(options, fileOptions); |
| 271 } | 317 } |
| 272 return options; | 318 return options; |
| 273 } | 319 } |
| 274 | 320 |
| 275 File getOptionsFile(String rootDirectoryPath) { | 321 /** |
| 322 * Return the analysis options file that should be used when analyzing code in |
| 323 * the directory with the given [path]. |
| 324 */ |
| 325 File getOptionsFile(String path) { |
| 276 if (defaultAnalysisOptionsFilePath != null) { | 326 if (defaultAnalysisOptionsFilePath != null) { |
| 277 return resourceProvider.getFile(defaultAnalysisOptionsFilePath); | 327 return resourceProvider.getFile(defaultAnalysisOptionsFilePath); |
| 278 } | 328 } |
| 279 Folder root = resourceProvider.getFolder(rootDirectoryPath); | 329 Folder root = resourceProvider.getFolder(path); |
| 280 for (Folder folder = root; folder != null; folder = folder.parent) { | 330 for (Folder folder = root; folder != null; folder = folder.parent) { |
| 281 File file = | 331 File file = |
| 282 folder.getChildAssumingFile(AnalysisEngine.ANALYSIS_OPTIONS_FILE); | 332 folder.getChildAssumingFile(AnalysisEngine.ANALYSIS_OPTIONS_FILE); |
| 283 if (file.exists) { | 333 if (file.exists) { |
| 284 return file; | 334 return file; |
| 285 } | 335 } |
| 286 file = folder | 336 file = folder |
| 287 .getChildAssumingFile(AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); | 337 .getChildAssumingFile(AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); |
| 288 if (file.exists) { | 338 if (file.exists) { |
| 289 return file; | 339 return file; |
| 290 } | 340 } |
| 291 } | 341 } |
| 292 return null; | 342 return null; |
| 293 } | 343 } |
| 294 } | 344 } |
| OLD | NEW |