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

Side by Side Diff: pkg/analyzer/lib/source/sdk_ext.dart

Issue 1230273002: Analyzer code cleanup. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 5 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
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/generated/incremental_resolver.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 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 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 source.sdk_ext; 5 library source.sdk_ext;
6 6
7 import 'dart:async';
8 import 'dart:collection';
9 import 'dart:convert'; 7 import 'dart:convert';
10 import 'dart:core' hide Resource; 8 import 'dart:core' hide Resource;
11 9
12 import 'package:analyzer/file_system/file_system.dart'; 10 import 'package:analyzer/file_system/file_system.dart';
13 import 'package:analyzer/source/package_map_resolver.dart'; 11 import 'package:analyzer/src/generated/java_io.dart' show JavaFile;
14 import 'package:analyzer/src/generated/source.dart'; 12 import 'package:analyzer/src/generated/source.dart';
15 import 'package:analyzer/src/generated/java_io.dart' show JavaFile;
16 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; 13 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource;
17 import 'package:path/path.dart' as pathos; 14 import 'package:path/path.dart' as pathos;
18 15
19 /// Given a packageMap (see [PackageMapProvider]), check in each package's lib 16 /// Given a packageMap (see [PackageMapProvider]), check in each package's lib
20 /// directory for the existence of a `_sdkext` file. This file must contain a 17 /// directory for the existence of a `_sdkext` file. This file must contain a
21 /// JSON encoded map. Each key in the map is a `dart:` library name. Each value 18 /// JSON encoded map. Each key in the map is a `dart:` library name. Each value
22 /// is a path (relative to the directory containing `_sdkext`) to a dart script 19 /// is a path (relative to the directory containing `_sdkext`) to a dart script
23 /// for the given library. For example: 20 /// for the given library. For example:
24 /// { 21 /// {
25 /// "dart:sky": "../sdk_ext/dart_sky.dart" 22 /// "dart:sky": "../sdk_ext/dart_sky.dart"
26 /// } 23 /// }
27 /// 24 ///
28 /// If a key doesn't begin with `dart:` it is ignored. 25 /// If a key doesn't begin with `dart:` it is ignored.
29 class SdkExtUriResolver extends UriResolver { 26 class SdkExtUriResolver extends UriResolver {
30 static const String SDK_EXT_NAME = '_sdkext'; 27 static const String SDK_EXT_NAME = '_sdkext';
31 static const String DART_COLON_PREFIX = 'dart:'; 28 static const String DART_COLON_PREFIX = 'dart:';
32 29
33 final Map<String, String> _urlMappings = <String,String>{}; 30 final Map<String, String> _urlMappings = <String,String>{};
34 31
35 /// Construct a [SdkExtUriResolver] from a package map 32 /// Construct a [SdkExtUriResolver] from a package map
36 /// (see [PackageMapProvider]). 33 /// (see [PackageMapProvider]).
37 SdkExtUriResolver(Map<String, List<Folder>> packageMap) { 34 SdkExtUriResolver(Map<String, List<Folder>> packageMap) {
38 if (packageMap == null) { 35 if (packageMap == null) {
39 return; 36 return;
40 } 37 }
41 packageMap.forEach(_processPackage); 38 packageMap.forEach(_processPackage);
42 } 39 }
43 40
41 /// Number of sdk extensions.
42 int get length => _urlMappings.length;
43
44 /// Return the path mapping for [libName] or null if there is none.
45 String operator[](String libName) => _urlMappings[libName];
46
44 /// Programmatically add a new SDK extension given a JSON description 47 /// Programmatically add a new SDK extension given a JSON description
45 /// ([sdkExtJSON]) and a lib directory ([libDir]). 48 /// ([sdkExtJSON]) and a lib directory ([libDir]).
46 void addSdkExt(String sdkExtJSON, Folder libDir) { 49 void addSdkExt(String sdkExtJSON, Folder libDir) {
47 _processSdkExt(sdkExtJSON, libDir); 50 _processSdkExt(sdkExtJSON, libDir);
48 } 51 }
49 52
50 /// Return the path mapping for [libName] or null if there is none.
51 String operator[](String libName) => _urlMappings[libName];
52
53 /// Number of sdk extensions.
54 int get length => _urlMappings.length;
55
56 /// Resolve a 'part' statement inside an sdk extension.
57 Source _resolvePart(Uri libraryEntry, String partPath, Uri importUri) {
58 // Library part.
59 var directory = pathos.dirname(libraryEntry.path);
60 var partUri = new Uri.file(pathos.join(directory, partPath));
61 assert(partUri.isAbsolute);
62 JavaFile javaFile = new JavaFile.fromUri(partUri);
63 return new FileBasedSource(javaFile, importUri);
64 }
65
66 /// Resolve an import of an sdk extension.
67 Source _resolveEntry(Uri libraryEntry, Uri importUri) {
68 // Library entry.
69 JavaFile javaFile = new JavaFile.fromUri(libraryEntry);
70 return new FileBasedSource(javaFile, importUri);
71 }
72
73 /// Return the library name of [importUri].
74 String _libraryName(Uri importUri) {
75 var uri = importUri.toString();
76 int index = uri.indexOf('/');
77 if (index >= 0) {
78 return uri.substring(0, index);
79 }
80 return uri;
81 }
82
83 /// Return the part path of [importUri].
84 String _partPath(Uri importUri) {
85 var uri = importUri.toString();
86 int index = uri.indexOf('/');
87 if (index >= 0) {
88 return uri.substring(index + 1);
89 }
90 return null;
91 }
92
93 /// Returns true if [libraryName] is a registered sdk extension.
94 bool _registeredSdkExtension(String libraryName) {
95 return _urlMappings[libraryName] != null;
96 }
97
98 @override 53 @override
99 Source resolveAbsolute(Uri importUri) { 54 Source resolveAbsolute(Uri importUri) {
100 String libraryName = _libraryName(importUri); 55 String libraryName = _libraryName(importUri);
101 String partPath = _partPath(importUri); 56 String partPath = _partPath(importUri);
102 // Lookup library name in mappings. 57 // Lookup library name in mappings.
103 String mapping = _urlMappings[libraryName]; 58 String mapping = _urlMappings[libraryName];
104 if (mapping == null) { 59 if (mapping == null) {
105 // Not found. 60 // Not found.
106 return null; 61 return null;
107 } 62 }
(...skipping 13 matching lines...) Expand all
121 76
122 @override 77 @override
123 Uri restoreAbsolute(Source source) { 78 Uri restoreAbsolute(Source source) {
124 String libraryName = _libraryName(source.uri); 79 String libraryName = _libraryName(source.uri);
125 if (_registeredSdkExtension(libraryName)) { 80 if (_registeredSdkExtension(libraryName)) {
126 return source.uri; 81 return source.uri;
127 } 82 }
128 return null; 83 return null;
129 } 84 }
130 85
86 /// Return the library name of [importUri].
87 String _libraryName(Uri importUri) {
88 var uri = importUri.toString();
89 int index = uri.indexOf('/');
90 if (index >= 0) {
91 return uri.substring(0, index);
92 }
93 return uri;
94 }
95
96 /// Return the part path of [importUri].
97 String _partPath(Uri importUri) {
98 var uri = importUri.toString();
99 int index = uri.indexOf('/');
100 if (index >= 0) {
101 return uri.substring(index + 1);
102 }
103 return null;
104 }
105
131 /// Given a package [name] and a list of folders ([libDirs]), 106 /// Given a package [name] and a list of folders ([libDirs]),
132 /// add any found sdk extensions. 107 /// add any found sdk extensions.
133 void _processPackage(String name, List<Folder> libDirs) { 108 void _processPackage(String name, List<Folder> libDirs) {
134 for (var libDir in libDirs) { 109 for (var libDir in libDirs) {
135 var sdkExt = _readDotSdkExt(libDir); 110 var sdkExt = _readDotSdkExt(libDir);
136 if (sdkExt != null) { 111 if (sdkExt != null) {
137 _processSdkExt(sdkExt, libDir); 112 _processSdkExt(sdkExt, libDir);
138 } 113 }
139 } 114 }
140 } 115 }
141 116
142 /// Read the contents of [libDir]/[SDK_EXT_NAME] as a string.
143 /// Returns null if the file doesn't exist.
144 String _readDotSdkExt(Folder libDir) {
145 var file = libDir.getChild(SDK_EXT_NAME);
146 try {
147 return file.readAsStringSync();
148 } on FileSystemException catch (e) {
149 // File can't be read.
150 return null;
151 }
152 }
153
154 /// Given the JSON for an SDK extension ([sdkExtJSON]) and a folder 117 /// Given the JSON for an SDK extension ([sdkExtJSON]) and a folder
155 /// ([libDir]), setup the uri mapping. 118 /// ([libDir]), setup the uri mapping.
156 void _processSdkExt(String sdkExtJSON, Folder libDir) { 119 void _processSdkExt(String sdkExtJSON, Folder libDir) {
157 var sdkExt; 120 var sdkExt;
158 try { 121 try {
159 sdkExt = JSON.decode(sdkExtJSON); 122 sdkExt = JSON.decode(sdkExtJSON);
160 } catch (e) { 123 } catch (e) {
161 return; 124 return;
162 } 125 }
163 if ((sdkExt == null) || (sdkExt is! Map)) { 126 if ((sdkExt == null) || (sdkExt is! Map)) {
164 return; 127 return;
165 } 128 }
166 sdkExt.forEach((k, v) => _processSdkExtension(k, v, libDir)); 129 sdkExt.forEach((k, v) => _processSdkExtension(k, v, libDir));
167 } 130 }
168 131
169 /// Install the mapping from [name] to [libDir]/[file]. 132 /// Install the mapping from [name] to [libDir]/[file].
170 void _processSdkExtension(String name, String file, Folder libDir) { 133 void _processSdkExtension(String name, String file, Folder libDir) {
171 if (!name.startsWith(DART_COLON_PREFIX)) { 134 if (!name.startsWith(DART_COLON_PREFIX)) {
172 // SDK extensions must begin with 'dart:'. 135 // SDK extensions must begin with 'dart:'.
173 return; 136 return;
174 } 137 }
175 var key = name; 138 var key = name;
176 var value = libDir.canonicalizePath(file); 139 var value = libDir.canonicalizePath(file);
177 _urlMappings[key] = value; 140 _urlMappings[key] = value;
178 } 141 }
142
143 /// Read the contents of [libDir]/[SDK_EXT_NAME] as a string.
144 /// Returns null if the file doesn't exist.
145 String _readDotSdkExt(Folder libDir) {
146 var file = libDir.getChild(SDK_EXT_NAME);
147 try {
148 return file.readAsStringSync();
149 } on FileSystemException {
150 // File can't be read.
151 return null;
152 }
153 }
154
155 /// Returns true if [libraryName] is a registered sdk extension.
156 bool _registeredSdkExtension(String libraryName) {
157 return _urlMappings[libraryName] != null;
158 }
159
160 /// Resolve an import of an sdk extension.
161 Source _resolveEntry(Uri libraryEntry, Uri importUri) {
162 // Library entry.
163 JavaFile javaFile = new JavaFile.fromUri(libraryEntry);
164 return new FileBasedSource(javaFile, importUri);
165 }
166
167 /// Resolve a 'part' statement inside an sdk extension.
168 Source _resolvePart(Uri libraryEntry, String partPath, Uri importUri) {
169 // Library part.
170 var directory = pathos.dirname(libraryEntry.path);
171 var partUri = new Uri.file(pathos.join(directory, partPath));
172 assert(partUri.isAbsolute);
173 JavaFile javaFile = new JavaFile.fromUri(partUri);
174 return new FileBasedSource(javaFile, importUri);
175 }
179 } 176 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/generated/incremental_resolver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698