mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
start implement generator code
This commit is contained in:
6
cherrypick_generator/.gitignore
vendored
6
cherrypick_generator/.gitignore
vendored
@@ -21,4 +21,8 @@ doc/api/
|
||||
*.js.map
|
||||
|
||||
# FVM Version Cache
|
||||
.fvm/
|
||||
.fvm/
|
||||
|
||||
melos_cherrypick_generator.iml
|
||||
|
||||
**/*.mocks.dart
|
||||
@@ -1,5 +1,16 @@
|
||||
builders:
|
||||
injectable:
|
||||
import: "package:cherrypick_generator/injectable_generator.dart"
|
||||
builder_factories: ["injectableBuilder"]
|
||||
build_extensions: {".dart": [".cherrypick_injectable.g.dart"]}
|
||||
auto_apply: dependents
|
||||
required_inputs: ["lib/**"]
|
||||
runs_before: []
|
||||
build_to: source
|
||||
|
||||
targets:
|
||||
$default:
|
||||
builders:
|
||||
cherrypick_generator|inject_builder:
|
||||
enabled: true
|
||||
cherrypick_generator|injectable:
|
||||
generate_for:
|
||||
- lib/**.dart
|
||||
@@ -1,6 +0,0 @@
|
||||
import 'package:cherrypick_generator/cherrypick_generator.dart';
|
||||
|
||||
void main() {
|
||||
var awesome = Awesome();
|
||||
print('awesome: ${awesome.isAwesome}');
|
||||
}
|
||||
@@ -1,8 +1,3 @@
|
||||
/// Support for doing something awesome.
|
||||
///
|
||||
/// More dartdocs go here.
|
||||
library;
|
||||
|
||||
export 'src/cherrypick_generator_base.dart';
|
||||
|
||||
// TODO: Export any libraries intended for clients of this package.
|
||||
export 'inject_generator.dart';
|
||||
|
||||
38
cherrypick_generator/lib/inject_generator.dart
Normal file
38
cherrypick_generator/lib/inject_generator.dart
Normal file
@@ -0,0 +1,38 @@
|
||||
import 'dart:async';
|
||||
import 'package:build/build.dart';
|
||||
import 'package:source_gen/source_gen.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
|
||||
class InjectGenerator extends GeneratorForAnnotation<Injectable> {
|
||||
@override
|
||||
FutureOr<String> generateForAnnotatedElement(
|
||||
Element element,
|
||||
ConstantReader annotation,
|
||||
BuildStep buildStep,
|
||||
) {
|
||||
print('[TRACE] Processing element: ${element.name}');
|
||||
|
||||
if (element is! FieldElement) {
|
||||
throw InvalidGenerationSourceError(
|
||||
'Inject can only be used on fields.',
|
||||
element: element,
|
||||
);
|
||||
}
|
||||
|
||||
print('[TRACE] Starting code generation for element: ${element.name}');
|
||||
|
||||
final className = element.enclosingElement.name;
|
||||
final fieldName = element.name;
|
||||
final fieldType = element.type.getDisplayString(withNullability: false);
|
||||
final annotationName = annotation.read('named').stringValue;
|
||||
|
||||
return '''
|
||||
extension \$${className}Inject on $className {
|
||||
void init$fieldName() {
|
||||
print("Injected $fieldType named '$annotationName' into $fieldName");
|
||||
}
|
||||
}
|
||||
''';
|
||||
}
|
||||
}
|
||||
33
cherrypick_generator/lib/injectable_generator.dart
Normal file
33
cherrypick_generator/lib/injectable_generator.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
import 'package:source_gen/source_gen.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:build/build.dart';
|
||||
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
|
||||
class InjectableGenerator extends GeneratorForAnnotation<Injectable> {
|
||||
@override
|
||||
generateForAnnotatedElement(
|
||||
Element element, ConstantReader annotation, BuildStep buildStep) {
|
||||
if (element is! ClassElement) return null;
|
||||
|
||||
final className = element.name;
|
||||
|
||||
// Используйте уникальное имя функции (например, привязанное к файлу/классу)
|
||||
return '''
|
||||
void \$initCherrypickGenerated() {
|
||||
print("Generate code success $className");
|
||||
}
|
||||
''';
|
||||
}
|
||||
}
|
||||
|
||||
Builder injectableBuilder(BuilderOptions options) =>
|
||||
PartBuilder([InjectableGenerator()], '.cherrypick_injectable.g.dart');
|
||||
|
||||
/*
|
||||
Builder injectableBuilder(BuilderOptions options) => SharedPartBuilder(
|
||||
[InjectableGenerator()],
|
||||
'injectable',
|
||||
allowSyntaxErrors: true,
|
||||
writeDescriptions: true,
|
||||
);
|
||||
*/
|
||||
@@ -1,6 +0,0 @@
|
||||
// TODO: Put public facing types in this file.
|
||||
|
||||
/// Checks if you are awesome. Spoiler: you are.
|
||||
class Awesome {
|
||||
bool get isAwesome => true;
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import 'dart:async';
|
||||
import 'package:build/build.dart';
|
||||
import 'package:source_gen/source_gen.dart';
|
||||
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
|
||||
class InjectGenerator extends GeneratorForAnnotation<Inject> {
|
||||
@override
|
||||
FutureOr<String> generateForAnnotatedElement(
|
||||
Element element, ConstantReader annotation, BuildStep buildStep) {
|
||||
if (element is! FieldElement) {
|
||||
throw InvalidGenerationSourceError(
|
||||
'Генератор не может работать с `${element.name}`.',
|
||||
);
|
||||
}
|
||||
|
||||
final named = annotation.peek('named')?.stringValue;
|
||||
final resolveSnippet = named == null
|
||||
? 'CherryPickProvider.of(context).openRootScope().resolve<${element.type}>()'
|
||||
: 'CherryPickProvider.of(context).openRootScope().resolve<${element.type}>(named: \'$named\')';
|
||||
|
||||
return '''
|
||||
void _initState(BuildContext context) {
|
||||
${element.name} = $resolveSnippet;
|
||||
}
|
||||
''';
|
||||
}
|
||||
}
|
||||
@@ -2,17 +2,22 @@ name: cherrypick_generator
|
||||
description: Code generator for cherrypick annotations
|
||||
version: 1.0.0
|
||||
# repository: https://github.com/my_org/my_repo
|
||||
publish_to: none
|
||||
|
||||
environment:
|
||||
sdk: ^3.5.2
|
||||
sdk: ">=3.5.2 <4.0.0"
|
||||
|
||||
# Add regular dependencies here.
|
||||
dependencies:
|
||||
build: ^2.0.0
|
||||
source_gen: ^1.0.0
|
||||
cherrypick_annotations: any
|
||||
cherrypick_annotations:
|
||||
path: ../cherrypick_annotations
|
||||
analyzer: any
|
||||
dart_style: any
|
||||
build: any
|
||||
build_runner: any
|
||||
source_gen: any
|
||||
|
||||
dev_dependencies:
|
||||
lints: ^4.0.0
|
||||
test: ^1.24.0
|
||||
build_runner: ^2.1.0
|
||||
lints: ^5.0.0
|
||||
mockito: any
|
||||
test: ^1.25.8
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
import 'package:cherrypick_generator/cherrypick_generator.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('A group of tests', () {
|
||||
final awesome = Awesome();
|
||||
|
||||
setUp(() {
|
||||
// Additional setup goes here.
|
||||
});
|
||||
|
||||
test('First Test', () {
|
||||
expect(awesome.isAwesome, isTrue);
|
||||
expect(2, 2);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user