Skip to content
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

feat: add an asset list variable in class #291

Merged
merged 6 commits into from Sep 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions example/lib/gen/assets.gen.dart
Expand Up @@ -19,6 +19,9 @@ class $AssetsFlareGen {

/// File path: assets/flare/Penguin.flr
FlareGenImage get penguin => const FlareGenImage('assets/flare/Penguin.flr');

/// List of all assets
List<FlareGenImage> get values => [penguin];
}

class $AssetsImagesGen {
Expand All @@ -43,27 +46,40 @@ class $AssetsImagesGen {
/// File path: assets/images/profile.png
AssetGenImage get profilePng =>
const AssetGenImage('assets/images/profile.png');

/// List of all assets
List<AssetGenImage> get values =>
[chip1, chip2, logo, profileJpg, profilePng];
}

class $AssetsJsonGen {
const $AssetsJsonGen();

/// File path: assets/json/fruits.json
String get fruits => 'assets/json/fruits.json';

/// List of all assets
List<String> get values => [fruits];
}

class $AssetsMovieGen {
const $AssetsMovieGen();

/// File path: assets/movie/the_earth.mp4
String get theEarth => 'assets/movie/the_earth.mp4';

/// List of all assets
List<String> get values => [theEarth];
}

class $AssetsRiveGen {
const $AssetsRiveGen();

/// File path: assets/rive/vehicles.riv
RiveGenImage get vehicles => const RiveGenImage('assets/rive/vehicles.riv');

/// List of all assets
List<RiveGenImage> get values => [vehicles];
}

class $AssetsUnknownGen {
Expand All @@ -77,6 +93,9 @@ class $AssetsUnknownGen {

/// File path: assets/unknown/unknown_mime_type.bk
String get unknownMimeType => 'assets/unknown/unknown_mime_type.bk';

/// List of all assets
List<String> get values => [changelog, readme, unknownMimeType];
}

class $AssetsImagesChip4Gen {
Expand All @@ -85,6 +104,9 @@ class $AssetsImagesChip4Gen {
/// File path: assets/images/chip4/chip4.jpg
AssetGenImage get chip4 =>
const AssetGenImage('assets/images/chip4/chip4.jpg');

/// List of all assets
List<AssetGenImage> get values => [chip4];
}

class $AssetsImagesIconsGen {
Expand All @@ -103,6 +125,9 @@ class $AssetsImagesIconsGen {

/// File path: assets/images/icons/paint.svg
SvgGenImage get paint => const SvgGenImage('assets/images/icons/paint.svg');

/// List of all assets
List<SvgGenImage> get values => [dartTest, fuchsia, kmm, paint];
}

class MyAssets {
Expand Down
39 changes: 36 additions & 3 deletions packages/core/lib/generators/assets_generator.dart
Expand Up @@ -167,6 +167,7 @@ _Statement? _createAssetTypeStatement(
name: name,
value: 'AssetGenImage(\'${posixStyle(assetType.path)}\')',
isConstConstructor: true,
isDirectory: false,
needDartDoc: true,
);
} else if (FileSystemEntity.isDirectorySync(childAssetAbsolutePath)) {
Expand All @@ -177,6 +178,7 @@ _Statement? _createAssetTypeStatement(
name: name,
value: '$childClassName()',
isConstConstructor: true,
isDirectory: true,
needDartDoc: false,
);
} else if (!assetType.isIgnoreFile) {
Expand All @@ -190,6 +192,7 @@ _Statement? _createAssetTypeStatement(
name: name,
value: '\'${posixStyle(assetType.path)}\'',
isConstConstructor: false,
isDirectory: false,
needDartDoc: true,
);
} else {
Expand All @@ -200,6 +203,7 @@ _Statement? _createAssetTypeStatement(
name: name,
value: integration.classInstantiate(posixStyle(assetType.path)),
isConstConstructor: integration.isConstConstructor,
isDirectory: false,
needDartDoc: true,
);
}
Expand Down Expand Up @@ -259,6 +263,7 @@ String _dotDelimiterStyleDefinition(
name: assetType.baseName.camelCase(),
value: '$className()',
isConstConstructor: true,
isDirectory: true,
needDartDoc: true,
));
}
Expand Down Expand Up @@ -340,7 +345,7 @@ String _flatStyleAssetsClassDefinition(
statements.map((statement) => '''${statement.toDartDocString()}
${statement.toStaticFieldString()}
''').join('\n');
return _assetsClassDefinition(className, statementsBlock);
return _assetsClassDefinition(className, statements, statementsBlock);
}

String _dotDelimiterStyleAssetsClassDefinition(
Expand All @@ -349,15 +354,38 @@ String _dotDelimiterStyleAssetsClassDefinition(
) {
final statementsBlock =
statements.map((statement) => statement.toStaticFieldString()).join('\n');
return _assetsClassDefinition(className, statementsBlock);
return _assetsClassDefinition(className, statements, statementsBlock);
}

String _assetsClassDefinition(String className, String statementsBlock) {
String _assetValuesDefinition(List<_Statement> statements) {
final values = statements.where((element) => !element.isDirectory);
if (values.isEmpty) return '';
final names = values.map((value) => value.name).join(', ');
var type = values.first.type;
for (var value in values) {
if (type != value.type) {
type = 'dynamic';
break;
}
}

return '''
/// List of all assets
List<$type> get values => [$names];''';
}

String _assetsClassDefinition(
String className,
List<_Statement> statements,
String statementsBlock,
) {
final valuesBlock = _assetValuesDefinition(statements);
return '''
class $className {
$className._();

$statementsBlock
$valuesBlock
}
''';
}
Expand All @@ -373,11 +401,14 @@ String _directoryClassGenDefinition(
'''
: statement.toGetterString())
.join('\n');
final valuesBlock = _assetValuesDefinition(statements);

return '''
class $className {
const $className();

$statementsBlock
$valuesBlock
}
''';
}
Expand Down Expand Up @@ -463,6 +494,7 @@ class _Statement {
required this.name,
required this.value,
required this.isConstConstructor,
required this.isDirectory,
required this.needDartDoc,
});

Expand All @@ -471,6 +503,7 @@ class _Statement {
final String name;
final String value;
final bool isConstConstructor;
final bool isDirectory;
final bool needDartDoc;

String toDartDocString() => '/// File path: ${posixStyle(filePath)}';
Expand Down
28 changes: 28 additions & 0 deletions packages/core/test_resources/actual_data/assets.gen.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.