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

Refactor config parser with json_serialization #76

Merged
merged 14 commits into from
Feb 6, 2021
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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ format:
build:
cd example && flutter build apk && cd ..

generate-config-model:
cd _internal && dart pub run build_runner build && cd ..
cp _internal/lib/src/* lib/src/settings

generate-with-command:
dart bin/flutter_gen_command.dart --config example/pubspec.yaml

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ $ fluttergen -c example/pubspec.yaml

flutter_gen:
output: lib/gen/ # Optional (default: lib/gen/)
lineLength: 80 # Optional (default: 80)
line_length: 80 # Optional (default: 80)

# Optional
integrations:
Expand Down
17 changes: 17 additions & 0 deletions _internal/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
targets:
$default:
builders:
json_serializable:
options:
any_map: true
checked: true
create_factory: true
create_to_json: false
disallow_unrecognized_keys: false
explicit_to_json: false
field_rename: none
generic_argument_factories: false
ignore_unannotated: false
include_if_null: true
nullable: false

143 changes: 143 additions & 0 deletions _internal/lib/src/pubspec.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import 'package:json_annotation/json_annotation.dart';

part 'pubspec.g.dart';

/// Edit the file under _internal/lib/src/, then run `make generate-config-model`

@JsonSerializable()
class Pubspec {
Pubspec({this.flutterGen, this.flutter});

@JsonKey(name: 'flutter_gen', required: true)
final FlutterGen flutterGen;

@JsonKey(name: 'flutter', required: true)
final Flutter flutter;

factory Pubspec.fromJson(Map json) => _$PubspecFromJson(json);
}

@JsonSerializable()
class Flutter {
Flutter({this.assets, this.fonts});

@JsonKey(name: 'assets', required: true)
final List<String> assets;

@JsonKey(name: 'fonts', required: true)
final List<FlutterFonts> fonts;

factory Flutter.fromJson(Map json) => _$FlutterFromJson(json);
}

@JsonSerializable()
class FlutterFonts {
FlutterFonts({this.family});

@JsonKey(name: 'family', required: true)
final String family;

factory FlutterFonts.fromJson(Map json) => _$FlutterFontsFromJson(json);
}

@JsonSerializable()
class FlutterGen {
FlutterGen({
this.output,
this.lineLength1,
this.lineLength0,
this.assets,
this.integrations,
this.colors,
}) {
// ignore: deprecated_member_use_from_same_package
if (lineLength1 <= 0 && lineLength0 <= 0) {
throw ArgumentError.value(
// ignore: deprecated_member_use_from_same_package
lineLength1 <= 0 ? lineLength1 : lineLength0,
lineLength1 <= 0 ? 'line_length' : 'lineLength',
);
}
// ignore: deprecated_member_use_from_same_package
if (lineLength0 > 0) {
print('Warning: key lineLength is deprecated, use line_length instead.');
}
}

@JsonKey(name: 'output', required: true)
final String output;

@JsonKey(name: 'line_length', required: true)
final int lineLength1;

@deprecated
@JsonKey(name: 'lineLength', required: true)
final int lineLength0;

@JsonKey(name: 'assets', required: true)
final FlutterGenAssets assets;

@JsonKey(name: 'integrations', required: true)
final FlutterGenIntegrations integrations;

@JsonKey(name: 'colors', required: true)
final FlutterGenColors colors;

// Backwards compatible
// ignore: deprecated_member_use_from_same_package
int get lineLength => lineLength0 > 0 ? lineLength0 : lineLength1;

factory FlutterGen.fromJson(Map json) => _$FlutterGenFromJson(json);
}

@JsonSerializable()
class FlutterGenColors {
FlutterGenColors({this.inputs});

@JsonKey(name: 'inputs', required: true)
final List<String> inputs;

factory FlutterGenColors.fromJson(Map json) =>
_$FlutterGenColorsFromJson(json);
}

@JsonSerializable()
class FlutterGenAssets {
static const String dotDelimiterStyle = 'dot-delimiter';
static const String snakeCaseStyle = 'snake-case';
static const String camelCaseStyle = 'camel-case';

FlutterGenAssets({this.style}) {
if (style != dotDelimiterStyle &&
style != snakeCaseStyle &&
style != camelCaseStyle) {
throw ArgumentError.value(style, 'style');
}
}

@JsonKey(name: 'style', required: true)
final String style;

bool get isDotDelimiterStyle => style == dotDelimiterStyle;

bool get isSnakeCaseStyle => style == snakeCaseStyle;

bool get isCamelCaseStyle => style == camelCaseStyle;

factory FlutterGenAssets.fromJson(Map json) =>
_$FlutterGenAssetsFromJson(json);
}

@JsonSerializable()
class FlutterGenIntegrations {
FlutterGenIntegrations({this.flutterSvg, this.flareFlutter});

@JsonKey(name: 'flutter_svg', required: true)
final bool flutterSvg;

@JsonKey(name: 'flare_flutter', required: true)
final bool flareFlutter;

factory FlutterGenIntegrations.fromJson(Map json) =>
_$FlutterGenIntegrationsFromJson(json);
}
109 changes: 109 additions & 0 deletions _internal/lib/src/pubspec.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading