Compare commits

..

3 Commits

Author SHA1 Message Date
Sergey Penkovsky
8fcb61ef3e chore(release): publish packages
- cherrypick@2.2.0
 - cherrypick_annotations@1.1.0
 - cherrypick_flutter@1.1.2
 - cherrypick_generator@1.1.0
2025-07-28 12:34:54 +03:00
Sergey Penkovsky
69e166644a fix(tests): update expected outputs in generator tests to match new formatting 2025-07-25 12:44:25 +03:00
Sergey Penkovsky
feb7258302 chore(generator): improve annotation validation, unify async type handling, and refactor BindSpec creation
- Enhance annotation validation in DI code generation.
- Move from manual Future<T> extraction to unified type parsing.
- Refactor BindSpec creation logic to provide better error messages and type consistency.
- Add missing source files for exceptions, annotation validation, and type parsing.

BREAKING CHANGE:
Invalid annotation combinations now produce custom generator errors. Async detection is now handled via unified type parser.
2025-07-25 11:58:56 +03:00
30 changed files with 1134 additions and 433 deletions

2
.gitignore vendored
View File

@@ -7,7 +7,7 @@
.idea/
.vscode/
**/generated
**/*.g.dart
**/*.gr.dart
**/*.freezed.dart

View File

@@ -3,7 +3,7 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## 2025-07-15
## 2025-07-28
### Changes
@@ -11,30 +11,30 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
Packages with breaking changes:
- [`cherrypick_generator` - `v1.1.0-dev.6`](#cherrypick_generator---v110-dev6)
- [`cherrypick_flutter` - `v1.1.2`](#cherrypick_flutter---v112)
Packages with other changes:
- [`cherrypick` - `v2.2.0-dev.2`](#cherrypick---v220-dev2)
- [`cherrypick_flutter` - `v1.1.2-dev.2`](#cherrypick_flutter---v112-dev2)
- [`cherrypick` - `v2.2.0`](#cherrypick---v220)
- [`cherrypick_annotations` - `v1.1.0`](#cherrypick_annotations---v110)
- [`cherrypick_generator` - `v1.1.0`](#cherrypick_generator---v110)
Packages with dependency updates only:
Packages graduated to a stable release (see pre-releases prior to the stable version for changelog entries):
> Packages listed below depend on other packages in this workspace that have had changes. Their versions have been incremented to bump the minimum dependency versions of the packages they depend upon in this project.
- `cherrypick_flutter` - `v1.1.2-dev.2`
- `cherrypick` - `v2.2.0`
- `cherrypick_annotations` - `v1.1.0`
- `cherrypick_flutter` - `v1.1.2`
- `cherrypick_generator` - `v1.1.0`
---
#### `cherrypick_generator` - `v1.1.0-dev.6`
#### `cherrypick_flutter` - `v1.1.2`
- **FIX**: format test code.
- **FEAT**(generator): support output_dir and build_extensions config for generated files.
- **BREAKING** **FEAT**(generator): complete code generation testing framework with 100% test coverage.
#### `cherrypick` - `v2.2.0`
#### `cherrypick` - `v2.2.0-dev.2`
#### `cherrypick_annotations` - `v1.1.0`
- **DOCS**: move and update quick start guides to ./doc directory.
#### `cherrypick_generator` - `v1.1.0`
## 2025-06-04

View File

@@ -1,6 +1,6 @@
## 2.2.0-dev.2
## 2.2.0
- **DOCS**: move and update quick start guides to ./doc directory.
- Graduate package to a stable release. See pre-releases prior to this version for changelog entries.
## 2.2.0-dev.1

View File

@@ -1,6 +1,6 @@
name: cherrypick
description: Cherrypick is a small dependency injection (DI) library for dart/flutter projects.
version: 2.2.0-dev.2
version: 2.2.0
homepage: https://pese-git.github.io/cherrypick-site/
documentation: https://github.com/pese-git/cherrypick/wiki
repository: https://github.com/pese-git/cherrypick

View File

@@ -1,3 +1,7 @@
## 1.1.0
- Graduate package to a stable release. See pre-releases prior to this version for changelog entries.
## 1.1.0-dev.1
- **FEAT**: implement InjectGenerator.

View File

@@ -1,7 +1,7 @@
name: cherrypick_annotations
description: |
Set of annotations for CherryPick dependency injection library. Enables code generation and declarative DI for Dart & Flutter projects.
version: 1.1.0-dev.1
version: 1.1.0
documentation: https://github.com/pese-git/cherrypick/wiki
repository: https://github.com/pese-git/cherrypick/cherrypick_annotations
issue_tracker: https://github.com/pese-git/cherrypick/issues

View File

@@ -1,6 +1,6 @@
## 1.1.2-dev.2
## 1.1.2
- Update a dependency to the latest release.
- Graduate package to a stable release. See pre-releases prior to this version for changelog entries.
## 1.1.2-dev.1

View File

@@ -1,6 +1,6 @@
name: cherrypick_flutter
description: "Flutter library that allows access to the root scope through the context using `CherryPickProvider`."
version: 1.1.2-dev.2
version: 1.1.2
homepage: https://pese-git.github.io/cherrypick-site/
documentation: https://github.com/pese-git/cherrypick/wiki
repository: https://github.com/pese-git/cherrypick
@@ -13,7 +13,7 @@ environment:
dependencies:
flutter:
sdk: flutter
cherrypick: ^2.2.0-dev.2
cherrypick: ^2.2.0
dev_dependencies:
flutter_test:

View File

@@ -1,10 +1,6 @@
## 1.1.0-dev.6
## 1.1.0
> Note: This release has breaking changes.
- **FIX**: format test code.
- **FEAT**(generator): support output_dir and build_extensions config for generated files.
- **BREAKING** **FEAT**(generator): complete code generation testing framework with 100% test coverage.
- Graduate package to a stable release. See pre-releases prior to this version for changelog entries.
## 1.1.0-dev.5

View File

@@ -4,50 +4,6 @@
---
### Advanced: Customizing Generated File Paths (`build_extensions`)
You can further control the filenames and subdirectory structure of generated files using the `build_extensions` option in `build.yaml`. This is especially useful in large apps for keeping DI artifacts organized under `lib/generated/` or any custom location.
**Example advanced build.yaml:**
```yaml
targets:
$default:
builders:
cherrypick_generator|inject_generator:
options:
build_extensions:
'^lib/app.dart': ['lib/generated/app.inject.cherrypick.g.dart']
output_dir: lib/generated
generate_for:
- lib/**.dart
cherrypick_generator|module_generator:
options:
build_extensions:
'^lib/di/{{}}.dart': ['lib/generated/di/{{}}.module.cherrypick.g.dart']
output_dir: lib/generated
generate_for:
- lib/**.dart
```
- **output_dir**: Path where all generated files are placed (e.g., `lib/generated`)
- **build_extensions**: Allows templating of generated filenames and locations. You can use wildcards like `{{}}` to keep directory structure or group related files.
**If you use these options, be sure to update your imports accordingly, for example:**
```dart
import 'package:your_package/generated/app.inject.cherrypick.g.dart';
import 'package:your_package/generated/di/app_module.module.cherrypick.g.dart';
```
### FAQ / Troubleshooting
- If files are missing or located in unexpected directories, double-check your `output_dir` and `build_extensions` configuration.
- If you change generation paths, always update your imports in the codebase.
- These options are backward compatible: omitting them preserves pre-existing (side-by-source) output behavior.
---
## Features
- **Automatic Field Injection:**
@@ -214,26 +170,6 @@ final class $MyModule extends MyModule {
## Advanced Usage
### Custom output directory for generated code (output_dir)
You can control the directory where the generated files (`*.inject.cherrypick.g.dart`, `*.module.cherrypick.g.dart`) are placed using the `output_dir` option in your `build.yaml`:
```yaml
targets:
$default:
builders:
cherrypick_generator|injectBuilder:
options:
output_dir: lib/generated
cherrypick_generator|moduleBuilder:
options:
output_dir: lib/generated
```
**If `output_dir` is omitted, generated files are placed next to the original sources (default behavior).**
After running code generation, you will find files like `lib/generated/app.inject.cherrypick.g.dart` and `lib/generated/your_module.module.cherrypick.g.dart`. You can import them as needed from that directory.
- **Combining Modules and Field Injection:**
It's possible to mix both style of DI — modules for binding, and field injection for consuming services.
- **Parameter and Named Injection:**

View File

@@ -1,18 +1,20 @@
builders:
inject_generator:
import: "package:cherrypick_generator/inject_generator.dart"
builder_factories: ["injectBuilder"]
build_extensions: {".dart": [".inject.cherrypick.g.dart"]}
auto_apply: dependents
build_to: source
applies_builders: ["source_gen|combining_builder"]
module_generator:
import: "package:cherrypick_generator/module_generator.dart"
builder_factories: ["moduleBuilder"]
build_extensions: {".dart": [".module.cherrypick.g.dart"]}
auto_apply: dependents
required_inputs: ["lib/**"]
runs_before: []
build_to: source
inject_generator:
import: "package:cherrypick_generator/inject_generator.dart"
builder_factories: ["injectBuilder"]
build_extensions: {".dart": [".inject.cherrypick.g.dart"]}
auto_apply: dependents
required_inputs: ["lib/**"]
runs_before: []
build_to: source
applies_builders: ["source_gen|combining_builder"]
targets:
$default:

View File

@@ -1,76 +0,0 @@
import 'dart:async';
import 'package:build/build.dart';
import 'package:path/path.dart' as p;
import 'package:source_gen/source_gen.dart';
import 'inject_generator.dart';
import 'module_generator.dart';
/// Универсальный Builder для генераторов Cherrypick с поддержкой кастомного output_dir
/// (указывает директорию для складывания сгенерированных файлов через build.yaml)
class CustomOutputBuilder extends Builder {
final Generator generator;
final String extension;
final String outputDir;
final Map<String, List<String>> customBuildExtensions;
CustomOutputBuilder(this.generator, this.extension, this.outputDir, this.customBuildExtensions);
@override
Map<String, List<String>> get buildExtensions {
if (customBuildExtensions.isNotEmpty) {
return customBuildExtensions;
}
// Дефолт: рядом с исходником, как PartBuilder
return {
'.dart': [extension],
};
}
@override
Future<void> build(BuildStep buildStep) async {
final inputId = buildStep.inputId;
print('[CustomOutputBuilder] build() called for input: \\${inputId.path}');
final library = await buildStep.resolver.libraryFor(inputId);
print('[CustomOutputBuilder] resolved library for: \\${inputId.path}');
final generated = await generator.generate(LibraryReader(library), buildStep);
print('[CustomOutputBuilder] gen result for input: \\${inputId.path}, isNull: \\${generated == null}, isEmpty: \\${generated?.isEmpty}');
if (generated == null || generated.isEmpty) return;
String outputPath;
if (customBuildExtensions.isNotEmpty) {
// Кастомная директория/шаблон
final inputPath = inputId.path;
final relativeInput = p.relative(inputPath, from: 'lib/');
final parts = p.split(relativeInput);
String subdir = '';
String baseName = parts.last.replaceAll('.dart', '');
if (parts.length > 1) {
subdir = parts.first; // Например, 'di'
}
outputPath = subdir.isEmpty
? p.join('lib', 'generated', '$baseName$extension')
: p.join('lib', 'generated', subdir, '$baseName$extension');
} else {
// Дефолт: рядом с исходником
outputPath = p.setExtension(inputId.path, extension);
}
final outputId = AssetId(inputId.package, outputPath);
// part of - всегда авто!
final partOfPath = p.relative(inputId.path, from: p.dirname(outputPath));
final codeWithPartOf = "part of '$partOfPath';\n\n$generated";
print('[CustomOutputBuilder] writing to output: \\${outputId.path}');
await buildStep.writeAsString(outputId, codeWithPartOf);
print('[CustomOutputBuilder] successfully written for input: \\${inputId.path}');
}
}
Builder injectCustomBuilder(BuilderOptions options) {
final outputDir = options.config['output_dir'] as String? ?? '';
final buildExtensions = (options.config['build_extensions'] as Map?)?.map((k,v)=>MapEntry(k.toString(), (v as List).map((item)=>item.toString()).toList())) ?? {};
return CustomOutputBuilder(InjectGenerator(), '.inject.cherrypick.g.dart', outputDir, buildExtensions);
}
Builder moduleCustomBuilder(BuilderOptions options) {
final outputDir = options.config['output_dir'] as String? ?? '';
final buildExtensions = (options.config['build_extensions'] as Map?)?.map((k,v)=>MapEntry(k.toString(), (v as List).map((item)=>item.toString()).toList())) ?? {};
return CustomOutputBuilder(ModuleGenerator(), '.module.cherrypick.g.dart', outputDir, buildExtensions);
}

View File

@@ -19,7 +19,6 @@ 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' as ann;
import 'cherrypick_custom_builders.dart' as custom;
/// InjectGenerator generates a mixin for a class marked with @injectable()
/// and injects all fields annotated with @inject(), using CherryPick DI.
@@ -205,4 +204,4 @@ class _ParsedInjectField {
///
/// Фабрика билдера. Используется build_runner.
Builder injectBuilder(BuilderOptions options) =>
custom.injectCustomBuilder(options);
PartBuilder([InjectGenerator()], '.inject.cherrypick.g.dart');

View File

@@ -15,8 +15,9 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';
import 'package:cherrypick_annotations/cherrypick_annotations.dart' as ann;
import 'src/generated_class.dart';
import 'cherrypick_custom_builders.dart' as custom;
/// ---------------------------------------------------------------------------
/// ModuleGenerator for code generation of dependency-injected modules.
///
@@ -88,8 +89,5 @@ class ModuleGenerator extends GeneratorForAnnotation<ann.module> {
/// Возвращает Builder, используемый build_runner для генерации кода для всех
/// файлов, где встречается @module().
/// ---------------------------------------------------------------------------
Builder moduleBuilder(BuilderOptions options) =>
custom.moduleCustomBuilder(options);
PartBuilder([ModuleGenerator()], '.module.cherrypick.g.dart');

View File

@@ -0,0 +1,321 @@
//
// Copyright 2021 Sergey Penkovsky (sergey.penkovsky@gmail.com)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import 'package:analyzer/dart/element/element.dart';
import 'exceptions.dart';
import 'metadata_utils.dart';
/// Validates annotation combinations and usage patterns
class AnnotationValidator {
/// Validates annotations on a method element
static void validateMethodAnnotations(MethodElement method) {
final annotations = _getAnnotationNames(method.metadata);
_validateMutuallyExclusiveAnnotations(method, annotations);
_validateAnnotationCombinations(method, annotations);
_validateAnnotationParameters(method);
}
/// Validates annotations on a field element
static void validateFieldAnnotations(FieldElement field) {
final annotations = _getAnnotationNames(field.metadata);
_validateInjectFieldAnnotations(field, annotations);
}
/// Validates annotations on a class element
static void validateClassAnnotations(ClassElement classElement) {
final annotations = _getAnnotationNames(classElement.metadata);
_validateModuleClassAnnotations(classElement, annotations);
_validateInjectableClassAnnotations(classElement, annotations);
}
static List<String> _getAnnotationNames(List<ElementAnnotation> metadata) {
return metadata
.map((m) => m.computeConstantValue()?.type?.getDisplayString())
.where((name) => name != null)
.cast<String>()
.toList();
}
static void _validateMutuallyExclusiveAnnotations(
MethodElement method,
List<String> annotations,
) {
// @instance and @provide are mutually exclusive
if (annotations.contains('instance') && annotations.contains('provide')) {
throw AnnotationValidationException(
'Method cannot have both @instance and @provide annotations',
element: method,
suggestion:
'Use either @instance for direct instances or @provide for factory methods',
context: {
'method_name': method.displayName,
'annotations': annotations,
},
);
}
}
static void _validateAnnotationCombinations(
MethodElement method,
List<String> annotations,
) {
// @params can only be used with @provide
if (annotations.contains('params') && !annotations.contains('provide')) {
throw AnnotationValidationException(
'@params annotation can only be used with @provide annotation',
element: method,
suggestion: 'Remove @params or add @provide annotation',
context: {
'method_name': method.displayName,
'annotations': annotations,
},
);
}
// Methods must have either @instance or @provide
if (!annotations.contains('instance') && !annotations.contains('provide')) {
throw AnnotationValidationException(
'Method must be marked with either @instance or @provide annotation',
element: method,
suggestion:
'Add @instance() for direct instances or @provide() for factory methods',
context: {
'method_name': method.displayName,
'available_annotations': annotations,
},
);
}
// @singleton validation
if (annotations.contains('singleton')) {
_validateSingletonUsage(method, annotations);
}
}
static void _validateSingletonUsage(
MethodElement method,
List<String> annotations,
) {
// Singleton with params might not make sense in some contexts
if (annotations.contains('params')) {
// This is a warning, not an error - could be useful for parameterized singletons
// We could add a warning system later
}
// Check if return type is suitable for singleton
final returnType = method.returnType.getDisplayString();
if (returnType == 'void') {
throw AnnotationValidationException(
'Singleton methods cannot return void',
element: method,
suggestion: 'Remove @singleton annotation or change return type',
context: {
'method_name': method.displayName,
'return_type': returnType,
},
);
}
}
static void _validateAnnotationParameters(MethodElement method) {
// Validate @named annotation parameters
final namedValue = MetadataUtils.getNamedValue(method.metadata);
if (namedValue != null) {
if (namedValue.isEmpty) {
throw AnnotationValidationException(
'@named annotation cannot have empty value',
element: method,
suggestion: 'Provide a non-empty string value for @named annotation',
context: {
'method_name': method.displayName,
'named_value': namedValue,
},
);
}
// Check for valid naming conventions
if (!RegExp(r'^[a-zA-Z_][a-zA-Z0-9_]*$').hasMatch(namedValue)) {
throw AnnotationValidationException(
'@named value should follow valid identifier naming conventions',
element: method,
suggestion:
'Use alphanumeric characters and underscores only, starting with a letter or underscore',
context: {
'method_name': method.displayName,
'named_value': namedValue,
},
);
}
}
// Validate method parameters for @params usage
for (final param in method.parameters) {
final paramAnnotations = _getAnnotationNames(param.metadata);
if (paramAnnotations.contains('params')) {
_validateParamsParameter(param, method);
}
}
}
static void _validateParamsParameter(
ParameterElement param, MethodElement method) {
// @params parameter should typically be dynamic or Map<String, dynamic>
final paramType = param.type.getDisplayString();
if (paramType != 'dynamic' &&
paramType != 'Map<String, dynamic>' &&
paramType != 'Map<String, dynamic>?') {
// This is more of a warning - other types might be valid
// We could add a warning system for this
}
// Check if parameter is required when using @params
try {
final hasDefault = (param as dynamic).defaultValue != null;
if (param.isRequired && !hasDefault) {
// This might be intentional, so we don't throw an error
// but we could warn about it
}
} catch (e) {
// Ignore if defaultValue is not available in this analyzer version
}
}
static void _validateInjectFieldAnnotations(
FieldElement field,
List<String> annotations,
) {
if (!annotations.contains('inject')) {
return; // Not an inject field, nothing to validate
}
// Check if field type is suitable for injection
final fieldType = field.type.getDisplayString();
if (fieldType == 'void') {
throw AnnotationValidationException(
'Cannot inject void type',
element: field,
suggestion: 'Use a concrete type instead of void',
context: {
'field_name': field.displayName,
'field_type': fieldType,
},
);
}
// Validate scope annotation if present
for (final meta in field.metadata) {
final obj = meta.computeConstantValue();
final type = obj?.type?.getDisplayString();
if (type == 'scope') {
// Empty scope name is treated as no scope (uses root scope)
// This is allowed for backward compatibility and convenience
}
}
}
static void _validateModuleClassAnnotations(
ClassElement classElement,
List<String> annotations,
) {
if (!annotations.contains('module')) {
return; // Not a module class
}
// Check if class has public methods
final publicMethods =
classElement.methods.where((m) => m.isPublic).toList();
if (publicMethods.isEmpty) {
throw AnnotationValidationException(
'Module class must have at least one public method',
element: classElement,
suggestion: 'Add public methods with @instance or @provide annotations',
context: {
'class_name': classElement.displayName,
'method_count': publicMethods.length,
},
);
}
// Validate that public methods have appropriate annotations
for (final method in publicMethods) {
final methodAnnotations = _getAnnotationNames(method.metadata);
if (!methodAnnotations.contains('instance') &&
!methodAnnotations.contains('provide')) {
throw AnnotationValidationException(
'Public methods in module class must have @instance or @provide annotation',
element: method,
suggestion: 'Add @instance() or @provide() annotation to the method',
context: {
'class_name': classElement.displayName,
'method_name': method.displayName,
},
);
}
}
}
static void _validateInjectableClassAnnotations(
ClassElement classElement,
List<String> annotations,
) {
if (!annotations.contains('injectable')) {
return; // Not an injectable class
}
// Check if class has injectable fields
final injectFields = classElement.fields.where((f) {
final fieldAnnotations = _getAnnotationNames(f.metadata);
return fieldAnnotations.contains('inject');
}).toList();
// Allow injectable classes without @inject fields to generate empty mixins
// This can be useful for classes that will have @inject fields added later
// or for testing purposes
if (injectFields.isEmpty) {
// Just log a warning but don't throw an exception
// print('Warning: Injectable class ${classElement.displayName} has no @inject fields');
}
// Validate that injectable fields are properly declared
for (final field in injectFields) {
// Injectable fields should be late final for immutability after injection
if (!field.isFinal) {
throw AnnotationValidationException(
'Injectable fields should be final for immutability',
element: field,
suggestion:
'Add final keyword to injectable field (preferably late final)',
context: {
'class_name': classElement.displayName,
'field_name': field.displayName,
},
);
}
// Check if field is late (recommended pattern)
try {
final isLate = (field as dynamic).isLate ?? false;
if (!isLate) {
// This is a warning, not an error - late final is recommended but not required
// We could add a warning system later
}
} catch (e) {
// Ignore if isLate is not available in this analyzer version
}
}
}
}

View File

@@ -12,10 +12,12 @@
//
import 'package:analyzer/dart/element/element.dart';
import 'package:source_gen/source_gen.dart';
import 'bind_parameters_spec.dart';
import 'metadata_utils.dart';
import 'exceptions.dart';
import 'type_parser.dart';
import 'annotation_validator.dart';
enum BindingType {
instance,
@@ -105,10 +107,42 @@ class BindSpec {
final indentStr = ' ' * indent;
final provide = _generateProvideClause(indent);
final postfix = _generatePostfix();
return '$indentStr'
'bind<$returnType>()'
'$provide'
'$postfix;';
// Create the full single-line version first
final singleLine = '${indentStr}bind<$returnType>()$provide$postfix;';
// Check if we need multiline formatting
final needsMultiline = singleLine.length > 80 || provide.contains('\n');
if (!needsMultiline) {
return singleLine;
}
// For multiline formatting, check if we need to break after bind<Type>()
if (provide.contains('\n')) {
// Provider clause is already multiline
if (postfix.isNotEmpty) {
// If there's a postfix, break after bind<Type>()
final multilinePostfix = _generateMultilinePostfix(indent);
return '${indentStr}bind<$returnType>()'
'\n${' ' * (indent + 4)}$provide'
'$multilinePostfix;';
} else {
// No postfix, keep bind<Type>() with provide start
return '${indentStr}bind<$returnType>()$provide;';
}
} else {
// Simple multiline: break after bind<Type>()
if (postfix.isNotEmpty) {
final multilinePostfix = _generateMultilinePostfix(indent);
return '${indentStr}bind<$returnType>()'
'\n${' ' * (indent + 4)}$provide'
'$multilinePostfix;';
} else {
return '${indentStr}bind<$returnType>()'
'\n${' ' * (indent + 4)}$provide;';
}
}
}
// Internal method: decides how the provide clause should be generated by param kind.
@@ -122,6 +156,7 @@ class BindSpec {
// Safe variable name for parameters.
const paramVar = 'args';
final fnArgs = parameters.map((p) => p.generateArg(paramVar)).join(', ');
// Use multiline format only if args are long or contain newlines
final multiLine = fnArgs.length > 60 || fnArgs.contains('\n');
switch (bindingType) {
case BindingType.instance:
@@ -146,7 +181,12 @@ class BindSpec {
/// EN / RU: Supports only injected dependencies, not runtime (@params).
String _generatePlainProvideClause(int indent) {
final argsStr = parameters.map((p) => p.generateArg()).join(', ');
final multiLine = argsStr.length > 60 || argsStr.contains('\n');
// Check if we need multiline formatting based on total line length
final singleLineCall = '$methodName($argsStr)';
final needsMultiline =
singleLineCall.length >= 45 || argsStr.contains('\n');
switch (bindingType) {
case BindingType.instance:
return isAsyncInstance
@@ -154,13 +194,25 @@ class BindSpec {
: '.toInstance($methodName($argsStr))';
case BindingType.provide:
if (isAsyncProvide) {
return multiLine
? '.toProvideAsync(\n${' ' * (indent + 2)}() => $methodName($argsStr))'
: '.toProvideAsync(() => $methodName($argsStr))';
if (needsMultiline) {
final lambdaIndent =
(isSingleton || named != null) ? indent + 6 : indent + 2;
final closingIndent =
(isSingleton || named != null) ? indent + 4 : indent;
return '.toProvideAsync(\n${' ' * lambdaIndent}() => $methodName($argsStr),\n${' ' * closingIndent})';
} else {
return '.toProvideAsync(() => $methodName($argsStr))';
}
} else {
return multiLine
? '.toProvide(\n${' ' * (indent + 2)}() => $methodName($argsStr))'
: '.toProvide(() => $methodName($argsStr))';
if (needsMultiline) {
final lambdaIndent =
(isSingleton || named != null) ? indent + 6 : indent + 2;
final closingIndent =
(isSingleton || named != null) ? indent + 4 : indent;
return '.toProvide(\n${' ' * lambdaIndent}() => $methodName($argsStr),\n${' ' * closingIndent})';
} else {
return '.toProvide(() => $methodName($argsStr))';
}
}
}
}
@@ -172,6 +224,20 @@ class BindSpec {
return '$namePart$singletonPart';
}
/// EN / RU: Generates multiline postfix with proper indentation.
String _generateMultilinePostfix(int indent) {
final parts = <String>[];
if (named != null) {
parts.add(".withName('$named')");
}
if (isSingleton) {
parts.add('.singleton()');
}
if (parts.isEmpty) return '';
return parts.map((part) => '\n${' ' * (indent + 4)}$part').join('');
}
/// -------------------------------------------------------------------------
/// fromMethod
///
@@ -186,73 +252,98 @@ class BindSpec {
/// асинхронности. Если нет @instance или @provide — кидает ошибку.
/// -------------------------------------------------------------------------
static BindSpec fromMethod(MethodElement method) {
var returnType = method.returnType.getDisplayString();
try {
// Validate method annotations
AnnotationValidator.validateMethodAnnotations(method);
final methodName = method.displayName;
// Check for @singleton annotation.
final isSingleton = MetadataUtils.anyMeta(method.metadata, 'singleton');
// Parse return type using improved type parser
final parsedReturnType = TypeParser.parseType(method.returnType, method);
// Get @named value if present.
final named = MetadataUtils.getNamedValue(method.metadata);
final methodName = method.displayName;
// Parse each method parameter.
final params = <BindParameterSpec>[];
bool hasParams = false;
for (final p in method.parameters) {
final typeStr = p.type.getDisplayString();
final paramNamed = MetadataUtils.getNamedValue(p.metadata);
final isParams = MetadataUtils.anyMeta(p.metadata, 'params');
if (isParams) hasParams = true;
params.add(BindParameterSpec(typeStr, paramNamed, isParams: isParams));
}
// Check for @singleton annotation.
final isSingleton = MetadataUtils.anyMeta(method.metadata, 'singleton');
// Determine bindingType: @instance or @provide.
final hasInstance = MetadataUtils.anyMeta(method.metadata, 'instance');
final hasProvide = MetadataUtils.anyMeta(method.metadata, 'provide');
if (!hasInstance && !hasProvide) {
throw InvalidGenerationSourceError(
'Method $methodName must be marked with @instance() or @provide().',
// Get @named value if present.
final named = MetadataUtils.getNamedValue(method.metadata);
// Parse each method parameter.
final params = <BindParameterSpec>[];
bool hasParams = false;
for (final p in method.parameters) {
final typeStr = p.type.getDisplayString();
final paramNamed = MetadataUtils.getNamedValue(p.metadata);
final isParams = MetadataUtils.anyMeta(p.metadata, 'params');
if (isParams) hasParams = true;
params.add(BindParameterSpec(typeStr, paramNamed, isParams: isParams));
}
// Determine bindingType: @instance or @provide.
final hasInstance = MetadataUtils.anyMeta(method.metadata, 'instance');
final hasProvide = MetadataUtils.anyMeta(method.metadata, 'provide');
if (!hasInstance && !hasProvide) {
throw AnnotationValidationException(
'Method must be marked with either @instance() or @provide() annotation',
element: method,
suggestion:
'Add @instance() for direct instances or @provide() for factory methods',
context: {
'method_name': methodName,
'return_type': parsedReturnType.displayString,
},
);
}
final bindingType =
hasInstance ? BindingType.instance : BindingType.provide;
// PROHIBIT @params with @instance bindings!
if (bindingType == BindingType.instance && hasParams) {
throw AnnotationValidationException(
'@params() (runtime arguments) cannot be used together with @instance()',
element: method,
suggestion: 'Use @provide() instead if you want runtime arguments',
context: {
'method_name': methodName,
'binding_type': 'instance',
'has_params': hasParams,
},
);
}
// Set async flags based on parsed type
final isAsyncInstance =
bindingType == BindingType.instance && parsedReturnType.isFuture;
final isAsyncProvide =
bindingType == BindingType.provide && parsedReturnType.isFuture;
return BindSpec(
returnType: parsedReturnType.codeGenType,
methodName: methodName,
isSingleton: isSingleton,
named: named,
parameters: params,
bindingType: bindingType,
isAsyncInstance: isAsyncInstance,
isAsyncProvide: isAsyncProvide,
hasParams: hasParams,
);
} catch (e) {
if (e is CherryPickGeneratorException) {
rethrow;
}
throw CodeGenerationException(
'Failed to create BindSpec from method "${method.displayName}"',
element: method,
suggestion:
'Check that the method has valid annotations and return type',
context: {
'method_name': method.displayName,
'return_type': method.returnType.getDisplayString(),
'error': e.toString(),
},
);
}
final bindingType =
hasInstance ? BindingType.instance : BindingType.provide;
// PROHIBIT @params with @instance bindings!
if (bindingType == BindingType.instance && hasParams) {
throw InvalidGenerationSourceError(
'@params() (runtime arguments) cannot be used together with @instance() on method $methodName. '
'Use @provide() instead if you want runtime arguments.',
element: method,
);
}
// -- Extract inner type for Future<T> and set async flags.
bool isAsyncInstance = false;
bool isAsyncProvide = false;
final futureInnerType = _extractFutureInnerType(returnType);
if (futureInnerType != null) {
returnType = futureInnerType;
if (bindingType == BindingType.instance) isAsyncInstance = true;
if (bindingType == BindingType.provide) isAsyncProvide = true;
}
return BindSpec(
returnType: returnType,
methodName: methodName,
isSingleton: isSingleton,
named: named,
parameters: params,
bindingType: bindingType,
isAsyncInstance: isAsyncInstance,
isAsyncProvide: isAsyncProvide,
hasParams: hasParams,
);
}
/// EN / RU: Extracts inner type from Future<T>, returns e.g. "T" or null.
static String? _extractFutureInnerType(String typeName) {
final match = RegExp(r'^Future<(.+)>$').firstMatch(typeName);
return match?.group(1)?.trim();
}
}

View File

@@ -0,0 +1,117 @@
//
// Copyright 2021 Sergey Penkovsky (sergey.penkovsky@gmail.com)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import 'package:analyzer/dart/element/element.dart';
import 'package:source_gen/source_gen.dart';
/// Enhanced exception class for CherryPick generator with detailed context information
class CherryPickGeneratorException extends InvalidGenerationSourceError {
final String category;
final String? suggestion;
final Map<String, dynamic>? context;
CherryPickGeneratorException(
String message, {
required Element element,
required this.category,
this.suggestion,
this.context,
}) : super(
_formatMessage(message, category, suggestion, context, element),
element: element,
);
static String _formatMessage(
String message,
String category,
String? suggestion,
Map<String, dynamic>? context,
Element element,
) {
final buffer = StringBuffer();
// Header with category
buffer.writeln('[$category] $message');
// Element context
buffer.writeln('');
buffer.writeln('Context:');
buffer.writeln(' Element: ${element.displayName}');
buffer.writeln(' Type: ${element.runtimeType}');
buffer.writeln(' Location: ${element.source?.fullName ?? 'unknown'}');
// Note: enclosingElement may not be available in all analyzer versions
try {
final enclosing = (element as dynamic).enclosingElement;
if (enclosing != null) {
buffer.writeln(' Enclosing: ${enclosing.displayName}');
}
} catch (e) {
// Ignore if enclosingElement is not available
}
// Additional context
if (context != null && context.isNotEmpty) {
buffer.writeln('');
buffer.writeln('Additional Context:');
context.forEach((key, value) {
buffer.writeln(' $key: $value');
});
}
// Suggestion
if (suggestion != null) {
buffer.writeln('');
buffer.writeln('💡 Suggestion: $suggestion');
}
return buffer.toString();
}
}
/// Specific exception types for different error categories
class AnnotationValidationException extends CherryPickGeneratorException {
AnnotationValidationException(
super.message, {
required super.element,
super.suggestion,
super.context,
}) : super(category: 'ANNOTATION_VALIDATION');
}
class TypeParsingException extends CherryPickGeneratorException {
TypeParsingException(
super.message, {
required super.element,
super.suggestion,
super.context,
}) : super(category: 'TYPE_PARSING');
}
class CodeGenerationException extends CherryPickGeneratorException {
CodeGenerationException(
super.message, {
required super.element,
super.suggestion,
super.context,
}) : super(category: 'CODE_GENERATION');
}
class DependencyResolutionException extends CherryPickGeneratorException {
DependencyResolutionException(
super.message, {
required super.element,
super.suggestion,
super.context,
}) : super(category: 'DEPENDENCY_RESOLUTION');
}

View File

@@ -49,10 +49,15 @@ class GeneratedClass {
/// Список всех обнаруженных биндингов
final List<BindSpec> binds;
/// Source file name for the part directive
/// Имя исходного файла для part директивы
final String sourceFile;
GeneratedClass(
this.className,
this.generatedClassName,
this.binds,
this.sourceFile,
);
/// -------------------------------------------------------------------------
@@ -72,13 +77,15 @@ class GeneratedClass {
final className = element.displayName;
// Generated class name with '$' prefix (standard for generated Dart code).
final generatedClassName = r'$' + className;
// Get source file name
final sourceFile = element.source.shortName;
// Collect bindings for all non-abstract methods.
final binds = element.methods
.where((m) => !m.isAbstract)
.map(BindSpec.fromMethod)
.toList();
return GeneratedClass(className, generatedClassName, binds);
return GeneratedClass(className, generatedClassName, binds, sourceFile);
}
/// -------------------------------------------------------------------------
@@ -95,11 +102,10 @@ class GeneratedClass {
/// и регистрирует все зависимости через методы bind<Type>()...
/// -------------------------------------------------------------------------
String generate() {
final buffer = StringBuffer();
buffer.writeln('final class $generatedClassName extends $className {');
buffer.writeln(' @override');
buffer.writeln(' void builder(Scope currentScope) {');
final buffer = StringBuffer()
..writeln('final class $generatedClassName extends $className {')
..writeln(' @override')
..writeln(' void builder(Scope currentScope) {');
// For each binding, generate bind<Type>() code string.
// Для каждого биндинга — генерируем строку bind<Type>()...
@@ -107,8 +113,9 @@ class GeneratedClass {
buffer.writeln(bind.generateBind(4));
}
buffer.writeln(' }');
buffer.writeln('}');
buffer
..writeln(' }')
..writeln('}');
return buffer.toString();
}

View File

@@ -0,0 +1,218 @@
//
// Copyright 2021 Sergey Penkovsky (sergey.penkovsky@gmail.com)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'exceptions.dart';
/// Enhanced type parser that uses AST analysis instead of regular expressions
class TypeParser {
/// Parses a DartType and extracts detailed type information
static ParsedType parseType(DartType dartType, Element context) {
try {
return _parseTypeInternal(dartType, context);
} catch (e) {
throw TypeParsingException(
'Failed to parse type: ${dartType.getDisplayString()}',
element: context,
suggestion: 'Ensure the type is properly imported and accessible',
context: {
'original_type': dartType.getDisplayString(),
'error': e.toString(),
},
);
}
}
static ParsedType _parseTypeInternal(DartType dartType, Element context) {
final displayString = dartType.getDisplayString();
final isNullable = dartType.nullabilitySuffix == NullabilitySuffix.question;
// Check if it's a Future type
if (dartType.isDartAsyncFuture) {
return _parseFutureType(dartType, context, isNullable);
}
// Check if it's a generic type (List, Map, etc.)
if (dartType is ParameterizedType && dartType.typeArguments.isNotEmpty) {
return _parseGenericType(dartType, context, isNullable);
}
// Simple type
return ParsedType(
displayString: displayString,
coreType: displayString.replaceAll('?', ''),
isNullable: isNullable,
isFuture: false,
isGeneric: false,
typeArguments: [],
);
}
static ParsedType _parseFutureType(
DartType dartType, Element context, bool isNullable) {
if (dartType is! ParameterizedType || dartType.typeArguments.isEmpty) {
throw TypeParsingException(
'Future type must have a type argument',
element: context,
suggestion: 'Use Future<T> instead of raw Future',
context: {'type': dartType.getDisplayString()},
);
}
final innerType = dartType.typeArguments.first;
final innerParsed = _parseTypeInternal(innerType, context);
return ParsedType(
displayString: dartType.getDisplayString(),
coreType: innerParsed.coreType,
isNullable: isNullable || innerParsed.isNullable,
isFuture: true,
isGeneric: innerParsed.isGeneric,
typeArguments: innerParsed.typeArguments,
innerType: innerParsed,
);
}
static ParsedType _parseGenericType(
ParameterizedType dartType, Element context, bool isNullable) {
final typeArguments = dartType.typeArguments
.map((arg) => _parseTypeInternal(arg, context))
.toList();
final baseType = dartType.element?.name ?? dartType.getDisplayString();
return ParsedType(
displayString: dartType.getDisplayString(),
coreType: baseType,
isNullable: isNullable,
isFuture: false,
isGeneric: true,
typeArguments: typeArguments,
);
}
/// Validates that a type is suitable for dependency injection
static void validateInjectableType(ParsedType parsedType, Element context) {
// Check for void type
if (parsedType.coreType == 'void') {
throw TypeParsingException(
'Cannot inject void type',
element: context,
suggestion: 'Use a concrete type instead of void',
);
}
// Check for dynamic type (warning)
if (parsedType.coreType == 'dynamic') {
// This could be a warning instead of an error
throw TypeParsingException(
'Using dynamic type reduces type safety',
element: context,
suggestion: 'Consider using a specific type instead of dynamic',
);
}
// Validate nested types for complex generics
for (final typeArg in parsedType.typeArguments) {
validateInjectableType(typeArg, context);
}
}
}
/// Represents a parsed type with detailed information
class ParsedType {
/// The full display string of the type (e.g., "Future<List<String>?>")
final String displayString;
/// The core type name without nullability and Future wrapper (e.g., "List<String>")
final String coreType;
/// Whether the type is nullable
final bool isNullable;
/// Whether the type is wrapped in Future
final bool isFuture;
/// Whether the type has generic parameters
final bool isGeneric;
/// Parsed type arguments for generic types
final List<ParsedType> typeArguments;
/// For Future types, the inner type
final ParsedType? innerType;
const ParsedType({
required this.displayString,
required this.coreType,
required this.isNullable,
required this.isFuture,
required this.isGeneric,
required this.typeArguments,
this.innerType,
});
/// Returns the type string suitable for code generation
String get codeGenType {
if (isFuture && innerType != null) {
return innerType!.codeGenType;
}
// For generic types, include type arguments
if (isGeneric && typeArguments.isNotEmpty) {
final args = typeArguments.map((arg) => arg.codeGenType).join(', ');
return '$coreType<$args>';
}
return coreType;
}
/// Returns whether this type should use tryResolve instead of resolve
bool get shouldUseTryResolve => isNullable;
/// Returns the appropriate resolve method name
String get resolveMethodName {
if (isFuture) {
return shouldUseTryResolve ? 'tryResolveAsync' : 'resolveAsync';
}
return shouldUseTryResolve ? 'tryResolve' : 'resolve';
}
@override
String toString() {
return 'ParsedType(displayString: $displayString, coreType: $coreType, '
'isNullable: $isNullable, isFuture: $isFuture, isGeneric: $isGeneric)';
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is ParsedType &&
other.displayString == displayString &&
other.coreType == coreType &&
other.isNullable == isNullable &&
other.isFuture == isFuture &&
other.isGeneric == isGeneric;
}
@override
int get hashCode {
return displayString.hashCode ^
coreType.hashCode ^
isNullable.hashCode ^
isFuture.hashCode ^
isGeneric.hashCode;
}
}

View File

@@ -2,7 +2,7 @@ name: cherrypick_generator
description: |
Source code generator for the cherrypick dependency injection system. Processes annotations to generate binding and module code for Dart & Flutter projects.
version: 1.1.0-dev.6
version: 1.1.0
documentation: https://github.com/pese-git/cherrypick/wiki
repository: https://github.com/pese-git/cherrypick/cherrypick_generator
issue_tracker: https://github.com/pese-git/cherrypick/issues
@@ -12,13 +12,12 @@ environment:
# Add regular dependencies here.
dependencies:
cherrypick_annotations: ^1.1.0-dev.1
cherrypick_annotations: ^1.1.0
analyzer: ^7.0.0
dart_style: ^3.0.0
build: ^2.4.1
source_gen: ^2.0.0
collection: ^1.18.0
path: ^1.9.1
dev_dependencies:
lints: ^4.0.0

View File

@@ -245,7 +245,10 @@ void main() {
expect(
result,
equals(
" bind<ApiClient>().toProvideAsync(() => createApiClient()).withName('mainApi').singleton();"));
" bind<ApiClient>()\n"
" .toProvideAsync(() => createApiClient())\n"
" .withName('mainApi')\n"
" .singleton();"));
});
test('should handle different indentation', () {

View File

@@ -202,9 +202,8 @@ part of 'test_widget.dart';
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.service = CherryPick.openScope(
scopeName: 'userScope',
).resolve<MyService>();
instance.service =
CherryPick.openScope(scopeName: 'userScope').resolve<MyService>();
}
}
''';
@@ -407,9 +406,10 @@ mixin _\$TestWidget {
instance.cacheService = CherryPick.openRootScope().tryResolve<CacheService>(
named: 'cache',
);
instance.dbService = CherryPick.openScope(
scopeName: 'dbScope',
).resolveAsync<DatabaseService>();
instance.dbService =
CherryPick.openScope(
scopeName: 'dbScope',
).resolveAsync<DatabaseService>();
}
}
''';
@@ -451,10 +451,10 @@ part of 'test_widget.dart';
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.stringList = CherryPick.openRootScope().resolve<List<String>>();
instance.stringIntMap = CherryPick.openRootScope()
.resolve<Map<String, int>>();
instance.futureStringList = CherryPick.openRootScope()
.resolveAsync<List<String>>();
instance.stringIntMap =
CherryPick.openRootScope().resolve<Map<String, int>>();
instance.futureStringList =
CherryPick.openRootScope().resolveAsync<List<String>>();
}
}
''';

View File

@@ -0,0 +1,235 @@
import 'package:test/test.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/source/source.dart';
import 'package:cherrypick_generator/src/type_parser.dart';
import 'package:cherrypick_generator/src/exceptions.dart';
void main() {
group('TypeParser', () {
group('parseType', () {
test('should parse simple types correctly', () {
// This would require setting up analyzer infrastructure
// For now, we'll test the ParsedType class directly
});
test('should parse Future types correctly', () {
// This would require setting up analyzer infrastructure
// For now, we'll test the ParsedType class directly
});
test('should parse nullable types correctly', () {
// This would require setting up analyzer infrastructure
// For now, we'll test the ParsedType class directly
});
test('should throw TypeParsingException for invalid types', () {
// This would require setting up analyzer infrastructure
// For now, we'll test the ParsedType class directly
});
});
group('validateInjectableType', () {
test('should throw for void type', () {
final parsedType = ParsedType(
displayString: 'void',
coreType: 'void',
isNullable: false,
isFuture: false,
isGeneric: false,
typeArguments: [],
);
expect(
() => TypeParser.validateInjectableType(
parsedType, _createMockElement()),
throwsA(isA<TypeParsingException>()),
);
});
test('should throw for dynamic type', () {
final parsedType = ParsedType(
displayString: 'dynamic',
coreType: 'dynamic',
isNullable: false,
isFuture: false,
isGeneric: false,
typeArguments: [],
);
expect(
() => TypeParser.validateInjectableType(
parsedType, _createMockElement()),
throwsA(isA<TypeParsingException>()),
);
});
test('should pass for valid types', () {
final parsedType = ParsedType(
displayString: 'String',
coreType: 'String',
isNullable: false,
isFuture: false,
isGeneric: false,
typeArguments: [],
);
expect(
() => TypeParser.validateInjectableType(
parsedType, _createMockElement()),
returnsNormally,
);
});
});
});
group('ParsedType', () {
test('should return correct codeGenType for simple types', () {
final parsedType = ParsedType(
displayString: 'String',
coreType: 'String',
isNullable: false,
isFuture: false,
isGeneric: false,
typeArguments: [],
);
expect(parsedType.codeGenType, equals('String'));
});
test('should return correct codeGenType for Future types', () {
final innerType = ParsedType(
displayString: 'String',
coreType: 'String',
isNullable: false,
isFuture: false,
isGeneric: false,
typeArguments: [],
);
final parsedType = ParsedType(
displayString: 'Future<String>',
coreType: 'Future<String>',
isNullable: false,
isFuture: true,
isGeneric: false,
typeArguments: [],
innerType: innerType,
);
expect(parsedType.codeGenType, equals('String'));
});
test('should return correct resolveMethodName for sync types', () {
final parsedType = ParsedType(
displayString: 'String',
coreType: 'String',
isNullable: false,
isFuture: false,
isGeneric: false,
typeArguments: [],
);
expect(parsedType.resolveMethodName, equals('resolve'));
});
test('should return correct resolveMethodName for nullable sync types', () {
final parsedType = ParsedType(
displayString: 'String?',
coreType: 'String',
isNullable: true,
isFuture: false,
isGeneric: false,
typeArguments: [],
);
expect(parsedType.resolveMethodName, equals('tryResolve'));
});
test('should return correct resolveMethodName for async types', () {
final parsedType = ParsedType(
displayString: 'Future<String>',
coreType: 'String',
isNullable: false,
isFuture: true,
isGeneric: false,
typeArguments: [],
);
expect(parsedType.resolveMethodName, equals('resolveAsync'));
});
test('should return correct resolveMethodName for nullable async types',
() {
final parsedType = ParsedType(
displayString: 'Future<String?>',
coreType: 'String',
isNullable: true,
isFuture: true,
isGeneric: false,
typeArguments: [],
);
expect(parsedType.resolveMethodName, equals('tryResolveAsync'));
});
test('should implement equality correctly', () {
final parsedType1 = ParsedType(
displayString: 'String',
coreType: 'String',
isNullable: false,
isFuture: false,
isGeneric: false,
typeArguments: [],
);
final parsedType2 = ParsedType(
displayString: 'String',
coreType: 'String',
isNullable: false,
isFuture: false,
isGeneric: false,
typeArguments: [],
);
expect(parsedType1, equals(parsedType2));
expect(parsedType1.hashCode, equals(parsedType2.hashCode));
});
test('should implement toString correctly', () {
final parsedType = ParsedType(
displayString: 'String',
coreType: 'String',
isNullable: false,
isFuture: false,
isGeneric: false,
typeArguments: [],
);
final result = parsedType.toString();
expect(result, contains('ParsedType'));
expect(result, contains('String'));
expect(result, contains('isNullable: false'));
expect(result, contains('isFuture: false'));
});
});
}
// Mock element for testing
Element _createMockElement() {
return _MockElement();
}
class _MockElement implements Element {
@override
String get displayName => 'MockElement';
@override
String get name => 'MockElement';
@override
Source? get source => null;
@override
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

View File

@@ -379,45 +379,6 @@ You can use CherryPick in Dart CLI, server apps, and microservices. All major fe
---
### Advanced: Customizing Generated Code Location
CherryPick's code generator now supports flexible output configuration via `build.yaml`.
You can control both the output directory (using `output_dir`) and filename templates (using `build_extensions`):
```yaml
targets:
$default:
builders:
cherrypick_generator|inject_generator:
options:
build_extensions:
'^lib/app.dart': ['lib/generated/app.inject.cherrypick.g.dart']
output_dir: lib/generated
generate_for:
- lib/**.dart
cherrypick_generator|module_generator:
options:
build_extensions:
'^lib/di/{{}}.dart': ['lib/generated/di/{{}}.module.cherrypick.g.dart']
output_dir: lib/generated
generate_for:
- lib/**.dart
```
- **output_dir**: Folder where all generated files will be placed.
- **build_extensions**: Allows full customization of generated file names and subfolders.
If you use these, be sure to update your imports accordingly, e.g.:
```dart
import 'package:your_project/generated/app.inject.cherrypick.g.dart';
```
If not specified, generated files will appear next to your source files, as before.
---
---
## Conclusion
**CherryPick** is a modern DI solution for Dart and Flutter, combining a concise API and advanced annotation/codegen features. Scopes, parameterized providers, named bindings, and field-injection make it great for both small and large-scale projects.

View File

@@ -382,45 +382,6 @@ class MyApp extends StatelessWidget {
---
### Продвинутая настройка путей генерации кода
В последних версиях генератора CherryPick добавлена поддержка гибкой настройки директорий и шаблонов имён файлов через `build.yaml`.
Вы можете управлять и папкой назначения (через `output_dir`), и шаблоном имён (через `build_extensions`):
```yaml
targets:
$default:
builders:
cherrypick_generator|inject_generator:
options:
build_extensions:
'^lib/app.dart': ['lib/generated/app.inject.cherrypick.g.dart']
output_dir: lib/generated
generate_for:
- lib/**.dart
cherrypick_generator|module_generator:
options:
build_extensions:
'^lib/di/{{}}.dart': ['lib/generated/di/{{}}.module.cherrypick.g.dart']
output_dir: lib/generated
generate_for:
- lib/**.dart
```
- **output_dir**: Папка, куда будут складываться все сгенерированные файлы.
- **build_extensions**: Полный контроль над именами итоговых файлов и подпапками.
Если вы это используете, обязательно обновляйте импорты, например:
```dart
import 'package:your_project/generated/app.inject.cherrypick.g.dart';
```
Если не задать параметры, файлы будут сгенерированы рядом с исходными — как и раньше.
---
---
## Заключение
**CherryPick** — это современное DI-решение для Dart и Flutter, сочетающее лаконичный API и расширенные возможности аннотирования и генерации кода. Гибкость Scopes, параметрические провайдеры, именованные биндинги и field-injection делают его особенно мощным как для небольших, так и для масштабных проектов.

View File

@@ -19,29 +19,7 @@ There are two main methods for initializing a custom instance `toInstance ()` an
Example:
```
---
## Advanced: Customizing Code Generation Output
You can configure where generated files will be placed by updating your `build.yaml` (supports `output_dir` and `build_extensions`):
```yaml
targets:
$default:
builders:
cherrypick_generator|inject_generator:
options:
output_dir: lib/generated
cherrypick_generator|module_generator:
options:
output_dir: lib/generated
```
For full control and more examples, see the "Full Tutorial" or documentation on `build_extensions`.
---
```dart
// initializing a text string instance through a method toInstance()
Binding<String>().toInstance("hello world");

View File

@@ -19,29 +19,7 @@ Binding - по сути это конфигуратор для пользов
Пример:
```
---
## Продвинутая настройка генерации кода
В файле `build.yaml` можно задать папку для сгенерированных файлов через параметр `output_dir` (а также использовать шаблон `build_extensions`):
```yaml
targets:
$default:
builders:
cherrypick_generator|inject_generator:
options:
output_dir: lib/generated
cherrypick_generator|module_generator:
options:
output_dir: lib/generated
```
Для полной настройки и шаблонов см. раздел “Полный гайд” или документацию по `build_extensions`.
---
```dart
// инициализация экземпляра текстовой строки через метод toInstance()
Binding<String>().toInstance("hello world");

View File

@@ -1,27 +0,0 @@
targets:
$default:
builders:
cherrypick_generator|inject_generator:
options:
build_extensions:
'^lib/app.dart': ['lib/generated/app.inject.cherrypick.g.dart']
output_dir: lib/generated
generate_for:
- lib/**.dart
cherrypick_generator|module_generator:
options:
build_extensions:
'^lib/di/{{}}.dart': ['lib/generated/di/{{}}.module.cherrypick.g.dart']
output_dir: lib/generated
generate_for:
- lib/**.dart
#targets:
# $default:
# builders:
# cherrypick_generator|module_generator:
# generate_for:
# - lib/**.dart
# cherrypick_generator|inject_generator:
# generate_for:
# - lib/**.dart

View File

@@ -7,7 +7,7 @@ import 'domain/repository/post_repository.dart';
import 'presentation/bloc/post_bloc.dart';
import 'router/app_router.dart';
part 'generated/app.inject.cherrypick.g.dart';
part 'app.inject.cherrypick.g.dart';
@injectable()
class MyApp extends StatelessWidget with _$MyApp {

View File

@@ -5,7 +5,7 @@ import '../data/network/json_placeholder_api.dart';
import '../data/post_repository_impl.dart';
import '../domain/repository/post_repository.dart';
part '../generated/di/app_module.module.cherrypick.g.dart';
part 'app_module.module.cherrypick.g.dart';
@module()
abstract class AppModule extends Module {