Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/husky-6.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabeef authored Apr 7, 2021
2 parents b87b475 + 0032ace commit 029795b
Show file tree
Hide file tree
Showing 24 changed files with 142 additions and 143 deletions.
9 changes: 3 additions & 6 deletions example/lib/gen/assets.gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ class Assets {
}

class AssetGenImage extends AssetImage {
const AssetGenImage(String assetName)
: _assetName = assetName,
super(assetName);
final String _assetName;
const AssetGenImage(String assetName) : super(assetName, package: 'example');

Image image({
Key? key,
Expand Down Expand Up @@ -139,7 +136,7 @@ class AssetGenImage extends AssetImage {
);
}

String get path => _assetName;
String get path => assetName;
}

class SvgGenImage {
Expand All @@ -151,7 +148,7 @@ class SvgGenImage {
Key? key,
bool matchTextDirection = false,
AssetBundle? bundle,
String? package,
String? package = 'example',
double? width,
double? height,
BoxFit fit = BoxFit.contain,
Expand Down
6 changes: 3 additions & 3 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ packages:
flutter_gen_core:
dependency: transitive
description:
path: "../packages/core"
relative: true
source: path
name: flutter_gen_core
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.2"
flutter_gen_runner:
dependency: "direct dev"
Expand Down
6 changes: 3 additions & 3 deletions packages/command/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ packages:
flutter_gen_core:
dependency: "direct main"
description:
name: flutter_gen_core
url: "https://pub.dartlang.org"
source: hosted
path: "../core"
relative: true
source: path
version: "3.0.2"
glob:
dependency: transitive
Expand Down
3 changes: 3 additions & 0 deletions packages/command/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ dependencies:
dev_dependencies:
effective_dart: '>=1.3.0 <2.0.0'

dependency_overrides:
flutter_gen_core:
path: ../core
6 changes: 3 additions & 3 deletions packages/core/lib/flutter_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class FlutterGenerator {

final output = config.flutterGen.output;
final lineLength = config.flutterGen.lineLength;
final nullSafety = config.flutterGen.nullSafety;
final formatter = DartFormatter(pageWidth: lineLength, lineEnding: '\n');

final absoluteOutput =
Expand All @@ -58,8 +57,9 @@ class FlutterGenerator {

if (config.flutterGen.assets.enabled && config.flutter.assets.isNotEmpty) {
final generated = generateAssets(
pubspecFile, formatter, config.flutterGen, config.flutter.assets,
nullSafety: nullSafety);
AssetsGenConfig.fromConfig(pubspecFile, config),
formatter,
);
final assets =
File(normalize(join(pubspecFile.parent.path, output, assetsName)));
writeAsString(generated, file: assets);
Expand Down
129 changes: 67 additions & 62 deletions packages/core/lib/generators/assets_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:dartx/dartx.dart';
import 'package:path/path.dart';

import '../settings/asset_type.dart';
import '../settings/config.dart';
import '../settings/pubspec.dart';
import '../utils/dart_style/dart_style.dart';
import '../utils/error.dart';
Expand All @@ -15,17 +16,34 @@ import 'integrations/flare_integration.dart';
import 'integrations/integration.dart';
import 'integrations/svg_integration.dart';

class AssetsGenConfig {
AssetsGenConfig._(
this.rootPath,
this.packageName,
this.flutterGen,
this.assets,
);

factory AssetsGenConfig.fromConfig(File pubspecFile, Config config) {
return AssetsGenConfig._(
pubspecFile.parent.path,
config.pubspec.packageName,
config.pubspec.flutterGen,
config.pubspec.flutter.assets,
);
}

final String rootPath;
final String packageName;
final FlutterGen flutterGen;
final List<String> assets;
}

String generateAssets(
File pubspecFile,
AssetsGenConfig config,
DartFormatter formatter,
FlutterGen flutterGen,
List<String> assets, {

// TODO: Until null safety generalizes
// ignore: type_annotate_public_apis
nullSafety = true,
}) {
if (assets.isEmpty) {
) {
if (config.assets.isEmpty) {
throw InvalidSettingsException(
'The value of "flutter/assets:" is incorrect.');
}
Expand All @@ -35,30 +53,29 @@ String generateAssets(

final integrations = <Integration>[
// TODO: Until null safety generalizes
if (flutterGen.integrations.flutterSvg)
SvgIntegration(nullSafety: nullSafety),
if (flutterGen.integrations.flareFlutter)
FlareIntegration(nullSafety: nullSafety),
if (config.flutterGen.integrations.flutterSvg)
SvgIntegration(config.packageName,
nullSafety: config.flutterGen.nullSafety),
if (config.flutterGen.integrations.flareFlutter)
FlareIntegration(nullSafety: config.flutterGen.nullSafety),
];

if (flutterGen.assets.isDotDelimiterStyle) {
classesBuffer.writeln(
_dotDelimiterStyleDefinition(pubspecFile, assets, integrations));
} else if (flutterGen.assets.isSnakeCaseStyle) {
classesBuffer
.writeln(_snakeCaseStyleDefinition(pubspecFile, assets, integrations));
} else if (flutterGen.assets.isCamelCaseStyle) {
classesBuffer
.writeln(_camelCaseStyleDefinition(pubspecFile, assets, integrations));
if (config.flutterGen.assets.isDotDelimiterStyle) {
classesBuffer.writeln(_dotDelimiterStyleDefinition(config, integrations));
} else if (config.flutterGen.assets.isSnakeCaseStyle) {
classesBuffer.writeln(_snakeCaseStyleDefinition(config, integrations));
} else if (config.flutterGen.assets.isCamelCaseStyle) {
classesBuffer.writeln(_camelCaseStyleDefinition(config, integrations));
} else {
throw 'The value of "flutter_gen/assets/style." is incorrect.';
}

// TODO: Until null safety generalizes
if (nullSafety) {
classesBuffer.writeln(_assetGenImageClassDefinition);
if (config.flutterGen.nullSafety) {
classesBuffer.writeln(_assetGenImageClassDefinition(config.packageName));
} else {
classesBuffer.writeln(_assetGenImageClassDefinitionWithNoNullSafety);
classesBuffer.writeln(
_assetGenImageClassDefinitionWithNoNullSafety(config.packageName));
}

final imports = <String>{'package:flutter/widgets.dart'};
Expand All @@ -75,7 +92,7 @@ String generateAssets(
final buffer = StringBuffer();

// TODO: Until null safety generalizes
if (nullSafety) {
if (config.flutterGen.nullSafety) {
buffer.writeln(header);
} else {
buffer.writeln(headerWithNoNullSafety);
Expand All @@ -86,21 +103,20 @@ String generateAssets(
}

List<String> _getAssetRelativePathList(
File pubspecFile,
String rootPath,
List<String> assets,
) {
final assetRelativePathList = <String>[];
for (final assetName in assets) {
final assetAbsolutePath = join(pubspecFile.parent.path, assetName);
final assetAbsolutePath = join(rootPath, assetName);
if (FileSystemEntity.isDirectorySync(assetAbsolutePath)) {
assetRelativePathList.addAll(Directory(assetAbsolutePath)
.listSync()
.whereType<File>()
.map((e) => relative(e.path, from: pubspecFile.parent.path))
.map((e) => relative(e.path, from: rootPath))
.toList());
} else if (FileSystemEntity.isFileSync(assetAbsolutePath)) {
assetRelativePathList
.add(relative(assetAbsolutePath, from: pubspecFile.parent.path));
assetRelativePathList.add(relative(assetAbsolutePath, from: rootPath));
}
}
return assetRelativePathList;
Expand Down Expand Up @@ -130,12 +146,12 @@ AssetType _constructAssetTree(List<String> assetRelativePathList) {
}

_Statement? _createAssetTypeStatement(
File pubspecFile,
String rootPath,
AssetType assetType,
List<Integration> integrations,
String name,
) {
final childAssetAbsolutePath = join(pubspecFile.parent.path, assetType.path);
final childAssetAbsolutePath = join(rootPath, assetType.path);
if (assetType.isSupportedImage) {
return _Statement(
type: 'AssetGenImage',
Expand Down Expand Up @@ -176,27 +192,27 @@ _Statement? _createAssetTypeStatement(

/// Generate style like Assets.foo.bar
String _dotDelimiterStyleDefinition(
File pubspecFile,
List<String> assets,
AssetsGenConfig config,
List<Integration> integrations,
) {
final buffer = StringBuffer();
final assetRelativePathList = _getAssetRelativePathList(pubspecFile, assets);
final assetRelativePathList =
_getAssetRelativePathList(config.rootPath, config.assets);
final assetsStaticStatements = <_Statement>[];

final assetTypeQueue = ListQueue<AssetType>.from(
_constructAssetTree(assetRelativePathList).children);

while (assetTypeQueue.isNotEmpty) {
final assetType = assetTypeQueue.removeFirst();
final assetAbsolutePath = join(pubspecFile.parent.path, assetType.path);
final assetAbsolutePath = join(config.rootPath, assetType.path);

if (FileSystemEntity.isDirectorySync(assetAbsolutePath)) {
final statements = assetType.children
.mapToIsUniqueWithoutExtension()
.map(
(e) => _createAssetTypeStatement(
pubspecFile,
config.rootPath,
e.assetType,
integrations,
(e.isUniqueWithoutExtension
Expand Down Expand Up @@ -234,13 +250,11 @@ String _dotDelimiterStyleDefinition(

/// Generate style like Assets.fooBar
String _camelCaseStyleDefinition(
File pubspecFile,
List<String> assets,
AssetsGenConfig config,
List<Integration> integrations,
) {
return _flatStyleDefinition(
pubspecFile,
assets,
config,
integrations,
(e) => (e.isUniqueWithoutExtension
? withoutExtension(e.assetType.path)
Expand All @@ -252,13 +266,11 @@ String _camelCaseStyleDefinition(

/// Generate style like Assets.foo_bar
String _snakeCaseStyleDefinition(
File pubspecFile,
List<String> assets,
AssetsGenConfig config,
List<Integration> integrations,
) {
return _flatStyleDefinition(
pubspecFile,
assets,
config,
integrations,
(e) => (e.isUniqueWithoutExtension
? withoutExtension(e.assetType.path)
Expand All @@ -269,19 +281,18 @@ String _snakeCaseStyleDefinition(
}

String _flatStyleDefinition(
File pubspecFile,
List<String> assets,
AssetsGenConfig config,
List<Integration> integrations,
String Function(AssetTypeIsUniqueWithoutExtension) createName,
) {
final statements = _getAssetRelativePathList(pubspecFile, assets)
final statements = _getAssetRelativePathList(config.rootPath, config.assets)
.distinct()
.sorted()
.map((relativePath) => AssetType(relativePath))
.mapToIsUniqueWithoutExtension()
.map(
(e) => _createAssetTypeStatement(
pubspecFile,
config.rootPath,
e.assetType,
integrations,
createName(e),
Expand Down Expand Up @@ -322,13 +333,10 @@ class $className {
}

/// Null Safety
const String _assetGenImageClassDefinition = '''
String _assetGenImageClassDefinition(String packageName) => '''
class AssetGenImage extends AssetImage {
const AssetGenImage(String assetName)
: _assetName = assetName,
super(assetName);
final String _assetName;
const AssetGenImage(String assetName) : super(assetName, package: '$packageName');
Image image({
Key? key,
Expand Down Expand Up @@ -373,19 +381,16 @@ class AssetGenImage extends AssetImage {
);
}
String get path => _assetName;
String get path => assetName;
}
''';

/// No Null Safety
/// TODO: Until null safety generalizes
const String _assetGenImageClassDefinitionWithNoNullSafety = '''
String _assetGenImageClassDefinitionWithNoNullSafety(String packageName) => '''
class AssetGenImage extends AssetImage {
const AssetGenImage(String assetName)
: _assetName = assetName,
super(assetName);
final String _assetName;
const AssetGenImage(String assetName) : super(assetName, package: '$packageName');
Image image({
Key key,
Expand Down Expand Up @@ -430,7 +435,7 @@ class AssetGenImage extends AssetImage {
);
}
String get path => _assetName;
String get path => assetName;
}
''';

Expand Down
Loading

0 comments on commit 029795b

Please sign in to comment.