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 dev_compiler.src.analysis_context; |
| 6 |
| 7 import 'package:analyzer/file_system/file_system.dart'; |
| 8 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 9 import 'package:analyzer/src/generated/engine.dart'; |
| 10 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; |
| 11 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; |
| 12 import 'package:analyzer/src/generated/source.dart' show DartUriResolver; |
| 13 import 'package:analyzer/src/generated/source_io.dart'; |
| 14 import 'package:path/path.dart' as path; |
| 15 |
| 16 import 'checker/resolver.dart'; |
| 17 import 'dart_sdk.dart'; |
| 18 import 'multi_package_resolver.dart'; |
| 19 import 'options.dart'; |
| 20 |
| 21 /// Creates an AnalysisContext with dev_compiler type rules and inference. |
| 22 /// |
| 23 /// Use [options.useMockSdk] to specify the SDK mode, or use [sdkResolver] |
| 24 /// to entirely override the DartUriResolver. |
| 25 /// |
| 26 /// If supplied, [fileResolvers] will override the default `file:` and |
| 27 /// `package:` URI resolvers. |
| 28 /// |
| 29 AnalysisContext createAnalysisContext(CompilerOptions options, |
| 30 {DartUriResolver sdkResolver, List fileResolvers}) { |
| 31 var context = _initContext(options); |
| 32 |
| 33 var sdkResolver = options.useMockSdk |
| 34 ? createMockSdkResolver(mockSdkSources) |
| 35 : createSdkPathResolver(options.dartSdkPath); |
| 36 |
| 37 var resolvers = [sdkResolver]; |
| 38 if (options.useImplicitHtml) { |
| 39 resolvers.add(_createImplicitEntryResolver(options)); |
| 40 } |
| 41 if (fileResolvers == null) { |
| 42 fileResolvers = [new FileUriResolver()]; |
| 43 fileResolvers.add(options.useMultiPackage |
| 44 ? new MultiPackageResolver(options.packagePaths) |
| 45 : new PackageUriResolver([new JavaFile(options.packageRoot)])); |
| 46 } |
| 47 resolvers.addAll(fileResolvers); |
| 48 context.sourceFactory = new SourceFactory(resolvers); |
| 49 return context; |
| 50 } |
| 51 |
| 52 /// Creates a [DartUriResolver] that uses a mock 'dart:' library contents. |
| 53 DartUriResolver createMockSdkResolver(Map<String, String> mockSources) => |
| 54 new MockDartSdk(mockSources, reportMissing: true).resolver; |
| 55 |
| 56 /// Creates a [DartUriResolver] that uses the SDK at the given [sdkPath]. |
| 57 DartUriResolver createSdkPathResolver(String sdkPath) => |
| 58 new DartUriResolver(new DirectoryBasedDartSdk(new JavaFile(sdkPath))); |
| 59 |
| 60 UriResolver _createImplicitEntryResolver(ResolverOptions options) { |
| 61 var entry = path.absolute(ResolverOptions.implicitHtmlFile); |
| 62 var src = path.absolute(options.entryPointFile); |
| 63 var provider = new MemoryResourceProvider(); |
| 64 provider.newFile( |
| 65 entry, '<body><script type="application/dart" src="$src"></script>'); |
| 66 return new ResourceUriResolver(provider); |
| 67 } |
| 68 |
| 69 /// Creates an analysis context that contains our restricted typing rules. |
| 70 AnalysisContext _initContext(ResolverOptions options) { |
| 71 var analysisOptions = new AnalysisOptionsImpl()..cacheSize = 512; |
| 72 AnalysisContextImpl res = AnalysisEngine.instance.createAnalysisContext(); |
| 73 res.analysisOptions = analysisOptions; |
| 74 res.libraryResolverFactory = |
| 75 (context) => new LibraryResolverWithInference(context, options); |
| 76 return res; |
| 77 } |
OLD | NEW |