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

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 sdk.ext;
Brian Wilkerson 2015/07/07 19:59:12 nit: I think following the library naming conventi
Cutch 2015/07/07 20:33:47 Done.
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
17 // Given a packageMap (from a PackageMapProvider), check in each package's lib
Brian Wilkerson 2015/07/07 19:59:11 nit: The comments should be valid dartdoc comments
Cutch 2015/07/07 20:33:47 Done.
18 // directory for the existence of a `.sdkext` file. This file must contain a
19 // JSON encoded map. Each key in the map is a `dart:` library name. Each value
20 // is the filename for the given library. For example:
21 // {
22 // "dart:sky": "sdk_ext/dart_sky.dart"
23 // }
24 //
25 // If a key doesn't begin with `dart:` it is ignored.
26 class SdkExtUriResolver extends UriResolver {
27 static const String DOT_SDK_EXT_NAME = '.sdkext';
28 static const String DART_COLON_PREFIX = 'dart:';
29
30 final Map<String, String> _urlMappings = <String,String>{};
31
32 SdkExtUriResolver(Map<String, List<Folder>> packageMap) {
33 if (packageMap == null) {
34 return;
35 }
36 packageMap.forEach(_processPackage);
37 }
38
39 @override
40 Source resolveAbsolute(Uri uri) {
41 String mapping = _urlMappings[uri.toString()];
42 if (mapping == null) return null;
43
44 Uri fileUri = new Uri.file(mapping);
45 if (!fileUri.isAbsolute) return null;
46
47 JavaFile javaFile = new JavaFile.fromUri(fileUri);
48 return new FileBasedSource(javaFile);
Brian Wilkerson 2015/07/07 19:59:11 This needs to be "new FileBasedSource(javaFile, ur
Cutch 2015/07/07 20:33:48 Done.
49 }
50
51 // Given a package name and a list of folders, add any sdk extensions.
52 void _processPackage(String name, List<Folder> libDirs) {
53 for (var libDir in libDirs) {
54 var sdkExt = _readDotSdkExt(libDir);
55 if (sdkExt != null) {
56 _processSdkExt(sdkExt, libDir);
57 }
58 }
59 }
60
61 // Read the contents of $libDir/$DOT_SDK_EXT_NAME as a string.
62 // Returns null if the file doesn't exist.
63 String _readDotSdkExt(Folder libDir) {
64 var file = libDir.getChild(DOT_SDK_EXT_NAME);
65 try {
66 return file.readAsStringSync();
67 } on FileSystemException catch (e) {
68 // File can't be read.
69 return null;
70 }
71 }
72
73 // Given the JSON for an SDK extension and a folder, setup the uri mapping.
74 void _processSdkExt(String sdkExtJSON, Folder libDir) {
75 var sdkExt;
76 try {
77 sdkExt = JSON.decode(sdkExtJSON);
78 } catch (e) {
79 return;
80 }
81 if ((sdkExt == null) || (sdkExt is! Map)) {
82 return;
83 }
84 sdkExt.forEach((k, v) => _processSdkExtension(k, v, libDir));
85 }
86
87 void _processSdkExtension(String name, String file, Folder libDir) {
88 if (!name.startsWith(DART_COLON_PREFIX)) {
89 // SDK extensions must begin with 'dart:'.
90 return;
91 }
92 var key = name;
93 var value = libDir.canonicalizePath(file);
94 _urlMappings[key] = value;
95 }
96 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698