Skip to content

Append extension to name if file basename is not unique #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/dart-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 24 additions & 14 deletions lib/src/generators/assets_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,22 @@ _Statement _createAssetTypeStatement(
File pubspecFile,
AssetType assetType,
List<Integration> 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,
);
} else if (FileSystemEntity.isDirectorySync(childAssetAbsolutePath)) {
final childClassName = '\$${assetType.path.camelCase().capitalize()}Gen';
statement = _Statement(
type: childClassName,
name: createName(assetType),
name: name,
value: '$childClassName\(\)',
isConstConstructor: true,
);
Expand All @@ -141,15 +141,15 @@ _Statement _createAssetTypeStatement(
if (integration == null) {
statement = _Statement(
type: 'String',
name: createName(assetType),
name: name,
value: '\'${posixStyle(assetType.path)}\'',
isConstConstructor: false,
);
} else {
integration.isEnabled = true;
statement = _Statement(
type: integration.className,
name: createName(assetType),
name: name,
value: integration.classInstantiate(posixStyle(assetType.path)),
isConstConstructor: integration.isConstConstructor,
);
Expand Down Expand Up @@ -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>()
Expand Down Expand Up @@ -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(),
);
Expand All @@ -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(),
);
Expand All @@ -248,17 +256,19 @@ String _flatStyleDefinition(
File pubspecFile,
List<String> assets,
List<Integration> 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>()
Expand Down
26 changes: 26 additions & 0 deletions lib/src/settings/asset_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssetType> {
Iterable<AssetTypeIsUniqueWithoutExtension> mapToIsUniqueWithoutExtension() {
return groupBy((e) => p.withoutExtension(e.path))
.values
.map(
(list) => list.map(
(e) => AssetTypeIsUniqueWithoutExtension(
assetType: e,
isUniqueWithoutExtension: list.length == 1,
),
),
)
.flatten();
}
}
5 changes: 4 additions & 1 deletion test_resources/actual_data/assets.gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion test_resources/actual_data/assets_camel_case.gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 3 additions & 1 deletion test_resources/actual_data/assets_snake_case.gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.