Skip to content

Commit ea8d400

Browse files
authored
⚡️ Generate package path for directory (#619)
## What does this change? Resolves #602 ## Type of change - [x] New feature (non-breaking change which adds functionality)
1 parent 946fd5f commit ea8d400

File tree

4 files changed

+283
-20
lines changed

4 files changed

+283
-20
lines changed

packages/core/lib/generators/assets_generator.dart

+14-20
Original file line numberDiff line numberDiff line change
@@ -316,19 +316,19 @@ Future<String> _dotDelimiterStyleDefinition(
316316
List<Integration> integrations,
317317
) async {
318318
final rootPath = Directory(config.rootPath).absolute.uri.toFilePath();
319-
final buffer = StringBuffer();
320-
final className = config.flutterGen.assets.outputs.className;
319+
final packageName = generatePackageNameForConfig(config);
320+
final outputs = config.flutterGen.assets.outputs;
321321
final assetRelativePathList = _getAssetRelativePathList(
322322
rootPath,
323323
config.assets,
324324
config.exclude,
325325
);
326-
final assetsStaticStatements = <_Statement>[];
327-
328326
final assetTypeQueue = ListQueue<AssetType>.from(
329327
_constructAssetTree(assetRelativePathList, rootPath).children,
330328
);
331329

330+
final assetsStaticStatements = <_Statement>[];
331+
final buffer = StringBuffer();
332332
while (assetTypeQueue.isNotEmpty) {
333333
final assetType = assetTypeQueue.removeFirst();
334334
String assetPath = join(rootPath, assetType.path);
@@ -346,13 +346,7 @@ Future<String> _dotDelimiterStyleDefinition(
346346
final List<_Statement?> results = await Future.wait(
347347
assetType.children
348348
.mapToUniqueAssetType(camelCase, justBasename: true)
349-
.map(
350-
(e) => _createAssetTypeStatement(
351-
config,
352-
e,
353-
integrations,
354-
),
355-
),
349+
.map((e) => _createAssetTypeStatement(config, e, integrations)),
356350
);
357351
final statements = results.whereType<_Statement>().toList();
358352

@@ -368,14 +362,15 @@ Future<String> _dotDelimiterStyleDefinition(
368362
assetsStaticStatements.add(statement!);
369363
} else {
370364
final className = '\$${assetType.path.camelCase().capitalize()}Gen';
365+
String? directoryPath;
366+
if (outputs.directoryPathEnabled) {
367+
directoryPath = assetType.posixStylePath;
368+
if (packageName != null) {
369+
directoryPath = 'packages/$packageName/$directoryPath';
370+
}
371+
}
371372
buffer.writeln(
372-
_directoryClassGenDefinition(
373-
className,
374-
statements,
375-
config.flutterGen.assets.outputs.directoryPathEnabled
376-
? assetType.posixStylePath
377-
: null,
378-
),
373+
_directoryClassGenDefinition(className, statements, directoryPath),
379374
);
380375
// Add this directory reference to Assets class
381376
// if we are not under the default asset folder
@@ -395,10 +390,9 @@ Future<String> _dotDelimiterStyleDefinition(
395390
assetTypeQueue.addAll(assetType.children);
396391
}
397392
}
398-
final String? packageName = generatePackageNameForConfig(config);
399393
buffer.writeln(
400394
_dotDelimiterStyleAssetsClassDefinition(
401-
className,
395+
outputs.className,
402396
assetsStaticStatements,
403397
packageName,
404398
),

packages/core/test/assets_gen_test.dart

+7
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ void main() {
102102
await expectedAssetsGen(pubspec, generated, fact);
103103
});
104104

105+
test('Assets with directory path and package parameter enabled', () async {
106+
const pubspec = 'test_resources/pubspec_assets_directory_path_with_package_parameter.yaml';
107+
const fact = 'test_resources/actual_data/assets_directory_path_with_package_parameter.gen.dart';
108+
const generated = 'test_resources/lib/gen/assets_directory_path_with_package_parameter.gen.dart';
109+
await expectedAssetsGen(pubspec, generated, fact);
110+
});
111+
105112
test('Assets with excluded files and directories', () async {
106113
const pubspec = 'test_resources/pubspec_assets_exclude_files.yaml';
107114
const fact =

packages/core/test_resources/actual_data/assets_directory_path_with_package_parameter.gen.dart

+246
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: test
2+
3+
flutter_gen:
4+
integrations:
5+
flutter_svg: true
6+
assets:
7+
outputs:
8+
directory_path_enabled: true
9+
package_parameter_enabled: true
10+
11+
flutter:
12+
assets:
13+
- assets/images/chip3/chip3.jpg
14+
- assets/images/icons/[email protected]
15+
- assets/images/icons/fuchsia.svg
16+
- assets/unknown/unknown_mime_type.bk

0 commit comments

Comments
 (0)