Skip to content

Commit

Permalink
Change gen-class-name for fonts colors assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Cu-Toof authored and toantq93 committed Aug 15, 2022
1 parent de64e75 commit 0e66987
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 28 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,16 +739,25 @@ flutter_gen:
# - snake-case
# - dot-delimiter
style: dot-delimiter
# Optional
outputs:
class_name: Assets
fonts:
# Optional
enabled: true
# Optional
outputs:
class_name: FontFamily
colors:
# Optional
enabled: true
# Optional
inputs: []
# Optional
outputs:
class_name: ColorName
flutter:
# See: https://flutter.dev/docs/development/ui/assets-and-images#specifying-assets
Expand Down
13 changes: 10 additions & 3 deletions packages/core/lib/flutter_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ class FlutterGenerator {
}

if (flutterGen.colors.enabled && flutterGen.colors.inputs.isNotEmpty) {
final generated =
generateColors(pubspecFile, formatter, flutterGen.colors);
final generated = generateColors(
pubspecFile,
formatter,
flutterGen.colors,
);
final colors =
File(normalize(join(pubspecFile.parent.path, output, colorsName)));
writeAsString(generated, file: colors);
Expand All @@ -76,7 +79,11 @@ class FlutterGenerator {
}

if (flutterGen.fonts.enabled && flutter.fonts.isNotEmpty) {
final generated = generateFonts(formatter, flutter.fonts);
final generated = generateFonts(
formatter,
flutterGen.fonts,
flutter.fonts,
);
final fonts =
File(normalize(join(pubspecFile.parent.path, output, fontsName)));
writeAsString(generated, file: fonts);
Expand Down
32 changes: 16 additions & 16 deletions packages/core/lib/generators/assets_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ String _dotDelimiterStyleDefinition(
List<Integration> integrations,
) {
final buffer = StringBuffer();
final className = config.flutterGen.assets.outputs?.className;
final assetRelativePathList =
_getAssetRelativePathList(config.rootPath, config.assets);
final assetsStaticStatements = <_Statement>[];
Expand Down Expand Up @@ -249,16 +250,14 @@ String _dotDelimiterStyleDefinition(
assetTypeQueue.addAll(assetType.children);
}
}
buffer
.writeln(_dotDelimiterStyleAssetsClassDefinition(assetsStaticStatements));
buffer.writeln(_dotDelimiterStyleAssetsClassDefinition(
className, assetsStaticStatements));
return buffer.toString();
}

/// Generate style like Assets.fooBar
String _camelCaseStyleDefinition(
AssetsGenConfig config,
List<Integration> integrations,
) {
AssetsGenConfig config, List<Integration> integrations) {
return _flatStyleDefinition(
config,
integrations,
Expand All @@ -272,9 +271,7 @@ String _camelCaseStyleDefinition(

/// Generate style like Assets.foo_bar
String _snakeCaseStyleDefinition(
AssetsGenConfig config,
List<Integration> integrations,
) {
AssetsGenConfig config, List<Integration> integrations) {
return _flatStyleDefinition(
config,
integrations,
Expand Down Expand Up @@ -306,27 +303,30 @@ 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
}
Expand Down
11 changes: 6 additions & 5 deletions packages/core/lib/generators/colors_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? 'FontFamily';
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();
Expand Down
6 changes: 4 additions & 2 deletions packages/core/lib/generators/fonts_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'generator_helper.dart';

String generateFonts(
DartFormatter formatter,
FlutterGenFonts genFonts,
List<FlutterFonts> fonts,
) {
if (fonts.isEmpty) {
Expand All @@ -18,10 +19,11 @@ String generateFonts(
}

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) {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/lib/settings/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
Expand Down
29 changes: 27 additions & 2 deletions packages/core/lib/settings/pubspec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,21 @@ 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;

@JsonKey(name: 'inputs', required: true)
final List<String> inputs;

@JsonKey(name: 'outputs', required: false)
final FlutterGenElementOutputs? outputs;

factory FlutterGenColors.fromJson(Map json) =>
_$FlutterGenColorsFromJson(json);
}
Expand All @@ -105,6 +112,7 @@ class FlutterGenAssets {
required this.enabled,
required this.packageParameterEnabled,
required this.style,
this.outputs,
}) {
if (style != dotDelimiterStyle &&
style != snakeCaseStyle &&
Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -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);
}
28 changes: 28 additions & 0 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.

0 comments on commit 0e66987

Please sign in to comment.