Skip to content

Commit d400c74

Browse files
committed
merge
2 parents 32d78f1 + d561ba8 commit d400c74

File tree

13 files changed

+456
-90
lines changed

13 files changed

+456
-90
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,5 @@ node_modules/
108108

109109
## Test coverage
110110
coverage/
111-
test/
111+
test/.test_coverage.dart
112112

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,27 @@ final svg = SvgPicture.asset(Assets.images.icons.paint);
232232
final json = await rootBundle.loadString(Assets.json.fruits);
233233
```
234234

235+
[FlutterGen] also support generating other style of `Assets` class:
236+
237+
```yaml
238+
# pubspec.yaml
239+
flutter_gen:
240+
241+
assets:
242+
# Assets.imagesChip
243+
style: camel-case
244+
245+
# Assets.images_chip
246+
# style: snake-case
247+
248+
# Assets.images.chip (default style)
249+
# style: dot-delimiter
250+
251+
flutter:
252+
assets:
253+
- assets/images/chip.png
254+
```
255+
235256
The root directory will be omitted if it is either **`assets`** or **`asset`**.
236257

237258
```

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void main() {
2727
Assets.images.icons.fuchsia.svg(),
2828
Assets.images.icons.paint.svg(
2929
width: 120,
30-
height: 120
30+
height: 120,
3131
),
3232
Assets.pictures.chip5.image(
3333
width: 120,

lib/src/generators/assets_generator.dart

Lines changed: 171 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@ import 'dart:collection';
22
import 'dart:io';
33

44
import 'package:dart_style/dart_style.dart';
5+
import 'package:dartx/dartx.dart';
56
import 'package:path/path.dart';
67

78
import '../settings/asset_type.dart';
89
import '../settings/flutter.dart';
910
import '../settings/flutter_gen.dart';
10-
import '../utils/camel_case.dart';
1111
import '../utils/error.dart';
12+
import '../utils/string.dart';
1213
import 'generator_helper.dart';
1314
import 'integrations/integration.dart';
1415
import '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

184273
String _assetsClassDefinition(List<_Statement> statements) {

lib/src/generators/colors_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import 'package:xml/xml.dart';
77

88
import '../settings/color_path.dart';
99
import '../settings/flutter_gen.dart';
10-
import '../utils/camel_case.dart';
1110
import '../utils/color.dart';
1211
import '../utils/error.dart';
12+
import '../utils/string.dart';
1313
import 'generator_helper.dart';
1414

1515
String generateColors(

lib/src/generators/fonts_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import 'package:dartx/dartx.dart';
33
import 'package:yaml/yaml.dart';
44

55
import '../settings/flutter.dart';
6-
import '../utils/camel_case.dart';
76
import '../utils/cast.dart';
87
import '../utils/error.dart';
8+
import '../utils/string.dart';
99
import 'generator_helper.dart';
1010

1111
String generateFonts(DartFormatter formatter, FlutterFonts fonts) {

0 commit comments

Comments
 (0)