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

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
19 /// 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
21 /// 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
23 /// for the given library. For example:
24 /// {
25 /// "dart:sky": "../sdk_ext/dart_sky.dart"
26 /// }
27 ///
28 /// If a key doesn't begin with `dart:` it is ignored.
29 class SdkExtUriResolver extends UriResolver {
Brian Wilkerson 2015/07/07 22:10:18 The lack of an implementation of restoreAbsolute w
Cutch 2015/07/08 14:18:17 I've implemented this and added tests but I'll not
Brian Wilkerson 2015/07/08 14:48:28 It's used in 5 places in the analysis server. I'm
30 static const String DOT_SDK_EXT_NAME = '.sdkext';
31 static const String DART_COLON_PREFIX = 'dart:';
32
33 final Map<String, String> _urlMappings = <String,String>{};
34
35 /// Construct a [SdkExtUriResolver] from a package map
36 /// (see [PackageMapProvider]).
37 SdkExtUriResolver(Map<String, List<Folder>> packageMap) {
38 if (packageMap == null) {
39 return;
40 }
41 packageMap.forEach(_processPackage);
42 }
43
44 /// Programmatically add a new SDK extension given a JSON description
45 /// ([sdkExtJSON]) and a lib directory ([libDir]).
46 void addSdkExt(String sdkExtJSON, Folder libDir) {
47 _processSdkExt(sdkExtJSON, libDir);
48 }
49
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 @override
57 Source resolveAbsolute(Uri uri) {
58 String mapping = _urlMappings[uri.toString()];
59 if (mapping == null) return null;
60 Uri fileUri = new Uri.file(mapping);
61 if (!fileUri.isAbsolute) return null;
62
63 JavaFile javaFile = new JavaFile.fromUri(fileUri);
64 return new FileBasedSource(javaFile, uri);
65 }
66
67 /// Given a package [name] and a list of folders ([libDirs]),
68 /// add any found sdk extensions.
69 void _processPackage(String name, List<Folder> libDirs) {
70 for (var libDir in libDirs) {
71 var sdkExt = _readDotSdkExt(libDir);
72 if (sdkExt != null) {
73 _processSdkExt(sdkExt, libDir);
74 }
75 }
76 }
77
78 /// Read the contents of [libDir]/[DOT_SDK_EXT_NAME] as a string.
79 /// Returns null if the file doesn't exist.
80 String _readDotSdkExt(Folder libDir) {
81 var file = libDir.getChild(DOT_SDK_EXT_NAME);
82 try {
83 return file.readAsStringSync();
84 } on FileSystemException catch (e) {
85 // File can't be read.
86 return null;
87 }
88 }
89
90 /// Given the JSON for an SDK extension ([sdkExtJSON]) and a folder
91 /// ([libDir]), setup the uri mapping.
92 void _processSdkExt(String sdkExtJSON, Folder libDir) {
93 var sdkExt;
94 try {
95 sdkExt = JSON.decode(sdkExtJSON);
96 } catch (e) {
97 return;
98 }
99 if ((sdkExt == null) || (sdkExt is! Map)) {
100 return;
101 }
102 sdkExt.forEach((k, v) => _processSdkExtension(k, v, libDir));
103 }
104
105 /// Install the mapping from [name] to [libDir]/[file].
106 void _processSdkExtension(String name, String file, Folder libDir) {
107 if (!name.startsWith(DART_COLON_PREFIX)) {
108 // SDK extensions must begin with 'dart:'.
109 return;
110 }
111 var key = name;
112 var value = libDir.canonicalizePath(file);
113 _urlMappings[key] = value;
114 }
115 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698