Skip to content

Commit

Permalink
fixes: remove duplicate imports
Browse files Browse the repository at this point in the history
  • Loading branch information
kishormainali committed Nov 28, 2023
1 parent b666cfb commit 03e37d4
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# 1.0.1
- remove duplicate imports from generated code

# 1.0.0
- initial release
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Type Register Generator
[![pub package](https://img.shields.io/pub/v/type_register.svg)](https://pub.dev/packages/type_register)

Code generator for creating helper functions for registering Hive types. It will generate method named registerAdapters with all the hive adapters for the types annotated with @HiveType.

Expand Down
3 changes: 3 additions & 0 deletions example/lib/another_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ part 'another_model.g.dart';

@HiveType(typeId: 1)
class AnotherModel {}

@HiveType(typeId: 2)
class AnothorAModel {}
25 changes: 25 additions & 0 deletions example/lib/another_model.g.dart

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

1 change: 1 addition & 0 deletions example/lib/src/core/register_adapters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import 'package:hive_local_storage/hive_local_storage.dart';

void registerAdapters() {
Hive.registerAdapter(AnotherModelAdapter());
Hive.registerAdapter(AnothorAModelAdapter());
Hive.registerAdapter(ModelAdapter());
}
29 changes: 19 additions & 10 deletions lib/src/type_register_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ class TypeRegisterBuilder implements Builder {

@override
FutureOr<void> build(BuildStep buildStep) async {
String hiveImport = await _parseHiveImport(buildStep);
final hiveImport = await _parseHiveImport(buildStep);
if (hiveImport == null || hiveImport.isEmpty) {
log.warning(
'hive or hive_generator is not in dependencies or dev_dependencies. please add hive or hive_local_storage to dependencies and hive_generator to dev_dependencies in pubspec.yaml.');
return;
}

/// parse output path from build.yaml
final outputPath = options.config['output_path'] as String;
Expand Down Expand Up @@ -63,11 +68,16 @@ class TypeRegisterBuilder implements Builder {
}
}

final imports = files.map((e) => Directive.import(e.uri)).toList();
/// generate unique imports
final uniqueImports = <String>{};
for (var element in files) {
uniqueImports.add(element.uri);
}
final imports = uniqueImports.map((uri) => Directive.import(uri)).toList();
imports.add(Directive.import(hiveImport));
imports.sort((a, b) => a.compareTo(b));

final library = Library(
var library = Library(
(builder) => builder
..directives.addAll(imports)
..body.addAll(
Expand All @@ -79,7 +89,6 @@ class TypeRegisterBuilder implements Builder {
],
),
);

final emitter = DartEmitter(
orderDirectives: true,
useNullSafetySyntax: true,
Expand All @@ -97,23 +106,23 @@ class TypeRegisterBuilder implements Builder {
};
}

FutureOr<String> _parseHiveImport(BuildStep buildStep) async {
FutureOr<String?> _parseHiveImport(BuildStep buildStep) async {
String? hiveImport;
final pubspecYaml = await buildStep
.readAsString(AssetId(buildStep.inputId.package, 'pubspec.yaml'));
final pubspec = loadYaml(pubspecYaml) as Map;
final dependenciesMap = pubspec['dependencies'] as Map;
final devDependenciesMap = pubspec['dev_dependencies'] as Map;

String hiveImport = '';
if (!devDependenciesMap.containsKey('hive_generator')) {
return null;
}

if (dependenciesMap.containsKey('hive')) {
hiveImport = 'package:hive/hive.dart';
} else if (dependenciesMap.containsKey('hive_local_storage')) {
hiveImport = 'package:hive_local_storage/hive_local_storage.dart';
}
if (hiveImport.isEmpty) {
throw Exception(
'You must add `hive` or `hive_local_storage` to your pubspec.yaml');
}
return hiveImport;
}
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: type_register
description: Generator which generates method named registerAdapters with all the hive adapters for the types annotated with @HiveType.
version: 1.0.0
version: 1.0.1
topics:
- type-register-generator
- dart
Expand All @@ -14,7 +14,6 @@ repository: https://github.com/kishormainali/type_register
environment:
sdk: ">=3.1.5 <4.0.0"


dependencies:
analyzer: ">=6.0.0 <7.0.0"
build: ^2.4.1
Expand All @@ -30,5 +29,6 @@ dependencies:
equatable: ^2.0.5



dev_dependencies:
flutter_lints: ^3.0.1

0 comments on commit 03e37d4

Please sign in to comment.