diff --git a/.github/workflows/dart-ci.yaml b/.github/workflows/dart-ci.yaml index 3dfc82640..5ef5eb6d7 100644 --- a/.github/workflows/dart-ci.yaml +++ b/.github/workflows/dart-ci.yaml @@ -25,6 +25,10 @@ jobs: - name: Run tests for our dart project. run: | make unit-test + + - name: Run coverage + if: matrix.os == 'ubuntu-latest' + run: | make coverage - name: Generate example diff --git a/lib/src/generators/assets_generator.dart b/lib/src/generators/assets_generator.dart index 1c5e5d194..c81e07c7b 100644 --- a/lib/src/generators/assets_generator.dart +++ b/lib/src/generators/assets_generator.dart @@ -114,14 +114,14 @@ _Statement _createAssetTypeStatement( File pubspecFile, AssetType assetType, List integrations, - String Function(AssetType) createName, + String name, ) { final childAssetAbsolutePath = join(pubspecFile.parent.path, assetType.path); _Statement statement; if (assetType.isSupportedImage) { statement = _Statement( type: 'AssetGenImage', - name: createName(assetType), + name: name, value: 'AssetGenImage\(\'${posixStyle(assetType.path)}\'\)', isConstConstructor: true, ); @@ -129,7 +129,7 @@ _Statement _createAssetTypeStatement( final childClassName = '\$${assetType.path.camelCase().capitalize()}Gen'; statement = _Statement( type: childClassName, - name: createName(assetType), + name: name, value: '$childClassName\(\)', isConstConstructor: true, ); @@ -141,7 +141,7 @@ _Statement _createAssetTypeStatement( if (integration == null) { statement = _Statement( type: 'String', - name: createName(assetType), + name: name, value: '\'${posixStyle(assetType.path)}\'', isConstConstructor: false, ); @@ -149,7 +149,7 @@ _Statement _createAssetTypeStatement( integration.isEnabled = true; statement = _Statement( type: integration.className, - name: createName(assetType), + name: name, value: integration.classInstantiate(posixStyle(assetType.path)), isConstConstructor: integration.isConstConstructor, ); @@ -177,12 +177,16 @@ String _dotDelimiterStyleDefinition( if (FileSystemEntity.isDirectorySync(assetAbsolutePath)) { final statements = assetType.children + .mapToIsUniqueWithoutExtension() .map( - (child) => _createAssetTypeStatement( + (e) => _createAssetTypeStatement( pubspecFile, - child, + e.assetType, integrations, - (element) => element.baseName.camelCase(), + (e.isUniqueWithoutExtension + ? basenameWithoutExtension(e.assetType.path) + : basename(e.assetType.path)) + .camelCase(), ), ) .whereType<_Statement>() @@ -222,7 +226,9 @@ String _camelCaseStyleDefinition( pubspecFile, assets, integrations, - (assetType) => withoutExtension(assetType.path) + (e) => (e.isUniqueWithoutExtension + ? withoutExtension(e.assetType.path) + : e.assetType.path) .replaceFirst(RegExp(r'asset(s)?'), '') .camelCase(), ); @@ -238,7 +244,9 @@ String _snakeCaseStyleDefinition( pubspecFile, assets, integrations, - (assetType) => withoutExtension(assetType.path) + (e) => (e.isUniqueWithoutExtension + ? withoutExtension(e.assetType.path) + : e.assetType.path) .replaceFirst(RegExp(r'asset(s)?'), '') .snakeCase(), ); @@ -248,17 +256,19 @@ String _flatStyleDefinition( File pubspecFile, List assets, List integrations, - String Function(AssetType) createName, + String Function(AssetTypeIsUniqueWithoutExtension) createName, ) { final statements = _getAssetRelativePathList(pubspecFile, assets) .distinct() .sorted() + .map((relativePath) => AssetType(relativePath)) + .mapToIsUniqueWithoutExtension() .map( - (relativePath) => _createAssetTypeStatement( + (e) => _createAssetTypeStatement( pubspecFile, - AssetType(relativePath), + e.assetType, integrations, - createName, + createName(e), ), ) .whereType<_Statement>() diff --git a/lib/src/settings/asset_type.dart b/lib/src/settings/asset_type.dart index ecfe0eebb..6dbd21f22 100644 --- a/lib/src/settings/asset_type.dart +++ b/lib/src/settings/asset_type.dart @@ -56,3 +56,29 @@ class AssetType { _children.add(type); } } + +class AssetTypeIsUniqueWithoutExtension { + AssetTypeIsUniqueWithoutExtension({ + this.assetType, + this.isUniqueWithoutExtension, + }); + + final AssetType assetType; + final bool isUniqueWithoutExtension; +} + +extension AssetTypeIterable on Iterable { + Iterable mapToIsUniqueWithoutExtension() { + return groupBy((e) => p.withoutExtension(e.path)) + .values + .map( + (list) => list.map( + (e) => AssetTypeIsUniqueWithoutExtension( + assetType: e, + isUniqueWithoutExtension: list.length == 1, + ), + ), + ) + .flatten(); + } +} diff --git a/test_resources/actual_data/assets.gen.dart b/test_resources/actual_data/assets.gen.dart index 81516226e..77d4ef544 100644 --- a/test_resources/actual_data/assets.gen.dart +++ b/test_resources/actual_data/assets.gen.dart @@ -30,7 +30,10 @@ class $AssetsImagesGen { $AssetsImagesChip4Gen get chip4 => const $AssetsImagesChip4Gen(); $AssetsImagesIconsGen get icons => const $AssetsImagesIconsGen(); AssetGenImage get logo => const AssetGenImage('assets/images/logo.png'); - AssetGenImage get profile => const AssetGenImage('assets/images/profile.jpg'); + AssetGenImage get profileJpg => + const AssetGenImage('assets/images/profile.jpg'); + AssetGenImage get profilePng => + const AssetGenImage('assets/images/profile.png'); } class $AssetsJsonGen { diff --git a/test_resources/actual_data/assets_camel_case.gen.dart b/test_resources/actual_data/assets_camel_case.gen.dart index 05bf7feea..2eba7d43b 100644 --- a/test_resources/actual_data/assets_camel_case.gen.dart +++ b/test_resources/actual_data/assets_camel_case.gen.dart @@ -21,8 +21,10 @@ class Assets { static const String imagesIconsPaint = 'assets/images/icons/paint.svg'; static const AssetGenImage imagesLogo = AssetGenImage('assets/images/logo.png'); - static const AssetGenImage imagesProfile = + static const AssetGenImage imagesProfileJpg = AssetGenImage('assets/images/profile.jpg'); + static const AssetGenImage imagesProfilePng = + AssetGenImage('assets/images/profile.png'); static const String jsonFruits = 'assets/json/fruits.json'; static const AssetGenImage picturesChip5 = AssetGenImage('pictures/chip5.jpg'); diff --git a/test_resources/actual_data/assets_snake_case.gen.dart b/test_resources/actual_data/assets_snake_case.gen.dart index 5396ee90a..37dc74a61 100644 --- a/test_resources/actual_data/assets_snake_case.gen.dart +++ b/test_resources/actual_data/assets_snake_case.gen.dart @@ -21,8 +21,10 @@ class Assets { static const String images_icons_paint = 'assets/images/icons/paint.svg'; static const AssetGenImage images_logo = AssetGenImage('assets/images/logo.png'); - static const AssetGenImage images_profile = + static const AssetGenImage images_profile_jpg = AssetGenImage('assets/images/profile.jpg'); + static const AssetGenImage images_profile_png = + AssetGenImage('assets/images/profile.png'); static const String json_fruits = 'assets/json/fruits.json'; static const AssetGenImage pictures_chip5 = AssetGenImage('pictures/chip5.jpg'); diff --git a/test_resources/assets/images/profile.png b/test_resources/assets/images/profile.png new file mode 100644 index 000000000..e69de29bb