Skip to content

fix: 287 Delete the generated files then flutter_gen won't generate files again #288

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

Merged
merged 4 commits into from
Sep 14, 2022
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
1 change: 0 additions & 1 deletion example/lib/freezed_sample.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// main.dart
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';

part 'freezed_sample.freezed.dart';

Expand Down
38 changes: 8 additions & 30 deletions example/lib/freezed_sample.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,17 @@ class __$$DataCopyWithImpl<$Res> extends _$UnionCopyWithImpl<$Res>

/// @nodoc

class _$Data with DiagnosticableTreeMixin implements Data {
class _$Data implements Data {
const _$Data(this.value);

@override
final int value;

@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
String toString() {
return 'Union(value: $value)';
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty('type', 'Union'))
..add(DiagnosticsProperty('value', value));
}

@override
bool operator ==(dynamic other) {
return identical(this, other) ||
Expand Down Expand Up @@ -216,7 +208,7 @@ class _$Data with DiagnosticableTreeMixin implements Data {
abstract class Data implements Union {
const factory Data(final int value) = _$Data;

int get value => throw _privateConstructorUsedError;
int get value;
@JsonKey(ignore: true)
_$$DataCopyWith<_$Data> get copyWith => throw _privateConstructorUsedError;
}
Expand All @@ -239,20 +231,14 @@ class __$$LoadingCopyWithImpl<$Res> extends _$UnionCopyWithImpl<$Res>

/// @nodoc

class _$Loading with DiagnosticableTreeMixin implements Loading {
class _$Loading implements Loading {
const _$Loading();

@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
String toString() {
return 'Union.loading()';
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty('type', 'Union.loading'));
}

@override
bool operator ==(dynamic other) {
return identical(this, other) ||
Expand Down Expand Up @@ -368,25 +354,17 @@ class __$$ErrorDetailsCopyWithImpl<$Res> extends _$UnionCopyWithImpl<$Res>

/// @nodoc

class _$ErrorDetails with DiagnosticableTreeMixin implements ErrorDetails {
class _$ErrorDetails implements ErrorDetails {
const _$ErrorDetails([this.message]);

@override
final String? message;

@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
String toString() {
return 'Union.error(message: $message)';
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty('type', 'Union.error'))
..add(DiagnosticsProperty('message', message));
}

@override
bool operator ==(dynamic other) {
return identical(this, other) ||
Expand Down Expand Up @@ -476,7 +454,7 @@ class _$ErrorDetails with DiagnosticableTreeMixin implements ErrorDetails {
abstract class ErrorDetails implements Union {
const factory ErrorDetails([final String? message]) = _$ErrorDetails;

String? get message => throw _privateConstructorUsedError;
String? get message;
@JsonKey(ignore: true)
_$$ErrorDetailsCopyWith<_$ErrorDetails> get copyWith =>
throw _privateConstructorUsedError;
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ dependencies:
local_auth: 2.0.1
flutter_hooks: 0.18.3
hooks_riverpod: 1.0.3
# async: 2.9.0
dio: 4.0.6
connectivity_plus: 2.3.0
ua_client_hints: 1.1.0
Expand All @@ -51,7 +50,7 @@ dependencies:
protobuf: 2.0.1
json_serializable: 6.2.0
json_annotation: ^4.5.0
freezed_annotation: 2.0.1
freezed_annotation: ^2.1.0
intl: 0.17.0
fixnum: 1.0.0
shared_preferences: 2.0.13
Expand Down Expand Up @@ -107,6 +106,7 @@ dev_dependencies:
integration_test:
sdk: flutter
build_runner: ^2.1.11
freezed: ^2.1.0+1
flutter_gen_runner: ^4.3.0

flutter_lints: ^2.0.1
Expand Down
55 changes: 24 additions & 31 deletions packages/core/lib/flutter_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'generators/assets_generator.dart';
import 'generators/colors_generator.dart';
import 'generators/fonts_generator.dart';
import 'settings/config.dart';
import 'utils/error.dart';
import 'utils/file.dart';

class FlutterGenerator {
Expand All @@ -23,24 +22,8 @@ class FlutterGenerator {
final String colorsName;
final String fontsName;

Future<Config?> getConfig() async {
Config config;

try {
config = await loadPubspecConfig(pubspecFile);
} on InvalidSettingsException catch (e) {
stderr.writeln(e.message);
return null;
} on FileSystemException catch (e) {
stderr.writeln(e.message);
return null;
}

return config;
}

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

final flutter = config.pubspec.flutter;
Expand All @@ -49,6 +32,16 @@ class FlutterGenerator {
final lineLength = config.pubspec.flutterGen.lineLength;
final formatter = DartFormatter(pageWidth: lineLength, lineEnding: '\n');

void defaultWriter(String contents, String path) {
final file = File(path);
if (!file.existsSync()) {
file.createSync(recursive: true);
}
file.writeAsStringSync(contents);
}

writer ??= defaultWriter;

final absoluteOutput =
Directory(normalize(join(pubspecFile.parent.path, output)));
if (!absoluteOutput.existsSync()) {
Expand All @@ -58,30 +51,30 @@ class FlutterGenerator {
if (flutterGen.colors.enabled && flutterGen.colors.inputs.isNotEmpty) {
final generated =
generateColors(pubspecFile, formatter, flutterGen.colors);
final colors =
File(normalize(join(pubspecFile.parent.path, output, colorsName)));
writeAsString(generated, file: colors);
stdout.writeln('Generated: ${colors.absolute.path}');
final colorsPath =
normalize(join(pubspecFile.parent.path, output, colorsName));
writer(generated, colorsPath);
stdout.writeln('Generated: $colorsPath');
}

if (flutterGen.assets.enabled && flutter.assets.isNotEmpty) {
final generated = generateAssets(
AssetsGenConfig.fromConfig(pubspecFile, config),
formatter,
);
final assets =
File(normalize(join(pubspecFile.parent.path, output, assetsName)));
writeAsString(generated, file: assets);
stdout.writeln('Generated: ${assets.absolute.path}');
final assetsPath =
normalize(join(pubspecFile.parent.path, output, assetsName));
writer(generated, assetsPath);
stdout.writeln('Generated: $assetsPath');
}

if (flutterGen.fonts.enabled && flutter.fonts.isNotEmpty) {
final generated =
generateFonts(formatter, flutter.fonts, flutterGen.fonts);
final fonts =
File(normalize(join(pubspecFile.parent.path, output, fontsName)));
writeAsString(generated, file: fonts);
stdout.writeln('Generated: ${fonts.absolute.path}');
final fontsPath =
normalize(join(pubspecFile.parent.path, output, fontsName));
writer(generated, fontsPath);
stdout.writeln('Generated: $fontsPath');
}

stdout.writeln('FlutterGen finished.');
Expand Down
19 changes: 14 additions & 5 deletions packages/core/lib/settings/config.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:io';

import 'package:flutter_gen_core/utils/error.dart';
import 'package:flutter_gen_core/utils/version.dart';
import 'package:path/path.dart';
import 'package:yaml/yaml.dart';
Expand All @@ -14,23 +15,31 @@ class Config {
final File pubspecFile;
}

Future<Config> loadPubspecConfig(File pubspecFile) async {
Config loadPubspecConfig(File pubspecFile) {
stdout.writeln('$flutterGenVersion Loading ... '
'${normalize(join(
basename(pubspecFile.parent.path),
basename(pubspecFile.path),
))}');
final content = await pubspecFile.readAsString().catchError((dynamic error) {
throw FileSystemException(
'Cannot open pubspec.yaml: ${pubspecFile.absolute}');
});
final content = pubspecFile.readAsStringSync();
final userMap = loadYaml(content) as Map?;
final defaultMap = loadYaml(_defaultConfig) as Map?;
final mergedMap = mergeMap([defaultMap, userMap]);
final pubspec = Pubspec.fromJson(mergedMap);
return Config._(pubspec: pubspec, pubspecFile: pubspecFile);
}

Config? loadPubspecConfigOrNull(File pubspecFile) {
try {
return loadPubspecConfig(pubspecFile);
} on FileSystemException catch (e) {
stderr.writeln(e.message);
} on InvalidSettingsException catch (e) {
stderr.writeln(e.message);
}
return null;
}

const _defaultConfig = '''
name: $invalidStringValue

Expand Down
9 changes: 1 addition & 8 deletions packages/core/lib/utils/file.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
import 'dart:io';

void writeAsString(String text, {required File file}) {
if (!file.existsSync()) {
file.createSync(recursive: true);
}
file.writeAsStringSync(text);
}
typedef FileWriter = void Function(String contents, String path);
2 changes: 1 addition & 1 deletion packages/core/test/assets_gen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void main() {

test('Assets with No lists on pubspec.yaml', () async {
final pubspec = File('test_resources/pubspec_assets_no_list.yaml');
final config = await loadPubspecConfig(pubspec);
final config = loadPubspecConfig(pubspec);
final formatter = DartFormatter(
pageWidth: config.pubspec.flutterGen.lineLength, lineEnding: '\n');

Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/colors_gen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void main() {

test('Wrong colors settings on pubspec.yaml', () async {
final pubspec = File('test_resources/pubspec_colors_no_inputs.yaml');
final config = await loadPubspecConfig(pubspec);
final config = loadPubspecConfig(pubspec);
final formatter = DartFormatter(
pageWidth: config.pubspec.flutterGen.lineLength, lineEnding: '\n');

Expand All @@ -34,7 +34,7 @@ void main() {

test('Wrong colors settings on pubspec.yaml', () async {
final pubspec = File('test_resources/pubspec_colors_no_inputs_list.yaml');
final config = await loadPubspecConfig(pubspec);
final config = loadPubspecConfig(pubspec);
final formatter = DartFormatter(
pageWidth: config.pubspec.flutterGen.lineLength, lineEnding: '\n');

Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/fonts_gen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main() {
});

test('Wrong fonts settings on pubspec.yaml', () async {
final config = await loadPubspecConfig(
final config = loadPubspecConfig(
File('test_resources/pubspec_fonts_no_family.yaml'),
);
final formatter = DartFormatter(
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/gen_test_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Future<void> expectedAssetsGen(
.build();

final pubspecFile = File(pubspec);
final config = await loadPubspecConfig(pubspecFile);
final config = loadPubspecConfig(pubspecFile);
final formatter = DartFormatter(
pageWidth: config.pubspec.flutterGen.lineLength, lineEnding: '\n');

Expand All @@ -42,7 +42,7 @@ Future<void> expectedColorsGen(
.build();

final pubspecFile = File(pubspec);
final config = await loadPubspecConfig(pubspecFile);
final config = loadPubspecConfig(pubspecFile);
final formatter = DartFormatter(
pageWidth: config.pubspec.flutterGen.lineLength, lineEnding: '\n');

Expand All @@ -64,7 +64,7 @@ Future<void> expectedFontsGen(
await FlutterGenerator(File(pubspec), fontsName: basename(generated)).build();

final pubspecFile = File(pubspec);
final config = await loadPubspecConfig(pubspecFile);
final config = loadPubspecConfig(pubspecFile);
final formatter = DartFormatter(
pageWidth: config.pubspec.flutterGen.lineLength, lineEnding: '\n');

Expand Down
Loading