@@ -13,11 +13,13 @@ import 'package:flutter_gen_core/generators/integrations/rive_integration.dart';
1313import 'package:flutter_gen_core/generators/integrations/svg_integration.dart' ;
1414import 'package:flutter_gen_core/settings/asset_type.dart' ;
1515import 'package:flutter_gen_core/settings/config.dart' ;
16+ import 'package:flutter_gen_core/settings/flavored_asset.dart' ;
1617import 'package:flutter_gen_core/settings/pubspec.dart' ;
1718import 'package:flutter_gen_core/utils/error.dart' ;
1819import 'package:flutter_gen_core/utils/string.dart' ;
1920import 'package:glob/glob.dart' ;
2021import 'package:path/path.dart' ;
22+ import 'package:yaml/yaml.dart' ;
2123
2224class AssetsGenConfig {
2325 AssetsGenConfig ._(
@@ -41,7 +43,7 @@ class AssetsGenConfig {
4143 final String rootPath;
4244 final String _packageName;
4345 final FlutterGen flutterGen;
44- final List <String > assets;
46+ final List <Object > assets;
4547 final List <Glob > exclude;
4648
4749 String get packageParameterLiteral =>
@@ -194,51 +196,89 @@ String? generatePackageNameForConfig(AssetsGenConfig config) {
194196 }
195197}
196198
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 (
199201 /// The absolute root path of the assets directory.
200202 String rootPath,
201203
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,
204207
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.
206210 List <Glob > excludes,
207211) {
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);
211244 if (FileSystemEntity .isDirectorySync (assetAbsolutePath)) {
212245 assetRelativePathList.addAll (Directory (assetAbsolutePath)
213246 .listSync ()
214247 .whereType <File >()
215- .map ((e) => relative (e.path, from: rootPath))
248+ .map (
249+ (e) => tempAsset.copyWith (path: relative (e.path, from: rootPath)),
250+ )
216251 .toList ());
217252 } else if (FileSystemEntity .isFileSync (assetAbsolutePath)) {
218- assetRelativePathList.add (relative (assetAbsolutePath, from: rootPath));
253+ assetRelativePathList.add (
254+ tempAsset.copyWith (path: relative (assetAbsolutePath, from: rootPath)),
255+ );
219256 }
220257 }
221258
222259 if (excludes.isEmpty) {
223260 return assetRelativePathList;
224261 }
225-
226262 return assetRelativePathList
227- .where ((file ) => ! excludes.any ((exclude) => exclude.matches (file )))
263+ .where ((asset ) => ! excludes.any ((exclude) => exclude.matches (asset.path )))
228264 .toList ();
229265}
230266
231267AssetType _constructAssetTree (
232- List <String > assetRelativePathList, String rootPath) {
268+ List <FlavoredAsset > assetRelativePathList,
269+ String rootPath,
270+ ) {
233271 // Relative path is the key
234272 final assetTypeMap = < String , AssetType > {
235- '.' : AssetType (rootPath: rootPath, path: '.' ),
273+ '.' : AssetType (rootPath: rootPath, path: '.' , flavors : {} ),
236274 };
237- for (final assetPath in assetRelativePathList) {
238- var path = assetPath ;
275+ for (final asset in assetRelativePathList) {
276+ String path = asset.path ;
239277 while (path != '.' ) {
240278 assetTypeMap.putIfAbsent (
241- path, () => AssetType (rootPath: rootPath, path: path));
279+ path,
280+ () => AssetType (rootPath: rootPath, path: path, flavors: asset.flavors),
281+ );
242282 path = dirname (path);
243283 }
244284 }
@@ -320,7 +360,8 @@ String _dotDelimiterStyleDefinition(
320360 final assetsStaticStatements = < _Statement > [];
321361
322362 final assetTypeQueue = ListQueue <AssetType >.from (
323- _constructAssetTree (assetRelativePathList, rootPath).children);
363+ _constructAssetTree (assetRelativePathList, rootPath).children,
364+ );
324365
325366 while (assetTypeQueue.isNotEmpty) {
326367 final assetType = assetTypeQueue.removeFirst ();
@@ -428,14 +469,20 @@ String _flatStyleDefinition(
428469 List <Integration > integrations,
429470 String Function (String ) style,
430471) {
431- final statements = _getAssetRelativePathList (
472+ final List < FlavoredAsset > paths = _getAssetRelativePathList (
432473 config.rootPath,
433474 config.assets,
434475 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+ )
439486 .mapToUniqueAssetType (style)
440487 .map (
441488 (e) => _createAssetTypeStatement (
0 commit comments