11import 'dart:async' ;
2+ import 'dart:convert' ;
3+ import 'dart:io' ;
24
35import 'package:build/build.dart' ;
46import 'package:collection/collection.dart' ;
@@ -36,10 +38,7 @@ class ClientOptionsBuilder implements Builder {
3638 };
3739
3840 Future <void > generateClientOptions (BuildStep buildStep) async {
39- final (mode, flutter) = await buildStep.loadProjectMode (options, buildStep);
40- if (mode != 'static' && mode != 'server' ) {
41- return ;
42- }
41+ final (_, flutter) = await buildStep.loadProjectMode (options, buildStep);
4342
4443 final serverId = AssetId (
4544 buildStep.inputId.package,
@@ -122,51 +121,64 @@ class ClientOptionsBuilder implements Builder {
122121}
123122
124123Future <List <Plugin >> loadWebPlugins (BuildStep buildStep) async {
125- final pubspecId = AssetId (buildStep.inputId.package, 'pubspec.yaml' );
126- if (! await buildStep.canRead (pubspecId)) {
127- return [];
124+ final pluginsDependenciesId = AssetId (buildStep.inputId.package, '.flutter-plugins-dependencies' );
125+
126+ Future <String ?> readPluginsDependencies () async {
127+ if (await buildStep.canRead (pluginsDependenciesId)) {
128+ return buildStep.readAsString (pluginsDependenciesId);
129+ }
130+ final file = File ('.flutter-plugins-dependencies' );
131+ if (await file.exists ()) {
132+ return file.readAsString ();
133+ }
134+ return null ;
128135 }
129136
130- final pubspecYaml = loadYaml ( await buildStep. readAsString (pubspecId) );
137+ var content = await readPluginsDependencies ( );
131138
132- final isWorkspace = switch (pubspecYaml) {
133- {'resolution' : 'workspace' } => true ,
134- _ => false ,
135- };
136- final dependencies = switch (pubspecYaml) {
137- {'dependencies' : final Map <Object ?, Object ?> deps} => deps.keys.cast <String >().toSet (),
138- _ => < String > {},
139- };
139+ if (content == null ) {
140+ try {
141+ final result = await Process .run ('flutter' , ['packages' , 'get' ]);
142+ if (result.exitCode != 0 ) {
143+ log.warning ('Failed to run `flutter packages get` in order to find Flutter web plugins.\n ${result .stderr }' );
144+ }
145+ } catch (_) {
146+ return [];
147+ }
148+ content = await readPluginsDependencies ();
149+ }
140150
141- final packageConfig = await buildStep.packageConfig;
151+ if (content == null ) {
152+ return [];
153+ }
142154
143- final packages = {for (final p in packageConfig.packages) p.name: p};
155+ return _parseWebPlugins (content, buildStep);
156+ }
144157
145- final plugins = < String , Plugin > {};
146- // If we're in a workspace, only consider packages that are direct dependencies.
147- final toVisit = isWorkspace ? dependencies.toList () : packages.keys.toList ();
158+ Future <List <Plugin >> _parseWebPlugins (String content, BuildStep buildStep) async {
159+ final pluginsDependencies = jsonDecode (content);
148160
149- while (toVisit.isNotEmpty) {
150- final packageName = toVisit.removeLast ();
151- if (plugins.containsKey (packageName)) {
152- continue ;
153- }
154- final package = packages[packageName];
155- if (package == null ) {
156- continue ;
157- }
161+ if (pluginsDependencies case {'plugins' : {'web' : final List <Object ?> webPluginsDependencies}}) {
162+ final plugins = < Plugin > [];
158163
159- final plugin = await _loadPluginForPackage (package.name, package.root, buildStep, toVisit);
160- if (plugin != null ) {
161- plugins[packageName] = plugin;
164+ for (final webPluginDependency in webPluginsDependencies) {
165+ if (webPluginDependency case {'name' : final String name, 'path' : final String path, 'dev_dependency' : false }) {
166+ final plugin = await _loadPluginForPackage (name, Uri .parse (path), buildStep);
167+ if (plugin != null ) {
168+ plugins.add (plugin);
169+ }
170+ }
162171 }
163- }
164172
165- return plugins.values.toList ();
173+ return plugins;
174+ } else {
175+ log.warning ('Failed to find Flutter web plugins in .flutter-plugins-dependencies file.' );
176+ return [];
177+ }
166178}
167179
168- Future <Plugin ?> _loadPluginForPackage (String pluginName, Uri root, BuildStep buildStep, List < String > toVisit ) async {
169- final pubspecId = AssetId . resolve (root. resolve ( 'pubspec.yaml' ) );
180+ Future <Plugin ?> _loadPluginForPackage (String pluginName, Uri root, BuildStep buildStep) async {
181+ final pubspecId = AssetId (pluginName, 'pubspec.yaml' );
170182 Object ? pubspec;
171183 try {
172184 pubspec = loadYaml (await buildStep.readAsString (pubspecId));
@@ -180,7 +192,9 @@ Future<Plugin?> _loadPluginForPackage(String pluginName, Uri root, BuildStep bui
180192 'flutter' : {'plugin' : {'platforms' : {'web' : final YamlMap webPlatformYaml}}},
181193 }) {
182194 if (webPlatformYaml case {'default_package' : final String defaultPackageName}) {
183- toVisit.add (defaultPackageName);
195+ log.warning (
196+ 'Failed to read Flutter web plugin for $pluginName . Default package $defaultPackageName not resolved.' ,
197+ );
184198 return null ;
185199 }
186200
0 commit comments