Skip to content

Commit 7aa122d

Browse files
committed
Refactor
1 parent ad4b93b commit 7aa122d

File tree

1 file changed

+65
-66
lines changed

1 file changed

+65
-66
lines changed

lib/src/generators/assets_generator.dart

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -112,59 +112,52 @@ AssetType _constructAssetTree(List<String> assetRelativePathList) {
112112
return assetTypeMap['.'];
113113
}
114114

115-
List<_Statement> _createAssetTypeStatements(
115+
_Statement _createAssetTypeStatements(
116116
File pubspecFile,
117117
AssetType assetType,
118118
List<Integration> integrations,
119119
String Function(AssetType) createName,
120120
) {
121-
final statements = assetType.children
122-
.map((child) {
123-
final childAssetAbsolutePath =
124-
join(pubspecFile.parent.path, child.path);
125-
_Statement statement;
126-
if (child.isSupportedImage) {
127-
statement = _Statement(
128-
type: 'AssetGenImage',
129-
name: createName(child),
130-
value: 'AssetGenImage\(\'${posixStyle(child.path)}\'\)',
131-
isConstConstructor: true,
132-
);
133-
} else if (FileSystemEntity.isDirectorySync(childAssetAbsolutePath)) {
134-
final childClassName = '\$${child.path.camelCase().capitalize()}Gen';
135-
statement = _Statement(
136-
type: childClassName,
137-
name: createName(child),
138-
value: '$childClassName\(\)',
139-
isConstConstructor: true,
140-
);
141-
} else if (!child.isUnKnownMime) {
142-
final integration = integrations.firstWhere(
143-
(element) => element.mime == child.mime,
144-
orElse: () => null,
145-
);
146-
if (integration == null) {
147-
statement = _Statement(
148-
type: 'String',
149-
name: createName(child),
150-
value: '\'${posixStyle(child.path)}\'',
151-
isConstConstructor: false,
152-
);
153-
} else {
154-
integration.isEnabled = true;
155-
statement = _Statement(
156-
type: integration.className,
157-
name: createName(child),
158-
value: integration.classInstantiate(posixStyle(child.path)),
159-
isConstConstructor: integration.isConstConstructor,
160-
);
161-
}
162-
}
163-
return statement;
164-
})
165-
.whereType<_Statement>()
166-
.toList();
167-
return statements;
121+
final childAssetAbsolutePath = join(pubspecFile.parent.path, assetType.path);
122+
_Statement statement;
123+
if (assetType.isSupportedImage) {
124+
statement = _Statement(
125+
type: 'AssetGenImage',
126+
name: createName(assetType),
127+
value: 'AssetGenImage\(\'${posixStyle(assetType.path)}\'\)',
128+
isConstConstructor: true,
129+
);
130+
} else if (FileSystemEntity.isDirectorySync(childAssetAbsolutePath)) {
131+
final childClassName = '\$${assetType.path.camelCase().capitalize()}Gen';
132+
statement = _Statement(
133+
type: childClassName,
134+
name: createName(assetType),
135+
value: '$childClassName\(\)',
136+
isConstConstructor: true,
137+
);
138+
} else if (!assetType.isUnKnownMime) {
139+
final integration = integrations.firstWhere(
140+
(element) => element.mime == assetType.mime,
141+
orElse: () => null,
142+
);
143+
if (integration == null) {
144+
statement = _Statement(
145+
type: 'String',
146+
name: createName(assetType),
147+
value: '\'${posixStyle(assetType.path)}\'',
148+
isConstConstructor: false,
149+
);
150+
} else {
151+
integration.isEnabled = true;
152+
statement = _Statement(
153+
type: integration.className,
154+
name: createName(assetType),
155+
value: integration.classInstantiate(posixStyle(assetType.path)),
156+
isConstConstructor: integration.isConstConstructor,
157+
);
158+
}
159+
}
160+
return statement;
168161
}
169162

170163
/// Generate style like Assets.foo.bar
@@ -185,12 +178,17 @@ String _dotDelimiterStyleDefinition(
185178
final assetAbsolutePath = join(pubspecFile.parent.path, assetType.path);
186179

187180
if (FileSystemEntity.isDirectorySync(assetAbsolutePath)) {
188-
final statements = _createAssetTypeStatements(
189-
pubspecFile,
190-
assetType,
191-
integrations,
192-
(e) => e.baseName.camelCase(),
193-
);
181+
final statements = assetType.children
182+
.map(
183+
(child) => _createAssetTypeStatements(
184+
pubspecFile,
185+
child,
186+
integrations,
187+
(e) => e.baseName.camelCase(),
188+
),
189+
)
190+
.whereType<_Statement>()
191+
.toList();
194192

195193
if (assetType.isDefaultAssetsDirectory) {
196194
assetsStaticStatements.addAll(statements);
@@ -221,19 +219,20 @@ String _snakeCaseStyleDefinition(
221219
FlutterAssets assets,
222220
List<Integration> integrations,
223221
) {
224-
final assetType = AssetType('.');
225-
_getAssetRelativePathList(pubspecFile, assets)
222+
final statements = _getAssetRelativePathList(pubspecFile, assets)
226223
.distinct()
227-
.map((e) => AssetType(e))
228-
.forEach(assetType.addChild);
229-
final statements = _createAssetTypeStatements(
230-
pubspecFile,
231-
assetType,
232-
integrations,
233-
(e) => withoutExtension(e.path)
234-
.replaceFirst(RegExp(r'asset(s)?/'), '')
235-
.snakeCase(),
236-
);
224+
.sorted()
225+
.map(
226+
(relativePath) => _createAssetTypeStatements(
227+
pubspecFile,
228+
AssetType(relativePath),
229+
integrations,
230+
(e) => withoutExtension(e.path)
231+
.replaceFirst(RegExp(r'asset(s)?/'), '')
232+
.snakeCase(),
233+
),
234+
)
235+
.toList();
237236
return _assetsClassDefinition(statements);
238237
}
239238

0 commit comments

Comments
 (0)