@@ -106,11 +106,15 @@ class PluginException implements Exception {
106106 String toString () => message;
107107}
108108
109+ /// The necessary files that define an analyzer plugin on disk.
109110class PluginFiles {
111+ /// The plugin entry point.
110112 final File execution;
111- final File packages;
112113
113- PluginFiles (this .execution, this .packages);
114+ /// The plugin package config file.
115+ final File packageConfig;
116+
117+ PluginFiles (this .execution, this .packageConfig);
114118}
115119
116120/// Information about a single plugin.
@@ -314,9 +318,11 @@ class PluginManager {
314318 /// Stream emitting an event when known [plugins] change.
315319 Stream <void > get pluginsChanged => _pluginsChanged.stream;
316320
317- /// Add the plugin with the given [path] to the list of plugins that should be
318- /// used when analyzing code for the given [contextRoot] . If the plugin had
319- /// not yet been started, then it will be started by this method.
321+ /// Adds the plugin with the given [path] to the list of plugins that should
322+ /// be used when analyzing code for the given [contextRoot] .
323+ ///
324+ /// If the plugin had not yet been started, then it will be started by this
325+ /// method.
320326 Future <void > addPluginToContextRoot (
321327 analyzer.ContextRoot contextRoot, String path) async {
322328 var plugin = _pluginMap[path];
@@ -336,7 +342,7 @@ class PluginManager {
336342 plugin = DiscoveredPluginInfo (
337343 path,
338344 pluginFiles.execution.path,
339- pluginFiles.packages .path,
345+ pluginFiles.packageConfig .path,
340346 notificationManager,
341347 instrumentationService);
342348 _pluginMap[path] = plugin;
@@ -425,8 +431,9 @@ class PluginManager {
425431 return responses;
426432 }
427433
428- /// Return the files associated with the plugin at the given [pluginPath] .
429- /// Throw a [PluginException] if there is a problem that prevents the plugin
434+ /// Returns the files associated with the plugin at the given [pluginPath] .
435+ ///
436+ /// Throws a [PluginException] if there is a problem that prevents the plugin
430437 /// from being executing.
431438 @visibleForTesting
432439 PluginFiles filesFor (String pluginPath) {
@@ -443,12 +450,11 @@ class PluginManager {
443450 // there is exactly one version of each package.
444451 return _computeFiles (pluginFolder, workspace: workspace);
445452 }
446- //
453+
447454 // Copy the plugin directory to a unique subdirectory of the plugin
448455 // manager's state location. The subdirectory's name is selected such that
449- // it will be invariant across sessions, reducing the number of times the
450- // plugin will need to be copied and pub will need to be run.
451- //
456+ // it will be invariant across sessions, reducing the number of times we
457+ // copy the plugin contents, and the number of times we run `pub`.
452458 var stateFolder = resourceProvider.getStateLocation ('.plugin_manager' );
453459 if (stateFolder == null ) {
454460 throw PluginException ('No state location, so plugin could not be copied' );
@@ -598,56 +604,56 @@ class PluginManager {
598604 }));
599605 }
600606
601- /// Compute the files to be returned by the enclosing method given that the
602- /// plugin should exist in the given [pluginFolder] .
607+ /// Computes the plugin files, given that the plugin should exist in
608+ /// [pluginFolder] .
603609 ///
604- /// Runs pub if [pubCommand] is provided and not null.
610+ /// Runs ` pub` if [pubCommand] is not ` null` .
605611 PluginFiles _computeFiles (Folder pluginFolder,
606612 {String ? pubCommand, Workspace ? workspace}) {
607613 var pluginFile = pluginFolder
608614 .getChildAssumingFolder ('bin' )
609615 .getChildAssumingFile ('plugin.dart' );
610616 if (! pluginFile.exists) {
611- throw PluginException (' File " ${pluginFile .path }" does not exist.' );
617+ throw PluginException (" File ' ${pluginFile .path }' does not exist." );
612618 }
613- String ? reason;
614- File ? packagesFile = pluginFolder
619+ File ? packageConfigFile = pluginFolder
615620 .getChildAssumingFolder (file_paths.dotDartTool)
616621 .getChildAssumingFile (file_paths.packageConfigJson);
622+
617623 if (pubCommand != null ) {
618624 var result = _runPubCommand (pubCommand, pluginFolder);
625+ String ? exceptionReason;
619626 if (result.exitCode != 0 ) {
620627 var buffer = StringBuffer ();
621628 buffer.writeln ('Failed to run pub $pubCommand ' );
622629 buffer.writeln (' pluginFolder = ${pluginFolder .path }' );
623630 buffer.writeln (' exitCode = ${result .exitCode }' );
624631 buffer.writeln (' stdout = ${result .stdout }' );
625632 buffer.writeln (' stderr = ${result .stderr }' );
626- reason = buffer.toString ();
627- instrumentationService.logError (reason);
628- }
629- if (! packagesFile.exists) {
630- reason ?? = 'File "${packagesFile .path }" does not exist.' ;
631- packagesFile = null ;
633+ exceptionReason = buffer.toString ();
634+ instrumentationService.logError (exceptionReason);
632635 }
633- } else if (! packagesFile.exists) {
634- if (workspace != null ) {
635- packagesFile =
636- _createPackagesFile (pluginFolder, workspace.packageUriResolver);
637- if (packagesFile == null ) {
638- var name = file_paths.packageConfigJson;
639- reason = 'Could not create $name file in workspace $workspace .' ;
640- }
641- } else {
642- reason = 'Could not create "${packagesFile .path }".' ;
643- packagesFile = null ;
636+ if (! packageConfigFile.exists) {
637+ exceptionReason ?? = 'File "${packageConfigFile .path }" does not exist.' ;
638+ throw PluginException (exceptionReason);
644639 }
640+ return PluginFiles (pluginFile, packageConfigFile);
645641 }
646- if (packagesFile == null ) {
647- reason ?? = 'Could not create packages file for an unknown reason.' ;
648- throw PluginException (reason);
642+
643+ if (! packageConfigFile.exists) {
644+ if (workspace == null ) {
645+ throw PluginException ('Could not create "${packageConfigFile .path }".' );
646+ }
647+
648+ packageConfigFile =
649+ _createPackageConfigFile (pluginFolder, workspace.packageUriResolver);
650+ if (packageConfigFile == null ) {
651+ throw PluginException (
652+ "Could not create the '${file_paths .packageConfigJson }' file in "
653+ "the workspace at '$workspace '." );
654+ }
649655 }
650- return PluginFiles (pluginFile, packagesFile );
656+ return PluginFiles (pluginFile, packageConfigFile );
651657 }
652658
653659 WatchEventType _convertChangeType (watcher.ChangeType type) {
@@ -663,17 +669,18 @@ class PluginManager {
663669 return WatchEvent (_convertChangeType (watchEvent.type), watchEvent.path);
664670 }
665671
666- /// Return a temporary `package_config.json` file that is appropriate for
667- /// the plugin in the given [pluginFolder] . The [packageUriResolver] is
668- /// used to determine the location of the packages that need to be included
669- /// in the packages file.
670- File ? _createPackagesFile (
672+ /// Returns a temporary `package_config.json` file that is appropriate for
673+ /// the plugin in the given [pluginFolder] .
674+ ///
675+ /// The [packageUriResolver] is used to determine the location of the
676+ /// packages that need to be included in the package config file.
677+ File ? _createPackageConfigFile (
671678 Folder pluginFolder, UriResolver packageUriResolver) {
672679 var pluginPath = pluginFolder.path;
673680 var stateFolder = resourceProvider.getStateLocation ('.plugin_manager' )! ;
674681 var stateName = '${_uniqueDirectoryName (pluginPath )}.packages' ;
675- var packagesFile = stateFolder.getChildAssumingFile (stateName);
676- if (! packagesFile .exists) {
682+ var packageConfigFile = stateFolder.getChildAssumingFile (stateName);
683+ if (! packageConfigFile .exists) {
677684 var pluginPubspec =
678685 pluginFolder.getChildAssumingFile (file_paths.pubspecYaml);
679686 if (! pluginPubspec.exists) {
@@ -723,20 +730,20 @@ class PluginManager {
723730 rootPath: package.root.path,
724731 );
725732 }
726- packagesFile .writeAsStringSync (
733+ packageConfigFile .writeAsStringSync (
727734 packageConfigBuilder.toContent (
728735 toUriStr: (path) {
729736 return resourceProvider.pathContext.toUri (path).toString ();
730737 },
731738 ),
732739 );
733740 } catch (exception) {
734- // If we are not able to produce a .packages file, return null so that
735- // callers will not try to load the plugin.
741+ // If we are not able to produce a package config file, return ` null` so
742+ // that callers will not try to load the plugin.
736743 return null ;
737744 }
738745 }
739- return packagesFile ;
746+ return packageConfigFile ;
740747 }
741748
742749 void _notifyPluginsChanged () => _pluginsChanged.add (null );
@@ -781,7 +788,7 @@ class PluginManager {
781788 return result;
782789 }
783790
784- /// Return a hex-encoded MD5 signature of the given file [path] .
791+ /// Returns a hex-encoded MD5 signature of the given file [path] .
785792 String _uniqueDirectoryName (String path) {
786793 var bytes = md5.convert (path.codeUnits).bytes;
787794 return hex.encode (bytes);
0 commit comments