@@ -13,11 +13,13 @@ import 'package:flutter_gen_core/generators/integrations/rive_integration.dart';
13
13
import 'package:flutter_gen_core/generators/integrations/svg_integration.dart' ;
14
14
import 'package:flutter_gen_core/settings/asset_type.dart' ;
15
15
import 'package:flutter_gen_core/settings/config.dart' ;
16
+ import 'package:flutter_gen_core/settings/flavored_asset.dart' ;
16
17
import 'package:flutter_gen_core/settings/pubspec.dart' ;
17
18
import 'package:flutter_gen_core/utils/error.dart' ;
18
19
import 'package:flutter_gen_core/utils/string.dart' ;
19
20
import 'package:glob/glob.dart' ;
20
21
import 'package:path/path.dart' ;
22
+ import 'package:yaml/yaml.dart' ;
21
23
22
24
class AssetsGenConfig {
23
25
AssetsGenConfig ._(
@@ -41,7 +43,7 @@ class AssetsGenConfig {
41
43
final String rootPath;
42
44
final String _packageName;
43
45
final FlutterGen flutterGen;
44
- final List <String > assets;
46
+ final List <Object > assets;
45
47
final List <Glob > exclude;
46
48
47
49
String get packageParameterLiteral =>
@@ -194,51 +196,89 @@ String? generatePackageNameForConfig(AssetsGenConfig config) {
194
196
}
195
197
}
196
198
197
- /// Returns a list of all releative path assets that are to be considered.
198
- List <String > _getAssetRelativePathList (
199
+ /// Returns a list of all relative path assets that are to be considered.
200
+ List <FlavoredAsset > _getAssetRelativePathList (
199
201
/// The absolute root path of the assets directory.
200
202
String rootPath,
201
203
202
- /// List of assets as provided the `flutter` .`assets` section in the pubspec.yaml.
203
- List <String > assets,
204
+ /// List of assets as provided the `flutter -> assets`
205
+ /// section in the pubspec.yaml.
206
+ List <Object > assets,
204
207
205
- /// List of globs as provided the `flutter_gen` .`assets` .`exclude` section in the pubspec.yaml.
208
+ /// List of globs as provided the `flutter_gen -> assets -> exclude`
209
+ /// section in the pubspec.yaml.
206
210
List <Glob > excludes,
207
211
) {
208
- final assetRelativePathList = < String > [];
209
- for (final assetName in assets) {
210
- final assetAbsolutePath = join (rootPath, assetName);
212
+ // Normalize.
213
+ final normalizedAssets = < Object > {...assets.whereType <String >()};
214
+ final normalizingMap = < String , Set <String >> {};
215
+ // Resolve flavored assets.
216
+ for (final map in assets.whereType <YamlMap >()) {
217
+ final path = (map['path' ] as String ).trim ();
218
+ final flavors =
219
+ (map['flavors' ] as YamlList ? )? .toSet ().cast <String >() ?? < String > {};
220
+ if (normalizingMap.containsKey (path)) {
221
+ // https://github.com/flutter/flutter/blob/5187cab7bdd434ca74abb45895d17e9fa553678a/packages/flutter_tools/lib/src/asset.dart#L1137-L1139
222
+ throw StateError (
223
+ 'Multiple assets entries include the file "$path ", '
224
+ 'but they specify different lists of flavors.' ,
225
+ );
226
+ }
227
+ normalizingMap[path] = flavors;
228
+ }
229
+ for (final entry in normalizingMap.entries) {
230
+ normalizedAssets.add (
231
+ YamlMap .wrap ({'path' : entry.key, 'flavors' : entry.value}),
232
+ );
233
+ }
234
+
235
+ final assetRelativePathList = < FlavoredAsset > [];
236
+ for (final asset in normalizedAssets) {
237
+ final FlavoredAsset tempAsset;
238
+ if (asset is YamlMap ) {
239
+ tempAsset = FlavoredAsset (path: asset['path' ], flavors: asset['flavors' ]);
240
+ } else {
241
+ tempAsset = FlavoredAsset (path: (asset as String ).trim ());
242
+ }
243
+ final assetAbsolutePath = join (rootPath, tempAsset.path);
211
244
if (FileSystemEntity .isDirectorySync (assetAbsolutePath)) {
212
245
assetRelativePathList.addAll (Directory (assetAbsolutePath)
213
246
.listSync ()
214
247
.whereType <File >()
215
- .map ((e) => relative (e.path, from: rootPath))
248
+ .map (
249
+ (e) => tempAsset.copyWith (path: relative (e.path, from: rootPath)),
250
+ )
216
251
.toList ());
217
252
} else if (FileSystemEntity .isFileSync (assetAbsolutePath)) {
218
- assetRelativePathList.add (relative (assetAbsolutePath, from: rootPath));
253
+ assetRelativePathList.add (
254
+ tempAsset.copyWith (path: relative (assetAbsolutePath, from: rootPath)),
255
+ );
219
256
}
220
257
}
221
258
222
259
if (excludes.isEmpty) {
223
260
return assetRelativePathList;
224
261
}
225
-
226
262
return assetRelativePathList
227
- .where ((file ) => ! excludes.any ((exclude) => exclude.matches (file )))
263
+ .where ((asset ) => ! excludes.any ((exclude) => exclude.matches (asset.path )))
228
264
.toList ();
229
265
}
230
266
231
267
AssetType _constructAssetTree (
232
- List <String > assetRelativePathList, String rootPath) {
268
+ List <FlavoredAsset > assetRelativePathList,
269
+ String rootPath,
270
+ ) {
233
271
// Relative path is the key
234
272
final assetTypeMap = < String , AssetType > {
235
- '.' : AssetType (rootPath: rootPath, path: '.' ),
273
+ '.' : AssetType (rootPath: rootPath, path: '.' , flavors : {} ),
236
274
};
237
- for (final assetPath in assetRelativePathList) {
238
- var path = assetPath ;
275
+ for (final asset in assetRelativePathList) {
276
+ String path = asset.path ;
239
277
while (path != '.' ) {
240
278
assetTypeMap.putIfAbsent (
241
- path, () => AssetType (rootPath: rootPath, path: path));
279
+ path,
280
+ () => AssetType (rootPath: rootPath, path: path, flavors: asset.flavors),
281
+ );
242
282
path = dirname (path);
243
283
}
244
284
}
@@ -320,7 +360,8 @@ String _dotDelimiterStyleDefinition(
320
360
final assetsStaticStatements = < _Statement > [];
321
361
322
362
final assetTypeQueue = ListQueue <AssetType >.from (
323
- _constructAssetTree (assetRelativePathList, rootPath).children);
363
+ _constructAssetTree (assetRelativePathList, rootPath).children,
364
+ );
324
365
325
366
while (assetTypeQueue.isNotEmpty) {
326
367
final assetType = assetTypeQueue.removeFirst ();
@@ -428,14 +469,20 @@ String _flatStyleDefinition(
428
469
List <Integration > integrations,
429
470
String Function (String ) style,
430
471
) {
431
- final statements = _getAssetRelativePathList (
472
+ final List < FlavoredAsset > paths = _getAssetRelativePathList (
432
473
config.rootPath,
433
474
config.assets,
434
475
config.exclude,
435
- )
436
- .distinct ()
437
- .sorted ()
438
- .map ((assetPath) => AssetType (rootPath: config.rootPath, path: assetPath))
476
+ );
477
+ paths.sort (((a, b) => a.path.compareTo (b.path)));
478
+ final statements = paths
479
+ .map (
480
+ (assetPath) => AssetType (
481
+ rootPath: config.rootPath,
482
+ path: assetPath.path,
483
+ flavors: assetPath.flavors,
484
+ ),
485
+ )
439
486
.mapToUniqueAssetType (style)
440
487
.map (
441
488
(e) => _createAssetTypeStatement (
0 commit comments