Chromium Code Reviews| Index: pkg/analysis_server/lib/src/plugin/plugin_locator.dart |
| diff --git a/pkg/analysis_server/lib/src/plugin/plugin_locator.dart b/pkg/analysis_server/lib/src/plugin/plugin_locator.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b1714c0c30c5f376cb4a4390fba2671dc5968339 |
| --- /dev/null |
| +++ b/pkg/analysis_server/lib/src/plugin/plugin_locator.dart |
| @@ -0,0 +1,86 @@ |
| +// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import 'package:analyzer/file_system/file_system.dart'; |
| +import 'package:yaml/yaml.dart'; |
| + |
| +/** |
| + * An object used to locate a plugin within a package. |
| + */ |
| +class PluginLocator { |
| + /** |
| + * The key used in the `pubspec.yaml` file to specify the location of the |
| + * analysis plugin. |
| + */ |
| + static const String analysisPluginKey = 'analysis_plugin'; |
| + |
| + /** |
| + * The name of the default plugin directory, located within the `tools` |
| + * directory. |
| + */ |
| + static const String defaultPluginFolderName = 'analysis_plugin'; |
| + |
| + /** |
| + * The name of the `pubspec.yaml` file. |
| + */ |
| + static const String pubspecFileName = 'pubspec.yaml'; |
| + |
| + /** |
| + * The name of the default plugin directory, located within the `tools` |
| + * directory. |
|
scheglov
2017/02/05 19:48:07
Update it?
Brian Wilkerson
2017/02/05 20:00:22
Done
|
| + */ |
| + static const String toolsFolderName = 'tools'; |
| + |
| + /** |
| + * The resource provider used to access the file system. |
| + */ |
| + final ResourceProvider resourceProvider; |
| + |
| + /** |
| + * Initialize a newly created plugin locator to use the given |
| + * [resourceProvider] to access the file system. |
| + */ |
| + PluginLocator(this.resourceProvider); |
| + |
| + /** |
| + * Given the root directory of a package (the [packageRoot]), return the path |
| + * to the plugin associated with the package, or `null` if there is no plugin |
| + * associated with the package. |
| + * |
| + * This will look first in the `pubspec.yaml` file in the package root for a |
| + * key indicating where the plugin is located. If such a key is not defined, |
| + * then it will fall back to a well known location within the package. |
| + * |
| + * This method does not validate the content of the plugin directory before |
| + * returning it. |
| + */ |
| + String findPlugin(String packageRoot) { |
| + Folder packageFolder = resourceProvider.getFolder(packageRoot); |
| + File pubspecFile = packageFolder.getChildAssumingFile(pubspecFileName); |
| + if (pubspecFile.exists) { |
| + try { |
| + YamlDocument document = loadYamlDocument(pubspecFile.readAsStringSync(), |
| + sourceUrl: pubspecFile.toUri()); |
| + YamlNode contents = document.contents; |
| + if (contents is YamlMap) { |
| + String pluginPath = contents[analysisPluginKey]; |
| + Folder pluginFolder = |
| + packageFolder.getChildAssumingFolder(pluginPath); |
| + if (pluginFolder.exists) { |
| + return pluginFolder.path; |
| + } |
| + } |
| + } catch (exception) { |
| + // If we can't read the file, or if it isn't valid YAML, then ignore it. |
| + } |
| + } |
| + Folder pluginFolder = packageFolder |
| + .getChildAssumingFolder(toolsFolderName) |
| + .getChildAssumingFolder(defaultPluginFolderName); |
| + if (pluginFolder.exists) { |
| + return pluginFolder.path; |
| + } |
| + return null; |
| + } |
| +} |