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 committed Sep 2, 2022
1 parent de64e75 commit 51a24fb
Show file tree
Hide file tree
Showing 18 changed files with 318 additions and 55 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
@@ -1,4 +1,4 @@
{
"dart.flutterSdkPath": "~/.asdf/installs/flutter/3.0.1-stable",
"dart.sdkPath": "~/.asdf/installs/dart/2.17.1/dart-sdk"
"dart.flutterSdkPath": "~/.asdf/installs/flutter/3.0.5-stable",
"dart.sdkPath": "~/.asdf/installs/dart/2.17.6/dart-sdk"
}
12 changes: 12 additions & 0 deletions README.md
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
17 changes: 0 additions & 17 deletions example/lib/gen/assets.gen.dart
Expand Up @@ -14,13 +14,6 @@ import 'package:flare_flutter/flare_actor.dart';
import 'package:flare_flutter/flare_controller.dart';
import 'package:rive/rive.dart';

class $PicturesGen {
const $PicturesGen();

/// File path: pictures/chip5.jpg
AssetGenImage get chip5 => const AssetGenImage('pictures/chip5.jpg');
}

class $AssetsFlareGen {
const $AssetsFlareGen();

Expand All @@ -37,7 +30,6 @@ class $AssetsImagesGen {
/// File path: assets/images/chip2.jpg
AssetGenImage get chip2 => const AssetGenImage('assets/images/chip2.jpg');

$AssetsImagesChip3Gen get chip3 => const $AssetsImagesChip3Gen();
$AssetsImagesChip4Gen get chip4 => const $AssetsImagesChip4Gen();
$AssetsImagesIconsGen get icons => const $AssetsImagesIconsGen();

Expand Down Expand Up @@ -87,14 +79,6 @@ class $AssetsUnknownGen {
String get unknownMimeType => 'assets/unknown/unknown_mime_type.bk';
}

class $AssetsImagesChip3Gen {
const $AssetsImagesChip3Gen();

/// File path: assets/images/chip3/chip3.jpg
AssetGenImage get chip3 =>
const AssetGenImage('assets/images/chip3/chip3.jpg');
}

class $AssetsImagesChip4Gen {
const $AssetsImagesChip4Gen();

Expand Down Expand Up @@ -130,7 +114,6 @@ class Assets {
static const $AssetsMovieGen movie = $AssetsMovieGen();
static const $AssetsRiveGen rive = $AssetsRiveGen();
static const $AssetsUnknownGen unknown = $AssetsUnknownGen();
static const $PicturesGen pictures = $PicturesGen();
}

class AssetGenImage {
Expand Down
12 changes: 6 additions & 6 deletions example/lib/main.dart
Expand Up @@ -55,12 +55,12 @@ void main() async {
width: 120,
height: 120,
),
Assets.pictures.chip5.image(
key: const Key("chip5"),
width: 120,
height: 120,
fit: BoxFit.scaleDown,
),
// Assets.pictures.chip5.image(
// key: const Key("chip5"),
// width: 120,
// height: 120,
// fit: BoxFit.scaleDown,
// ),
const Text(
'Hi there, I\'m FlutterGen',
style: TextStyle(
Expand Down
6 changes: 5 additions & 1 deletion example/pubspec.yaml
Expand Up @@ -7,7 +7,7 @@ version: 1.0.0+2

environment:
sdk: ">=2.16.0 <3.0.0"
flutter: ">=2.10.4"
flutter: ">=3.0.0"

dependencies:
flutter:
Expand Down Expand Up @@ -131,6 +131,10 @@ flutter_gen:

# Assets.images.chip (default style)
# style: dot-delimiter
exclude:
- assets/images/chip3/chip3.jpg
- pictures/chip5.jpg
- assets/flare/

fonts:
enabled: true
Expand Down
2 changes: 1 addition & 1 deletion example_resources/pubspec.yaml
Expand Up @@ -7,7 +7,7 @@ version: 1.0.0+2

environment:
sdk: ">=2.16.0 <3.0.0"
flutter: ">=2.10.4"
flutter: ">=3.0.0"

dependencies:
flutter:
Expand Down
6 changes: 5 additions & 1 deletion packages/core/lib/flutter_generator.dart
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
57 changes: 43 additions & 14 deletions packages/core/lib/generators/assets_generator.dart
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';
import 'package:collection/collection.dart';
import 'package:dart_style/dart_style.dart';
import 'package:dartx/dartx.dart';
import 'package:glob/glob.dart';
import 'package:path/path.dart';

import '../settings/asset_type.dart';
Expand All @@ -23,6 +24,7 @@ class AssetsGenConfig {
this._packageName,
this.flutterGen,
this.assets,
this.exclude,
);

factory AssetsGenConfig.fromConfig(File pubspecFile, Config config) {
Expand All @@ -31,13 +33,17 @@ class AssetsGenConfig {
config.pubspec.packageName,
config.pubspec.flutterGen,
config.pubspec.flutter.assets,
config.pubspec.flutterGen.assets.exclude
.map((pattern) => Glob(pattern))
.toList(),
);
}

final String rootPath;
final String _packageName;
final FlutterGen flutterGen;
final List<String> assets;
final List<Glob> exclude;

String get packageParameterLiteral =>
flutterGen.assets.packageParameterEnabled ? _packageName : '';
Expand Down Expand Up @@ -99,6 +105,7 @@ String generateAssets(
List<String> _getAssetRelativePathList(
String rootPath,
List<String> assets,
List<Glob> excludes,
) {
final assetRelativePathList = <String>[];
for (final assetName in assets) {
Expand All @@ -113,7 +120,14 @@ List<String> _getAssetRelativePathList(
assetRelativePathList.add(relative(assetAbsolutePath, from: rootPath));
}
}
return assetRelativePathList;

if (excludes.isEmpty) {
return assetRelativePathList;
}

return assetRelativePathList
.where((file) => !excludes.any((exclude) => exclude.matches(file)))
.toList();
}

AssetType _constructAssetTree(List<String> assetRelativePathList) {
Expand Down Expand Up @@ -199,8 +213,12 @@ String _dotDelimiterStyleDefinition(
List<Integration> integrations,
) {
final buffer = StringBuffer();
final assetRelativePathList =
_getAssetRelativePathList(config.rootPath, config.assets);
final className = config.flutterGen.assets.outputs?.className;
final assetRelativePathList = _getAssetRelativePathList(
config.rootPath,
config.assets,
config.exclude,
);
final assetsStaticStatements = <_Statement>[];

final assetTypeQueue = ListQueue<AssetType>.from(
Expand Down Expand Up @@ -249,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 @@ -291,7 +309,11 @@ String _flatStyleDefinition(
List<Integration> integrations,
String Function(AssetTypeIsUniqueWithoutExtension) createName,
) {
final statements = _getAssetRelativePathList(config.rootPath, config.assets)
final statements = _getAssetRelativePathList(
config.rootPath,
config.assets,
config.exclude,
)
.distinct()
.sorted()
.map((relativePath) => AssetType(relativePath))
Expand All @@ -306,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
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
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
7 changes: 7 additions & 0 deletions packages/core/lib/settings/config.dart
Expand Up @@ -47,13 +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

0 comments on commit 51a24fb

Please sign in to comment.