Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(256)

Side by Side Diff: pkg/analyzer/lib/src/summary/package_bundle_reader.dart

Issue 1830463002: Change analyzer_cli's "package mode" into a "build mode". (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 import 'dart:io' as io; 1 import 'dart:io' as io;
2 2
3 import 'package:analyzer/dart/element/element.dart'; 3 import 'package:analyzer/dart/element/element.dart';
4 import 'package:analyzer/src/context/cache.dart'; 4 import 'package:analyzer/src/context/cache.dart';
5 import 'package:analyzer/src/context/context.dart'; 5 import 'package:analyzer/src/context/context.dart';
6 import 'package:analyzer/src/generated/engine.dart'; 6 import 'package:analyzer/src/generated/engine.dart';
7 import 'package:analyzer/src/generated/resolver.dart'; 7 import 'package:analyzer/src/generated/resolver.dart';
8 import 'package:analyzer/src/generated/source.dart'; 8 import 'package:analyzer/src/generated/source.dart';
9 import 'package:analyzer/src/summary/idl.dart'; 9 import 'package:analyzer/src/summary/idl.dart';
10 import 'package:analyzer/src/summary/resynthesize.dart'; 10 import 'package:analyzer/src/summary/resynthesize.dart';
11 import 'package:analyzer/src/summary/summary_sdk.dart'; 11 import 'package:analyzer/src/summary/summary_sdk.dart';
12 import 'package:analyzer/src/task/dart.dart'; 12 import 'package:analyzer/src/task/dart.dart';
13 import 'package:analyzer/task/dart.dart'; 13 import 'package:analyzer/task/dart.dart';
14 import 'package:analyzer/task/model.dart'; 14 import 'package:analyzer/task/model.dart';
15 import 'package:path/path.dart' as pathos; 15 import 'package:path/path.dart' as pathos;
16 16
17 /** 17 /**
18 * If [uri] has the `package` scheme in form of `package:pkg/file.dart`,
19 * return the `pkg` name. Otherwise return `null`.
20 */
21 String _getPackageName(Uri uri) {
22 if (uri.scheme != 'package') {
23 return null;
24 }
25 String path = uri.path;
26 int index = path.indexOf('/');
27 if (index == -1) {
28 return null;
29 }
30 return path.substring(0, index);
31 }
32
33 /**
34 * The [ResultProvider] that provides results from input package summaries. 18 * The [ResultProvider] that provides results from input package summaries.
35 */ 19 */
36 class InputPackagesResultProvider extends ResultProvider { 20 class InputPackagesResultProvider extends ResultProvider {
37 final InternalAnalysisContext _context; 21 final InternalAnalysisContext _context;
38 final Map<String, String> _packageSummaryInputs;
39 22
40 _FileBasedSummaryResynthesizer _resynthesizer; 23 _FileBasedSummaryResynthesizer _resynthesizer;
41 SummaryResultProvider _sdkProvider; 24 SummaryResultProvider _sdkProvider;
42 25
43 InputPackagesResultProvider(this._context, this._packageSummaryInputs) { 26 InputPackagesResultProvider(this._context, SummaryDataStore dataStore) {
44 InternalAnalysisContext sdkContext = _context.sourceFactory.dartSdk.context; 27 InternalAnalysisContext sdkContext = _context.sourceFactory.dartSdk.context;
45 _sdkProvider = sdkContext.resultProvider; 28 _sdkProvider = sdkContext.resultProvider;
46 // Set the type provider to prevent the context from computing it. 29 // Set the type provider to prevent the context from computing it.
47 _context.typeProvider = sdkContext.typeProvider; 30 _context.typeProvider = sdkContext.typeProvider;
48 // Create a chained resynthesizer. 31 // Create a chained resynthesizer.
49 _resynthesizer = new _FileBasedSummaryResynthesizer( 32 _resynthesizer = new _FileBasedSummaryResynthesizer(
50 _sdkProvider.resynthesizer, 33 _sdkProvider.resynthesizer,
51 _context, 34 _context,
52 _context.typeProvider, 35 _context.typeProvider,
53 _context.sourceFactory, 36 _context.sourceFactory,
54 _context.analysisOptions.strongMode, 37 _context.analysisOptions.strongMode,
55 _packageSummaryInputs.values.toList()); 38 dataStore);
56 } 39 }
57 40
58 @override 41 @override
59 bool compute(CacheEntry entry, ResultDescriptor result) { 42 bool compute(CacheEntry entry, ResultDescriptor result) {
60 if (_sdkProvider.compute(entry, result)) { 43 if (_sdkProvider.compute(entry, result)) {
61 return true; 44 return true;
62 } 45 }
63 AnalysisTarget target = entry.target; 46 AnalysisTarget target = entry.target;
64 // Only library results are supported for now. 47 // Only library results are supported for now.
65 if (target is Source) { 48 if (target is Source) {
66 Uri uri = target.uri; 49 Uri uri = target.uri;
67 // We know how to server results to input packages. 50 // We know how to server results to input packages.
68 String sourcePackageName = _getPackageName(uri); 51 String uriString = uri.toString();
69 if (!_packageSummaryInputs.containsKey(sourcePackageName)) { 52 if (!_resynthesizer.hasLibrarySummary(uriString)) {
70 return false; 53 return false;
71 } 54 }
72 // Provide known results. 55 // Provide known results.
73 String uriString = uri.toString();
74 if (result == LIBRARY_ELEMENT1 || 56 if (result == LIBRARY_ELEMENT1 ||
75 result == LIBRARY_ELEMENT2 || 57 result == LIBRARY_ELEMENT2 ||
76 result == LIBRARY_ELEMENT3 || 58 result == LIBRARY_ELEMENT3 ||
77 result == LIBRARY_ELEMENT4 || 59 result == LIBRARY_ELEMENT4 ||
78 result == LIBRARY_ELEMENT5 || 60 result == LIBRARY_ELEMENT5 ||
79 result == LIBRARY_ELEMENT6 || 61 result == LIBRARY_ELEMENT6 ||
80 result == LIBRARY_ELEMENT7 || 62 result == LIBRARY_ELEMENT7 ||
81 result == LIBRARY_ELEMENT8 || 63 result == LIBRARY_ELEMENT8 ||
82 result == LIBRARY_ELEMENT || 64 result == LIBRARY_ELEMENT ||
83 false) { 65 false) {
84 LibraryElement libraryElement = 66 LibraryElement libraryElement =
85 _resynthesizer.getLibraryElement(uriString); 67 _resynthesizer.getLibraryElement(uriString);
86 entry.setValue(result, libraryElement, TargetedResult.EMPTY_LIST); 68 entry.setValue(result, libraryElement, TargetedResult.EMPTY_LIST);
87 return true; 69 return true;
88 } else if (result == READY_LIBRARY_ELEMENT2 || 70 } else if (result == READY_LIBRARY_ELEMENT2 ||
89 result == READY_LIBRARY_ELEMENT5 || 71 result == READY_LIBRARY_ELEMENT5 ||
90 result == READY_LIBRARY_ELEMENT6) { 72 result == READY_LIBRARY_ELEMENT6) {
91 entry.setValue(result, true, TargetedResult.EMPTY_LIST); 73 entry.setValue(result, true, TargetedResult.EMPTY_LIST);
92 return true; 74 return true;
93 } else if (result == SOURCE_KIND) { 75 } else if (result == SOURCE_KIND) {
94 if (_resynthesizer.linkedMap.containsKey(uriString)) { 76 if (_resynthesizer._dataStore.linkedMap.containsKey(uriString)) {
95 entry.setValue(result, SourceKind.LIBRARY, TargetedResult.EMPTY_LIST); 77 entry.setValue(result, SourceKind.LIBRARY, TargetedResult.EMPTY_LIST);
96 return true; 78 return true;
97 } 79 }
98 if (_resynthesizer.unlinkedMap.containsKey(uriString)) { 80 if (_resynthesizer._dataStore.unlinkedMap.containsKey(uriString)) {
99 entry.setValue(result, SourceKind.PART, TargetedResult.EMPTY_LIST); 81 entry.setValue(result, SourceKind.PART, TargetedResult.EMPTY_LIST);
Jennifer Messerly 2016/03/23 17:37:28 Dumb question about this... Here, it looks like a
scheglov 2016/03/23 17:43:22 Yes, library URIs are in both maps.
100 return true; 82 return true;
101 } 83 }
102 return false; 84 return false;
103 } 85 }
104 } 86 }
105 return false; 87 return false;
106 } 88 }
107 } 89 }
108 90
109 /** 91 /**
110 * The [UriResolver] that knows about sources that are parts of packages which 92 * The [UriResolver] that knows about sources that are served from their
111 * are served from their summaries. 93 * summaries.
112 */ 94 */
113 class InSummaryPackageUriResolver extends UriResolver { 95 class InSummaryPackageUriResolver extends UriResolver {
114 final Map<String, String> _packageSummaryInputs; 96 final SummaryDataStore _dataStore;
115 97
116 InSummaryPackageUriResolver(this._packageSummaryInputs); 98 InSummaryPackageUriResolver(this._dataStore);
117 99
118 @override 100 @override
119 Source resolveAbsolute(Uri uri, [Uri actualUri]) { 101 Source resolveAbsolute(Uri uri, [Uri actualUri]) {
120 actualUri ??= uri; 102 actualUri ??= uri;
121 String packageName = _getPackageName(actualUri); 103 if (_dataStore.unlinkedMap.containsKey(uri.toString())) {
122 if (_packageSummaryInputs.containsKey(packageName)) {
123 return new _InSummarySource(actualUri); 104 return new _InSummarySource(actualUri);
124 } 105 }
125 return null; 106 return null;
126 } 107 }
127 } 108 }
128 109
129 /** 110 /**
130 * A concrete resynthesizer that serves summaries from given file paths. 111 * A [SummaryDataStore] is a container for the data extracted from a set of
112 * summary package bundles. It contains maps which can be used to find linked
113 * and unlinked summaries by URI.
131 */ 114 */
132 class _FileBasedSummaryResynthesizer extends SummaryResynthesizer { 115 class SummaryDataStore {
116 /**
117 * Map from the URI of a compilation unit to the unlinked summary of that
118 * compilation unit.
119 */
133 final Map<String, UnlinkedUnit> unlinkedMap = <String, UnlinkedUnit>{}; 120 final Map<String, UnlinkedUnit> unlinkedMap = <String, UnlinkedUnit>{};
121
122 /**
123 * Map from the URI of a library to the linked summary of that library.
124 */
134 final Map<String, LinkedLibrary> linkedMap = <String, LinkedLibrary>{}; 125 final Map<String, LinkedLibrary> linkedMap = <String, LinkedLibrary>{};
135 126
136 _FileBasedSummaryResynthesizer( 127 SummaryDataStore(Iterable<String> summaryPaths) {
137 SummaryResynthesizer parent,
138 AnalysisContext context,
139 TypeProvider typeProvider,
140 SourceFactory sourceFactory,
141 bool strongMode,
142 List<String> summaryPaths)
143 : super(parent, context, typeProvider, sourceFactory, strongMode) {
144 summaryPaths.forEach(_fillMaps); 128 summaryPaths.forEach(_fillMaps);
145 } 129 }
146 130
147 @override
148 LinkedLibrary getLinkedSummary(String uri) {
149 return linkedMap[uri];
150 }
151
152 @override
153 UnlinkedUnit getUnlinkedSummary(String uri) {
154 return unlinkedMap[uri];
155 }
156
157 @override
158 bool hasLibrarySummary(String uri) {
159 return linkedMap.containsKey(uri);
160 }
161
162 void _fillMaps(String path) { 131 void _fillMaps(String path) {
163 io.File file = new io.File(path); 132 io.File file = new io.File(path);
164 List<int> buffer = file.readAsBytesSync(); 133 List<int> buffer = file.readAsBytesSync();
165 PackageBundle bundle = new PackageBundle.fromBuffer(buffer); 134 PackageBundle bundle = new PackageBundle.fromBuffer(buffer);
166 for (int i = 0; i < bundle.unlinkedUnitUris.length; i++) { 135 for (int i = 0; i < bundle.unlinkedUnitUris.length; i++) {
167 unlinkedMap[bundle.unlinkedUnitUris[i]] = bundle.unlinkedUnits[i]; 136 unlinkedMap[bundle.unlinkedUnitUris[i]] = bundle.unlinkedUnits[i];
168 } 137 }
169 for (int i = 0; i < bundle.linkedLibraryUris.length; i++) { 138 for (int i = 0; i < bundle.linkedLibraryUris.length; i++) {
170 linkedMap[bundle.linkedLibraryUris[i]] = bundle.linkedLibraries[i]; 139 linkedMap[bundle.linkedLibraryUris[i]] = bundle.linkedLibraries[i];
171 } 140 }
172 } 141 }
173 } 142 }
174 143
144 /**
145 * A concrete resynthesizer that serves summaries from given file paths.
146 */
147 class _FileBasedSummaryResynthesizer extends SummaryResynthesizer {
148 final SummaryDataStore _dataStore;
149
150 _FileBasedSummaryResynthesizer(
151 SummaryResynthesizer parent,
152 AnalysisContext context,
153 TypeProvider typeProvider,
154 SourceFactory sourceFactory,
155 bool strongMode,
156 this._dataStore)
157 : super(parent, context, typeProvider, sourceFactory, strongMode);
158
159 @override
160 LinkedLibrary getLinkedSummary(String uri) {
161 return _dataStore.linkedMap[uri];
162 }
163
164 @override
165 UnlinkedUnit getUnlinkedSummary(String uri) {
166 return _dataStore.unlinkedMap[uri];
167 }
168
169 @override
170 bool hasLibrarySummary(String uri) {
171 return _dataStore.linkedMap.containsKey(uri);
172 }
173 }
174
175 /** 175 /**
176 * A placeholder of a source that is part of a package whose analysis results 176 * A placeholder of a source that is part of a package whose analysis results
177 * are served from its summary. This source uses its URI as [fullName] and has 177 * are served from its summary. This source uses its URI as [fullName] and has
178 * empty contents. 178 * empty contents.
179 */ 179 */
180 class _InSummarySource extends Source { 180 class _InSummarySource extends Source {
181 final Uri uri; 181 final Uri uri;
182 182
183 _InSummarySource(this.uri); 183 _InSummarySource(this.uri);
184 184
(...skipping 30 matching lines...) Expand all
215 215
216 @override 216 @override
217 Uri resolveRelativeUri(Uri relativeUri) { 217 Uri resolveRelativeUri(Uri relativeUri) {
218 Uri baseUri = uri; 218 Uri baseUri = uri;
219 return baseUri.resolveUri(relativeUri); 219 return baseUri.resolveUri(relativeUri);
220 } 220 }
221 221
222 @override 222 @override
223 String toString() => uri.toString(); 223 String toString() => uri.toString();
224 } 224 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698