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

Change gen-class-name for fonts colors assets [PR-280](https://github.com/FlutterGen/flutter_gen/pull/280) #286

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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion packages/core/lib/flutter_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 18 additions & 10 deletions packages/core/lib/generators/assets_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ String _dotDelimiterStyleDefinition(
List<Integration> integrations,
) {
final buffer = StringBuffer();
final className = config.flutterGen.assets.outputs?.className;
final assetRelativePathList = _getAssetRelativePathList(
config.rootPath,
config.assets,
Expand Down Expand Up @@ -266,8 +267,8 @@ String _dotDelimiterStyleDefinition(
assetTypeQueue.addAll(assetType.children);
}
}
buffer
.writeln(_dotDelimiterStyleAssetsClassDefinition(assetsStaticStatements));
buffer.writeln(_dotDelimiterStyleAssetsClassDefinition(
className, assetsStaticStatements));
return buffer.toString();
}

Expand Down Expand Up @@ -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
}
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 ?? '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();
Expand Down
10 changes: 6 additions & 4 deletions packages/core/lib/generators/fonts_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ import 'generator_helper.dart';

String generateFonts(
DartFormatter formatter,
List<FlutterFonts> fonts,
) {
List<FlutterFonts> 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) {
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,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: []
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,
required this.exclude,
}) {
if (style != dotDelimiterStyle &&
Expand All @@ -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<String> exclude;

Expand All @@ -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);
}

Expand All @@ -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);
}
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.