Skip to content
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
10 changes: 6 additions & 4 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ analyzer:
linter:
rules:
# always_use_package_imports: true
avoid_equals_and_hash_code_on_mutable_classes: true
avoid_classes_with_only_static_members: true
avoid_equals_and_hash_code_on_mutable_classes: true
curly_braces_in_flow_control_structures: true
directives_ordering: true
flutter_style_todos: true
prefer_const_constructors: true
prefer_const_constructors_in_immutables: true
prefer_final_in_for_each: true
prefer_int_literals: true
prefer_single_quotes: true
# require_trailing_commas: true
# sort_constructors_first: true
require_trailing_commas: true
sort_constructors_first: true
sort_unnamed_constructors_first: true
unawaited_futures: true
unnecessary_await_in_return: true
use_to_and_as_if_applicable: true
unnecessary_lambdas: true
use_to_and_as_if_applicable: true
use_raw_strings: true
use_super_parameters: false
4 changes: 3 additions & 1 deletion packages/command/bin/flutter_gen_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ void main(List<String> args) async {
}
} on FormatException catch (e) {
stderr.writeAll(
<String>[e.message, 'usage: flutter_gen [options...]', ''], '\n');
<String>[e.message, 'usage: flutter_gen [options...]', ''],
'\n',
);
return;
}

Expand Down
6 changes: 4 additions & 2 deletions packages/command/test/flutter_gen_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ void main() {
'dart',
['bin/flutter_gen_command.dart', '--help'],
);
expect(await process.stdout.next,
equals('-c, --config Set the path of pubspec.yaml.'));
expect(
await process.stdout.next,
equals('-c, --config Set the path of pubspec.yaml.'),
);
final line = await process.stdout.next;
expect(line.trim(), equals('(defaults to "pubspec.yaml")'));
await process.shouldExit(0);
Expand Down
4 changes: 3 additions & 1 deletion packages/core/lib/flutter_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class FlutterGenerator {

Future<void> build({Config? config, FileWriter? writer}) async {
config ??= loadPubspecConfigOrNull(pubspecFile, buildFile: buildFile);
if (config == null) return;
if (config == null) {
return;
}

final flutter = config.pubspec.flutter;
final flutterGen = config.pubspec.flutterGen;
Expand Down
70 changes: 44 additions & 26 deletions packages/core/lib/generators/assets_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,23 @@ Future<String> generateAssets(
}

final integrations = <Integration>[
ImageIntegration(config.packageParameterLiteral,
parseMetadata: config.flutterGen.parseMetadata),
ImageIntegration(
config.packageParameterLiteral,
parseMetadata: config.flutterGen.parseMetadata,
),
if (config.flutterGen.integrations.flutterSvg)
SvgIntegration(config.packageParameterLiteral,
parseMetadata: config.flutterGen.parseMetadata),
SvgIntegration(
config.packageParameterLiteral,
parseMetadata: config.flutterGen.parseMetadata,
),
if (config.flutterGen.integrations.rive)
RiveIntegration(config.packageParameterLiteral),
RiveIntegration(
config.packageParameterLiteral,
),
if (config.flutterGen.integrations.lottie)
LottieIntegration(config.packageParameterLiteral),
LottieIntegration(
config.packageParameterLiteral,
),
];

// Warn for deprecated configs.
Expand Down Expand Up @@ -198,13 +206,16 @@ List<FlavoredAsset> _getAssetRelativePathList(
}
final assetAbsolutePath = join(rootPath, tempAsset.path);
if (FileSystemEntity.isDirectorySync(assetAbsolutePath)) {
assetRelativePathList.addAll(Directory(assetAbsolutePath)
.listSync()
.whereType<File>()
.map(
(e) => tempAsset.copyWith(path: relative(e.path, from: rootPath)),
)
.toList());
assetRelativePathList.addAll(
Directory(assetAbsolutePath)
.listSync()
.whereType<File>()
.map(
(file) =>
tempAsset.copyWith(path: relative(file.path, from: rootPath)),
)
.toList(),
);
} else if (FileSystemEntity.isFileSync(assetAbsolutePath)) {
assetRelativePathList.add(
tempAsset.copyWith(path: relative(assetAbsolutePath, from: rootPath)),
Expand Down Expand Up @@ -376,15 +387,17 @@ Future<String> _dotDelimiterStyleDefinition(
// Add this directory reference to Assets class
// if we are not under the default asset folder
if (dirname(assetType.path) == '.') {
assetsStaticStatements.add(_Statement(
type: className,
filePath: assetType.posixStylePath,
name: assetType.baseName.camelCase(),
value: '$className()',
isConstConstructor: true,
isDirectory: true,
needDartDoc: true,
));
assetsStaticStatements.add(
_Statement(
type: className,
filePath: assetType.posixStylePath,
name: assetType.baseName.camelCase(),
value: '$className()',
isConstConstructor: true,
isDirectory: true,
needDartDoc: true,
),
);
}
}

Expand Down Expand Up @@ -470,10 +483,13 @@ String _flatStyleAssetsClassDefinition(
List<_Statement> statements,
String? packageName,
) {
final statementsBlock =
statements.map((statement) => '''${statement.toDartDocString()}
final statementsBlock = statements
.map(
(statement) => '''${statement.toDartDocString()}
${statement.toStaticFieldString()}
''').join('\n');
''',
)
.join('\n');
final valuesBlock = _assetValuesDefinition(statements, static: true);
return _assetsClassDefinition(
className,
Expand Down Expand Up @@ -506,7 +522,9 @@ String _assetValuesDefinition(
bool static = false,
}) {
final values = statements.where((element) => !element.isDirectory);
if (values.isEmpty) return '';
if (values.isEmpty) {
return '';
}
final names = values.map((value) => value.name).join(', ');
final type = values.every((element) => element.type == values.first.type)
? values.first.type
Expand Down
10 changes: 6 additions & 4 deletions packages/core/lib/generators/colors_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ String generateColors(
) {
if (colorsConfig.inputs.isEmpty) {
throw const InvalidSettingsException(
'The value of "flutter_gen/colors:" is incorrect.');
'The value of "flutter_gen/colors:" is incorrect.',
);
}

final buffer = StringBuffer();
Expand All @@ -39,9 +40,10 @@ String generateColors(
final data = colorFile.file.readAsStringSync();
if (colorFile.isXml) {
colorList.addAll(
XmlDocument.parse(data).findAllElements('color').map((element) {
return _Color.fromXmlElement(element);
}));
XmlDocument.parse(data).findAllElements('color').map((element) {
return _Color.fromXmlElement(element);
}),
);
} else {
throw 'Not supported file type ${colorFile.mime}.';
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/lib/generators/fonts_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ String generateFonts(
final fontsConfig = config.flutterGen.fonts;
if (fonts.isEmpty) {
throw InvalidSettingsException(
'The value of "flutter/fonts:" is incorrect.');
'The value of "flutter/fonts:" is incorrect.',
);
}

final buffer = StringBuffer();
Expand Down
14 changes: 8 additions & 6 deletions packages/core/lib/generators/generator_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ String sBuildDeprecation(
'```yaml',
'flutter_gen:',
...migration,
'```'
'```',
];

final longestLineLength = lines
.map((line) => line
.split('\n')
.sorted((a, b) => b.length.compareTo(b.length))
.first
.length)
.map(
(line) => line
.split('\n')
.sorted((a, b) => b.length.compareTo(b.length))
.first
.length,
)
.sorted((a, b) => b.compareTo(a))
.first;

Expand Down
4 changes: 2 additions & 2 deletions packages/core/lib/generators/integrations/integration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ const String deprecationMessagePackage =
/// Currently only contains the width and height, but could contain more in
/// future.
class ImageMetadata {
const ImageMetadata(this.width, this.height);

final double width;
final double height;

const ImageMetadata(this.width, this.height);
}
40 changes: 25 additions & 15 deletions packages/core/lib/settings/asset_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,26 +142,32 @@ extension AssetTypeIterable on Iterable<AssetType> {
String Function(String) style, {
bool justBasename = false,
}) {
List<UniqueAssetType> assets = map((e) => UniqueAssetType(
assetType: e,
style: style,
needExtension: false,
suffix: '',
basenameOnly: justBasename,
)).toList();
List<UniqueAssetType> assets = map(
(e) => UniqueAssetType(
assetType: e,
style: style,
needExtension: false,
suffix: '',
basenameOnly: justBasename,
),
).toList();

while (true) {
// Check if we have any name collisions.
final dups = assets.groupBy((e) => e.name).values;
final duplicates = assets.groupBy((e) => e.name).values;

// No more duplicates, so we can bail.
if (dups.every((list) => list.length == 1)) break;
if (duplicates.every((list) => list.length == 1)) {
break;
}

// Otherwise start to process the list and mutate the assets as needed.
assets = dups
assets = duplicates
.map((list) {
assert(list.isNotEmpty,
'The groupBy list of assets should not be empty.');
assert(
list.isNotEmpty,
'The groupBy list of assets should not be empty.',
);

// Check the first element in the list. Since we grouped by each
// list element should have the same name.
Expand Down Expand Up @@ -190,7 +196,9 @@ extension AssetTypeIterable on Iterable<AssetType> {
list.forEachIndexed((asset, index) {
// Shouldn't need to mutate the first item (unless it's an invalid
// identifer).
if (index == 0 && isValidIdentifer) return;
if (index == 0 && isValidIdentifer) {
return;
}

// Append a extra suffixes to each item so they hopefully become unique
suffix = '${suffix}_';
Expand All @@ -203,8 +211,10 @@ extension AssetTypeIterable on Iterable<AssetType> {
.toList();
}

assert(assets.map((e) => e.name).distinct().length == assets.length,
'There are duplicate names in the asset list.');
assert(
assets.map((e) => e.name).distinct().length == assets.length,
'There are duplicate names in the asset list.',
);

return assets;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/lib/settings/import.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class Import {

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (identical(this, other)) {
return true;
}
return other is Import &&
identical(other.import, import) &&
identical(other.alias, alias);
Expand Down
Loading