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

Side by Side Diff: pkg/analyzer/lib/src/context/context_factory.dart

Issue 2043963004: Package map processing abstraction. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 6 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) 2016, 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 analyzer.src.context_factory;
6
7 import 'dart:convert';
8 import 'dart:core' hide Resource;
9
10 import 'package:analyzer/file_system/file_system.dart';
11 import 'package:analyzer/src/generated/sdk.dart';
12 import 'package:yaml/yaml.dart';
13 import 'package:analyzer/src/generated/engine.dart';
14 import 'dart:io' as io;
15
16 /// (Placeholder)
17 abstract class ContextFactory {
18 /// Create an analysis context for the given [source] directory or file, with
19 /// the given [defaultOptions].
20 AnalysisContext createContext(
21 io.FileSystemEntity source, AnalysisOptions defaultOptions);
22 }
23
24 /// Processes package maps, extracting SDK embedders and extenders, creating a
25 /// consolidated [libraryMap].
26 class PackageMapProcessor {
27 static const String _EMBEDDED_LIB_MAP_KEY = 'embedded_libs';
28 static const String _EMBEDDER_FILE_NAME = '_embedder.yaml';
29 static const String _SDK_EXT_NAME = '_sdkext';
30
31 /// Map of processed embedder libraries.
32 final LibraryMap embeddedLibraries = new LibraryMap();
33
34 /// Map of processed SDK extension libraries.
35 final LibraryMap extendedLibraries = new LibraryMap();
36
37 /// Combined map of processed libraries.
38 LibraryMap get libraryMap {
39 LibraryMap libraryMap = new LibraryMap();
40 for (String uri in embeddedLibraries.uris) {
41 libraryMap.setLibrary(uri, embeddedLibraries.getLibrary(uri));
42 }
43 // Extenders extend but do not override.
44 for (String uri in extendedLibraries.uris) {
Brian Wilkerson 2016/06/07 20:48:48 It would be more efficient to add the extensions t
pquitslund 2016/06/07 20:59:58 Done.
45 if (embeddedLibraries.getLibrary(uri) == null) {
46 libraryMap.setLibrary(uri, extendedLibraries.getLibrary(uri));
47 }
48 }
49 return libraryMap;
50 }
51
52 /// Create a processor for the given [packageMap].
53 PackageMapProcessor(Map<String, List<Folder>> packageMap) {
54 packageMap?.forEach(_processPackage);
55 }
56
57 /// Whether the package map contains an SDK embedder.
58 bool get hasEmbedder => embeddedLibraries.size() > 0;
59
60 /// Whether the package map contains an SDK extension.
61 bool get hasSdkExtension => extendedLibraries.size() > 0;
62
63 void _processEmbedderYaml(String embedderYaml, Folder libDir) {
64 try {
65 YamlNode map = loadYaml(embedderYaml);
66 if (map is YamlMap) {
67 YamlNode embedded_libs = map[_EMBEDDED_LIB_MAP_KEY];
68 if (embedded_libs is YamlMap) {
69 embedded_libs.forEach(
70 (k, v) => _processMapping(embeddedLibraries, k, v, libDir));
71 }
72 }
73 } catch (_) {
74 // Ignored.
75 }
76 }
77
78 void _processMapping(
79 LibraryMap libraryMap, String name, String file, Folder libDir) {
80 if (!_hasDartPrefix(name)) {
81 // SDK libraries must begin with 'dart:'.
82 return;
83 }
84 if (libraryMap.getLibrary(name) != null) {
85 // Libraries can't be redefined.
86 return;
87 }
88 String libPath = libDir.canonicalizePath(file);
89 SdkLibraryImpl library = new SdkLibraryImpl(name)..path = libPath;
90 libraryMap.setLibrary(name, library);
91 }
92
93 void _processPackage(String name, List<Folder> libDirs) {
94 for (Folder libDir in libDirs) {
95 String embedderYaml = _readEmbedderYaml(libDir);
96 if (embedderYaml != null) {
97 _processEmbedderYaml(embedderYaml, libDir);
98 }
99 String sdkExt = _readDotSdkExt(libDir);
100 if (sdkExt != null) {
101 _processSdkExt(sdkExt, libDir);
102 }
103 }
104 }
105
106 void _processSdkExt(String sdkExtJSON, Folder libDir) {
107 try {
108 var sdkExt = JSON.decode(sdkExtJSON);
109 if (sdkExt is Map) {
110 sdkExt.forEach(
111 (k, v) => _processMapping(extendedLibraries, k, v, libDir));
112 }
113 } catch (_) {
114 // Ignored.
115 }
116 }
117
118 static bool _hasDartPrefix(String uri) =>
119 uri.startsWith(DartSdk.DART_LIBRARY_PREFIX);
120
121 static String _readDotSdkExt(Folder libDir) =>
122 _safeRead(libDir.getChild(_SDK_EXT_NAME));
123
124 static String _readEmbedderYaml(Folder libDir) =>
125 _safeRead(libDir.getChild(_EMBEDDER_FILE_NAME));
126
127 static String _safeRead(Resource file) {
128 try {
129 if (file is File) {
130 return file.readAsStringSync();
131 }
132 } on FileSystemException {
133 // File can't be read.
134 }
135 return null;
136 }
137 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/generated/sdk.dart » ('j') | pkg/analyzer/test/src/context/context_factory_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698