From 655ef51c2a29e59414d3606b288ca321488e365c Mon Sep 17 00:00:00 2001 From: Cu-Toof Date: Sun, 4 Sep 2022 12:02:22 +0700 Subject: [PATCH] Change gen-class-name for fonts colors assets --- README.md | 12 ++++++++ packages/core/lib/flutter_generator.dart | 6 +++- .../core/lib/generators/assets_generator.dart | 28 +++++++++++------- .../core/lib/generators/colors_generator.dart | 11 +++---- .../core/lib/generators/fonts_generator.dart | 10 ++++--- packages/core/lib/settings/config.dart | 6 ++++ packages/core/lib/settings/pubspec.dart | 29 +++++++++++++++++-- packages/core/lib/settings/pubspec.g.dart | 28 ++++++++++++++++++ 8 files changed, 108 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index d720352b..04fc392f 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 8ff32d5f..067b97ce 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 f3a70b3d..accf25d1 100644 --- a/packages/core/lib/generators/assets_generator.dart +++ b/packages/core/lib/generators/assets_generator.dart @@ -213,6 +213,7 @@ String _dotDelimiterStyleDefinition( List integrations, ) { final buffer = StringBuffer(); + final className = config.flutterGen.assets.outputs?.className; final assetRelativePathList = _getAssetRelativePathList( config.rootPath, config.assets, @@ -266,8 +267,8 @@ String _dotDelimiterStyleDefinition( assetTypeQueue.addAll(assetType.children); } } - buffer - .writeln(_dotDelimiterStyleAssetsClassDefinition(assetsStaticStatements)); + buffer.writeln(_dotDelimiterStyleAssetsClassDefinition( + className, assetsStaticStatements)); return buffer.toString(); } @@ -327,27 +328,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 37ae1dd5..e421637f 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 f96e597a..a67ccdd1 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 da08b0c0..aa962085 100644 --- a/packages/core/lib/settings/config.dart +++ b/packages/core/lib/settings/config.dart @@ -47,14 +47,20 @@ flutter_gen: enabled: true package_parameter_enabled: false style: dot-delimiter + outputs: + class_name: Assets exclude: [] 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 637c715b..5cb38a41 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, required this.exclude, }) { if (style != dotDelimiterStyle && @@ -123,6 +131,9 @@ class FlutterGenAssets { @JsonKey(name: 'style', required: true) final String style; + @JsonKey(name: 'outputs', required: false) + final FlutterGenElementOutputs? outputs; + @JsonKey(name: 'exclude', required: true) final List exclude; @@ -138,11 +149,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); } @@ -166,3 +180,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 245adcb6..15d7b4a6 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; }, @@ -129,6 +134,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)), exclude: $checkedConvert('exclude', (v) => (v as List).map((e) => e as String).toList()), ); @@ -149,6 +159,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; }, @@ -175,3 +190,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'}, + );