@@ -2,19 +2,24 @@ import 'dart:collection';
22import 'dart:io' ;
33
44import 'package:dart_style/dart_style.dart' ;
5+ import 'package:dartx/dartx.dart' ;
56import 'package:path/path.dart' ;
67
78import '../settings/asset_type.dart' ;
89import '../settings/flutter.dart' ;
910import '../settings/flutter_gen.dart' ;
10- import '../utils/camel_case.dart' ;
1111import '../utils/error.dart' ;
12+ import '../utils/string.dart' ;
1213import 'generator_helper.dart' ;
1314import 'integrations/integration.dart' ;
1415import 'integrations/svg_integration.dart' ;
1516
16- String generateAssets (File pubspecFile, DartFormatter formatter,
17- FlutterGen flutterGen, FlutterAssets assets) {
17+ String generateAssets (
18+ File pubspecFile,
19+ DartFormatter formatter,
20+ FlutterGen flutterGen,
21+ FlutterAssets assets,
22+ ) {
1823 if (assets == null || ! assets.hasAssets) {
1924 throw InvalidSettingsException (
2025 'The value of "flutter/assets:" is incorrect.' );
@@ -23,49 +28,28 @@ String generateAssets(File pubspecFile, DartFormatter formatter,
2328 final importsBuffer = StringBuffer ();
2429 final classesBuffer = StringBuffer ();
2530
26- final assetRelativePathList = _getAssetRelativePathList (pubspecFile, assets);
27- final assetTypeQueue = ListQueue <AssetType >.from (
28- _constructAssetTree (assetRelativePathList).children);
29- final assetsStaticStatements = < _Statement > [];
30-
3131 final integrations = < Integration > [];
3232 if (flutterGen != null &&
3333 flutterGen.hasIntegrations &&
3434 flutterGen.integrations.hasFlutterSvg) {
3535 integrations.add (SvgIntegration ());
3636 }
3737
38- while (assetTypeQueue.isNotEmpty) {
39- final assetType = assetTypeQueue.removeFirst ();
40- final assetAbsolutePath = join (pubspecFile.parent.path, assetType.path);
41-
42- if (FileSystemEntity .isDirectorySync (assetAbsolutePath)) {
43- final statements = _createDirectoryClassGenStatements (
44- pubspecFile, assetType, integrations);
45-
46- if (assetType.isDefaultAssetsDirectory) {
47- assetsStaticStatements.addAll (statements);
48- } else {
49- final className = '\$ ${assetType .path .camelCase ().capitalize ()}Gen' ;
50- classesBuffer
51- .writeln (_directoryClassGenDefinition (className, statements));
52- // Add this directory reference to Assets class
53- // if we are not under the default asset folder
54- if (dirname (assetType.path) == '.' ) {
55- assetsStaticStatements.add (_Statement (
56- type: className,
57- name: assetType.baseName.camelCase (),
58- value: '$className \(\) ' ,
59- isConstConstructor: true ,
60- ));
61- }
62- }
63-
64- assetTypeQueue.addAll (assetType.children);
65- }
38+ if (flutterGen == null ||
39+ ! flutterGen.hasAssets ||
40+ flutterGen.assets.isDefaultStyle) {
41+ classesBuffer.writeln (
42+ _dotDelimiterStyleDefinition (pubspecFile, assets, integrations));
43+ } else if (flutterGen.assets.isSnakeCaseStyle) {
44+ classesBuffer
45+ .writeln (_snakeCaseStyleDefinition (pubspecFile, assets, integrations));
46+ } else if (flutterGen.assets.isCamelCaseStyle) {
47+ classesBuffer
48+ .writeln (_camelCaseStyleDefinition (pubspecFile, assets, integrations));
49+ } else {
50+ throw 'The value of "flutter_gen/assets/style." is incorrect.' ;
6651 }
6752
68- classesBuffer.writeln (_assetsClassDefinition (assetsStaticStatements));
6953 classesBuffer.writeln (_assetGenImageClassDefinition);
7054
7155 final imports = < String > {'package:flutter/widgets.dart' };
@@ -130,55 +114,160 @@ AssetType _constructAssetTree(List<String> assetRelativePathList) {
130114 return assetTypeMap['.' ];
131115}
132116
133- List <_Statement > _createDirectoryClassGenStatements (
134- File pubspecFile, AssetType assetType, List <Integration > integrations) {
135- final statements = assetType.children
136- .map ((child) {
137- final childAssetAbsolutePath =
138- join (pubspecFile.parent.path, child.path);
139- _Statement statement;
140- if (child.isSupportedImage) {
141- statement = _Statement (
142- type: 'AssetGenImage' ,
143- name: child.baseName.camelCase (),
144- value: 'AssetGenImage\(\' ${posixStyle (child .path )}\'\) ' ,
145- isConstConstructor: true ,
146- );
147- } else if (FileSystemEntity .isDirectorySync (childAssetAbsolutePath)) {
148- final childClassName = '\$ ${child .path .camelCase ().capitalize ()}Gen' ;
149- statement = _Statement (
150- type: childClassName,
151- name: child.baseName.camelCase (),
152- value: '$childClassName \(\) ' ,
117+ _Statement _createAssetTypeStatement (
118+ File pubspecFile,
119+ AssetType assetType,
120+ List <Integration > integrations,
121+ String Function (AssetType ) createName,
122+ ) {
123+ final childAssetAbsolutePath = join (pubspecFile.parent.path, assetType.path);
124+ _Statement statement;
125+ if (assetType.isSupportedImage) {
126+ statement = _Statement (
127+ type: 'AssetGenImage' ,
128+ name: createName (assetType),
129+ value: 'AssetGenImage\(\' ${posixStyle (assetType .path )}\'\) ' ,
130+ isConstConstructor: true ,
131+ );
132+ } else if (FileSystemEntity .isDirectorySync (childAssetAbsolutePath)) {
133+ final childClassName = '\$ ${assetType .path .camelCase ().capitalize ()}Gen' ;
134+ statement = _Statement (
135+ type: childClassName,
136+ name: createName (assetType),
137+ value: '$childClassName \(\) ' ,
138+ isConstConstructor: true ,
139+ );
140+ } else if (! assetType.isUnKnownMime) {
141+ final integration = integrations.firstWhere (
142+ (element) => element.mime == assetType.mime,
143+ orElse: () => null ,
144+ );
145+ if (integration == null ) {
146+ statement = _Statement (
147+ type: 'String' ,
148+ name: createName (assetType),
149+ value: '\' ${posixStyle (assetType .path )}\' ' ,
150+ isConstConstructor: false ,
151+ );
152+ } else {
153+ integration.isEnabled = true ;
154+ statement = _Statement (
155+ type: integration.className,
156+ name: createName (assetType),
157+ value: integration.classInstantiate (posixStyle (assetType.path)),
158+ isConstConstructor: integration.isConstConstructor,
159+ );
160+ }
161+ }
162+ return statement;
163+ }
164+
165+ /// Generate style like Assets.foo.bar
166+ String _dotDelimiterStyleDefinition (
167+ File pubspecFile,
168+ FlutterAssets assets,
169+ List <Integration > integrations,
170+ ) {
171+ final buffer = StringBuffer ();
172+ final assetRelativePathList = _getAssetRelativePathList (pubspecFile, assets);
173+ final assetsStaticStatements = < _Statement > [];
174+
175+ final assetTypeQueue = ListQueue <AssetType >.from (
176+ _constructAssetTree (assetRelativePathList).children);
177+
178+ while (assetTypeQueue.isNotEmpty) {
179+ final assetType = assetTypeQueue.removeFirst ();
180+ final assetAbsolutePath = join (pubspecFile.parent.path, assetType.path);
181+
182+ if (FileSystemEntity .isDirectorySync (assetAbsolutePath)) {
183+ final statements = assetType.children
184+ .map (
185+ (child) => _createAssetTypeStatement (
186+ pubspecFile,
187+ child,
188+ integrations,
189+ (element) => element.baseName.camelCase (),
190+ ),
191+ )
192+ .whereType <_Statement >()
193+ .toList ();
194+
195+ if (assetType.isDefaultAssetsDirectory) {
196+ assetsStaticStatements.addAll (statements);
197+ } else {
198+ final className = '\$ ${assetType .path .camelCase ().capitalize ()}Gen' ;
199+ buffer.writeln (_directoryClassGenDefinition (className, statements));
200+ // Add this directory reference to Assets class
201+ // if we are not under the default asset folder
202+ if (dirname (assetType.path) == '.' ) {
203+ assetsStaticStatements.add (_Statement (
204+ type: className,
205+ name: assetType.baseName.camelCase (),
206+ value: '$className \(\) ' ,
153207 isConstConstructor: true ,
154- );
155- } else if (! child.isUnKnownMime) {
156- final integration = integrations.firstWhere (
157- (element) => element.mime == child.mime,
158- orElse: () => null ,
159- );
160- if (integration == null ) {
161- statement ?? = _Statement (
162- type: 'String' ,
163- name: child.baseName.camelCase (),
164- value: '\' ${posixStyle (child .path )}\' ' ,
165- isConstConstructor: false ,
166- );
167- } else {
168- integration.isEnabled = true ;
169- statement = _Statement (
170- type: integration.className,
171- name: child.baseName.camelCase (),
172- value: integration.classInstantiate (posixStyle (child.path)),
173- isConstConstructor: integration.isConstConstructor,
174- );
175- }
208+ ));
176209 }
177- return statement;
178- })
210+ }
211+
212+ assetTypeQueue.addAll (assetType.children);
213+ }
214+ }
215+ buffer.writeln (_assetsClassDefinition (assetsStaticStatements));
216+ return buffer.toString ();
217+ }
218+
219+ /// Generate style like Assets.fooBar
220+ String _camelCaseStyleDefinition (
221+ File pubspecFile,
222+ FlutterAssets assets,
223+ List <Integration > integrations,
224+ ) {
225+ return _flatStyleDefinition (
226+ pubspecFile,
227+ assets,
228+ integrations,
229+ (assetType) => withoutExtension (assetType.path)
230+ .replaceFirst (RegExp (r'asset(s)?' ), '' )
231+ .camelCase (),
232+ );
233+ }
234+
235+ /// Generate style like Assets.foo_bar
236+ String _snakeCaseStyleDefinition (
237+ File pubspecFile,
238+ FlutterAssets assets,
239+ List <Integration > integrations,
240+ ) {
241+ return _flatStyleDefinition (
242+ pubspecFile,
243+ assets,
244+ integrations,
245+ (assetType) => withoutExtension (assetType.path)
246+ .replaceFirst (RegExp (r'asset(s)?' ), '' )
247+ .snakeCase (),
248+ );
249+ }
250+
251+ String _flatStyleDefinition (
252+ File pubspecFile,
253+ FlutterAssets assets,
254+ List <Integration > integrations,
255+ String Function (AssetType ) createName,
256+ ) {
257+ final statements = _getAssetRelativePathList (pubspecFile, assets)
258+ .distinct ()
259+ .sorted ()
260+ .map (
261+ (relativePath) => _createAssetTypeStatement (
262+ pubspecFile,
263+ AssetType (relativePath),
264+ integrations,
265+ createName,
266+ ),
267+ )
179268 .whereType <_Statement >()
180269 .toList ();
181- return statements;
270+ return _assetsClassDefinition ( statements) ;
182271}
183272
184273String _assetsClassDefinition (List <_Statement > statements) {
0 commit comments