Skip to content

✨ Add Import abstraction for package imports alias #548

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
Aug 9, 2024
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
62 changes: 33 additions & 29 deletions examples/example/lib/gen/assets.gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
// ignore_for_file: type=lint
// ignore_for_file: directives_ordering,unnecessary_import,implicit_dynamic_list_literal,deprecated_member_use

import 'package:flutter/widgets.dart';
import 'package:flare_flutter/flare_actor.dart' as _flare_actor;
import 'package:flare_flutter/flare_controller.dart' as _flare_controller;
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:vector_graphics/vector_graphics.dart';
import 'package:flare_flutter/flare_actor.dart';
import 'package:flare_flutter/flare_controller.dart';
import 'package:rive/rive.dart';
import 'package:lottie/lottie.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_svg/flutter_svg.dart' as _svg;
import 'package:lottie/lottie.dart' as _lottie;
import 'package:rive/rive.dart' as _rive;
import 'package:vector_graphics/vector_graphics.dart' as _vg;

class $AssetsFlareGen {
const $AssetsFlareGen();
Expand Down Expand Up @@ -316,7 +316,7 @@ class SvgGenImage {
final Set<String> flavors;
final bool _isVecFormat;

SvgPicture svg({
_svg.SvgPicture svg({
Key? key,
bool matchTextDirection = false,
AssetBundle? bundle,
Expand All @@ -329,29 +329,29 @@ class SvgGenImage {
WidgetBuilder? placeholderBuilder,
String? semanticsLabel,
bool excludeFromSemantics = false,
SvgTheme? theme,
_svg.SvgTheme? theme,
ColorFilter? colorFilter,
Clip clipBehavior = Clip.hardEdge,
@deprecated Color? color,
@deprecated BlendMode colorBlendMode = BlendMode.srcIn,
@deprecated bool cacheColorFilter = false,
}) {
final BytesLoader loader;
final _svg.BytesLoader loader;
if (_isVecFormat) {
loader = AssetBytesLoader(
loader = _vg.AssetBytesLoader(
_assetName,
assetBundle: bundle,
packageName: package,
);
} else {
loader = SvgAssetLoader(
loader = _svg.SvgAssetLoader(
_assetName,
assetBundle: bundle,
packageName: package,
theme: theme,
);
}
return SvgPicture(
return _svg.SvgPicture(
loader,
key: key,
matchTextDirection: matchTextDirection,
Expand Down Expand Up @@ -384,22 +384,22 @@ class FlareGenImage {
final String _assetName;
final Set<String> flavors;

FlareActor flare({
_flare_actor.FlareActor flare({
String? boundsNode,
String? animation,
BoxFit fit = BoxFit.contain,
Alignment alignment = Alignment.center,
bool isPaused = false,
bool snapToEnd = false,
FlareController? controller,
FlareCompletedCallback? callback,
_flare_controller.FlareController? controller,
_flare_actor.FlareCompletedCallback? callback,
Color? color,
bool shouldClip = true,
bool sizeFromArtboard = false,
String? artboard,
bool antialias = true,
}) {
return FlareActor(
return _flare_actor.FlareActor(
_assetName,
boundsNode: boundsNode,
animation: animation,
Expand Down Expand Up @@ -431,7 +431,7 @@ class RiveGenImage {
final String _assetName;
final Set<String> flavors;

RiveAnimation rive({
_rive.RiveAnimation rive({
String? artboard,
List<String> animations = const [],
List<String> stateMachines = const [],
Expand All @@ -440,10 +440,10 @@ class RiveGenImage {
Widget? placeHolder,
bool antialiasing = true,
bool useArtboardSize = false,
List<RiveAnimationController> controllers = const [],
OnInitCallback? onInit,
List<_rive.RiveAnimationController> controllers = const [],
_rive.OnInitCallback? onInit,
}) {
return RiveAnimation.asset(
return _rive.RiveAnimation.asset(
_assetName,
artboard: artboard,
animations: animations,
Expand Down Expand Up @@ -472,19 +472,23 @@ class LottieGenImage {
final String _assetName;
final Set<String> flavors;

LottieBuilder lottie({
_lottie.LottieBuilder lottie({
Animation<double>? controller,
bool? animate,
FrameRate? frameRate,
_lottie.FrameRate? frameRate,
bool? repeat,
bool? reverse,
LottieDelegates? delegates,
LottieOptions? options,
void Function(LottieComposition)? onLoaded,
LottieImageProviderFactory? imageProviderFactory,
_lottie.LottieDelegates? delegates,
_lottie.LottieOptions? options,
void Function(_lottie.LottieComposition)? onLoaded,
_lottie.LottieImageProviderFactory? imageProviderFactory,
Key? key,
AssetBundle? bundle,
Widget Function(BuildContext, Widget, LottieComposition?)? frameBuilder,
Widget Function(
BuildContext,
Widget,
_lottie.LottieComposition?,
)? frameBuilder,
ImageErrorWidgetBuilder? errorBuilder,
double? width,
double? height,
Expand All @@ -495,7 +499,7 @@ class LottieGenImage {
FilterQuality? filterQuality,
void Function(String)? onWarning,
}) {
return Lottie.asset(
return _lottie.Lottie.asset(
_assetName,
controller: controller,
animate: animate,
Expand Down
62 changes: 33 additions & 29 deletions examples/example_resources/lib/gen/assets.gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
// ignore_for_file: type=lint
// ignore_for_file: directives_ordering,unnecessary_import,implicit_dynamic_list_literal,deprecated_member_use

import 'package:flutter/widgets.dart';
import 'package:flare_flutter/flare_actor.dart' as _flare_actor;
import 'package:flare_flutter/flare_controller.dart' as _flare_controller;
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:vector_graphics/vector_graphics.dart';
import 'package:flare_flutter/flare_actor.dart';
import 'package:flare_flutter/flare_controller.dart';
import 'package:rive/rive.dart';
import 'package:lottie/lottie.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_svg/flutter_svg.dart' as _svg;
import 'package:lottie/lottie.dart' as _lottie;
import 'package:rive/rive.dart' as _rive;
import 'package:vector_graphics/vector_graphics.dart' as _vg;

class $AssetsImagesGen {
const $AssetsImagesGen();
Expand Down Expand Up @@ -167,7 +167,7 @@ class SvgGenImage {

static const String package = 'example_resources';

SvgPicture svg({
_svg.SvgPicture svg({
Key? key,
bool matchTextDirection = false,
AssetBundle? bundle,
Expand All @@ -181,29 +181,29 @@ class SvgGenImage {
WidgetBuilder? placeholderBuilder,
String? semanticsLabel,
bool excludeFromSemantics = false,
SvgTheme? theme,
_svg.SvgTheme? theme,
ColorFilter? colorFilter,
Clip clipBehavior = Clip.hardEdge,
@deprecated Color? color,
@deprecated BlendMode colorBlendMode = BlendMode.srcIn,
@deprecated bool cacheColorFilter = false,
}) {
final BytesLoader loader;
final _svg.BytesLoader loader;
if (_isVecFormat) {
loader = AssetBytesLoader(
loader = _vg.AssetBytesLoader(
_assetName,
assetBundle: bundle,
packageName: package,
);
} else {
loader = SvgAssetLoader(
loader = _svg.SvgAssetLoader(
_assetName,
assetBundle: bundle,
packageName: package,
theme: theme,
);
}
return SvgPicture(
return _svg.SvgPicture(
loader,
key: key,
matchTextDirection: matchTextDirection,
Expand Down Expand Up @@ -238,22 +238,22 @@ class FlareGenImage {

static const String package = 'example_resources';

FlareActor flare({
_flare_actor.FlareActor flare({
String? boundsNode,
String? animation,
BoxFit fit = BoxFit.contain,
Alignment alignment = Alignment.center,
bool isPaused = false,
bool snapToEnd = false,
FlareController? controller,
FlareCompletedCallback? callback,
_flare_controller.FlareController? controller,
_flare_actor.FlareCompletedCallback? callback,
Color? color,
bool shouldClip = true,
bool sizeFromArtboard = false,
String? artboard,
bool antialias = true,
}) {
return FlareActor(
return _flare_actor.FlareActor(
'packages/example_resources/$_assetName',
boundsNode: boundsNode,
animation: animation,
Expand Down Expand Up @@ -287,7 +287,7 @@ class RiveGenImage {

static const String package = 'example_resources';

RiveAnimation rive({
_rive.RiveAnimation rive({
String? artboard,
List<String> animations = const [],
List<String> stateMachines = const [],
Expand All @@ -296,10 +296,10 @@ class RiveGenImage {
Widget? placeHolder,
bool antialiasing = true,
bool useArtboardSize = false,
List<RiveAnimationController> controllers = const [],
OnInitCallback? onInit,
List<_rive.RiveAnimationController> controllers = const [],
_rive.OnInitCallback? onInit,
}) {
return RiveAnimation.asset(
return _rive.RiveAnimation.asset(
'packages/example_resources/$_assetName',
artboard: artboard,
animations: animations,
Expand Down Expand Up @@ -330,19 +330,23 @@ class LottieGenImage {

static const String package = 'example_resources';

LottieBuilder lottie({
_lottie.LottieBuilder lottie({
Animation<double>? controller,
bool? animate,
FrameRate? frameRate,
_lottie.FrameRate? frameRate,
bool? repeat,
bool? reverse,
LottieDelegates? delegates,
LottieOptions? options,
void Function(LottieComposition)? onLoaded,
LottieImageProviderFactory? imageProviderFactory,
_lottie.LottieDelegates? delegates,
_lottie.LottieOptions? options,
void Function(_lottie.LottieComposition)? onLoaded,
_lottie.LottieImageProviderFactory? imageProviderFactory,
Key? key,
AssetBundle? bundle,
Widget Function(BuildContext, Widget, LottieComposition?)? frameBuilder,
Widget Function(
BuildContext,
Widget,
_lottie.LottieComposition?,
)? frameBuilder,
ImageErrorWidgetBuilder? errorBuilder,
double? width,
double? height,
Expand All @@ -354,7 +358,7 @@ class LottieGenImage {
FilterQuality? filterQuality,
void Function(String)? onWarning,
}) {
return Lottie.asset(
return _lottie.Lottie.asset(
_assetName,
controller: controller,
animate: animate,
Expand Down
25 changes: 11 additions & 14 deletions packages/core/lib/generators/assets_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import 'dart:io';

import 'package:collection/collection.dart';
import 'package:dart_style/dart_style.dart';
import 'package:dartx/dartx.dart';
import 'package:dartx/dartx.dart' hide IterableSorted;
import 'package:flutter_gen_core/generators/generator_helper.dart';
import 'package:flutter_gen_core/generators/integrations/flare_integration.dart';
import 'package:flutter_gen_core/generators/integrations/image_integration.dart';
import 'package:flutter_gen_core/generators/integrations/integration.dart';
import 'package:flutter_gen_core/generators/integrations/lottie_integration.dart';
import 'package:flutter_gen_core/generators/integrations/rive_integration.dart';
import 'package:flutter_gen_core/generators/integrations/svg_integration.dart';
import 'package:flutter_gen_core/settings/asset_type.dart';
import 'package:flutter_gen_core/settings/config.dart';
import 'package:flutter_gen_core/settings/flavored_asset.dart';
import 'package:flutter_gen_core/settings/pubspec.dart';
Expand Down Expand Up @@ -56,12 +55,10 @@ String generateAssets(
) {
if (config.assets.isEmpty) {
throw const InvalidSettingsException(
'The value of "flutter/assets:" is incorrect.');
'The value of "flutter/assets:" is incorrect.',
);
}

final importsBuffer = StringBuffer();
final classesBuffer = StringBuffer();

final integrations = <Integration>[
ImageIntegration(config.packageParameterLiteral,
parseMetadata: config.flutterGen.parseMetadata),
Expand Down Expand Up @@ -158,6 +155,7 @@ String generateAssets(
└────────────────────────────────────────────────────────────────────────────────────────┘''');
}

final classesBuffer = StringBuffer();
if (config.flutterGen.assets.outputs.isDotDelimiterStyle) {
classesBuffer.writeln(_dotDelimiterStyleDefinition(config, integrations));
} else if (config.flutterGen.assets.outputs.isSnakeCaseStyle) {
Expand All @@ -168,19 +166,18 @@ String generateAssets(
throw 'The value of "flutter_gen/assets/style." is incorrect.';
}

final imports = <String>{};
integrations
.where((integration) => integration.isEnabled)
.forEach((integration) {
final imports = <Import>{};
for (final integration in integrations.where((e) => e.isEnabled)) {
imports.addAll(integration.requiredImports);
classesBuffer.writeln(integration.classOutput);
});
for (final package in imports) {
importsBuffer.writeln(import(package));
}

final buffer = StringBuffer();
final importsBuffer = StringBuffer();
for (final e in imports.sorted((a, b) => a.import.compareTo(b.import))) {
importsBuffer.writeln(import(e));
}

final buffer = StringBuffer();
buffer.writeln(header);
buffer.writeln(ignore);
buffer.writeln(importsBuffer.toString());
Expand Down
7 changes: 6 additions & 1 deletion packages/core/lib/generators/generator_helper.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:flutter_gen_core/settings/import.dart';

String get header {
return '''/// GENERATED CODE - DO NOT MODIFY BY HAND
/// *****************************************************
Expand All @@ -15,4 +17,7 @@ String get ignore {
''';
}

String import(String package) => 'import \'$package\';';
String import(Import package) {
return 'import \'${package.import}\''
'${package.alias != null ? ' as ${package.alias}' : ''};';
}
Loading
Loading