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

Side by Side Diff: pkg/analysis_server/lib/src/plugin/plugin_locator.dart

Issue 2671773007: Add code to locate a plugin within a package (Closed)
Patch Set: Created 3 years, 10 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
« no previous file with comments | « no previous file | pkg/analysis_server/test/src/plugin/plugin_locator_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2017, 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 import 'package:analyzer/file_system/file_system.dart';
6 import 'package:yaml/yaml.dart';
7
8 /**
9 * An object used to locate a plugin within a package.
10 */
11 class PluginLocator {
12 /**
13 * The key used in the `pubspec.yaml` file to specify the location of the
14 * analysis plugin.
15 */
16 static const String analysisPluginKey = 'analysis_plugin';
17
18 /**
19 * The name of the default plugin directory, located within the `tools`
20 * directory.
21 */
22 static const String defaultPluginFolderName = 'analysis_plugin';
23
24 /**
25 * The name of the `pubspec.yaml` file.
26 */
27 static const String pubspecFileName = 'pubspec.yaml';
28
29 /**
30 * The name of the default plugin directory, located within the `tools`
31 * directory.
scheglov 2017/02/05 19:48:07 Update it?
Brian Wilkerson 2017/02/05 20:00:22 Done
32 */
33 static const String toolsFolderName = 'tools';
34
35 /**
36 * The resource provider used to access the file system.
37 */
38 final ResourceProvider resourceProvider;
39
40 /**
41 * Initialize a newly created plugin locator to use the given
42 * [resourceProvider] to access the file system.
43 */
44 PluginLocator(this.resourceProvider);
45
46 /**
47 * Given the root directory of a package (the [packageRoot]), return the path
48 * to the plugin associated with the package, or `null` if there is no plugin
49 * associated with the package.
50 *
51 * This will look first in the `pubspec.yaml` file in the package root for a
52 * key indicating where the plugin is located. If such a key is not defined,
53 * then it will fall back to a well known location within the package.
54 *
55 * This method does not validate the content of the plugin directory before
56 * returning it.
57 */
58 String findPlugin(String packageRoot) {
59 Folder packageFolder = resourceProvider.getFolder(packageRoot);
60 File pubspecFile = packageFolder.getChildAssumingFile(pubspecFileName);
61 if (pubspecFile.exists) {
62 try {
63 YamlDocument document = loadYamlDocument(pubspecFile.readAsStringSync(),
64 sourceUrl: pubspecFile.toUri());
65 YamlNode contents = document.contents;
66 if (contents is YamlMap) {
67 String pluginPath = contents[analysisPluginKey];
68 Folder pluginFolder =
69 packageFolder.getChildAssumingFolder(pluginPath);
70 if (pluginFolder.exists) {
71 return pluginFolder.path;
72 }
73 }
74 } catch (exception) {
75 // If we can't read the file, or if it isn't valid YAML, then ignore it.
76 }
77 }
78 Folder pluginFolder = packageFolder
79 .getChildAssumingFolder(toolsFolderName)
80 .getChildAssumingFolder(defaultPluginFolderName);
81 if (pluginFolder.exists) {
82 return pluginFolder.path;
83 }
84 return null;
85 }
86 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/test/src/plugin/plugin_locator_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698