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

Fix package_parameter_enabled being ignored for Flare, Rive and Lottie #300

Merged
merged 4 commits into from
Sep 29, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/core/lib/generators/assets_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ String generateAssets(
final integrations = <Integration>[
if (config.flutterGen.integrations.flutterSvg)
SvgIntegration(config.packageParameterLiteral),
if (config.flutterGen.integrations.flareFlutter) FlareIntegration(),
if (config.flutterGen.integrations.rive) RiveIntegration(),
if (config.flutterGen.integrations.lottie) LottieIntegration(),
if (config.flutterGen.integrations.flareFlutter)
FlareIntegration(config.packageParameterLiteral),
if (config.flutterGen.integrations.rive)
RiveIntegration(config.packageParameterLiteral),
if (config.flutterGen.integrations.lottie)
LottieIntegration(config.packageParameterLiteral),
];

// TODO: This code will be removed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import '../../settings/asset_type.dart';
import 'integration.dart';

class FlareIntegration extends Integration {

FlareIntegration(String packageParameterLiteral)
: super(packageParameterLiteral);

String? get packageExpression => packageParameterLiteral.isNotEmpty
? 'packages/$packageParameterLiteral/'
: null;

@override
List<String> get requiredImports => [
'package:flare_flutter/flare_actor.dart',
Expand All @@ -11,7 +19,7 @@ class FlareIntegration extends Integration {
@override
String get classOutput => _classDefinition;

final String _classDefinition = '''class FlareGenImage {
String get _classDefinition => '''class FlareGenImage {
const FlareGenImage(this._assetName);

final String _assetName;
Expand All @@ -32,7 +40,7 @@ class FlareIntegration extends Integration {
bool antialias = true,
}) {
return FlareActor(
_assetName,
${packageExpression == null ? '_assetName' : '\'$packageExpression\$_assetName\''},
boundsNode: boundsNode,
animation: animation,
fit: fit,
Expand Down
5 changes: 5 additions & 0 deletions packages/core/lib/generators/integrations/integration.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import '../../settings/asset_type.dart';

abstract class Integration {

Integration(this.packageParameterLiteral);

final String packageParameterLiteral;

bool isEnabled = false;

List<String> get requiredImports;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import 'dart:io';
import 'package:path/path.dart' as p;

class LottieIntegration extends Integration {

LottieIntegration(String packageParameterLiteral)
: super(packageParameterLiteral);

// These are required keys for this integration.
static const lottieKeys = [
'w', // width
Expand All @@ -18,6 +22,10 @@ class LottieIntegration extends Integration {
'layers', // Must include layers
];

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

@override
List<String> get requiredImports => [
'package:lottie/lottie.dart',
Expand All @@ -26,7 +34,7 @@ class LottieIntegration extends Integration {
@override
String get classOutput => _classDefinition;

final String _classDefinition = '''class LottieGenImage {
String get _classDefinition => '''class LottieGenImage {
const LottieGenImage(this._assetName);

final String _assetName;
Expand All @@ -49,7 +57,7 @@ class LottieIntegration extends Integration {
double? height,
BoxFit? fit,
AlignmentGeometry? alignment,
String? package,
String? package$packageExpression,
bool? addRepaintBoundary,
FilterQuality? filterQuality,
void Function(String)? onWarning,
Expand Down
12 changes: 10 additions & 2 deletions packages/core/lib/generators/integrations/rive_integration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import '../../settings/asset_type.dart';
import 'integration.dart';

class RiveIntegration extends Integration {

RiveIntegration(String packageParameterLiteral)
: super(packageParameterLiteral);

String? get packageExpression => packageParameterLiteral.isNotEmpty
? 'packages/$packageParameterLiteral/'
: null;

@override
List<String> get requiredImports => [
'package:rive/rive.dart',
Expand All @@ -10,7 +18,7 @@ class RiveIntegration extends Integration {
@override
String get classOutput => _classDefinition;

final String _classDefinition = '''class RiveGenImage {
String get _classDefinition => '''class RiveGenImage {
const RiveGenImage(this._assetName);

final String _assetName;
Expand All @@ -27,7 +35,7 @@ class RiveIntegration extends Integration {
OnInitCallback? onInit,
}) {
return RiveAnimation.asset(
_assetName,
${packageExpression == null ? '_assetName' : '\'$packageExpression\$_assetName\''},
artboard: artboard,
animations: animations,
stateMachines: stateMachines,
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);

final String _packageParameterLiteral;
SvgIntegration(String packageParameterLiteral)
: super(packageParameterLiteral);

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

@override
Expand Down
40 changes: 36 additions & 4 deletions packages/core/test/assets_gen_integrations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void main() {

await expectedAssetsGen(pubspec, generated, fact);

final integration = SvgIntegration('package_name');
final integration = SvgIntegration('');
expect(integration.className, 'SvgGenImage');
expect(integration.classInstantiate('assets/path'),
'SvgGenImage(\'assets/path\')');
Expand All @@ -42,6 +42,14 @@ void main() {
AssetType(rootPath: resPath, path: 'assets/path/dog.png')),
isFalse);
expect(integration.isConstConstructor, isTrue);
expect(integration.classOutput.contains('String? package,'), isTrue);

final integrationWithPackage = SvgIntegration('package_name');
expect(
integrationWithPackage.classOutput
.contains('String? package = \'package_name\','),
isTrue,
);
});

test('Assets with Flare integrations on pubspec.yaml', () async {
Expand All @@ -53,7 +61,7 @@ void main() {

await expectedAssetsGen(pubspec, generated, fact);

final integration = FlareIntegration();
final integration = FlareIntegration('');
expect(integration.className, 'FlareGenImage');
expect(integration.classInstantiate('assets/path'),
'FlareGenImage(\'assets/path\')');
Expand All @@ -66,6 +74,14 @@ void main() {
AssetType(rootPath: resPath, path: 'assets/path/dog.json')),
isFalse);
expect(integration.isConstConstructor, isTrue);
expect(integration.classOutput.contains('_assetName,'), isTrue);

final integrationWithPackage = FlareIntegration('package_name');
expect(
integrationWithPackage.classOutput
.contains('\'packages/package_name/\$_assetName\','),
isTrue,
);
});

test('Assets with Rive integrations on pubspec.yaml', () async {
Expand All @@ -77,7 +93,7 @@ void main() {

await expectedAssetsGen(pubspec, generated, fact);

final integration = RiveIntegration();
final integration = RiveIntegration('');
expect(integration.className, 'RiveGenImage');
expect(integration.classInstantiate('assets/path'),
'RiveGenImage(\'assets/path\')');
Expand All @@ -90,6 +106,14 @@ void main() {
AssetType(rootPath: resPath, path: 'assets/path/dog.json')),
isFalse);
expect(integration.isConstConstructor, isTrue);
expect(integration.classOutput.contains('_assetName,'), isTrue);

final integrationWithPackage = RiveIntegration('package_name');
expect(
integrationWithPackage.classOutput
.contains('\'packages/package_name/\$_assetName\','),
isTrue,
);
});

test('Assets with Lottie integrations on pubspec.yaml', () async {
Expand All @@ -101,7 +125,7 @@ void main() {

await expectedAssetsGen(pubspec, generated, fact);

final integration = LottieIntegration();
final integration = LottieIntegration('');
expect(integration.className, 'LottieGenImage');
expect(integration.classInstantiate('assets/lottie'),
'LottieGenImage(\'assets/lottie\')');
Expand All @@ -115,6 +139,14 @@ void main() {
path: 'assets/lottie/hamburger_arrow_without_version.json')),
isFalse);
expect(integration.isConstConstructor, isTrue);
expect(integration.classOutput.contains('String? package,'), isTrue);

final integrationWithPackage = LottieIntegration('package_name');
expect(
integrationWithPackage.classOutput
.contains('String? package = \'package_name\','),
isTrue,
);
});
});
}