serveAvailableExtensions method

Future<void> serveAvailableExtensions(
  1. String? rootPath
)

Serves any available DevTools extensions for the given rootPath, where rootPath is the root for a Dart or Flutter project containing the .dart_tool/ directory.

This method first looks up the available extensions using package:extension_discovery, and the available extension's assets will be copied to the build/devtools_extensions directory that DevTools server is serving.

Implementation

Future<void> serveAvailableExtensions(String? rootPath) async {
  devtoolsExtensions.clear();

  if (rootPath != null) {
    late final List<Extension> extensions;
    try {
      extensions = await findExtensions(
        'devtools',
        packageConfig: Uri.parse(
          path.join(
            rootPath,
            '.dart_tool',
            'package_config.json',
          ),
        ),
      );
    } catch (e) {
      print('[ERROR] `findExtensions` failed: $e');
      extensions = <Extension>[];
    }
    for (final extension in extensions) {
      final config = extension.config;
      // This should be relative to the 'extension/devtools/' directory and
      // defaults to 'build';
      final relativeExtensionLocation =
          config['buildLocation'] as String? ?? 'build';

      final location = path.join(
        extension.rootUri.toFilePath(),
        extensionLocation,
        relativeExtensionLocation,
      );

      try {
        final extensionConfig = DevToolsExtensionConfig.parse({
          ...config,
          DevToolsExtensionConfig.pathKey: location,
        });
        devtoolsExtensions.add(extensionConfig);
      } on StateError catch (e) {
        print(e.message);
        continue;
      }
    }
  }

  _resetServedPluginsDir();
  await Future.wait([
    for (final extension in devtoolsExtensions)
      _moveToServedExtensionsDir(extension.name, extension.path),
  ]);
}