Skip to content

Commit 33549d2

Browse files
authored
Effective dart (#18)
* Folder snake case * No unnecessary getter * Group flutter and flutter_gen to their own package * Move color to colors_generator * Reset pubspec.lock
1 parent a630f9c commit 33549d2

14 files changed

+115
-152
lines changed

lib/src/generators/assets_generator.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import 'package:flutter_gen/src/generators/generator_helper.dart';
66
import 'package:flutter_gen/src/generators/integrations/integration.dart';
77
import 'package:flutter_gen/src/generators/integrations/svg_integration.dart';
88
import 'package:flutter_gen/src/settings/asset_type.dart';
9-
import 'package:flutter_gen/src/settings/flutter/flutter_assets.dart';
10-
import 'package:flutter_gen/src/settings/flutterGen/flutter_gen.dart';
9+
import 'package:flutter_gen/src/settings/flutter.dart';
10+
import 'package:flutter_gen/src/settings/flutter_gen.dart';
1111
import 'package:flutter_gen/src/utils/camel_case.dart';
1212
import 'package:path/path.dart';
1313

lib/src/generators/colors_generator.dart

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import 'package:dart_style/dart_style.dart';
44
import 'package:dartx/dartx.dart';
55
import 'package:flutter_gen/src/generators/generator_helper.dart';
66
import 'package:flutter_gen/src/settings/color_path.dart';
7-
import 'package:flutter_gen/src/settings/color_set.dart';
8-
import 'package:flutter_gen/src/settings/flutterGen/flutter_gen_colors.dart';
7+
import 'package:flutter_gen/src/settings/flutter_gen.dart';
98
import 'package:flutter_gen/src/utils/camel_case.dart';
109
import 'package:flutter_gen/src/utils/color.dart';
1110
import 'package:path/path.dart';
@@ -27,7 +26,7 @@ String generateColors(
2726
buffer.writeln(' ColorName._();');
2827
buffer.writeln();
2928

30-
final colorList = <Color>[];
29+
final colorList = <_Color>[];
3130
colors.inputs
3231
.cast<String>()
3332
.map((file) => ColorPath(join(pubspecFile.parent.path, file)))
@@ -36,7 +35,7 @@ String generateColors(
3635
if (colorFile.isXml) {
3736
colorList.addAll(
3837
XmlDocument.parse(data).findAllElements('color').map((element) {
39-
return Color.fromXmlElement(element);
38+
return _Color.fromXmlElement(element);
4039
}));
4140
} else {
4241
throw 'Not supported file type ${colorFile.mime}.';
@@ -45,14 +44,14 @@ String generateColors(
4544

4645
colorList
4746
.distinctBy((color) => color.hex)
48-
.map(_colorToStatement)
47+
.map(_colorStatement)
4948
.forEach(buffer.writeln);
5049

5150
buffer.writeln('}');
5251
return formatter.format(buffer.toString());
5352
}
5453

55-
String _colorToStatement(Color color) {
54+
String _colorStatement(_Color color) {
5655
final hex = colorFromHex(color.hex);
5756
if (color.type == 'material') {
5857
final swatch = swatchFromPrimaryHex(hex);
@@ -68,3 +67,24 @@ String _colorToStatement(Color color) {
6867
}
6968
throw 'Not supported color type ${color.type}.';
7069
}
70+
71+
class _Color {
72+
const _Color(
73+
this.name,
74+
this.type,
75+
this.hex,
76+
);
77+
78+
_Color.fromXmlElement(XmlElement element)
79+
: this(
80+
element.getAttribute('name'),
81+
element.getAttribute('type'),
82+
element.text,
83+
);
84+
85+
final String name;
86+
87+
final String hex;
88+
89+
final String type;
90+
}

lib/src/generators/fonts_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:dart_style/dart_style.dart';
22
import 'package:flutter_gen/src/generators/generator_helper.dart';
3-
import 'package:flutter_gen/src/settings/flutter/flutter_fonts.dart';
3+
import 'package:flutter_gen/src/settings/flutter.dart';
44
import 'package:flutter_gen/src/utils/camel_case.dart';
55
import 'package:flutter_gen/src/utils/cast.dart';
66
import 'package:yaml/yaml.dart';

lib/src/settings/asset_type.dart

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ import 'package:path/path.dart';
33

44
/// https://github.com/dart-lang/mime/blob/master/lib/src/default_extension_map.dart
55
class AssetType {
6-
AssetType(this._path);
6+
AssetType(this.path);
77

8-
final String _path;
8+
final String path;
99

1010
final List<AssetType> _children = List.empty(growable: true);
1111

12-
String get path => _path;
12+
bool get isDefaultAssetsDirectory => path == 'assets' || path == 'asset';
1313

14-
bool get isDefaultAssetsDirectory => _path == 'assets' || _path == 'asset';
15-
16-
String get mime => lookupMimeType(_path);
14+
String get mime => lookupMimeType(path);
1715

1816
/// https://api.flutter.dev/flutter/widgets/Image-class.html
1917
bool get isSupportedImage {
@@ -32,7 +30,7 @@ class AssetType {
3230

3331
bool get isUnKnownMime => mime == null;
3432

35-
String get baseName => basenameWithoutExtension(_path);
33+
String get baseName => basenameWithoutExtension(path);
3634

3735
List<AssetType> get children => _children;
3836

lib/src/settings/color_path.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import 'package:mime/mime.dart';
44

55
/// https://github.com/dart-lang/mime/blob/master/lib/src/default_extension_map.dart
66
class ColorPath {
7-
const ColorPath(this._path);
7+
const ColorPath(this.path);
88

9-
final String _path;
9+
final String path;
1010

11-
String get path => _path;
11+
File get file => File(path);
1212

13-
File get file => File(_path);
14-
15-
String get mime => lookupMimeType(_path);
13+
String get mime => lookupMimeType(path);
1614

1715
/// https://api.flutter.dev/flutter/widgets/Image-class.html
1816
bool get isXml => mime == 'application/xml';
@@ -23,9 +21,9 @@ class ColorPath {
2321
identical(this, other) ||
2422
other is ColorPath &&
2523
runtimeType == other.runtimeType &&
26-
_path == other._path;
24+
path == other.path;
2725

2826
@override
2927
// ignore: avoid_equals_and_hash_code_on_mutable_classes
30-
int get hashCode => _path.hashCode;
28+
int get hashCode => path.hashCode;
3129
}

lib/src/settings/color_set.dart

Lines changed: 0 additions & 29 deletions
This file was deleted.

lib/src/settings/config.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dart:io';
22

3-
import 'package:flutter_gen/src/settings/flutter/flutter.dart';
4-
import 'package:flutter_gen/src/settings/flutterGen/flutter_gen.dart';
3+
import 'package:flutter_gen/src/settings/flutter.dart';
4+
import 'package:flutter_gen/src/settings/flutter_gen.dart';
55
import 'package:flutter_gen/src/utils/cast.dart';
66
import 'package:path/path.dart';
77
import 'package:yaml/yaml.dart';

lib/src/settings/flutter.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import 'package:flutter_gen/src/utils/cast.dart';
2+
import 'package:yaml/yaml.dart';
3+
4+
class Flutter {
5+
Flutter(YamlMap flutterMap) {
6+
if (flutterMap != null) {
7+
assets = FlutterAssets(safeCast<YamlList>(flutterMap['assets']));
8+
fonts = FlutterFonts(safeCast<YamlList>(flutterMap['fonts']));
9+
}
10+
}
11+
12+
FlutterAssets assets;
13+
14+
FlutterFonts fonts;
15+
16+
bool get hasAssets => assets != null && assets.hasAssets;
17+
18+
bool get hasFonts => fonts != null && fonts.hasFonts;
19+
}
20+
21+
class FlutterAssets {
22+
FlutterAssets(YamlList assets) {
23+
if (assets != null) {
24+
this.assets = assets.cast<String>();
25+
}
26+
}
27+
28+
List<String> assets;
29+
30+
bool get hasAssets => assets != null && assets.isNotEmpty;
31+
}
32+
33+
class FlutterFonts {
34+
FlutterFonts(YamlList fonts) {
35+
if (fonts != null) {
36+
this.fonts = fonts.cast<String>();
37+
}
38+
}
39+
40+
List<String> fonts;
41+
42+
bool get hasFonts => fonts != null && fonts.isNotEmpty;
43+
}

lib/src/settings/flutter/flutter.dart

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/src/settings/flutter/flutter_assets.dart

Lines changed: 0 additions & 15 deletions
This file was deleted.

lib/src/settings/flutter/flutter_fonts.dart

Lines changed: 0 additions & 15 deletions
This file was deleted.

lib/src/settings/flutterGen/flutter_gen_colors.dart

Lines changed: 0 additions & 16 deletions
This file was deleted.

lib/src/settings/flutterGen/flutter_gen_integrations.dart

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import 'dart:io';
22

33
import 'package:flutter_gen/src/settings/config.dart';
4-
import 'package:flutter_gen/src/settings/flutterGen/flutter_gen_colors.dart';
5-
import 'package:flutter_gen/src/settings/flutterGen/flutter_gen_integrations.dart';
64
import 'package:flutter_gen/src/utils/cast.dart';
75
import 'package:yaml/yaml.dart';
86

97
class FlutterGen {
108
FlutterGen(YamlMap flutterGenMap) {
119
if (flutterGenMap != null) {
1210
_output = safeCast<String>(flutterGenMap['output']);
13-
_integrations = FlutterGenIntegrations(
11+
integrations = FlutterGenIntegrations(
1412
safeCast<YamlMap>(flutterGenMap['integrations']));
1513
_lineLength = safeCast<int>(flutterGenMap['lineLength']);
16-
_colors = FlutterGenColors(safeCast<YamlMap>(flutterGenMap['colors']));
14+
colors = FlutterGenColors(safeCast<YamlMap>(flutterGenMap['colors']));
1715
}
1816
}
1917

@@ -28,15 +26,37 @@ class FlutterGen {
2826

2927
int get lineLength => _lineLength ?? Config.DEFAULT_LINE_LENGTH;
3028

31-
FlutterGenIntegrations _integrations;
32-
33-
FlutterGenIntegrations get integrations => _integrations;
29+
FlutterGenIntegrations integrations;
3430

3531
bool get hasIntegrations => integrations != null;
3632

37-
FlutterGenColors _colors;
38-
39-
FlutterGenColors get colors => _colors;
33+
FlutterGenColors colors;
4034

4135
bool get hasColors => colors != null;
4236
}
37+
38+
class FlutterGenColors {
39+
FlutterGenColors(YamlMap flutterGenMap) {
40+
if (flutterGenMap != null) {
41+
inputs = safeCast<YamlList>(flutterGenMap['inputs']);
42+
}
43+
}
44+
45+
YamlList inputs;
46+
47+
bool get hasInputs => inputs != null && inputs.isNotEmpty;
48+
}
49+
50+
class FlutterGenIntegrations {
51+
FlutterGenIntegrations(YamlMap flutterGenMap) {
52+
if (flutterGenMap != null) {
53+
_flutterSvg = safeCast<bool>(flutterGenMap['flutter_svg']);
54+
}
55+
}
56+
57+
bool _flutterSvg;
58+
59+
bool get flutterSvg => _flutterSvg ?? false;
60+
61+
bool get hasFlutterSvg => flutterSvg;
62+
}

0 commit comments

Comments
 (0)