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

Side by Side Diff: pkg/analysis_server/lib/src/sdk_ext.dart

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

Powered by Google App Engine
This is Rietveld 408576698