Skip to content

Commit

Permalink
feat: add type parameter on registerAdapter method
Browse files Browse the repository at this point in the history
  • Loading branch information
kishormainali committed Feb 20, 2024
1 parent 303a5b7 commit 7ccdc62
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 45 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

# .github/workflows/publish.yml
name: Publish to pub.dev
on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+*' # for tags like: 'v1.2.3'

jobs:
publish:
runs-on: ubuntu-latest
steps:

- name: 📚 Git Checkout
uses: actions/checkout@v4

- name: 🐦 Setup Flutter
uses: subosito/flutter-action@v2
with:
channel: stable
cache: true

- name: 📦 Install Dependencies
run: flutter pub get

- name: 🔐 Setup Pub Credentials
run: |
mkdir -p $XDG_CONFIG_HOME/dart
echo "$XDG_CONFIG_HOME/dart/pub-credentials.json"
cat <<EOF > "$XDG_CONFIG_HOME/dart/pub-credentials.json"
{
"accessToken":"${{ secrets.OAUTH_ACCESS_TOKEN }}",
"refreshToken":"${{ secrets.OAUTH_REFRESH_TOKEN }}",
"tokenEndpoint":"${{ secrets.OAUTH_TOKEN_ENDPOINT }}",
"scopes": ${{ secrets.OAUTH_SCOPE }},
"expiration": ${{ secrets.OAUTH_EXPIRY_TIME }}
}
EOF
- name: 📝 Format Code
run: dart format .

- name: 📝 Analyze Code
run: dart analyze .

- name: 🚀 Publish package
run: dart pub publish -f%
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.0.3
- added type parameter on `registerAdapter` method
- updated min dart sdk version to 3.2.6

# 1.0.2
- added comments on generated code

Expand Down
11 changes: 11 additions & 0 deletions example/.flutter-plugins
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This is a generated file; do not edit or check into version control.
flutter_secure_storage=/Users/kishor/.pub-cache/hosted/pub.dev/flutter_secure_storage-9.0.0/
flutter_secure_storage_linux=/Users/kishor/.pub-cache/hosted/pub.dev/flutter_secure_storage_linux-1.2.0/
flutter_secure_storage_macos=/Users/kishor/.pub-cache/hosted/pub.dev/flutter_secure_storage_macos-3.0.1/
flutter_secure_storage_web=/Users/kishor/.pub-cache/hosted/pub.dev/flutter_secure_storage_web-1.1.2/
flutter_secure_storage_windows=/Users/kishor/.pub-cache/hosted/pub.dev/flutter_secure_storage_windows-3.0.0/
path_provider=/Users/kishor/.pub-cache/hosted/pub.dev/path_provider-2.1.2/
path_provider_android=/Users/kishor/.pub-cache/hosted/pub.dev/path_provider_android-2.2.2/
path_provider_foundation=/Users/kishor/.pub-cache/hosted/pub.dev/path_provider_foundation-2.3.2/
path_provider_linux=/Users/kishor/.pub-cache/hosted/pub.dev/path_provider_linux-2.2.1/
path_provider_windows=/Users/kishor/.pub-cache/hosted/pub.dev/path_provider_windows-2.2.1/
1 change: 1 addition & 0 deletions example/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_secure_storage","path":"/Users/kishor/.pub-cache/hosted/pub.dev/flutter_secure_storage-9.0.0/","native_build":true,"dependencies":[]},{"name":"path_provider_foundation","path":"/Users/kishor/.pub-cache/hosted/pub.dev/path_provider_foundation-2.3.2/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"android":[{"name":"flutter_secure_storage","path":"/Users/kishor/.pub-cache/hosted/pub.dev/flutter_secure_storage-9.0.0/","native_build":true,"dependencies":[]},{"name":"path_provider_android","path":"/Users/kishor/.pub-cache/hosted/pub.dev/path_provider_android-2.2.2/","native_build":true,"dependencies":[]}],"macos":[{"name":"flutter_secure_storage_macos","path":"/Users/kishor/.pub-cache/hosted/pub.dev/flutter_secure_storage_macos-3.0.1/","native_build":true,"dependencies":[]},{"name":"path_provider_foundation","path":"/Users/kishor/.pub-cache/hosted/pub.dev/path_provider_foundation-2.3.2/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"linux":[{"name":"flutter_secure_storage_linux","path":"/Users/kishor/.pub-cache/hosted/pub.dev/flutter_secure_storage_linux-1.2.0/","native_build":true,"dependencies":[]},{"name":"path_provider_linux","path":"/Users/kishor/.pub-cache/hosted/pub.dev/path_provider_linux-2.2.1/","native_build":false,"dependencies":[]}],"windows":[{"name":"flutter_secure_storage_windows","path":"/Users/kishor/.pub-cache/hosted/pub.dev/flutter_secure_storage_windows-3.0.0/","native_build":true,"dependencies":[]},{"name":"path_provider_windows","path":"/Users/kishor/.pub-cache/hosted/pub.dev/path_provider_windows-2.2.1/","native_build":false,"dependencies":[]}],"web":[{"name":"flutter_secure_storage_web","path":"/Users/kishor/.pub-cache/hosted/pub.dev/flutter_secure_storage_web-1.1.2/","dependencies":[]}]},"dependencyGraph":[{"name":"flutter_secure_storage","dependencies":["flutter_secure_storage_linux","flutter_secure_storage_macos","flutter_secure_storage_web","flutter_secure_storage_windows"]},{"name":"flutter_secure_storage_linux","dependencies":[]},{"name":"flutter_secure_storage_macos","dependencies":[]},{"name":"flutter_secure_storage_web","dependencies":[]},{"name":"flutter_secure_storage_windows","dependencies":["path_provider"]},{"name":"path_provider","dependencies":["path_provider_android","path_provider_foundation","path_provider_linux","path_provider_windows"]},{"name":"path_provider_android","dependencies":[]},{"name":"path_provider_foundation","dependencies":[]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_windows","dependencies":[]}],"date_created":"2024-02-20 13:44:30.387915","version":"3.19.0"}
2 changes: 1 addition & 1 deletion example/lib/another_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ part 'another_model.g.dart';
@HiveType(typeId: 1)
class AnotherModel {}

@HiveType(typeId: 2)
@HiveType(typeId: 2, adapterName: 'ModelAAdapter')
class AnothorAModel {}
4 changes: 2 additions & 2 deletions example/lib/another_model.g.dart

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

12 changes: 6 additions & 6 deletions example/lib/src/core/register_adapters.dart

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

8 changes: 6 additions & 2 deletions lib/src/_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import 'package:equatable/equatable.dart';
class Adapter extends Equatable {
/// {@macro adapter}
const Adapter({
required this.name,
required this.adapterName,
required this.uri,
});

/// name of model
final String name;

/// name of the adapter extracted from the annotation
final String adapterName;

Expand All @@ -18,9 +22,9 @@ class Adapter extends Equatable {

@override
String toString() {
return 'Adapter(adapterName: $adapterName, uri: $uri)';
return 'Adapter(name:$name,adapterName: $adapterName, uri: $uri)';
}

@override
List<Object?> get props => [adapterName, uri];
List<Object?> get props => [name, adapterName, uri];
}
41 changes: 17 additions & 24 deletions lib/src/type_register_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class TypeRegisterBuilder implements Builder {
FutureOr<void> build(BuildStep buildStep) async {
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.');
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;
}

Expand All @@ -45,25 +44,27 @@ class TypeRegisterBuilder implements Builder {
final files = <Adapter>[];
await for (final input in buildStep.findAssets(_allFilesInLib)) {
final library = await buildStep.resolver.libraryFor(input);
final classesInLibrary = LibraryReader(library)
.annotatedWith(const TypeChecker.fromRuntime(HiveType));
final classesInLibrary = LibraryReader(library).annotatedWith(const TypeChecker.fromRuntime(HiveType));
final sortedClasses = classesInLibrary.toList()
..sort((a, b) {
final aTypeId = a.annotation.read('typeId').intValue;
final bTypeId = b.annotation.read('typeId').intValue;
return aTypeId.compareTo(bTypeId);
});
for (final model in sortedClasses) {
final adapterName = model.annotation.read('adapterName').isNull
? null
: model.annotation.read('adapterName').stringValue;
final adapterName = model.annotation.read('adapterName').literalValue as String?;
if (adapterName != null && adapterName.isNotEmpty) {
files.add(
Adapter(adapterName: adapterName, uri: input.uri.toString()));
files.add(Adapter(
name: model.element.name!,
adapterName: adapterName,
uri: input.uri.toString(),
));
} else {
files.add(Adapter(
adapterName: '${model.element.name}Adapter',
uri: input.uri.toString()));
name: model.element.name!,
adapterName: '${model.element.name}Adapter',
uri: input.uri.toString(),
));
}
}
}
Expand All @@ -82,22 +83,15 @@ class TypeRegisterBuilder implements Builder {
..comments.addAll([
'GENERATED CODE - DO NOT MODIFY BY HAND',
'ignore_for_file: lines_longer_than_80_chars',
'******************************************************************',
'Type Adapters',
'******************************************************************'
])
..directives.addAll(imports)
..body.addAll(
[
const Code('\n'),
const Code(
'//*****************************************************************************'),
const Code('\n'),
const Code('// Type Adapters'),
const Code('\n'),
const Code(
'//*****************************************************************************'),
const Code('\n'),
const Code('void registerAdapters(){'),
...files
.map((e) => Code('Hive.registerAdapter(${e.adapterName}());')),
...files.map((file) => Code('Hive.registerAdapter<${file.name}>(${file.adapterName}());')),
const Code('}'),
],
),
Expand All @@ -121,8 +115,7 @@ class TypeRegisterBuilder implements Builder {

FutureOr<String?> _parseHiveImport(BuildStep buildStep) async {
String? hiveImport;
final pubspecYaml = await buildStep
.readAsString(AssetId(buildStep.inputId.package, 'pubspec.yaml'));
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;
Expand Down
3 changes: 1 addition & 2 deletions lib/type_register.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ import 'package:build/build.dart';
import 'src/type_register_builder.dart';

/// Builder for type register.
Builder registerAdapters(BuilderOptions options) =>
TypeRegisterBuilder(options);
Builder registerAdapters(BuilderOptions options) => TypeRegisterBuilder(options);
16 changes: 8 additions & 8 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: type_register
description: Generator which generates method named registerAdapters with all the hive adapters for the types annotated with @HiveType.
version: 1.0.2
topics:
version: 1.0.3
topics:
- type-register-generator
- dart
- hive
Expand All @@ -12,19 +12,19 @@ homepage: https://github.com/kishormainali/type_register
repository: https://github.com/kishormainali/type_register

environment:
sdk: ">=3.1.5 <4.0.0"
sdk: ">=3.2.6 <4.0.0"

dependencies:
analyzer: ">=6.0.0 <7.0.0"
dependencies:
analyzer: ">=6.4.1 <7.0.0"
build: ^2.4.1
code_builder: ^4.8.0
code_builder: ^4.10.0
dart_style: ^2.3.4
glob: ^2.1.2
source_gen: ^1.4.0
source_gen: ^1.5.0
yaml: ^3.1.2
hive: ^2.2.3

path: ^1.8.3
path: ">=1.8.3 <2.0.0"

equatable: ^2.0.5

Expand Down

0 comments on commit 7ccdc62

Please sign in to comment.