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

Extending package information to asset types other than AssetGenImage. #162

Closed
Closed
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ $ fluttergen -c example/pubspec.yaml
## Configuration file

[FlutterGen] generates dart files based on the key **`flutter`** and **`flutter_gen`** of [`pubspec.yaml`](https://dart.dev/tools/pub/pubspec).
Default configuration can be found [here](#default-configuration).
Default configuration can be found [here](#default-configuration).

```yaml
# pubspec.yaml
Expand Down Expand Up @@ -735,7 +735,7 @@ flutter_gen:
# - snake-case
# - dot-delimiter
style: dot-delimiter

fonts:
# Optional
enabled: true
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dev_dependencies:

flutter_gen:
output: lib/gen/ # Optional (default: lib/gen/)
line_length: 80 # Optional (default: 80)
line_length: 80 # Optional (default: 80)

integrations:
flutter_svg: true
Expand Down
24 changes: 14 additions & 10 deletions packages/core/lib/generators/assets_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class AssetsGenConfig {
final FlutterGen flutterGen;
final List<String> assets;

String get packageParameterLiteral =>
flutterGen.assets.packageParameterEnabled ? _packageName : '';
String get packageDependencyLiteral =>
flutterGen.assets.packageDependencyEnabled ? _packageName : '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do this too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll check until this weekend.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any update on this?

}

String generateAssets(
Expand All @@ -57,7 +57,7 @@ String generateAssets(

final integrations = <Integration>[
if (config.flutterGen.integrations.flutterSvg)
SvgIntegration(config.packageParameterLiteral),
SvgIntegration(config.packageDependencyLiteral),
if (config.flutterGen.integrations.flareFlutter) FlareIntegration(),
if (config.flutterGen.integrations.rive) RiveIntegration(),
];
Expand All @@ -73,7 +73,7 @@ String generateAssets(
}

classesBuffer.writeln(_assetGenImageClassDefinition(
config.packageParameterLiteral,
config.packageDependencyLiteral,
));

final imports = <String>{'package:flutter/widgets.dart'};
Expand Down Expand Up @@ -140,12 +140,12 @@ AssetType _constructAssetTree(List<String> assetRelativePathList) {
}

_Statement? _createAssetTypeStatement(
String rootPath,
AssetsGenConfig config,
AssetType assetType,
List<Integration> integrations,
String name,
) {
final childAssetAbsolutePath = join(rootPath, assetType.path);
final childAssetAbsolutePath = join(config.rootPath, assetType.path);
if (assetType.isSupportedImage) {
return _Statement(
type: 'AssetGenImage',
Expand All @@ -169,12 +169,15 @@ _Statement? _createAssetTypeStatement(
final integration = integrations.firstWhereOrNull(
(element) => element.isSupport(assetType),
);
final packagePrefix = config.packageDependencyLiteral.isNotEmpty
? 'packages/${config.packageDependencyLiteral}/'
: '';
if (integration == null) {
return _Statement(
type: 'String',
filePath: assetType.path,
name: name,
value: '\'${posixStyle(assetType.path)}\'',
value: '\'${posixStyle('$packagePrefix${assetType.path}')}\'',
isConstConstructor: false,
needDartDoc: true,
);
Expand All @@ -184,7 +187,8 @@ _Statement? _createAssetTypeStatement(
type: integration.className,
filePath: assetType.path,
name: name,
value: integration.classInstantiate(posixStyle(assetType.path)),
value: integration
.classInstantiate(posixStyle('$packagePrefix${assetType.path}')),
isConstConstructor: integration.isConstConstructor,
needDartDoc: true,
);
Expand Down Expand Up @@ -214,7 +218,7 @@ String _dotDelimiterStyleDefinition(
.mapToIsUniqueWithoutExtension()
.map(
(e) => _createAssetTypeStatement(
config.rootPath,
config,
e.assetType,
integrations,
(e.isUniqueWithoutExtension
Expand Down Expand Up @@ -297,7 +301,7 @@ String _flatStyleDefinition(
.mapToIsUniqueWithoutExtension()
.map(
(e) => _createAssetTypeStatement(
config.rootPath,
config,
e.assetType,
integrations,
createName(e),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import '../../settings/asset_type.dart';
import 'integration.dart';

class SvgIntegration extends Integration {
SvgIntegration(this._packageParameterLiteral);
SvgIntegration(this._packageDependencyLiteral);

final String _packageParameterLiteral;
final String _packageDependencyLiteral;

String get packageExpression => _packageParameterLiteral.isNotEmpty
? ' = \'$_packageParameterLiteral\''
String get packageExpression => _packageDependencyLiteral.isNotEmpty
? ' = \'$_packageDependencyLiteral\''
: '';

@override
Expand Down
4 changes: 2 additions & 2 deletions packages/core/lib/settings/pubspec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class FlutterGenAssets {

FlutterGenAssets({
required this.enabled,
required this.packageParameterEnabled,
required this.packageDependencyEnabled,
required this.style,
}) {
if (style != dotDelimiterStyle &&
Expand All @@ -117,7 +117,7 @@ class FlutterGenAssets {
final bool enabled;

@JsonKey(name: 'package_parameter_enabled', required: true)
final bool packageParameterEnabled;
final bool packageDependencyEnabled;

@JsonKey(name: 'style', required: true)
final String style;
Expand Down
10 changes: 7 additions & 3 deletions packages/core/lib/settings/pubspec.g.dart

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

8 changes: 4 additions & 4 deletions packages/core/test/assets_gen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ void main() {
}, throwsA(isA<InvalidSettingsException>()));
});

test('Assets with package parameter enabled', () async {
const pubspec = 'test_resources/pubspec_assets_package_parameter.yaml';
test('Assets with package dependency enabled', () async {
const pubspec = 'test_resources/pubspec_assets_package_dependency.yaml';
const fact =
'test_resources/actual_data/assets_package_parameter.gen.dart';
'test_resources/actual_data/assets_package_dependency.gen.dart';
const generated =
'test_resources/lib/gen/assets_package_parameter.gen.dart';
'test_resources/lib/gen/assets_package_dependency.gen.dart';

await expectedAssetsGen(pubspec, generated, fact);
});
Expand Down

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