@@ -49,10 +49,10 @@ class AssetsGenConfig {
49
49
flutterGen.assets.outputs.packageParameterEnabled ? _packageName : '' ;
50
50
}
51
51
52
- String generateAssets (
52
+ Future < String > generateAssets (
53
53
AssetsGenConfig config,
54
54
DartFormatter formatter,
55
- ) {
55
+ ) async {
56
56
if (config.assets.isEmpty) {
57
57
throw const InvalidSettingsException (
58
58
'The value of "flutter/assets:" is incorrect.' ,
@@ -157,11 +157,14 @@ String generateAssets(
157
157
158
158
final classesBuffer = StringBuffer ();
159
159
if (config.flutterGen.assets.outputs.isDotDelimiterStyle) {
160
- classesBuffer.writeln (_dotDelimiterStyleDefinition (config, integrations));
160
+ final definition = await _dotDelimiterStyleDefinition (config, integrations);
161
+ classesBuffer.writeln (definition);
161
162
} else if (config.flutterGen.assets.outputs.isSnakeCaseStyle) {
162
- classesBuffer.writeln (_snakeCaseStyleDefinition (config, integrations));
163
+ final definition = await _snakeCaseStyleDefinition (config, integrations);
164
+ classesBuffer.writeln (definition);
163
165
} else if (config.flutterGen.assets.outputs.isCamelCaseStyle) {
164
- classesBuffer.writeln (_camelCaseStyleDefinition (config, integrations));
166
+ final definition = await _camelCaseStyleDefinition (config, integrations);
167
+ classesBuffer.writeln (definition);
165
168
} else {
166
169
throw 'The value of "flutter_gen/assets/style." is incorrect.' ;
167
170
}
@@ -290,11 +293,11 @@ AssetType _constructAssetTree(
290
293
return assetTypeMap['.' ]! ;
291
294
}
292
295
293
- _Statement ? _createAssetTypeStatement (
296
+ Future < _Statement ?> _createAssetTypeStatement (
294
297
AssetsGenConfig config,
295
298
UniqueAssetType assetType,
296
299
List <Integration > integrations,
297
- ) {
300
+ ) async {
298
301
final childAssetAbsolutePath = join (config.rootPath, assetType.path);
299
302
if (FileSystemEntity .isDirectorySync (childAssetAbsolutePath)) {
300
303
final childClassName = '\$ ${assetType .path .camelCase ().capitalize ()}Gen' ;
@@ -308,9 +311,20 @@ _Statement? _createAssetTypeStatement(
308
311
needDartDoc: false ,
309
312
);
310
313
} else if (! assetType.isIgnoreFile) {
311
- final integration = integrations.firstWhereOrNull (
312
- (element) => element.isSupport (assetType),
313
- );
314
+ Integration ? integration;
315
+ for (final element in integrations) {
316
+ final call = element.isSupport (assetType);
317
+ final bool isSupport;
318
+ if (call is Future <bool >) {
319
+ isSupport = await call;
320
+ } else {
321
+ isSupport = call;
322
+ }
323
+ if (isSupport) {
324
+ integration = element;
325
+ break ;
326
+ }
327
+ }
314
328
if (integration == null ) {
315
329
var assetKey = assetType.posixStylePath;
316
330
if (config.flutterGen.assets.outputs.packageParameterEnabled) {
@@ -342,10 +356,10 @@ _Statement? _createAssetTypeStatement(
342
356
}
343
357
344
358
/// Generate style like Assets.foo.bar
345
- String _dotDelimiterStyleDefinition (
359
+ Future < String > _dotDelimiterStyleDefinition (
346
360
AssetsGenConfig config,
347
361
List <Integration > integrations,
348
- ) {
362
+ ) async {
349
363
final rootPath = Directory (config.rootPath).absolute.uri.toFilePath ();
350
364
final buffer = StringBuffer ();
351
365
final className = config.flutterGen.assets.outputs.className;
@@ -374,29 +388,29 @@ String _dotDelimiterStyleDefinition(
374
388
File (assetPath).parent.absolute.uri.toFilePath () == rootPath;
375
389
// Handles directories, and explicitly handles root path assets.
376
390
if (isDirectory || isRootAsset) {
377
- final statements = assetType.children
378
- .mapToUniqueAssetType (camelCase, justBasename: true )
379
- .map (
380
- (e) => _createAssetTypeStatement (
381
- config,
382
- e,
383
- integrations,
391
+ final List <_Statement ?> results = await Future .wait (
392
+ assetType.children
393
+ .mapToUniqueAssetType (camelCase, justBasename: true )
394
+ .map (
395
+ (e) => _createAssetTypeStatement (
396
+ config,
397
+ e,
398
+ integrations,
399
+ ),
384
400
),
385
- )
386
- .whereType <_Statement >()
387
- .toList ();
401
+ );
402
+ final statements = results.whereType <_Statement >().toList ();
388
403
389
404
if (assetType.isDefaultAssetsDirectory) {
390
405
assetsStaticStatements.addAll (statements);
391
406
} else if (! isDirectory && isRootAsset) {
392
407
// Creates explicit statement.
393
- assetsStaticStatements.add (
394
- _createAssetTypeStatement (
395
- config,
396
- UniqueAssetType (assetType: assetType, style: camelCase),
397
- integrations,
398
- )! ,
408
+ final statement = await _createAssetTypeStatement (
409
+ config,
410
+ UniqueAssetType (assetType: assetType, style: camelCase),
411
+ integrations,
399
412
);
413
+ assetsStaticStatements.add (statement! );
400
414
} else {
401
415
final className = '\$ ${assetType .path .camelCase ().capitalize ()}Gen' ;
402
416
buffer.writeln (
@@ -437,59 +451,60 @@ String _dotDelimiterStyleDefinition(
437
451
return buffer.toString ();
438
452
}
439
453
440
- /// Generate style like Assets.fooBar
441
- String _camelCaseStyleDefinition (
454
+ /// Generate style like Assets.foo_bar
455
+ Future < String > _snakeCaseStyleDefinition (
442
456
AssetsGenConfig config,
443
457
List <Integration > integrations,
444
458
) {
445
459
return _flatStyleDefinition (
446
460
config,
447
461
integrations,
448
- camelCase ,
462
+ snakeCase ,
449
463
);
450
464
}
451
465
452
- /// Generate style like Assets.foo_bar
453
- String _snakeCaseStyleDefinition (
466
+ /// Generate style like Assets.fooBar
467
+ Future < String > _camelCaseStyleDefinition (
454
468
AssetsGenConfig config,
455
469
List <Integration > integrations,
456
470
) {
457
471
return _flatStyleDefinition (
458
472
config,
459
473
integrations,
460
- snakeCase ,
474
+ camelCase ,
461
475
);
462
476
}
463
477
464
- String _flatStyleDefinition (
478
+ Future < String > _flatStyleDefinition (
465
479
AssetsGenConfig config,
466
480
List <Integration > integrations,
467
481
String Function (String ) style,
468
- ) {
482
+ ) async {
469
483
final List <FlavoredAsset > paths = _getAssetRelativePathList (
470
484
config.rootPath,
471
485
config.assets,
472
486
config.exclude,
473
487
);
474
488
paths.sort (((a, b) => a.path.compareTo (b.path)));
475
- final statements = paths
476
- .map (
477
- (assetPath) => AssetType (
478
- rootPath: config.rootPath,
479
- path: assetPath.path,
480
- flavors: assetPath.flavors,
481
- ),
482
- )
483
- .mapToUniqueAssetType (style)
484
- .map (
485
- (e) => _createAssetTypeStatement (
486
- config,
487
- e,
488
- integrations,
489
+ final List <_Statement ?> results = await Future .wait (
490
+ paths
491
+ .map (
492
+ (assetPath) => AssetType (
493
+ rootPath: config.rootPath,
494
+ path: assetPath.path,
495
+ flavors: assetPath.flavors,
496
+ ),
497
+ )
498
+ .mapToUniqueAssetType (style)
499
+ .map (
500
+ (e) => _createAssetTypeStatement (
501
+ config,
502
+ e,
503
+ integrations,
504
+ ),
489
505
),
490
- )
491
- .whereType <_Statement >()
492
- .toList ();
506
+ );
507
+ final statements = results.whereType <_Statement >().toList ();
493
508
final className = config.flutterGen.assets.outputs.className;
494
509
final String ? packageName = generatePackageNameForConfig (config);
495
510
return _flatStyleAssetsClassDefinition (className, statements, packageName);
0 commit comments