OLD | NEW |
| (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 analyzer_cli.src.plugin.plugin_manager; | |
6 | |
7 import 'dart:io'; | |
8 | |
9 import 'package:analyzer/src/plugin/plugin_configuration.dart'; | |
10 import 'package:path/path.dart' as path; | |
11 | |
12 const _manifestFileName = 'plugins.yaml'; | |
13 | |
14 /// Given a local configuration (as defined in an analysis options file) and | |
15 /// information from a plugin manifest, return plugin info appropriate for | |
16 /// configuring this plugin. | |
17 PluginInfo combine(PluginInfo localConfig, PluginInfo manifestInfo) { | |
18 return new PluginInfo( | |
19 name: localConfig.name, | |
20 version: manifestInfo.version, | |
21 className: manifestInfo.className, | |
22 libraryUri: manifestInfo.libraryUri); | |
23 } | |
24 | |
25 /// Call-back to allow for the injection of manifest readers that do not need | |
26 /// to go to disk (for testing purposes). | |
27 typedef String ManifestReader(Uri uri); | |
28 | |
29 /// Wraps a [plugin] info object elaborated with any configuration information | |
30 /// extracted from an associated manifest and [status]. | |
31 class PluginDetails { | |
32 /// Plugin status. | |
33 final PluginStatus status; | |
34 | |
35 /// Plugin info. | |
36 final PluginInfo plugin; | |
37 | |
38 /// Wrap a [plugin] with [status] info. | |
39 PluginDetails(this.plugin) : status = PluginStatus.Applicable; | |
40 PluginDetails.notApplicable(this.plugin) | |
41 : status = PluginStatus.NotApplicable; | |
42 PluginDetails.notFound(this.plugin) : status = PluginStatus.NotFound; | |
43 } | |
44 | |
45 /// Manages plugin information derived from plugin manifests. | |
46 class PluginManager { | |
47 /// Mapping from package name to package location. | |
48 final Map<String, Uri> _packageMap; | |
49 | |
50 /// The package naming the app to host plugins. | |
51 final String hostPackage; | |
52 | |
53 /// Function to perform the reading of manifest URIs. (For testing.) | |
54 ManifestReader _manifestReader; | |
55 | |
56 /// Create a plugin manager with backing package map information. | |
57 PluginManager(this._packageMap, this.hostPackage, | |
58 [ManifestReader manifestReader]) { | |
59 _manifestReader = | |
60 manifestReader != null ? manifestReader : _findAndReadManifestAtUri; | |
61 } | |
62 | |
63 /// Find a plugin manifest describing the given [pluginPackage]. | |
64 PluginManifest findManifest(String pluginPackage) { | |
65 Uri uri = _packageMap[pluginPackage]; | |
66 String contents = _manifestReader(uri); | |
67 if (contents == null) { | |
68 return null; | |
69 } | |
70 return parsePluginManifestString(contents); | |
71 } | |
72 | |
73 /// Return [PluginDetails] derived from associated plugin manifests | |
74 /// corresponding to plugins specified in the given [config]. | |
75 Iterable<PluginDetails> getPluginDetails(PluginConfig config) => | |
76 config.plugins.map((PluginInfo localConfig) { | |
77 PluginManifest manifest = findManifest(localConfig.name); | |
78 return _getDetails(localConfig, manifest); | |
79 }); | |
80 | |
81 String _findAndReadManifestAtUri(Uri uri) { | |
82 File manifestFile = _findManifest(uri); | |
83 return manifestFile?.readAsStringSync(); | |
84 } | |
85 | |
86 File _findManifest(Uri uri) { | |
87 if (uri == null) { | |
88 return null; | |
89 } | |
90 | |
91 Directory directory = new Directory.fromUri(uri); | |
92 File file = new File(path.join(directory.path, _manifestFileName)); | |
93 | |
94 return file.existsSync() ? file : null; | |
95 } | |
96 | |
97 PluginDetails _getDetails(PluginInfo localConfig, PluginManifest manifest) { | |
98 if (manifest == null) { | |
99 return new PluginDetails.notFound(localConfig); | |
100 } | |
101 if (!manifest.contributesTo.contains(hostPackage)) { | |
102 return new PluginDetails.notApplicable(localConfig); | |
103 } | |
104 | |
105 return new PluginDetails(combine(localConfig, manifest.plugin)); | |
106 } | |
107 } | |
108 | |
109 /// Describes plugin status. | |
110 enum PluginStatus { Applicable, NotApplicable, NotFound } | |
OLD | NEW |