diff --git a/README.md b/README.md index d720352b3..04fc392fc 100644 --- a/README.md +++ b/README.md @@ -739,16 +739,28 @@ flutter_gen: # - snake-case # - dot-delimiter style: dot-delimiter + # Optional + outputs: + # Default is Assets + class_name: MyAssets fonts: # Optional enabled: true + # Optional + outputs: + # Default is FontFamily + class_name: MyFontFamily colors: # Optional enabled: true # Optional inputs: [] + # Optional + outputs: + # Default is ColorName + class_name: MyColorName flutter: # See: https://flutter.dev/docs/development/ui/assets-and-images#specifying-assets diff --git a/packages/core/lib/flutter_generator.dart b/packages/core/lib/flutter_generator.dart index 8ff32d5f5..067b97ce9 100644 --- a/packages/core/lib/flutter_generator.dart +++ b/packages/core/lib/flutter_generator.dart @@ -76,7 +76,11 @@ class FlutterGenerator { } if (flutterGen.fonts.enabled && flutter.fonts.isNotEmpty) { - final generated = generateFonts(formatter, flutter.fonts); + final generated = generateFonts( + formatter, + flutter.fonts, + genFonts: flutterGen.fonts, + ); final fonts = File(normalize(join(pubspecFile.parent.path, output, fontsName))); writeAsString(generated, file: fonts); diff --git a/packages/core/lib/generators/assets_generator.dart b/packages/core/lib/generators/assets_generator.dart index 8f9dd355c..8dd6e6b32 100644 --- a/packages/core/lib/generators/assets_generator.dart +++ b/packages/core/lib/generators/assets_generator.dart @@ -199,6 +199,7 @@ String _dotDelimiterStyleDefinition( List integrations, ) { final buffer = StringBuffer(); + final className = config.flutterGen.assets.outputs?.className; final assetRelativePathList = _getAssetRelativePathList(config.rootPath, config.assets); final assetsStaticStatements = <_Statement>[]; @@ -249,8 +250,8 @@ String _dotDelimiterStyleDefinition( assetTypeQueue.addAll(assetType.children); } } - buffer - .writeln(_dotDelimiterStyleAssetsClassDefinition(assetsStaticStatements)); + buffer.writeln(_dotDelimiterStyleAssetsClassDefinition( + className, assetsStaticStatements)); return buffer.toString(); } @@ -306,27 +307,34 @@ String _flatStyleDefinition( ) .whereType<_Statement>() .toList(); - return _flatStyleAssetsClassDefinition(statements); + final className = config.flutterGen.assets.outputs?.className; + return _flatStyleAssetsClassDefinition(className, statements); } -String _flatStyleAssetsClassDefinition(List<_Statement> statements) { +String _flatStyleAssetsClassDefinition( + String? className, + List<_Statement> statements, +) { final statementsBlock = statements.map((statement) => '''${statement.toDartDocString()} ${statement.toStaticFieldString()} ''').join('\n'); - return _assetsClassDefinition(statementsBlock); + return _assetsClassDefinition(className, statementsBlock); } -String _dotDelimiterStyleAssetsClassDefinition(List<_Statement> statements) { +String _dotDelimiterStyleAssetsClassDefinition( + String? className, + List<_Statement> statements, +) { final statementsBlock = statements.map((statement) => statement.toStaticFieldString()).join('\n'); - return _assetsClassDefinition(statementsBlock); + return _assetsClassDefinition(className, statementsBlock); } -String _assetsClassDefinition(String statementsBlock) { +String _assetsClassDefinition(String? className, String statementsBlock) { return ''' -class Assets { - Assets._(); +class ${className ?? 'Assets'} { + ${className ?? 'Assets'}._(); $statementsBlock } diff --git a/packages/core/lib/generators/colors_generator.dart b/packages/core/lib/generators/colors_generator.dart index 37ae1dd59..e421637ff 100644 --- a/packages/core/lib/generators/colors_generator.dart +++ b/packages/core/lib/generators/colors_generator.dart @@ -15,25 +15,26 @@ import 'generator_helper.dart'; String generateColors( File pubspecFile, DartFormatter formatter, - FlutterGenColors colors, + FlutterGenColors genColors, ) { - if (colors.inputs.isEmpty) { + if (genColors.inputs.isEmpty) { throw const InvalidSettingsException( 'The value of "flutter_gen/colors:" is incorrect.'); } final buffer = StringBuffer(); + final className = genColors.outputs?.className ?? 'ColorName'; buffer.writeln(header); buffer.writeln(ignore); buffer.writeln("import 'package:flutter/painting.dart';"); buffer.writeln("import 'package:flutter/material.dart';"); buffer.writeln(); - buffer.writeln('class ColorName {'); - buffer.writeln('ColorName._();'); + buffer.writeln('class $className {'); + buffer.writeln('$className._();'); buffer.writeln(); final colorList = <_Color>[]; - colors.inputs + genColors.inputs .map((file) => ColorPath(join(pubspecFile.parent.path, file))) .forEach((colorFile) { final data = colorFile.file.readAsStringSync(); diff --git a/packages/core/lib/generators/fonts_generator.dart b/packages/core/lib/generators/fonts_generator.dart index f96e597ad..a67ccdd17 100644 --- a/packages/core/lib/generators/fonts_generator.dart +++ b/packages/core/lib/generators/fonts_generator.dart @@ -10,18 +10,20 @@ import 'generator_helper.dart'; String generateFonts( DartFormatter formatter, - List fonts, -) { + List fonts, { + FlutterGenFonts? genFonts, +}) { if (fonts.isEmpty) { throw InvalidSettingsException( 'The value of "flutter/fonts:" is incorrect.'); } final buffer = StringBuffer(); + final className = genFonts?.outputs?.className ?? 'FontFamily'; buffer.writeln(header); buffer.writeln(ignore); - buffer.writeln('class FontFamily {'); - buffer.writeln('FontFamily._();'); + buffer.writeln('class $className {'); + buffer.writeln('$className._();'); buffer.writeln(); fonts.map((element) => element.family).distinct().sorted().forEach((family) { diff --git a/packages/core/lib/settings/config.dart b/packages/core/lib/settings/config.dart index 1b5cb9cfc..1a18ed9b1 100644 --- a/packages/core/lib/settings/config.dart +++ b/packages/core/lib/settings/config.dart @@ -47,13 +47,19 @@ flutter_gen: enabled: true package_parameter_enabled: false style: dot-delimiter + outputs: + class_name: Assets fonts: enabled: true + outputs: + class_name: FontFamily colors: enabled: true inputs: [] + outputs: + class_name: ColorName flutter: assets: [] diff --git a/packages/core/lib/settings/pubspec.dart b/packages/core/lib/settings/pubspec.dart index 76f388d7a..2f33775f7 100644 --- a/packages/core/lib/settings/pubspec.dart +++ b/packages/core/lib/settings/pubspec.dart @@ -83,7 +83,11 @@ class FlutterGen { @JsonSerializable() class FlutterGenColors { - FlutterGenColors({required this.enabled, required this.inputs}); + FlutterGenColors({ + required this.enabled, + required this.inputs, + this.outputs, + }); @JsonKey(name: 'enabled', required: true) final bool enabled; @@ -91,6 +95,9 @@ class FlutterGenColors { @JsonKey(name: 'inputs', required: true) final List inputs; + @JsonKey(name: 'outputs', required: false) + final FlutterGenElementOutputs? outputs; + factory FlutterGenColors.fromJson(Map json) => _$FlutterGenColorsFromJson(json); } @@ -105,6 +112,7 @@ class FlutterGenAssets { required this.enabled, required this.packageParameterEnabled, required this.style, + this.outputs, }) { if (style != dotDelimiterStyle && style != snakeCaseStyle && @@ -122,6 +130,9 @@ class FlutterGenAssets { @JsonKey(name: 'style', required: true) final String style; + @JsonKey(name: 'outputs', required: false) + final FlutterGenElementOutputs? outputs; + bool get isDotDelimiterStyle => style == dotDelimiterStyle; bool get isSnakeCaseStyle => style == snakeCaseStyle; @@ -134,11 +145,14 @@ class FlutterGenAssets { @JsonSerializable() class FlutterGenFonts { - FlutterGenFonts({required this.enabled}); + FlutterGenFonts({required this.enabled, this.outputs}); @JsonKey(name: 'enabled', required: true) final bool enabled; + @JsonKey(name: 'outputs', required: false) + final FlutterGenElementOutputs? outputs; + factory FlutterGenFonts.fromJson(Map json) => _$FlutterGenFontsFromJson(json); } @@ -162,3 +176,14 @@ class FlutterGenIntegrations { factory FlutterGenIntegrations.fromJson(Map json) => _$FlutterGenIntegrationsFromJson(json); } + +@JsonSerializable() +class FlutterGenElementOutputs { + FlutterGenElementOutputs({this.className}); + + @JsonKey(name: 'class_name', required: false) + final String? className; + + factory FlutterGenElementOutputs.fromJson(Map json) => + _$FlutterGenElementOutputsFromJson(json); +} diff --git a/packages/core/lib/settings/pubspec.g.dart b/packages/core/lib/settings/pubspec.g.dart index ea42e5f82..089a74a27 100644 --- a/packages/core/lib/settings/pubspec.g.dart +++ b/packages/core/lib/settings/pubspec.g.dart @@ -106,6 +106,11 @@ FlutterGenColors _$FlutterGenColorsFromJson(Map json) => $checkedCreate( enabled: $checkedConvert('enabled', (v) => v as bool), inputs: $checkedConvert('inputs', (v) => (v as List).map((e) => e as String).toList()), + outputs: $checkedConvert( + 'outputs', + (v) => v == null + ? null + : FlutterGenElementOutputs.fromJson(v as Map)), ); return val; }, @@ -124,6 +129,11 @@ FlutterGenAssets _$FlutterGenAssetsFromJson(Map json) => $checkedCreate( packageParameterEnabled: $checkedConvert('package_parameter_enabled', (v) => v as bool), style: $checkedConvert('style', (v) => v as String), + outputs: $checkedConvert( + 'outputs', + (v) => v == null + ? null + : FlutterGenElementOutputs.fromJson(v as Map)), ); return val; }, @@ -142,6 +152,11 @@ FlutterGenFonts _$FlutterGenFontsFromJson(Map json) => $checkedCreate( ); final val = FlutterGenFonts( enabled: $checkedConvert('enabled', (v) => v as bool), + outputs: $checkedConvert( + 'outputs', + (v) => v == null + ? null + : FlutterGenElementOutputs.fromJson(v as Map)), ); return val; }, @@ -168,3 +183,16 @@ FlutterGenIntegrations _$FlutterGenIntegrationsFromJson(Map json) => 'flareFlutter': 'flare_flutter' }, ); + +FlutterGenElementOutputs _$FlutterGenElementOutputsFromJson(Map json) => + $checkedCreate( + 'FlutterGenElementOutputs', + json, + ($checkedConvert) { + final val = FlutterGenElementOutputs( + className: $checkedConvert('class_name', (v) => v as String?), + ); + return val; + }, + fieldKeyMap: const {'className': 'class_name'}, + );