mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 13:47:24 +00:00
Compare commits
5 Commits
cherrypick
...
cli
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
76c77b1f6d | ||
|
|
edc2a14ad7 | ||
|
|
71d3ef77a9 | ||
|
|
0eec549b57 | ||
|
|
a3648209b9 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -7,7 +7,7 @@
|
||||
.idea/
|
||||
.vscode/
|
||||
|
||||
|
||||
**/generated
|
||||
**/*.g.dart
|
||||
**/*.gr.dart
|
||||
**/*.freezed.dart
|
||||
@@ -18,5 +18,3 @@ pubspec_overrides.yaml
|
||||
melos_cherrypick.iml
|
||||
melos_cherrypick_workspace.iml
|
||||
melos_cherrypick_flutter.iml
|
||||
|
||||
coverage
|
||||
75
CHANGELOG.md
75
CHANGELOG.md
@@ -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-30
|
||||
## 2025-07-15
|
||||
|
||||
### Changes
|
||||
|
||||
@@ -11,83 +11,30 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
- [`cherrypick_generator` - `v1.1.0-dev.6`](#cherrypick_generator---v110-dev6)
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.0-dev.1`](#cherrypick---v300-dev1)
|
||||
- [`cherrypick_flutter` - `v1.1.3-dev.1`](#cherrypick_flutter---v113-dev1)
|
||||
- [`cherrypick` - `v2.2.0-dev.2`](#cherrypick---v220-dev2)
|
||||
- [`cherrypick_flutter` - `v1.1.2-dev.2`](#cherrypick_flutter---v112-dev2)
|
||||
|
||||
Packages with dependency updates only:
|
||||
|
||||
> 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.3-dev.1`
|
||||
- `cherrypick_flutter` - `v1.1.2-dev.2`
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick` - `v3.0.0-dev.1`
|
||||
#### `cherrypick_generator` - `v1.1.0-dev.6`
|
||||
|
||||
- **DOCS**: add quick guide for circular dependency detection to README.
|
||||
- **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-dev.2`
|
||||
|
||||
## 2025-07-30
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.0-dev.0`](#cherrypick---v300-dev0)
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick_flutter` - `v1.1.3-dev.0`](#cherrypick_flutter---v113-dev0)
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick` - `v3.0.0-dev.0`
|
||||
|
||||
- **BREAKING** **FEAT**: implement comprehensive circular dependency detection system.
|
||||
|
||||
#### `cherrypick_flutter` - `v1.1.3-dev.0`
|
||||
|
||||
- **FIX**: update deps.
|
||||
|
||||
|
||||
## 2025-07-28
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- [`cherrypick_flutter` - `v1.1.2`](#cherrypick_flutter---v112)
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick` - `v2.2.0`](#cherrypick---v220)
|
||||
- [`cherrypick_annotations` - `v1.1.0`](#cherrypick_annotations---v110)
|
||||
- [`cherrypick_generator` - `v1.1.0`](#cherrypick_generator---v110)
|
||||
|
||||
Packages graduated to a stable release (see pre-releases prior to the stable version for changelog entries):
|
||||
|
||||
- `cherrypick` - `v2.2.0`
|
||||
- `cherrypick_annotations` - `v1.1.0`
|
||||
- `cherrypick_flutter` - `v1.1.2`
|
||||
- `cherrypick_generator` - `v1.1.0`
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick_flutter` - `v1.1.2`
|
||||
|
||||
#### `cherrypick` - `v2.2.0`
|
||||
|
||||
#### `cherrypick_annotations` - `v1.1.0`
|
||||
|
||||
#### `cherrypick_generator` - `v1.1.0`
|
||||
- **DOCS**: move and update quick start guides to ./doc directory.
|
||||
|
||||
|
||||
## 2025-06-04
|
||||
|
||||
@@ -99,7 +99,7 @@ final scope = CherryPick.openRootScope()
|
||||
..installModules([$MyModule()]);
|
||||
|
||||
final repo = scope.resolve<DataRepository>();
|
||||
final greeting = scope.resolve<String>(params: 'John'); // 'Hello, John!'
|
||||
final greeting = scope.resolveWithParams<String>('John'); // 'Hello, John!'
|
||||
```
|
||||
|
||||
_For Flutter, wrap your app with `CherryPickProvider` for DI scopes in the widget tree:_
|
||||
|
||||
2
cherrypick/.gitignore
vendored
2
cherrypick/.gitignore
vendored
@@ -22,5 +22,3 @@ doc/api/
|
||||
|
||||
# FVM Version Cache
|
||||
.fvm/
|
||||
|
||||
pubspec_overrides.yaml
|
||||
@@ -1,16 +1,6 @@
|
||||
## 3.0.0-dev.1
|
||||
## 2.2.0-dev.2
|
||||
|
||||
- **DOCS**: add quick guide for circular dependency detection to README.
|
||||
|
||||
## 3.0.0-dev.0
|
||||
|
||||
> Note: This release has breaking changes.
|
||||
|
||||
- **BREAKING** **FEAT**: implement comprehensive circular dependency detection system.
|
||||
|
||||
## 2.2.0
|
||||
|
||||
- Graduate package to a stable release. See pre-releases prior to this version for changelog entries.
|
||||
- **DOCS**: move and update quick start guides to ./doc directory.
|
||||
|
||||
## 2.2.0-dev.1
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
A **Binding** acts as a configuration for how to create or provide a particular dependency. Bindings support:
|
||||
|
||||
|
||||
- Direct instance assignment (`toInstance()`, `toInstanceAsync()`)
|
||||
- Lazy providers (sync/async functions)
|
||||
- Provider functions supporting dynamic parameters
|
||||
@@ -236,80 +237,6 @@ class ApiClientImpl implements ApiClient {
|
||||
- [x] Singleton Lifecycle Management
|
||||
- [x] Modular and Hierarchical Composition
|
||||
- [x] Null-safe Resolution (tryResolve/tryResolveAsync)
|
||||
- [x] Circular Dependency Detection (Local and Global)
|
||||
|
||||
## Quick Guide: Circular Dependency Detection
|
||||
|
||||
CherryPick can detect circular dependencies in your DI configuration, helping you avoid infinite loops and hard-to-debug errors.
|
||||
|
||||
**How to use:**
|
||||
|
||||
### 1. Enable Cycle Detection for Development
|
||||
|
||||
**Local detection (within one scope):**
|
||||
```dart
|
||||
final scope = CherryPick.openSafeRootScope(); // Local detection enabled by default
|
||||
// or, for an existing scope:
|
||||
scope.enableCycleDetection();
|
||||
```
|
||||
|
||||
**Global detection (across all scopes):**
|
||||
```dart
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
final rootScope = CherryPick.openGlobalSafeRootScope();
|
||||
```
|
||||
|
||||
### 2. Error Example
|
||||
|
||||
If you declare mutually dependent services:
|
||||
```dart
|
||||
class A { A(B b); }
|
||||
class B { B(A a); }
|
||||
|
||||
scope.installModules([
|
||||
Module((bind) {
|
||||
bind<A>().to((s) => A(s.resolve<B>()));
|
||||
bind<B>().to((s) => B(s.resolve<A>()));
|
||||
}),
|
||||
]);
|
||||
|
||||
scope.resolve<A>(); // Throws CircularDependencyException
|
||||
```
|
||||
|
||||
### 3. Typical Usage Pattern
|
||||
|
||||
- **Always enable detection** in debug and test environments for maximum safety.
|
||||
- **Disable detection** in production for performance (after code is tested).
|
||||
|
||||
```dart
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
void main() {
|
||||
if (kDebugMode) {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
}
|
||||
runApp(MyApp());
|
||||
}
|
||||
```
|
||||
|
||||
### 4. Handling and Debugging Errors
|
||||
|
||||
On detection, `CircularDependencyException` is thrown with a readable dependency chain:
|
||||
```dart
|
||||
try {
|
||||
scope.resolve<MyService>();
|
||||
} on CircularDependencyException catch (e) {
|
||||
print('Dependency chain: ${e.dependencyChain}');
|
||||
}
|
||||
```
|
||||
|
||||
**More details:** See [cycle_detection.en.md](doc/cycle_detection.en.md)
|
||||
|
||||
## Documentation
|
||||
|
||||
- [Circular Dependency Detection (English)](doc/cycle_detection.en.md)
|
||||
- [Обнаружение циклических зависимостей (Русский)](doc/cycle_detection.ru.md)
|
||||
|
||||
## Contributing
|
||||
|
||||
|
||||
@@ -1,230 +0,0 @@
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
|
||||
// Пример сервисов для демонстрации
|
||||
class DatabaseService {
|
||||
void connect() => print('🔌 Connecting to database');
|
||||
}
|
||||
|
||||
class ApiService {
|
||||
final DatabaseService database;
|
||||
ApiService(this.database);
|
||||
|
||||
void fetchData() {
|
||||
database.connect();
|
||||
print('📡 Fetching data via API');
|
||||
}
|
||||
}
|
||||
|
||||
class UserService {
|
||||
final ApiService apiService;
|
||||
UserService(this.apiService);
|
||||
|
||||
void getUser(String id) {
|
||||
apiService.fetchData();
|
||||
print('👤 Fetching user: $id');
|
||||
}
|
||||
}
|
||||
|
||||
// Модули для различных feature
|
||||
class DatabaseModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<DatabaseService>().singleton().toProvide(() => DatabaseService());
|
||||
}
|
||||
}
|
||||
|
||||
class ApiModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<ApiService>().toProvide(() => ApiService(
|
||||
currentScope.resolve<DatabaseService>()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
class UserModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<UserService>().toProvide(() => UserService(
|
||||
currentScope.resolve<ApiService>()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Пример циклических зависимостей для демонстрации обнаружения
|
||||
class CircularServiceA {
|
||||
final CircularServiceB serviceB;
|
||||
CircularServiceA(this.serviceB);
|
||||
}
|
||||
|
||||
class CircularServiceB {
|
||||
final CircularServiceA serviceA;
|
||||
CircularServiceB(this.serviceA);
|
||||
}
|
||||
|
||||
class CircularModuleA extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<CircularServiceA>().toProvide(() => CircularServiceA(
|
||||
currentScope.resolve<CircularServiceB>()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
class CircularModuleB extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<CircularServiceB>().toProvide(() => CircularServiceB(
|
||||
currentScope.resolve<CircularServiceA>()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
print('=== Improved CherryPick Helper Demonstration ===\n');
|
||||
|
||||
// Example 1: Global enabling of cycle detection
|
||||
print('1. Globally enable cycle detection:');
|
||||
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
print('✅ Global cycle detection enabled: ${CherryPick.isGlobalCycleDetectionEnabled}');
|
||||
|
||||
// All new scopes will automatically have cycle detection enabled
|
||||
final globalScope = CherryPick.openRootScope();
|
||||
print('✅ Root scope has cycle detection enabled: ${globalScope.isCycleDetectionEnabled}');
|
||||
|
||||
// Install modules without circular dependencies
|
||||
globalScope.installModules([
|
||||
DatabaseModule(),
|
||||
ApiModule(),
|
||||
UserModule(),
|
||||
]);
|
||||
|
||||
final userService = globalScope.resolve<UserService>();
|
||||
userService.getUser('user123');
|
||||
print('');
|
||||
|
||||
// Example 2: Safe scope creation
|
||||
print('2. Creating safe scopes:');
|
||||
|
||||
CherryPick.closeRootScope(); // Закрываем предыдущий скоуп
|
||||
CherryPick.disableGlobalCycleDetection(); // Отключаем глобальную настройку
|
||||
|
||||
// Создаем безопасный скоуп (с автоматически включенным обнаружением)
|
||||
final safeScope = CherryPick.openSafeRootScope();
|
||||
print('✅ Safe scope created with cycle detection: ${safeScope.isCycleDetectionEnabled}');
|
||||
|
||||
safeScope.installModules([
|
||||
DatabaseModule(),
|
||||
ApiModule(),
|
||||
UserModule(),
|
||||
]);
|
||||
|
||||
final safeUserService = safeScope.resolve<UserService>();
|
||||
safeUserService.getUser('safe_user456');
|
||||
print('');
|
||||
|
||||
// Example 3: Detecting cycles
|
||||
print('3. Detecting circular dependencies:');
|
||||
|
||||
final cyclicScope = CherryPick.openSafeRootScope();
|
||||
cyclicScope.installModules([
|
||||
CircularModuleA(),
|
||||
CircularModuleB(),
|
||||
]);
|
||||
|
||||
try {
|
||||
cyclicScope.resolve<CircularServiceA>();
|
||||
print('❌ This should not be executed');
|
||||
} catch (e) {
|
||||
if (e is CircularDependencyException) {
|
||||
print('❌ Circular dependency detected!');
|
||||
print(' Message: ${e.message}');
|
||||
print(' Chain: ${e.dependencyChain.join(' -> ')}');
|
||||
}
|
||||
}
|
||||
print('');
|
||||
|
||||
// Example 4: Managing detection for specific scopes
|
||||
print('4. Managing detection for specific scopes:');
|
||||
|
||||
CherryPick.closeRootScope();
|
||||
|
||||
// Создаем скоуп без обнаружения
|
||||
// ignore: unused_local_variable
|
||||
final specificScope = CherryPick.openRootScope();
|
||||
print(' Detection in root scope: ${CherryPick.isCycleDetectionEnabledForScope()}');
|
||||
|
||||
// Включаем обнаружение для конкретного скоупа
|
||||
CherryPick.enableCycleDetectionForScope();
|
||||
print('✅ Detection enabled for root scope: ${CherryPick.isCycleDetectionEnabledForScope()}');
|
||||
|
||||
// Создаем дочерний скоуп
|
||||
// ignore: unused_local_variable
|
||||
final featureScope = CherryPick.openScope(scopeName: 'feature.auth');
|
||||
print(' Detection in feature.auth scope: ${CherryPick.isCycleDetectionEnabledForScope(scopeName: 'feature.auth')}');
|
||||
|
||||
// Включаем обнаружение для дочернего скоупа
|
||||
CherryPick.enableCycleDetectionForScope(scopeName: 'feature.auth');
|
||||
print('✅ Detection enabled for feature.auth scope: ${CherryPick.isCycleDetectionEnabledForScope(scopeName: 'feature.auth')}');
|
||||
print('');
|
||||
|
||||
// Example 5: Creating safe child scopes
|
||||
print('5. Creating safe child scopes:');
|
||||
|
||||
final safeFeatureScope = CherryPick.openSafeScope(scopeName: 'feature.payments');
|
||||
print('✅ Safe feature scope created: ${safeFeatureScope.isCycleDetectionEnabled}');
|
||||
|
||||
// You can create a complex hierarchy of scopes
|
||||
final complexScope = CherryPick.openSafeScope(scopeName: 'app.feature.auth.login');
|
||||
print('✅ Complex scope created: ${complexScope.isCycleDetectionEnabled}');
|
||||
print('');
|
||||
|
||||
// Example 6: Tracking resolution chains
|
||||
print('6. Tracking dependency resolution chains:');
|
||||
|
||||
final trackingScope = CherryPick.openSafeRootScope();
|
||||
trackingScope.installModules([
|
||||
DatabaseModule(),
|
||||
ApiModule(),
|
||||
UserModule(),
|
||||
]);
|
||||
|
||||
print(' Chain before resolve: ${CherryPick.getCurrentResolutionChain()}');
|
||||
|
||||
// The chain is populated during resolution, but cleared after completion
|
||||
// ignore: unused_local_variable
|
||||
final trackedUserService = trackingScope.resolve<UserService>();
|
||||
print(' Chain after resolve: ${CherryPick.getCurrentResolutionChain()}');
|
||||
print('');
|
||||
|
||||
// Example 7: Usage recommendations
|
||||
print('7. Recommended usage:');
|
||||
print('');
|
||||
|
||||
print('🔧 Development mode:');
|
||||
print(' CherryPick.enableGlobalCycleDetection(); // Enable globally');
|
||||
print(' or');
|
||||
print(' final scope = CherryPick.openSafeRootScope(); // Safe scope');
|
||||
print('');
|
||||
|
||||
print('🚀 Production mode:');
|
||||
print(' CherryPick.disableGlobalCycleDetection(); // Disable for performance');
|
||||
print(' final scope = CherryPick.openRootScope(); // Regular scope');
|
||||
print('');
|
||||
|
||||
print('🧪 Testing:');
|
||||
print(' setUp(() => CherryPick.enableGlobalCycleDetection());');
|
||||
print(' tearDown(() => CherryPick.closeRootScope());');
|
||||
print('');
|
||||
|
||||
print('🎯 Feature-specific:');
|
||||
print(' CherryPick.enableCycleDetectionForScope(scopeName: "feature.critical");');
|
||||
print(' // Enable only for critical features');
|
||||
|
||||
// Cleanup
|
||||
CherryPick.closeRootScope();
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
|
||||
print('\n=== Demonstration complete ===');
|
||||
}
|
||||
@@ -1,197 +0,0 @@
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
|
||||
// Пример сервисов с циклической зависимостью
|
||||
class UserService {
|
||||
final OrderService orderService;
|
||||
|
||||
UserService(this.orderService);
|
||||
|
||||
void createUser(String name) {
|
||||
print('Creating user: $name');
|
||||
// Пытаемся получить заказы пользователя, что создает циклическую зависимость
|
||||
orderService.getOrdersForUser(name);
|
||||
}
|
||||
}
|
||||
|
||||
class OrderService {
|
||||
final UserService userService;
|
||||
|
||||
OrderService(this.userService);
|
||||
|
||||
void getOrdersForUser(String userName) {
|
||||
print('Getting orders for user: $userName');
|
||||
// Пытаемся получить информацию о пользователе, что создает циклическую зависимость
|
||||
userService.createUser(userName);
|
||||
}
|
||||
}
|
||||
|
||||
// Модули с циклическими зависимостями
|
||||
class UserModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<UserService>().toProvide(() => UserService(
|
||||
currentScope.resolve<OrderService>()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
class OrderModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<OrderService>().toProvide(() => OrderService(
|
||||
currentScope.resolve<UserService>()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Правильная реализация без циклических зависимостей
|
||||
class UserRepository {
|
||||
void createUser(String name) {
|
||||
print('Creating user in repository: $name');
|
||||
}
|
||||
|
||||
String getUserInfo(String name) {
|
||||
return 'User info for: $name';
|
||||
}
|
||||
}
|
||||
|
||||
class OrderRepository {
|
||||
void createOrder(String orderId, String userName) {
|
||||
print('Creating order $orderId for user: $userName');
|
||||
}
|
||||
|
||||
List<String> getOrdersForUser(String userName) {
|
||||
return ['order1', 'order2', 'order3'];
|
||||
}
|
||||
}
|
||||
|
||||
class ImprovedUserService {
|
||||
final UserRepository userRepository;
|
||||
|
||||
ImprovedUserService(this.userRepository);
|
||||
|
||||
void createUser(String name) {
|
||||
userRepository.createUser(name);
|
||||
}
|
||||
|
||||
String getUserInfo(String name) {
|
||||
return userRepository.getUserInfo(name);
|
||||
}
|
||||
}
|
||||
|
||||
class ImprovedOrderService {
|
||||
final OrderRepository orderRepository;
|
||||
final ImprovedUserService userService;
|
||||
|
||||
ImprovedOrderService(this.orderRepository, this.userService);
|
||||
|
||||
void createOrder(String orderId, String userName) {
|
||||
// Проверяем, что пользователь существует
|
||||
final userInfo = userService.getUserInfo(userName);
|
||||
print('User exists: $userInfo');
|
||||
|
||||
orderRepository.createOrder(orderId, userName);
|
||||
}
|
||||
|
||||
List<String> getOrdersForUser(String userName) {
|
||||
return orderRepository.getOrdersForUser(userName);
|
||||
}
|
||||
}
|
||||
|
||||
// Правильные модули без циклических зависимостей
|
||||
class ImprovedUserModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<UserRepository>().singleton().toProvide(() => UserRepository());
|
||||
bind<ImprovedUserService>().toProvide(() => ImprovedUserService(
|
||||
currentScope.resolve<UserRepository>()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
class ImprovedOrderModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<OrderRepository>().singleton().toProvide(() => OrderRepository());
|
||||
bind<ImprovedOrderService>().toProvide(() => ImprovedOrderService(
|
||||
currentScope.resolve<OrderRepository>(),
|
||||
currentScope.resolve<ImprovedUserService>()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
print('=== Circular Dependency Detection Example ===\n');
|
||||
|
||||
// Example 1: Demonstrate circular dependency
|
||||
print('1. Attempt to create a scope with circular dependencies:');
|
||||
try {
|
||||
final scope = Scope(null);
|
||||
scope.enableCycleDetection(); // Включаем обнаружение циклических зависимостей
|
||||
|
||||
scope.installModules([
|
||||
UserModule(),
|
||||
OrderModule(),
|
||||
]);
|
||||
|
||||
// Это должно выбросить CircularDependencyException
|
||||
final userService = scope.resolve<UserService>();
|
||||
print('UserService created: $userService');
|
||||
} catch (e) {
|
||||
print('❌ Circular dependency detected: $e\n');
|
||||
}
|
||||
|
||||
// Example 2: Without circular dependency detection (dangerous!)
|
||||
print('2. Same code without circular dependency detection:');
|
||||
try {
|
||||
final scope = Scope(null);
|
||||
// НЕ включаем обнаружение циклических зависимостей
|
||||
|
||||
scope.installModules([
|
||||
UserModule(),
|
||||
OrderModule(),
|
||||
]);
|
||||
|
||||
// Это приведет к StackOverflowError при попытке использования
|
||||
final userService = scope.resolve<UserService>();
|
||||
print('UserService создан: $userService');
|
||||
|
||||
// Попытка использовать сервис приведет к бесконечной рекурсии
|
||||
// userService.createUser('John'); // Раскомментируйте для демонстрации StackOverflow
|
||||
print('⚠️ UserService created, but using it will cause StackOverflow\n');
|
||||
} catch (e) {
|
||||
print('❌ Error: $e\n');
|
||||
}
|
||||
|
||||
// Example 3: Correct architecture without circular dependencies
|
||||
print('3. Correct architecture without circular dependencies:');
|
||||
try {
|
||||
final scope = Scope(null);
|
||||
scope.enableCycleDetection(); // Включаем для безопасности
|
||||
|
||||
scope.installModules([
|
||||
ImprovedUserModule(),
|
||||
ImprovedOrderModule(),
|
||||
]);
|
||||
|
||||
final userService = scope.resolve<ImprovedUserService>();
|
||||
final orderService = scope.resolve<ImprovedOrderService>();
|
||||
|
||||
print('✅ Services created successfully');
|
||||
|
||||
// Демонстрация работы
|
||||
userService.createUser('John');
|
||||
orderService.createOrder('ORD-001', 'John');
|
||||
final orders = orderService.getOrdersForUser('John');
|
||||
print('✅ Orders for user John: $orders');
|
||||
|
||||
} catch (e) {
|
||||
print('❌ Error: $e');
|
||||
}
|
||||
|
||||
print('\n=== Recommendations ===');
|
||||
print('1. Always enable circular dependency detection in development mode.');
|
||||
print('2. Use repositories and services to separate concerns.');
|
||||
print('3. Avoid mutual dependencies between services at the same level.');
|
||||
print('4. Use events or mediators to decouple components.');
|
||||
}
|
||||
@@ -14,8 +14,6 @@ library;
|
||||
//
|
||||
|
||||
export 'package:cherrypick/src/binding.dart';
|
||||
export 'package:cherrypick/src/cycle_detector.dart';
|
||||
export 'package:cherrypick/src/global_cycle_detector.dart';
|
||||
export 'package:cherrypick/src/helper.dart';
|
||||
export 'package:cherrypick/src/module.dart';
|
||||
export 'package:cherrypick/src/scope.dart';
|
||||
|
||||
@@ -1,167 +0,0 @@
|
||||
//
|
||||
// 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 'dart:collection';
|
||||
|
||||
/// RU: Исключение, выбрасываемое при обнаружении циклической зависимости.
|
||||
/// ENG: Exception thrown when a circular dependency is detected.
|
||||
class CircularDependencyException implements Exception {
|
||||
final String message;
|
||||
final List<String> dependencyChain;
|
||||
|
||||
const CircularDependencyException(this.message, this.dependencyChain);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
final chain = dependencyChain.join(' -> ');
|
||||
return 'CircularDependencyException: $message\nDependency chain: $chain';
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Детектор циклических зависимостей для CherryPick DI контейнера.
|
||||
/// ENG: Circular dependency detector for CherryPick DI container.
|
||||
class CycleDetector {
|
||||
// Стек текущих разрешаемых зависимостей
|
||||
final Set<String> _resolutionStack = HashSet<String>();
|
||||
|
||||
// История разрешения для построения цепочки зависимостей
|
||||
final List<String> _resolutionHistory = [];
|
||||
|
||||
/// RU: Начинает отслеживание разрешения зависимости.
|
||||
/// ENG: Starts tracking dependency resolution.
|
||||
///
|
||||
/// Throws [CircularDependencyException] if circular dependency is detected.
|
||||
void startResolving<T>({String? named}) {
|
||||
final dependencyKey = _createDependencyKey<T>(named);
|
||||
|
||||
if (_resolutionStack.contains(dependencyKey)) {
|
||||
// Найдена циклическая зависимость
|
||||
final cycleStartIndex = _resolutionHistory.indexOf(dependencyKey);
|
||||
final cycle = _resolutionHistory.sublist(cycleStartIndex)..add(dependencyKey);
|
||||
|
||||
throw CircularDependencyException(
|
||||
'Circular dependency detected for $dependencyKey',
|
||||
cycle,
|
||||
);
|
||||
}
|
||||
|
||||
_resolutionStack.add(dependencyKey);
|
||||
_resolutionHistory.add(dependencyKey);
|
||||
}
|
||||
|
||||
/// RU: Завершает отслеживание разрешения зависимости.
|
||||
/// ENG: Finishes tracking dependency resolution.
|
||||
void finishResolving<T>({String? named}) {
|
||||
final dependencyKey = _createDependencyKey<T>(named);
|
||||
_resolutionStack.remove(dependencyKey);
|
||||
|
||||
// Удаляем из истории только если это последний элемент
|
||||
if (_resolutionHistory.isNotEmpty &&
|
||||
_resolutionHistory.last == dependencyKey) {
|
||||
_resolutionHistory.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Очищает все состояние детектора.
|
||||
/// ENG: Clears all detector state.
|
||||
void clear() {
|
||||
_resolutionStack.clear();
|
||||
_resolutionHistory.clear();
|
||||
}
|
||||
|
||||
/// RU: Проверяет, находится ли зависимость в процессе разрешения.
|
||||
/// ENG: Checks if dependency is currently being resolved.
|
||||
bool isResolving<T>({String? named}) {
|
||||
final dependencyKey = _createDependencyKey<T>(named);
|
||||
return _resolutionStack.contains(dependencyKey);
|
||||
}
|
||||
|
||||
/// RU: Возвращает текущую цепочку разрешения зависимостей.
|
||||
/// ENG: Returns current dependency resolution chain.
|
||||
List<String> get currentResolutionChain => List.unmodifiable(_resolutionHistory);
|
||||
|
||||
/// RU: Создает уникальный ключ для зависимости.
|
||||
/// ENG: Creates unique key for dependency.
|
||||
String _createDependencyKey<T>(String? named) {
|
||||
final typeName = T.toString();
|
||||
return named != null ? '$typeName@$named' : typeName;
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Миксин для добавления поддержки обнаружения циклических зависимостей.
|
||||
/// ENG: Mixin for adding circular dependency detection support.
|
||||
mixin CycleDetectionMixin {
|
||||
CycleDetector? _cycleDetector;
|
||||
|
||||
/// RU: Включает обнаружение циклических зависимостей.
|
||||
/// ENG: Enables circular dependency detection.
|
||||
void enableCycleDetection() {
|
||||
_cycleDetector = CycleDetector();
|
||||
}
|
||||
|
||||
/// RU: Отключает обнаружение циклических зависимостей.
|
||||
/// ENG: Disables circular dependency detection.
|
||||
void disableCycleDetection() {
|
||||
_cycleDetector?.clear();
|
||||
_cycleDetector = null;
|
||||
}
|
||||
|
||||
/// RU: Проверяет, включено ли обнаружение циклических зависимостей.
|
||||
/// ENG: Checks if circular dependency detection is enabled.
|
||||
bool get isCycleDetectionEnabled => _cycleDetector != null;
|
||||
|
||||
/// RU: Выполняет действие с отслеживанием циклических зависимостей.
|
||||
/// ENG: Executes action with circular dependency tracking.
|
||||
T withCycleDetection<T>(
|
||||
Type dependencyType,
|
||||
String? named,
|
||||
T Function() action,
|
||||
) {
|
||||
if (_cycleDetector == null) {
|
||||
return action();
|
||||
}
|
||||
|
||||
final dependencyKey = named != null
|
||||
? '${dependencyType.toString()}@$named'
|
||||
: dependencyType.toString();
|
||||
|
||||
if (_cycleDetector!._resolutionStack.contains(dependencyKey)) {
|
||||
final cycleStartIndex = _cycleDetector!._resolutionHistory.indexOf(dependencyKey);
|
||||
final cycle = _cycleDetector!._resolutionHistory.sublist(cycleStartIndex)
|
||||
..add(dependencyKey);
|
||||
|
||||
throw CircularDependencyException(
|
||||
'Circular dependency detected for $dependencyKey',
|
||||
cycle,
|
||||
);
|
||||
}
|
||||
|
||||
_cycleDetector!._resolutionStack.add(dependencyKey);
|
||||
_cycleDetector!._resolutionHistory.add(dependencyKey);
|
||||
|
||||
try {
|
||||
return action();
|
||||
} finally {
|
||||
_cycleDetector!._resolutionStack.remove(dependencyKey);
|
||||
if (_cycleDetector!._resolutionHistory.isNotEmpty &&
|
||||
_cycleDetector!._resolutionHistory.last == dependencyKey) {
|
||||
_cycleDetector!._resolutionHistory.removeLast();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Возвращает текущую цепочку разрешения зависимостей.
|
||||
/// ENG: Returns current dependency resolution chain.
|
||||
List<String> get currentResolutionChain =>
|
||||
_cycleDetector?.currentResolutionChain ?? [];
|
||||
}
|
||||
@@ -1,222 +0,0 @@
|
||||
//
|
||||
// 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 'dart:collection';
|
||||
import 'package:cherrypick/src/cycle_detector.dart';
|
||||
|
||||
/// RU: Глобальный детектор циклических зависимостей для всей иерархии скоупов.
|
||||
/// ENG: Global circular dependency detector for entire scope hierarchy.
|
||||
class GlobalCycleDetector {
|
||||
static GlobalCycleDetector? _instance;
|
||||
|
||||
// Глобальный стек разрешения зависимостей
|
||||
final Set<String> _globalResolutionStack = HashSet<String>();
|
||||
|
||||
// История разрешения для построения цепочки зависимостей
|
||||
final List<String> _globalResolutionHistory = [];
|
||||
|
||||
// Карта активных детекторов по скоупам
|
||||
final Map<String, CycleDetector> _scopeDetectors = HashMap<String, CycleDetector>();
|
||||
|
||||
GlobalCycleDetector._internal();
|
||||
|
||||
/// RU: Получить единственный экземпляр глобального детектора.
|
||||
/// ENG: Get singleton instance of global detector.
|
||||
static GlobalCycleDetector get instance {
|
||||
_instance ??= GlobalCycleDetector._internal();
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
/// RU: Сбросить глобальный детектор (полезно для тестов).
|
||||
/// ENG: Reset global detector (useful for tests).
|
||||
static void reset() {
|
||||
_instance?._globalResolutionStack.clear();
|
||||
_instance?._globalResolutionHistory.clear();
|
||||
_instance?._scopeDetectors.clear();
|
||||
_instance = null;
|
||||
}
|
||||
|
||||
/// RU: Начать отслеживание разрешения зависимости в глобальном контексте.
|
||||
/// ENG: Start tracking dependency resolution in global context.
|
||||
void startGlobalResolving<T>({String? named, String? scopeId}) {
|
||||
final dependencyKey = _createDependencyKeyFromType(T, named, scopeId);
|
||||
|
||||
if (_globalResolutionStack.contains(dependencyKey)) {
|
||||
// Найдена глобальная циклическая зависимость
|
||||
final cycleStartIndex = _globalResolutionHistory.indexOf(dependencyKey);
|
||||
final cycle = _globalResolutionHistory.sublist(cycleStartIndex)..add(dependencyKey);
|
||||
|
||||
throw CircularDependencyException(
|
||||
'Global circular dependency detected for $dependencyKey',
|
||||
cycle,
|
||||
);
|
||||
}
|
||||
|
||||
_globalResolutionStack.add(dependencyKey);
|
||||
_globalResolutionHistory.add(dependencyKey);
|
||||
}
|
||||
|
||||
/// RU: Завершить отслеживание разрешения зависимости в глобальном контексте.
|
||||
/// ENG: Finish tracking dependency resolution in global context.
|
||||
void finishGlobalResolving<T>({String? named, String? scopeId}) {
|
||||
final dependencyKey = _createDependencyKeyFromType(T, named, scopeId);
|
||||
_globalResolutionStack.remove(dependencyKey);
|
||||
|
||||
// Удаляем из истории только если это последний элемент
|
||||
if (_globalResolutionHistory.isNotEmpty &&
|
||||
_globalResolutionHistory.last == dependencyKey) {
|
||||
_globalResolutionHistory.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Выполнить действие с глобальным отслеживанием циклических зависимостей.
|
||||
/// ENG: Execute action with global circular dependency tracking.
|
||||
T withGlobalCycleDetection<T>(
|
||||
Type dependencyType,
|
||||
String? named,
|
||||
String? scopeId,
|
||||
T Function() action,
|
||||
) {
|
||||
final dependencyKey = _createDependencyKeyFromType(dependencyType, named, scopeId);
|
||||
|
||||
if (_globalResolutionStack.contains(dependencyKey)) {
|
||||
final cycleStartIndex = _globalResolutionHistory.indexOf(dependencyKey);
|
||||
final cycle = _globalResolutionHistory.sublist(cycleStartIndex)
|
||||
..add(dependencyKey);
|
||||
|
||||
throw CircularDependencyException(
|
||||
'Global circular dependency detected for $dependencyKey',
|
||||
cycle,
|
||||
);
|
||||
}
|
||||
|
||||
_globalResolutionStack.add(dependencyKey);
|
||||
_globalResolutionHistory.add(dependencyKey);
|
||||
|
||||
try {
|
||||
return action();
|
||||
} finally {
|
||||
_globalResolutionStack.remove(dependencyKey);
|
||||
if (_globalResolutionHistory.isNotEmpty &&
|
||||
_globalResolutionHistory.last == dependencyKey) {
|
||||
_globalResolutionHistory.removeLast();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Получить детектор для конкретного скоупа.
|
||||
/// ENG: Get detector for specific scope.
|
||||
CycleDetector getScopeDetector(String scopeId) {
|
||||
return _scopeDetectors.putIfAbsent(scopeId, () => CycleDetector());
|
||||
}
|
||||
|
||||
/// RU: Удалить детектор для скоупа.
|
||||
/// ENG: Remove detector for scope.
|
||||
void removeScopeDetector(String scopeId) {
|
||||
_scopeDetectors.remove(scopeId);
|
||||
}
|
||||
|
||||
/// RU: Проверить, находится ли зависимость в процессе глобального разрешения.
|
||||
/// ENG: Check if dependency is currently being resolved globally.
|
||||
bool isGloballyResolving<T>({String? named, String? scopeId}) {
|
||||
final dependencyKey = _createDependencyKeyFromType(T, named, scopeId);
|
||||
return _globalResolutionStack.contains(dependencyKey);
|
||||
}
|
||||
|
||||
/// RU: Получить текущую глобальную цепочку разрешения зависимостей.
|
||||
/// ENG: Get current global dependency resolution chain.
|
||||
List<String> get globalResolutionChain => List.unmodifiable(_globalResolutionHistory);
|
||||
|
||||
/// RU: Очистить все состояние детектора.
|
||||
/// ENG: Clear all detector state.
|
||||
void clear() {
|
||||
_globalResolutionStack.clear();
|
||||
_globalResolutionHistory.clear();
|
||||
_scopeDetectors.values.forEach(_detectorClear);
|
||||
_scopeDetectors.clear();
|
||||
}
|
||||
|
||||
void _detectorClear(detector) => detector.clear();
|
||||
|
||||
/// RU: Создать уникальный ключ для зависимости с учетом скоупа.
|
||||
/// ENG: Create unique key for dependency including scope.
|
||||
//String _createDependencyKey<T>(String? named, String? scopeId) {
|
||||
// return _createDependencyKeyFromType(T, named, scopeId);
|
||||
//}
|
||||
|
||||
/// RU: Создать уникальный ключ для зависимости по типу с учетом скоупа.
|
||||
/// ENG: Create unique key for dependency by type including scope.
|
||||
String _createDependencyKeyFromType(Type type, String? named, String? scopeId) {
|
||||
final typeName = type.toString();
|
||||
final namePrefix = named != null ? '@$named' : '';
|
||||
final scopePrefix = scopeId != null ? '[$scopeId]' : '';
|
||||
return '$scopePrefix$typeName$namePrefix';
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Улучшенный миксин для глобального обнаружения циклических зависимостей.
|
||||
/// ENG: Enhanced mixin for global circular dependency detection.
|
||||
mixin GlobalCycleDetectionMixin {
|
||||
String? _scopeId;
|
||||
bool _globalCycleDetectionEnabled = false;
|
||||
|
||||
/// RU: Установить идентификатор скоупа для глобального отслеживания.
|
||||
/// ENG: Set scope identifier for global tracking.
|
||||
void setScopeId(String scopeId) {
|
||||
_scopeId = scopeId;
|
||||
}
|
||||
|
||||
/// RU: Получить идентификатор скоупа.
|
||||
/// ENG: Get scope identifier.
|
||||
String? get scopeId => _scopeId;
|
||||
|
||||
/// RU: Включить глобальное обнаружение циклических зависимостей.
|
||||
/// ENG: Enable global circular dependency detection.
|
||||
void enableGlobalCycleDetection() {
|
||||
_globalCycleDetectionEnabled = true;
|
||||
}
|
||||
|
||||
/// RU: Отключить глобальное обнаружение циклических зависимостей.
|
||||
/// ENG: Disable global circular dependency detection.
|
||||
void disableGlobalCycleDetection() {
|
||||
_globalCycleDetectionEnabled = false;
|
||||
}
|
||||
|
||||
/// RU: Проверить, включено ли глобальное обнаружение циклических зависимостей.
|
||||
/// ENG: Check if global circular dependency detection is enabled.
|
||||
bool get isGlobalCycleDetectionEnabled => _globalCycleDetectionEnabled;
|
||||
|
||||
/// RU: Выполнить действие с глобальным отслеживанием циклических зависимостей.
|
||||
/// ENG: Execute action with global circular dependency tracking.
|
||||
T withGlobalCycleDetection<T>(
|
||||
Type dependencyType,
|
||||
String? named,
|
||||
T Function() action,
|
||||
) {
|
||||
if (!_globalCycleDetectionEnabled) {
|
||||
return action();
|
||||
}
|
||||
|
||||
return GlobalCycleDetector.instance.withGlobalCycleDetection<T>(
|
||||
dependencyType,
|
||||
named,
|
||||
_scopeId,
|
||||
action,
|
||||
);
|
||||
}
|
||||
|
||||
/// RU: Получить текущую глобальную цепочку разрешения зависимостей.
|
||||
/// ENG: Get current global dependency resolution chain.
|
||||
List<String> get globalResolutionChain =>
|
||||
GlobalCycleDetector.instance.globalResolutionChain;
|
||||
}
|
||||
@@ -11,12 +11,9 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
import 'package:cherrypick/src/scope.dart';
|
||||
import 'package:cherrypick/src/global_cycle_detector.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
Scope? _rootScope;
|
||||
bool _globalCycleDetectionEnabled = false;
|
||||
bool _globalCrossScopeCycleDetectionEnabled = false;
|
||||
|
||||
class CherryPick {
|
||||
/// RU: Метод открывает главный [Scope].
|
||||
@@ -25,17 +22,6 @@ class CherryPick {
|
||||
/// return
|
||||
static Scope openRootScope() {
|
||||
_rootScope ??= Scope(null);
|
||||
|
||||
// Применяем глобальную настройку обнаружения циклических зависимостей
|
||||
if (_globalCycleDetectionEnabled && !_rootScope!.isCycleDetectionEnabled) {
|
||||
_rootScope!.enableCycleDetection();
|
||||
}
|
||||
|
||||
// Применяем глобальную настройку обнаружения между скоупами
|
||||
if (_globalCrossScopeCycleDetectionEnabled && !_rootScope!.isGlobalCycleDetectionEnabled) {
|
||||
_rootScope!.enableGlobalCycleDetection();
|
||||
}
|
||||
|
||||
return _rootScope!;
|
||||
}
|
||||
|
||||
@@ -49,150 +35,6 @@ class CherryPick {
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Глобально включает обнаружение циклических зависимостей для всех новых скоупов.
|
||||
/// ENG: Globally enables circular dependency detection for all new scopes.
|
||||
///
|
||||
/// Этот метод влияет на все скоупы, создаваемые через CherryPick.
|
||||
/// This method affects all scopes created through CherryPick.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// CherryPick.enableGlobalCycleDetection();
|
||||
/// final scope = CherryPick.openRootScope(); // Автоматически включено обнаружение
|
||||
/// ```
|
||||
static void enableGlobalCycleDetection() {
|
||||
_globalCycleDetectionEnabled = true;
|
||||
|
||||
// Включаем для уже существующего root scope, если он есть
|
||||
if (_rootScope != null) {
|
||||
_rootScope!.enableCycleDetection();
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Глобально отключает обнаружение циклических зависимостей.
|
||||
/// ENG: Globally disables circular dependency detection.
|
||||
///
|
||||
/// Рекомендуется использовать в production для максимальной производительности.
|
||||
/// Recommended for production use for maximum performance.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// CherryPick.disableGlobalCycleDetection();
|
||||
/// ```
|
||||
static void disableGlobalCycleDetection() {
|
||||
_globalCycleDetectionEnabled = false;
|
||||
|
||||
// Отключаем для уже существующего root scope, если он есть
|
||||
if (_rootScope != null) {
|
||||
_rootScope!.disableCycleDetection();
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Проверяет, включено ли глобальное обнаружение циклических зависимостей.
|
||||
/// ENG: Checks if global circular dependency detection is enabled.
|
||||
///
|
||||
/// return true если включено, false если отключено
|
||||
/// return true if enabled, false if disabled
|
||||
static bool get isGlobalCycleDetectionEnabled => _globalCycleDetectionEnabled;
|
||||
|
||||
/// RU: Включает обнаружение циклических зависимостей для конкретного скоупа.
|
||||
/// ENG: Enables circular dependency detection for a specific scope.
|
||||
///
|
||||
/// [scopeName] - имя скоупа (пустая строка для root scope)
|
||||
/// [scopeName] - scope name (empty string for root scope)
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// CherryPick.enableCycleDetectionForScope(); // Для root scope
|
||||
/// CherryPick.enableCycleDetectionForScope(scopeName: 'feature.auth'); // Для конкретного scope
|
||||
/// ```
|
||||
static void enableCycleDetectionForScope({String scopeName = '', String separator = '.'}) {
|
||||
final scope = _getScope(scopeName, separator);
|
||||
scope.enableCycleDetection();
|
||||
}
|
||||
|
||||
/// RU: Отключает обнаружение циклических зависимостей для конкретного скоупа.
|
||||
/// ENG: Disables circular dependency detection for a specific scope.
|
||||
///
|
||||
/// [scopeName] - имя скоупа (пустая строка для root scope)
|
||||
/// [scopeName] - scope name (empty string for root scope)
|
||||
static void disableCycleDetectionForScope({String scopeName = '', String separator = '.'}) {
|
||||
final scope = _getScope(scopeName, separator);
|
||||
scope.disableCycleDetection();
|
||||
}
|
||||
|
||||
/// RU: Проверяет, включено ли обнаружение циклических зависимостей для конкретного скоупа.
|
||||
/// ENG: Checks if circular dependency detection is enabled for a specific scope.
|
||||
///
|
||||
/// [scopeName] - имя скоупа (пустая строка для root scope)
|
||||
/// [scopeName] - scope name (empty string for root scope)
|
||||
///
|
||||
/// return true если включено, false если отключено
|
||||
/// return true if enabled, false if disabled
|
||||
static bool isCycleDetectionEnabledForScope({String scopeName = '', String separator = '.'}) {
|
||||
final scope = _getScope(scopeName, separator);
|
||||
return scope.isCycleDetectionEnabled;
|
||||
}
|
||||
|
||||
/// RU: Возвращает текущую цепочку разрешения зависимостей для конкретного скоупа.
|
||||
/// ENG: Returns current dependency resolution chain for a specific scope.
|
||||
///
|
||||
/// Полезно для отладки и анализа зависимостей.
|
||||
/// Useful for debugging and dependency analysis.
|
||||
///
|
||||
/// [scopeName] - имя скоупа (пустая строка для root scope)
|
||||
/// [scopeName] - scope name (empty string for root scope)
|
||||
///
|
||||
/// return список имен зависимостей в текущей цепочке разрешения
|
||||
/// return list of dependency names in current resolution chain
|
||||
static List<String> getCurrentResolutionChain({String scopeName = '', String separator = '.'}) {
|
||||
final scope = _getScope(scopeName, separator);
|
||||
return scope.currentResolutionChain;
|
||||
}
|
||||
|
||||
/// RU: Создает новый скоуп с автоматически включенным обнаружением циклических зависимостей.
|
||||
/// ENG: Creates a new scope with automatically enabled circular dependency detection.
|
||||
///
|
||||
/// Удобный метод для создания безопасных скоупов в development режиме.
|
||||
/// Convenient method for creating safe scopes in development mode.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final scope = CherryPick.openSafeRootScope();
|
||||
/// // Обнаружение циклических зависимостей автоматически включено
|
||||
/// ```
|
||||
static Scope openSafeRootScope() {
|
||||
final scope = openRootScope();
|
||||
scope.enableCycleDetection();
|
||||
return scope;
|
||||
}
|
||||
|
||||
/// RU: Создает новый дочерний скоуп с автоматически включенным обнаружением циклических зависимостей.
|
||||
/// ENG: Creates a new child scope with automatically enabled circular dependency detection.
|
||||
///
|
||||
/// [scopeName] - имя скоупа
|
||||
/// [scopeName] - scope name
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final scope = CherryPick.openSafeScope(scopeName: 'feature.auth');
|
||||
/// // Обнаружение циклических зависимостей автоматически включено
|
||||
/// ```
|
||||
static Scope openSafeScope({String scopeName = '', String separator = '.'}) {
|
||||
final scope = openScope(scopeName: scopeName, separator: separator);
|
||||
scope.enableCycleDetection();
|
||||
return scope;
|
||||
}
|
||||
|
||||
/// RU: Внутренний метод для получения скоупа по имени.
|
||||
/// ENG: Internal method to get scope by name.
|
||||
static Scope _getScope(String scopeName, String separator) {
|
||||
if (scopeName.isEmpty) {
|
||||
return openRootScope();
|
||||
}
|
||||
return openScope(scopeName: scopeName, separator: separator);
|
||||
}
|
||||
|
||||
/// RU: Метод открывает дочерний [Scope].
|
||||
/// ENG: The method open the child [Scope].
|
||||
///
|
||||
@@ -217,22 +59,10 @@ class CherryPick {
|
||||
throw Exception('Can not open sub scope because scopeName can not split');
|
||||
}
|
||||
|
||||
final scope = nameParts.fold(
|
||||
return nameParts.fold(
|
||||
openRootScope(),
|
||||
(Scope previousValue, String element) =>
|
||||
previousValue.openSubScope(element));
|
||||
|
||||
// Применяем глобальную настройку обнаружения циклических зависимостей
|
||||
if (_globalCycleDetectionEnabled && !scope.isCycleDetectionEnabled) {
|
||||
scope.enableCycleDetection();
|
||||
}
|
||||
|
||||
// Применяем глобальную настройку обнаружения между скоупами
|
||||
if (_globalCrossScopeCycleDetectionEnabled && !scope.isGlobalCycleDetectionEnabled) {
|
||||
scope.enableGlobalCycleDetection();
|
||||
}
|
||||
|
||||
return scope;
|
||||
}
|
||||
|
||||
/// RU: Метод открывает дочерний [Scope].
|
||||
@@ -272,106 +102,4 @@ class CherryPick {
|
||||
openRootScope().closeSubScope(nameParts[0]);
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Глобально включает обнаружение циклических зависимостей между скоупами.
|
||||
/// ENG: Globally enables cross-scope circular dependency detection.
|
||||
///
|
||||
/// Этот режим обнаруживает циклические зависимости во всей иерархии скоупов.
|
||||
/// This mode detects circular dependencies across the entire scope hierarchy.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
/// ```
|
||||
static void enableGlobalCrossScopeCycleDetection() {
|
||||
_globalCrossScopeCycleDetectionEnabled = true;
|
||||
|
||||
// Включаем для уже существующего root scope, если он есть
|
||||
if (_rootScope != null) {
|
||||
_rootScope!.enableGlobalCycleDetection();
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Глобально отключает обнаружение циклических зависимостей между скоупами.
|
||||
/// ENG: Globally disables cross-scope circular dependency detection.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// CherryPick.disableGlobalCrossScopeCycleDetection();
|
||||
/// ```
|
||||
static void disableGlobalCrossScopeCycleDetection() {
|
||||
_globalCrossScopeCycleDetectionEnabled = false;
|
||||
|
||||
// Отключаем для уже существующего root scope, если он есть
|
||||
if (_rootScope != null) {
|
||||
_rootScope!.disableGlobalCycleDetection();
|
||||
}
|
||||
|
||||
// Очищаем глобальный детектор
|
||||
GlobalCycleDetector.instance.clear();
|
||||
}
|
||||
|
||||
/// RU: Проверяет, включено ли глобальное обнаружение циклических зависимостей между скоупами.
|
||||
/// ENG: Checks if global cross-scope circular dependency detection is enabled.
|
||||
///
|
||||
/// return true если включено, false если отключено
|
||||
/// return true if enabled, false if disabled
|
||||
static bool get isGlobalCrossScopeCycleDetectionEnabled => _globalCrossScopeCycleDetectionEnabled;
|
||||
|
||||
/// RU: Возвращает глобальную цепочку разрешения зависимостей.
|
||||
/// ENG: Returns global dependency resolution chain.
|
||||
///
|
||||
/// Полезно для отладки циклических зависимостей между скоупами.
|
||||
/// Useful for debugging circular dependencies across scopes.
|
||||
///
|
||||
/// return список имен зависимостей в глобальной цепочке разрешения
|
||||
/// return list of dependency names in global resolution chain
|
||||
static List<String> getGlobalResolutionChain() {
|
||||
return GlobalCycleDetector.instance.globalResolutionChain;
|
||||
}
|
||||
|
||||
/// RU: Очищает все состояние глобального детектора циклических зависимостей.
|
||||
/// ENG: Clears all global circular dependency detector state.
|
||||
///
|
||||
/// Полезно для тестов и сброса состояния.
|
||||
/// Useful for tests and state reset.
|
||||
static void clearGlobalCycleDetector() {
|
||||
GlobalCycleDetector.reset();
|
||||
}
|
||||
|
||||
/// RU: Создает новый скоуп с автоматически включенным глобальным обнаружением циклических зависимостей.
|
||||
/// ENG: Creates a new scope with automatically enabled global circular dependency detection.
|
||||
///
|
||||
/// Этот скоуп будет отслеживать циклические зависимости во всей иерархии.
|
||||
/// This scope will track circular dependencies across the entire hierarchy.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final scope = CherryPick.openGlobalSafeRootScope();
|
||||
/// // Глобальное обнаружение циклических зависимостей автоматически включено
|
||||
/// ```
|
||||
static Scope openGlobalSafeRootScope() {
|
||||
final scope = openRootScope();
|
||||
scope.enableCycleDetection();
|
||||
scope.enableGlobalCycleDetection();
|
||||
return scope;
|
||||
}
|
||||
|
||||
/// RU: Создает новый дочерний скоуп с автоматически включенным глобальным обнаружением циклических зависимостей.
|
||||
/// ENG: Creates a new child scope with automatically enabled global circular dependency detection.
|
||||
///
|
||||
/// [scopeName] - имя скоупа
|
||||
/// [scopeName] - scope name
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final scope = CherryPick.openGlobalSafeScope(scopeName: 'feature.auth');
|
||||
/// // Глобальное обнаружение циклических зависимостей автоматически включено
|
||||
/// ```
|
||||
static Scope openGlobalSafeScope({String scopeName = '', String separator = '.'}) {
|
||||
final scope = openScope(scopeName: scopeName, separator: separator);
|
||||
scope.enableCycleDetection();
|
||||
scope.enableGlobalCycleDetection();
|
||||
return scope;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,16 +11,13 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
import 'dart:collection';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:cherrypick/src/binding.dart';
|
||||
import 'package:cherrypick/src/cycle_detector.dart';
|
||||
import 'package:cherrypick/src/global_cycle_detector.dart';
|
||||
import 'package:cherrypick/src/module.dart';
|
||||
|
||||
Scope openRootScope() => Scope(null);
|
||||
|
||||
class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
class Scope {
|
||||
final Scope? _parentScope;
|
||||
|
||||
/// RU: Метод возвращает родительский [Scope].
|
||||
@@ -32,22 +29,10 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
|
||||
final Map<String, Scope> _scopeMap = HashMap();
|
||||
|
||||
Scope(this._parentScope) {
|
||||
// Генерируем уникальный ID для скоупа
|
||||
setScopeId(_generateScopeId());
|
||||
}
|
||||
Scope(this._parentScope);
|
||||
|
||||
final Set<Module> _modulesList = HashSet();
|
||||
|
||||
/// RU: Генерирует уникальный идентификатор для скоупа.
|
||||
/// ENG: Generates unique identifier for scope.
|
||||
String _generateScopeId() {
|
||||
final random = Random();
|
||||
final timestamp = DateTime.now().millisecondsSinceEpoch;
|
||||
final randomPart = random.nextInt(10000);
|
||||
return 'scope_${timestamp}_$randomPart';
|
||||
}
|
||||
|
||||
/// RU: Метод открывает дочерний (дополнительный) [Scope].
|
||||
///
|
||||
/// ENG: The method opens child (additional) [Scope].
|
||||
@@ -55,17 +40,7 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
/// return [Scope]
|
||||
Scope openSubScope(String name) {
|
||||
if (!_scopeMap.containsKey(name)) {
|
||||
final childScope = Scope(this);
|
||||
|
||||
// Наследуем настройки обнаружения циклических зависимостей
|
||||
if (isCycleDetectionEnabled) {
|
||||
childScope.enableCycleDetection();
|
||||
}
|
||||
if (isGlobalCycleDetectionEnabled) {
|
||||
childScope.enableGlobalCycleDetection();
|
||||
}
|
||||
|
||||
_scopeMap[name] = childScope;
|
||||
_scopeMap[name] = Scope(this);
|
||||
}
|
||||
return _scopeMap[name]!;
|
||||
}
|
||||
@@ -76,13 +51,6 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
///
|
||||
/// return [Scope]
|
||||
void closeSubScope(String name) {
|
||||
final childScope = _scopeMap[name];
|
||||
if (childScope != null) {
|
||||
// Очищаем детектор для дочернего скоупа
|
||||
if (childScope.scopeId != null) {
|
||||
GlobalCycleDetector.instance.removeScopeDetector(childScope.scopeId!);
|
||||
}
|
||||
}
|
||||
_scopeMap.remove(name);
|
||||
}
|
||||
|
||||
@@ -123,59 +91,19 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
/// return - returns an object of type [T] or [StateError]
|
||||
///
|
||||
T resolve<T>({String? named, dynamic params}) {
|
||||
// Используем глобальное отслеживание, если включено
|
||||
if (isGlobalCycleDetectionEnabled) {
|
||||
return withGlobalCycleDetection<T>(T, named, () {
|
||||
return _resolveWithLocalDetection<T>(named: named, params: params);
|
||||
});
|
||||
var resolved = tryResolve<T>(named: named, params: params);
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
} else {
|
||||
return _resolveWithLocalDetection<T>(named: named, params: params);
|
||||
throw StateError(
|
||||
'Can\'t resolve dependency `$T`. Maybe you forget register it?');
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Разрешение с локальным детектором циклических зависимостей.
|
||||
/// ENG: Resolution with local circular dependency detector.
|
||||
T _resolveWithLocalDetection<T>({String? named, dynamic params}) {
|
||||
return withCycleDetection<T>(T, named, () {
|
||||
var resolved = _tryResolveInternal<T>(named: named, params: params);
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
} else {
|
||||
throw StateError(
|
||||
'Can\'t resolve dependency `$T`. Maybe you forget register it?');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// RU: Возвращает найденную зависимость типа [T] или null, если она не может быть найдена.
|
||||
/// ENG: Returns found dependency of type [T] or null if it cannot be found.
|
||||
///
|
||||
T? tryResolve<T>({String? named, dynamic params}) {
|
||||
// Используем глобальное отслеживание, если включено
|
||||
if (isGlobalCycleDetectionEnabled) {
|
||||
return withGlobalCycleDetection<T?>(T, named, () {
|
||||
return _tryResolveWithLocalDetection<T>(named: named, params: params);
|
||||
});
|
||||
} else {
|
||||
return _tryResolveWithLocalDetection<T>(named: named, params: params);
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Попытка разрешения с локальным детектором циклических зависимостей.
|
||||
/// ENG: Try resolution with local circular dependency detector.
|
||||
T? _tryResolveWithLocalDetection<T>({String? named, dynamic params}) {
|
||||
if (isCycleDetectionEnabled) {
|
||||
return withCycleDetection<T?>(T, named, () {
|
||||
return _tryResolveInternal<T>(named: named, params: params);
|
||||
});
|
||||
} else {
|
||||
return _tryResolveInternal<T>(named: named, params: params);
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Внутренний метод для разрешения зависимостей без проверки циклических зависимостей.
|
||||
/// ENG: Internal method for dependency resolution without circular dependency checking.
|
||||
T? _tryResolveInternal<T>({String? named, dynamic params}) {
|
||||
// 1 Поиск зависимости по всем модулям текущего скоупа
|
||||
if (_modulesList.isNotEmpty) {
|
||||
for (var module in _modulesList) {
|
||||
@@ -202,7 +130,7 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
}
|
||||
|
||||
// 2 Поиск зависимостей в родительском скоупе
|
||||
return _parentScope?._tryResolveInternal(named: named, params: params);
|
||||
return _parentScope?.tryResolve(named: named, params: params);
|
||||
}
|
||||
|
||||
/// RU: Асинхронно возвращает найденную зависимость, определенную параметром типа [T].
|
||||
@@ -216,56 +144,16 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
/// return - returns an object of type [T] or [StateError]
|
||||
///
|
||||
Future<T> resolveAsync<T>({String? named, dynamic params}) async {
|
||||
// Используем глобальное отслеживание, если включено
|
||||
if (isGlobalCycleDetectionEnabled) {
|
||||
return withGlobalCycleDetection<Future<T>>(T, named, () async {
|
||||
return await _resolveAsyncWithLocalDetection<T>(named: named, params: params);
|
||||
});
|
||||
var resolved = await tryResolveAsync<T>(named: named, params: params);
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
} else {
|
||||
return await _resolveAsyncWithLocalDetection<T>(named: named, params: params);
|
||||
throw StateError(
|
||||
'Can\'t resolve async dependency `$T`. Maybe you forget register it?');
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Асинхронное разрешение с локальным детектором циклических зависимостей.
|
||||
/// ENG: Async resolution with local circular dependency detector.
|
||||
Future<T> _resolveAsyncWithLocalDetection<T>({String? named, dynamic params}) async {
|
||||
return withCycleDetection<Future<T>>(T, named, () async {
|
||||
var resolved = await _tryResolveAsyncInternal<T>(named: named, params: params);
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
} else {
|
||||
throw StateError(
|
||||
'Can\'t resolve async dependency `$T`. Maybe you forget register it?');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<T?> tryResolveAsync<T>({String? named, dynamic params}) async {
|
||||
// Используем глобальное отслеживание, если включено
|
||||
if (isGlobalCycleDetectionEnabled) {
|
||||
return withGlobalCycleDetection<Future<T?>>(T, named, () async {
|
||||
return await _tryResolveAsyncWithLocalDetection<T>(named: named, params: params);
|
||||
});
|
||||
} else {
|
||||
return await _tryResolveAsyncWithLocalDetection<T>(named: named, params: params);
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Асинхронная попытка разрешения с локальным детектором циклических зависимостей.
|
||||
/// ENG: Async try resolution with local circular dependency detector.
|
||||
Future<T?> _tryResolveAsyncWithLocalDetection<T>({String? named, dynamic params}) async {
|
||||
if (isCycleDetectionEnabled) {
|
||||
return withCycleDetection<Future<T?>>(T, named, () async {
|
||||
return await _tryResolveAsyncInternal<T>(named: named, params: params);
|
||||
});
|
||||
} else {
|
||||
return await _tryResolveAsyncInternal<T>(named: named, params: params);
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Внутренний метод для асинхронного разрешения зависимостей без проверки циклических зависимостей.
|
||||
/// ENG: Internal method for async dependency resolution without circular dependency checking.
|
||||
Future<T?> _tryResolveAsyncInternal<T>({String? named, dynamic params}) async {
|
||||
if (_modulesList.isNotEmpty) {
|
||||
for (var module in _modulesList) {
|
||||
for (var binding in module.bindingSet) {
|
||||
@@ -290,6 +178,6 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
}
|
||||
}
|
||||
}
|
||||
return _parentScope?._tryResolveAsyncInternal(named: named, params: params);
|
||||
return _parentScope?.tryResolveAsync(named: named, params: params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
name: cherrypick
|
||||
description: Cherrypick is a small dependency injection (DI) library for dart/flutter projects.
|
||||
version: 3.0.0-dev.1
|
||||
version: 2.2.0-dev.2
|
||||
homepage: https://pese-git.github.io/cherrypick-site/
|
||||
documentation: https://github.com/pese-git/cherrypick/wiki
|
||||
repository: https://github.com/pese-git/cherrypick
|
||||
|
||||
@@ -1,158 +0,0 @@
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('Cross-Scope Circular Dependency Detection', () {
|
||||
tearDown(() {
|
||||
CherryPick.closeRootScope();
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
});
|
||||
|
||||
test('should detect circular dependency across parent-child scopes', () {
|
||||
// Создаем родительский скоуп с сервисом A
|
||||
final parentScope = CherryPick.openSafeRootScope();
|
||||
parentScope.installModules([ParentScopeModule()]);
|
||||
|
||||
// Создаем дочерний скоуп с сервисом B, который зависит от A
|
||||
final childScope = parentScope.openSubScope('child');
|
||||
childScope.enableCycleDetection();
|
||||
childScope.installModules([ChildScopeModule()]);
|
||||
|
||||
// Сервис A в родительском скоупе пытается получить сервис B из дочернего скоупа
|
||||
// Это создает циклическую зависимость между скоупами
|
||||
expect(
|
||||
() => parentScope.resolve<CrossScopeServiceA>(),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should detect circular dependency in complex scope hierarchy', () {
|
||||
final rootScope = CherryPick.openSafeRootScope();
|
||||
final level1Scope = rootScope.openSubScope('level1');
|
||||
final level2Scope = level1Scope.openSubScope('level2');
|
||||
|
||||
level1Scope.enableCycleDetection();
|
||||
level2Scope.enableCycleDetection();
|
||||
|
||||
// Устанавливаем модули на разных уровнях
|
||||
rootScope.installModules([RootLevelModule()]);
|
||||
level1Scope.installModules([Level1Module()]);
|
||||
level2Scope.installModules([Level2Module()]);
|
||||
|
||||
// Попытка разрешить зависимость, которая создает цикл через все уровни
|
||||
expect(
|
||||
() => level2Scope.resolve<Level2Service>(),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('current implementation limitation - may not detect cross-scope cycles', () {
|
||||
// Этот тест демонстрирует ограничение текущей реализации
|
||||
final parentScope = CherryPick.openRootScope();
|
||||
parentScope.enableCycleDetection();
|
||||
|
||||
final childScope = parentScope.openSubScope('child');
|
||||
// НЕ включаем cycle detection для дочернего скоупа
|
||||
|
||||
parentScope.installModules([ParentScopeModule()]);
|
||||
childScope.installModules([ChildScopeModule()]);
|
||||
|
||||
// В текущей реализации это может не обнаружить циклическую зависимость
|
||||
// если детекторы работают независимо в каждом скоупе
|
||||
try {
|
||||
// ignore: unused_local_variable
|
||||
final service = parentScope.resolve<CrossScopeServiceA>();
|
||||
// Если мы дошли сюда, значит циклическая зависимость не была обнаружена
|
||||
print('Циклическая зависимость между скоупами не обнаружена');
|
||||
} catch (e) {
|
||||
if (e is CircularDependencyException) {
|
||||
print('Циклическая зависимость обнаружена: ${e.message}');
|
||||
} else {
|
||||
print('Другая ошибка: $e');
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Тестовые сервисы для демонстрации циклических зависимостей между скоупами
|
||||
|
||||
class CrossScopeServiceA {
|
||||
final CrossScopeServiceB serviceB;
|
||||
CrossScopeServiceA(this.serviceB);
|
||||
}
|
||||
|
||||
class CrossScopeServiceB {
|
||||
final CrossScopeServiceA serviceA;
|
||||
CrossScopeServiceB(this.serviceA);
|
||||
}
|
||||
|
||||
class ParentScopeModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<CrossScopeServiceA>().toProvide(() {
|
||||
// Пытаемся получить сервис B из дочернего скоупа
|
||||
final childScope = currentScope.openSubScope('child');
|
||||
return CrossScopeServiceA(childScope.resolve<CrossScopeServiceB>());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class ChildScopeModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<CrossScopeServiceB>().toProvide(() {
|
||||
// Пытаемся получить сервис A из родительского скоупа
|
||||
final parentScope = currentScope.parentScope!;
|
||||
return CrossScopeServiceB(parentScope.resolve<CrossScopeServiceA>());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Сервисы для сложной иерархии скоупов
|
||||
|
||||
class RootLevelService {
|
||||
final Level1Service level1Service;
|
||||
RootLevelService(this.level1Service);
|
||||
}
|
||||
|
||||
class Level1Service {
|
||||
final Level2Service level2Service;
|
||||
Level1Service(this.level2Service);
|
||||
}
|
||||
|
||||
class Level2Service {
|
||||
final RootLevelService rootService;
|
||||
Level2Service(this.rootService);
|
||||
}
|
||||
|
||||
class RootLevelModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<RootLevelService>().toProvide(() {
|
||||
final level1Scope = currentScope.openSubScope('level1');
|
||||
return RootLevelService(level1Scope.resolve<Level1Service>());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class Level1Module extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<Level1Service>().toProvide(() {
|
||||
final level2Scope = currentScope.openSubScope('level2');
|
||||
return Level1Service(level2Scope.resolve<Level2Service>());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class Level2Module extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<Level2Service>().toProvide(() {
|
||||
// Идем к корневому скоупу через цепочку родителей
|
||||
var rootScope = currentScope.parentScope?.parentScope;
|
||||
return Level2Service(rootScope!.resolve<RootLevelService>());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,213 +0,0 @@
|
||||
import 'package:cherrypick/src/cycle_detector.dart';
|
||||
import 'package:cherrypick/src/module.dart';
|
||||
import 'package:cherrypick/src/scope.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('CycleDetector', () {
|
||||
late CycleDetector detector;
|
||||
|
||||
setUp(() {
|
||||
detector = CycleDetector();
|
||||
});
|
||||
|
||||
test('should detect simple circular dependency', () {
|
||||
detector.startResolving<String>();
|
||||
|
||||
expect(
|
||||
() => detector.startResolving<String>(),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should detect circular dependency with named bindings', () {
|
||||
detector.startResolving<String>(named: 'test');
|
||||
|
||||
expect(
|
||||
() => detector.startResolving<String>(named: 'test'),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should allow different types to be resolved simultaneously', () {
|
||||
detector.startResolving<String>();
|
||||
detector.startResolving<int>();
|
||||
|
||||
expect(() => detector.finishResolving<int>(), returnsNormally);
|
||||
expect(() => detector.finishResolving<String>(), returnsNormally);
|
||||
});
|
||||
|
||||
test('should detect complex circular dependency chain', () {
|
||||
detector.startResolving<String>();
|
||||
detector.startResolving<int>();
|
||||
detector.startResolving<bool>();
|
||||
|
||||
expect(
|
||||
() => detector.startResolving<String>(),
|
||||
throwsA(predicate((e) =>
|
||||
e is CircularDependencyException &&
|
||||
e.dependencyChain.contains('String') &&
|
||||
e.dependencyChain.length > 1
|
||||
)),
|
||||
);
|
||||
});
|
||||
|
||||
test('should clear state properly', () {
|
||||
detector.startResolving<String>();
|
||||
detector.clear();
|
||||
|
||||
expect(() => detector.startResolving<String>(), returnsNormally);
|
||||
});
|
||||
|
||||
test('should track resolution history correctly', () {
|
||||
detector.startResolving<String>();
|
||||
detector.startResolving<int>();
|
||||
|
||||
expect(detector.currentResolutionChain, contains('String'));
|
||||
expect(detector.currentResolutionChain, contains('int'));
|
||||
expect(detector.currentResolutionChain.length, equals(2));
|
||||
|
||||
detector.finishResolving<int>();
|
||||
expect(detector.currentResolutionChain.length, equals(1));
|
||||
expect(detector.currentResolutionChain, contains('String'));
|
||||
});
|
||||
});
|
||||
|
||||
group('Scope with Cycle Detection', () {
|
||||
test('should detect circular dependency in real scenario', () {
|
||||
final scope = Scope(null);
|
||||
scope.enableCycleDetection();
|
||||
|
||||
// Создаем циклическую зависимость: A зависит от B, B зависит от A
|
||||
scope.installModules([
|
||||
CircularModuleA(),
|
||||
CircularModuleB(),
|
||||
]);
|
||||
|
||||
expect(
|
||||
() => scope.resolve<ServiceA>(),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should work normally without cycle detection enabled', () {
|
||||
final scope = Scope(null);
|
||||
// Не включаем обнаружение циклических зависимостей
|
||||
|
||||
scope.installModules([
|
||||
SimpleModule(),
|
||||
]);
|
||||
|
||||
expect(() => scope.resolve<SimpleService>(), returnsNormally);
|
||||
expect(scope.resolve<SimpleService>(), isA<SimpleService>());
|
||||
});
|
||||
|
||||
test('should allow disabling cycle detection', () {
|
||||
final scope = Scope(null);
|
||||
scope.enableCycleDetection();
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
|
||||
scope.disableCycleDetection();
|
||||
expect(scope.isCycleDetectionEnabled, isFalse);
|
||||
});
|
||||
|
||||
test('should handle named dependencies in cycle detection', () {
|
||||
final scope = Scope(null);
|
||||
scope.enableCycleDetection();
|
||||
|
||||
scope.installModules([
|
||||
NamedCircularModule(),
|
||||
]);
|
||||
|
||||
expect(
|
||||
() => scope.resolve<String>(named: 'circular'),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should detect cycles in async resolution', () async {
|
||||
final scope = Scope(null);
|
||||
scope.enableCycleDetection();
|
||||
|
||||
scope.installModules([
|
||||
AsyncCircularModule(),
|
||||
]);
|
||||
|
||||
expect(
|
||||
() => scope.resolveAsync<AsyncServiceA>(),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Test services and modules for circular dependency testing
|
||||
|
||||
class ServiceA {
|
||||
final ServiceB serviceB;
|
||||
ServiceA(this.serviceB);
|
||||
}
|
||||
|
||||
class ServiceB {
|
||||
final ServiceA serviceA;
|
||||
ServiceB(this.serviceA);
|
||||
}
|
||||
|
||||
class CircularModuleA extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<ServiceA>().toProvide(() => ServiceA(currentScope.resolve<ServiceB>()));
|
||||
}
|
||||
}
|
||||
|
||||
class CircularModuleB extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<ServiceB>().toProvide(() => ServiceB(currentScope.resolve<ServiceA>()));
|
||||
}
|
||||
}
|
||||
|
||||
class SimpleService {
|
||||
SimpleService();
|
||||
}
|
||||
|
||||
class SimpleModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<SimpleService>().toProvide(() => SimpleService());
|
||||
}
|
||||
}
|
||||
|
||||
class NamedCircularModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<String>()
|
||||
.withName('circular')
|
||||
.toProvide(() => currentScope.resolve<String>(named: 'circular'));
|
||||
}
|
||||
}
|
||||
|
||||
class AsyncServiceA {
|
||||
final AsyncServiceB serviceB;
|
||||
AsyncServiceA(this.serviceB);
|
||||
}
|
||||
|
||||
class AsyncServiceB {
|
||||
final AsyncServiceA serviceA;
|
||||
AsyncServiceB(this.serviceA);
|
||||
}
|
||||
|
||||
class AsyncCircularModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<AsyncServiceA>().toProvideAsync(() async {
|
||||
final serviceB = await currentScope.resolveAsync<AsyncServiceB>();
|
||||
return AsyncServiceA(serviceB);
|
||||
});
|
||||
|
||||
bind<AsyncServiceB>().toProvideAsync(() async {
|
||||
final serviceA = await currentScope.resolveAsync<AsyncServiceA>();
|
||||
return AsyncServiceB(serviceA);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,274 +0,0 @@
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('Global Cycle Detection', () {
|
||||
setUp(() {
|
||||
// Сбрасываем состояние перед каждым тестом
|
||||
CherryPick.closeRootScope();
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
CherryPick.disableGlobalCrossScopeCycleDetection();
|
||||
CherryPick.clearGlobalCycleDetector();
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
// Очищаем состояние после каждого теста
|
||||
CherryPick.closeRootScope();
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
CherryPick.disableGlobalCrossScopeCycleDetection();
|
||||
CherryPick.clearGlobalCycleDetector();
|
||||
});
|
||||
|
||||
group('Global Cross-Scope Cycle Detection', () {
|
||||
test('should enable global cross-scope cycle detection', () {
|
||||
expect(CherryPick.isGlobalCrossScopeCycleDetectionEnabled, isFalse);
|
||||
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
|
||||
expect(CherryPick.isGlobalCrossScopeCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should disable global cross-scope cycle detection', () {
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
expect(CherryPick.isGlobalCrossScopeCycleDetectionEnabled, isTrue);
|
||||
|
||||
CherryPick.disableGlobalCrossScopeCycleDetection();
|
||||
|
||||
expect(CherryPick.isGlobalCrossScopeCycleDetectionEnabled, isFalse);
|
||||
});
|
||||
|
||||
test('should automatically enable global cycle detection for new root scope', () {
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
|
||||
final scope = CherryPick.openRootScope();
|
||||
|
||||
expect(scope.isGlobalCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should automatically enable global cycle detection for existing root scope', () {
|
||||
final scope = CherryPick.openRootScope();
|
||||
expect(scope.isGlobalCycleDetectionEnabled, isFalse);
|
||||
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
|
||||
expect(scope.isGlobalCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('Global Safe Scope Creation', () {
|
||||
test('should create global safe root scope with both detections enabled', () {
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
expect(scope.isGlobalCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should create global safe sub-scope with both detections enabled', () {
|
||||
final scope = CherryPick.openGlobalSafeScope(scopeName: 'feature.global');
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
expect(scope.isGlobalCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('Cross-Scope Circular Dependency Detection', () {
|
||||
test('should detect circular dependency across parent-child scopes', () {
|
||||
final parentScope = CherryPick.openGlobalSafeRootScope();
|
||||
parentScope.installModules([GlobalParentModule()]);
|
||||
|
||||
final childScope = parentScope.openSubScope('child');
|
||||
childScope.installModules([GlobalChildModule()]);
|
||||
|
||||
expect(
|
||||
() => parentScope.resolve<GlobalServiceA>(),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should detect circular dependency in complex scope hierarchy', () {
|
||||
final rootScope = CherryPick.openGlobalSafeRootScope();
|
||||
final level1Scope = rootScope.openSubScope('level1');
|
||||
final level2Scope = level1Scope.openSubScope('level2');
|
||||
|
||||
// Устанавливаем модули на разных уровнях
|
||||
rootScope.installModules([GlobalRootModule()]);
|
||||
level1Scope.installModules([GlobalLevel1Module()]);
|
||||
level2Scope.installModules([GlobalLevel2Module()]);
|
||||
|
||||
expect(
|
||||
() => level2Scope.resolve<GlobalLevel2Service>(),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should provide detailed global resolution chain in exception', () {
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
scope.installModules([GlobalParentModule()]);
|
||||
|
||||
final childScope = scope.openSubScope('child');
|
||||
childScope.installModules([GlobalChildModule()]);
|
||||
|
||||
try {
|
||||
scope.resolve<GlobalServiceA>();
|
||||
fail('Expected CircularDependencyException');
|
||||
} catch (e) {
|
||||
expect(e, isA<CircularDependencyException>());
|
||||
final circularError = e as CircularDependencyException;
|
||||
|
||||
// Проверяем, что цепочка содержит информацию о скоупах
|
||||
expect(circularError.dependencyChain, isNotEmpty);
|
||||
expect(circularError.dependencyChain.length, greaterThan(1));
|
||||
|
||||
// Цепочка должна содержать оба сервиса
|
||||
final chainString = circularError.dependencyChain.join(' -> ');
|
||||
expect(chainString, contains('GlobalServiceA'));
|
||||
expect(chainString, contains('GlobalServiceB'));
|
||||
}
|
||||
});
|
||||
|
||||
test('should track global resolution chain', () {
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
scope.installModules([SimpleGlobalModule()]);
|
||||
|
||||
// До разрешения цепочка должна быть пустой
|
||||
expect(CherryPick.getGlobalResolutionChain(), isEmpty);
|
||||
|
||||
final service = scope.resolve<SimpleGlobalService>();
|
||||
expect(service, isA<SimpleGlobalService>());
|
||||
|
||||
// После разрешения цепочка должна быть очищена
|
||||
expect(CherryPick.getGlobalResolutionChain(), isEmpty);
|
||||
});
|
||||
|
||||
test('should clear global cycle detector state', () {
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
// ignore: unused_local_variable
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
|
||||
expect(CherryPick.getGlobalResolutionChain(), isEmpty);
|
||||
|
||||
CherryPick.clearGlobalCycleDetector();
|
||||
|
||||
// После очистки детектор должен быть сброшен
|
||||
expect(CherryPick.getGlobalResolutionChain(), isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
group('Inheritance of Global Settings', () {
|
||||
test('should inherit global cycle detection in child scopes', () {
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
|
||||
final parentScope = CherryPick.openRootScope();
|
||||
final childScope = parentScope.openSubScope('child');
|
||||
|
||||
expect(parentScope.isGlobalCycleDetectionEnabled, isTrue);
|
||||
expect(childScope.isGlobalCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should inherit both local and global cycle detection', () {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
|
||||
final scope = CherryPick.openScope(scopeName: 'feature.test');
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
expect(scope.isGlobalCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Test services for global circular dependency testing
|
||||
|
||||
class GlobalServiceA {
|
||||
final GlobalServiceB serviceB;
|
||||
GlobalServiceA(this.serviceB);
|
||||
}
|
||||
|
||||
class GlobalServiceB {
|
||||
final GlobalServiceA serviceA;
|
||||
GlobalServiceB(this.serviceA);
|
||||
}
|
||||
|
||||
class GlobalParentModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<GlobalServiceA>().toProvide(() {
|
||||
// Получаем сервис B из дочернего скоупа
|
||||
final childScope = currentScope.openSubScope('child');
|
||||
return GlobalServiceA(childScope.resolve<GlobalServiceB>());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class GlobalChildModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<GlobalServiceB>().toProvide(() {
|
||||
// Получаем сервис A из родительского скоупа
|
||||
final parentScope = currentScope.parentScope!;
|
||||
return GlobalServiceB(parentScope.resolve<GlobalServiceA>());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Services for complex hierarchy testing
|
||||
|
||||
class GlobalRootService {
|
||||
final GlobalLevel1Service level1Service;
|
||||
GlobalRootService(this.level1Service);
|
||||
}
|
||||
|
||||
class GlobalLevel1Service {
|
||||
final GlobalLevel2Service level2Service;
|
||||
GlobalLevel1Service(this.level2Service);
|
||||
}
|
||||
|
||||
class GlobalLevel2Service {
|
||||
final GlobalRootService rootService;
|
||||
GlobalLevel2Service(this.rootService);
|
||||
}
|
||||
|
||||
class GlobalRootModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<GlobalRootService>().toProvide(() {
|
||||
final level1Scope = currentScope.openSubScope('level1');
|
||||
return GlobalRootService(level1Scope.resolve<GlobalLevel1Service>());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class GlobalLevel1Module extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<GlobalLevel1Service>().toProvide(() {
|
||||
final level2Scope = currentScope.openSubScope('level2');
|
||||
return GlobalLevel1Service(level2Scope.resolve<GlobalLevel2Service>());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class GlobalLevel2Module extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<GlobalLevel2Service>().toProvide(() {
|
||||
// Идем к корневому скоупу через цепочку родителей
|
||||
var rootScope = currentScope.parentScope?.parentScope;
|
||||
return GlobalLevel2Service(rootScope!.resolve<GlobalRootService>());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Simple service for non-circular testing
|
||||
|
||||
class SimpleGlobalService {
|
||||
SimpleGlobalService();
|
||||
}
|
||||
|
||||
class SimpleGlobalModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<SimpleGlobalService>().toProvide(() => SimpleGlobalService());
|
||||
}
|
||||
}
|
||||
@@ -1,240 +0,0 @@
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('CherryPick Cycle Detection Helper Methods', () {
|
||||
setUp(() {
|
||||
// Сбрасываем состояние перед каждым тестом
|
||||
CherryPick.closeRootScope();
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
// Очищаем состояние после каждого теста
|
||||
CherryPick.closeRootScope();
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
});
|
||||
|
||||
group('Global Cycle Detection', () {
|
||||
test('should enable global cycle detection', () {
|
||||
expect(CherryPick.isGlobalCycleDetectionEnabled, isFalse);
|
||||
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
|
||||
expect(CherryPick.isGlobalCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should disable global cycle detection', () {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
expect(CherryPick.isGlobalCycleDetectionEnabled, isTrue);
|
||||
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
|
||||
expect(CherryPick.isGlobalCycleDetectionEnabled, isFalse);
|
||||
});
|
||||
|
||||
test('should automatically enable cycle detection for new root scope when global is enabled', () {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
|
||||
final scope = CherryPick.openRootScope();
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should automatically enable cycle detection for existing root scope when global is enabled', () {
|
||||
final scope = CherryPick.openRootScope();
|
||||
expect(scope.isCycleDetectionEnabled, isFalse);
|
||||
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should automatically disable cycle detection for existing root scope when global is disabled', () {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
final scope = CherryPick.openRootScope();
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isFalse);
|
||||
});
|
||||
|
||||
test('should apply global setting to sub-scopes', () {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
|
||||
final scope = CherryPick.openScope(scopeName: 'test.subscope');
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('Scope-specific Cycle Detection', () {
|
||||
test('should enable cycle detection for root scope', () {
|
||||
final scope = CherryPick.openRootScope();
|
||||
expect(scope.isCycleDetectionEnabled, isFalse);
|
||||
|
||||
CherryPick.enableCycleDetectionForScope();
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(), isTrue);
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should disable cycle detection for root scope', () {
|
||||
CherryPick.enableCycleDetectionForScope();
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(), isTrue);
|
||||
|
||||
CherryPick.disableCycleDetectionForScope();
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(), isFalse);
|
||||
});
|
||||
|
||||
test('should enable cycle detection for specific scope', () {
|
||||
final scopeName = 'feature.auth';
|
||||
CherryPick.openScope(scopeName: scopeName);
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: scopeName), isFalse);
|
||||
|
||||
CherryPick.enableCycleDetectionForScope(scopeName: scopeName);
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: scopeName), isTrue);
|
||||
});
|
||||
|
||||
test('should disable cycle detection for specific scope', () {
|
||||
final scopeName = 'feature.auth';
|
||||
CherryPick.enableCycleDetectionForScope(scopeName: scopeName);
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: scopeName), isTrue);
|
||||
|
||||
CherryPick.disableCycleDetectionForScope(scopeName: scopeName);
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: scopeName), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('Safe Scope Creation', () {
|
||||
test('should create safe root scope with cycle detection enabled', () {
|
||||
final scope = CherryPick.openSafeRootScope();
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should create safe sub-scope with cycle detection enabled', () {
|
||||
final scope = CherryPick.openSafeScope(scopeName: 'feature.safe');
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('safe scope should work independently of global setting', () {
|
||||
// Глобальная настройка отключена
|
||||
expect(CherryPick.isGlobalCycleDetectionEnabled, isFalse);
|
||||
|
||||
final scope = CherryPick.openSafeScope(scopeName: 'feature.independent');
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('Resolution Chain Tracking', () {
|
||||
test('should return empty resolution chain for scope without cycle detection', () {
|
||||
CherryPick.openRootScope();
|
||||
|
||||
final chain = CherryPick.getCurrentResolutionChain();
|
||||
|
||||
expect(chain, isEmpty);
|
||||
});
|
||||
|
||||
test('should return empty resolution chain for scope with cycle detection but no active resolution', () {
|
||||
CherryPick.enableCycleDetectionForScope();
|
||||
|
||||
final chain = CherryPick.getCurrentResolutionChain();
|
||||
|
||||
expect(chain, isEmpty);
|
||||
});
|
||||
|
||||
test('should track resolution chain for specific scope', () {
|
||||
final scopeName = 'feature.tracking';
|
||||
CherryPick.enableCycleDetectionForScope(scopeName: scopeName);
|
||||
|
||||
final chain = CherryPick.getCurrentResolutionChain(scopeName: scopeName);
|
||||
|
||||
expect(chain, isEmpty); // Пустая, так как нет активного разрешения
|
||||
});
|
||||
});
|
||||
|
||||
group('Integration with Circular Dependencies', () {
|
||||
test('should detect circular dependency with global cycle detection enabled', () {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.installModules([CircularTestModule()]);
|
||||
|
||||
expect(
|
||||
() => scope.resolve<CircularServiceA>(),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should detect circular dependency with safe scope', () {
|
||||
final scope = CherryPick.openSafeRootScope();
|
||||
scope.installModules([CircularTestModule()]);
|
||||
|
||||
expect(
|
||||
() => scope.resolve<CircularServiceA>(),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should not detect circular dependency when cycle detection is disabled', () {
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.installModules([CircularTestModule()]);
|
||||
|
||||
// Без обнаружения циклических зависимостей не будет выброшено CircularDependencyException,
|
||||
// но может произойти StackOverflowError при попытке создания объекта
|
||||
expect(() => scope.resolve<CircularServiceA>(),
|
||||
throwsA(isA<StackOverflowError>()));
|
||||
});
|
||||
});
|
||||
|
||||
group('Scope Name Handling', () {
|
||||
test('should handle empty scope name as root scope', () {
|
||||
CherryPick.enableCycleDetectionForScope(scopeName: '');
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: ''), isTrue);
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(), isTrue);
|
||||
});
|
||||
|
||||
test('should handle complex scope names', () {
|
||||
final complexScopeName = 'app.feature.auth.login';
|
||||
CherryPick.enableCycleDetectionForScope(scopeName: complexScopeName);
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: complexScopeName), isTrue);
|
||||
});
|
||||
|
||||
test('should handle custom separator', () {
|
||||
final scopeName = 'app/feature/auth';
|
||||
CherryPick.enableCycleDetectionForScope(scopeName: scopeName, separator: '/');
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: scopeName, separator: '/'), isTrue);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Test services for circular dependency testing
|
||||
class CircularServiceA {
|
||||
final CircularServiceB serviceB;
|
||||
CircularServiceA(this.serviceB);
|
||||
}
|
||||
|
||||
class CircularServiceB {
|
||||
final CircularServiceA serviceA;
|
||||
CircularServiceB(this.serviceA);
|
||||
}
|
||||
|
||||
class CircularTestModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<CircularServiceA>().toProvide(() => CircularServiceA(currentScope.resolve<CircularServiceB>()));
|
||||
bind<CircularServiceB>().toProvide(() => CircularServiceB(currentScope.resolve<CircularServiceA>()));
|
||||
}
|
||||
}
|
||||
2
cherrypick_annotations/.gitignore
vendored
2
cherrypick_annotations/.gitignore
vendored
@@ -24,5 +24,3 @@ doc/api/
|
||||
.fvm/
|
||||
|
||||
melos_cherrypick_annotations.iml
|
||||
|
||||
pubspec_overrides.yaml
|
||||
@@ -1,7 +1,3 @@
|
||||
## 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.
|
||||
|
||||
@@ -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
|
||||
version: 1.1.0-dev.1
|
||||
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
|
||||
|
||||
201
cherrypick_cli/LICENSE
Normal file
201
cherrypick_cli/LICENSE
Normal file
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
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.
|
||||
93
cherrypick_cli/README.md
Normal file
93
cherrypick_cli/README.md
Normal file
@@ -0,0 +1,93 @@
|
||||
# CherryPick CLI
|
||||
|
||||
A command-line tool for managing and generating `build.yaml` configuration for the [CherryPick](https://github.com/pese-git/cherrypick) dependency injection ecosystem for Dart & Flutter.
|
||||
|
||||
---
|
||||
|
||||
## Features
|
||||
- 📦 Quickly add or update CherryPick generator sections in your project's `build.yaml`.
|
||||
- 🛡️ Safely preserves unrelated configs and packages.
|
||||
- 📝 Always outputs a human-friendly, formatted YAML file.
|
||||
- 🏷️ Supports custom output directories and custom build.yaml file paths.
|
||||
|
||||
---
|
||||
|
||||
## Getting Started
|
||||
|
||||
1. **Navigate to the CLI package directory:**
|
||||
```bash
|
||||
cd cherrypick_cli
|
||||
```
|
||||
2. **Get dependencies:**
|
||||
```bash
|
||||
dart pub get
|
||||
```
|
||||
3. **Run the CLI:**
|
||||
```bash
|
||||
dart run cherrypick_cli init --output_dir=lib/generated
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Usage
|
||||
|
||||
### Show help
|
||||
```bash
|
||||
dart run cherrypick_cli --help
|
||||
```
|
||||
|
||||
### Add or update CherryPick sections in build.yaml
|
||||
```bash
|
||||
dart run cherrypick_cli init --output_dir=lib/generated
|
||||
```
|
||||
|
||||
#### Options:
|
||||
- `--output_dir`, `-o` — Directory for generated code (default: `lib/generated`)
|
||||
- `--build_yaml`, `-f` — Path to build.yaml file (default: `build.yaml`)
|
||||
|
||||
#### Example with custom build.yaml
|
||||
```bash
|
||||
dart run cherrypick_cli init --output_dir=custom/dir --build_yaml=custom_build.yaml
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## What does it do?
|
||||
- Adds or updates the following sections in your `build.yaml` (or custom file):
|
||||
- `cherrypick_generator|inject_generator`
|
||||
- `cherrypick_generator|module_generator`
|
||||
- Ensures all YAML is pretty-printed and readable.
|
||||
- Leaves unrelated configs untouched.
|
||||
|
||||
---
|
||||
|
||||
## Example Output
|
||||
```yaml
|
||||
targets:
|
||||
$default:
|
||||
builders:
|
||||
cherrypick_generator|inject_generator:
|
||||
options:
|
||||
build_extensions:
|
||||
^lib/{{}}.dart:
|
||||
- lib/generated/{{}}.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
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Contributing
|
||||
Pull requests and issues are welcome! See the [main CherryPick repo](https://github.com/pese-git/cherrypick) for more.
|
||||
|
||||
## License
|
||||
See [LICENSE](LICENSE).
|
||||
8
cherrypick_cli/bin/cherrypick_cli.dart
Normal file
8
cherrypick_cli/bin/cherrypick_cli.dart
Normal file
@@ -0,0 +1,8 @@
|
||||
import 'package:args/command_runner.dart';
|
||||
import 'package:cherrypick_cli/src/commands/init_command.dart';
|
||||
|
||||
void main(List<String> args) {
|
||||
final runner = CommandRunner('cherrypick_cli', 'CherryPick CLI')
|
||||
..addCommand(InitCommand());
|
||||
runner.run(args);
|
||||
}
|
||||
34
cherrypick_cli/lib/src/commands/init_command.dart
Normal file
34
cherrypick_cli/lib/src/commands/init_command.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
import 'package:args/command_runner.dart';
|
||||
import '../utils/build_yaml_updater.dart';
|
||||
|
||||
class InitCommand extends Command {
|
||||
@override
|
||||
final name = 'init';
|
||||
@override
|
||||
final description = 'Adds or updates cherrypick_generator sections in build.yaml, preserving other packages.';
|
||||
|
||||
InitCommand() {
|
||||
argParser.addOption(
|
||||
'output_dir',
|
||||
abbr: 'o',
|
||||
defaultsTo: 'lib/generated',
|
||||
help: 'Directory for generated code.',
|
||||
);
|
||||
argParser.addOption(
|
||||
'build_yaml',
|
||||
abbr: 'f',
|
||||
defaultsTo: 'build.yaml',
|
||||
help: 'Path to build.yaml file.',
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void run() {
|
||||
final outputDir = argResults?['output_dir'] as String? ?? 'lib/generated';
|
||||
final buildYaml = argResults?['build_yaml'] as String? ?? 'build.yaml';
|
||||
updateCherrypickBuildYaml(
|
||||
buildYamlPath: buildYaml,
|
||||
outputDir: outputDir,
|
||||
);
|
||||
}
|
||||
}
|
||||
76
cherrypick_cli/lib/src/utils/build_yaml_updater.dart
Normal file
76
cherrypick_cli/lib/src/utils/build_yaml_updater.dart
Normal file
@@ -0,0 +1,76 @@
|
||||
import 'dart:io';
|
||||
import 'package:yaml/yaml.dart';
|
||||
import 'package:json2yaml/json2yaml.dart';
|
||||
|
||||
void updateCherrypickBuildYaml({
|
||||
String buildYamlPath = 'build.yaml',
|
||||
String outputDir = 'lib/generated',
|
||||
}) {
|
||||
final file = File(buildYamlPath);
|
||||
final exists = file.existsSync();
|
||||
Map config = {};
|
||||
if (exists) {
|
||||
final content = file.readAsStringSync();
|
||||
final loaded = loadYaml(content);
|
||||
config = _deepYamlToMap(loaded);
|
||||
}
|
||||
|
||||
// Гарантируем вложенность
|
||||
config['targets'] ??= {};
|
||||
final targets = config['targets'] as Map;
|
||||
targets['\$default'] ??= {};
|
||||
final def = targets['\$default'] as Map;
|
||||
def['builders'] ??= {};
|
||||
final builders = def['builders'] as Map;
|
||||
|
||||
builders['cherrypick_generator|inject_generator'] = {
|
||||
'options': {
|
||||
'build_extensions': {
|
||||
'^lib/{{}}.dart': ['${outputDir}/{{}}.inject.cherrypick.g.dart']
|
||||
},
|
||||
'output_dir': outputDir
|
||||
},
|
||||
'generate_for': ['lib/**.dart']
|
||||
};
|
||||
|
||||
builders['cherrypick_generator|module_generator'] = {
|
||||
'options': {
|
||||
'build_extensions': {
|
||||
'^lib/di/{{}}.dart': ['${outputDir}/di/{{}}.module.cherrypick.g.dart']
|
||||
},
|
||||
'output_dir': outputDir
|
||||
},
|
||||
'generate_for': ['lib/**.dart']
|
||||
};
|
||||
|
||||
final yamlString = json2yaml(_stringifyKeys(config), yamlStyle: YamlStyle.pubspecYaml);
|
||||
file.writeAsStringSync(yamlString);
|
||||
print('✅ build.yaml has been successfully updated and formatted (cherrypick sections added/updated).');
|
||||
}
|
||||
|
||||
dynamic _stringifyKeys(dynamic node) {
|
||||
if (node is Map) {
|
||||
return Map.fromEntries(
|
||||
node.entries.map(
|
||||
(e) => MapEntry(e.key.toString(), _stringifyKeys(e.value)),
|
||||
),
|
||||
);
|
||||
} else if (node is List) {
|
||||
return node.map(_stringifyKeys).toList();
|
||||
} else {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Рекурсивно преобразует YamlMap/YamlList в обычные Map/List
|
||||
dynamic _deepYamlToMap(dynamic node) {
|
||||
if (node is YamlMap) {
|
||||
return Map.fromEntries(node.entries.map((e) => MapEntry(e.key, _deepYamlToMap(e.value))));
|
||||
} else if (node is YamlList) {
|
||||
return node.map(_deepYamlToMap).toList();
|
||||
} else {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
85
cherrypick_cli/pubspec.lock
Normal file
85
cherrypick_cli/pubspec.lock
Normal file
@@ -0,0 +1,85 @@
|
||||
# Generated by pub
|
||||
# See https://dart.dev/tools/pub/glossary#lockfile
|
||||
packages:
|
||||
args:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: args
|
||||
sha256: d0481093c50b1da8910eb0bb301626d4d8eb7284aa739614d2b394ee09e3ea04
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.7.0"
|
||||
collection:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: collection
|
||||
sha256: "2f5709ae4d3d59dd8f7cd309b4e023046b57d8a6c82130785d2b0e5868084e76"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.19.1"
|
||||
json2yaml:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: json2yaml
|
||||
sha256: da94630fbc56079426fdd167ae58373286f603371075b69bf46d848d63ba3e51
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.0.1"
|
||||
meta:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: meta
|
||||
sha256: "23f08335362185a5ea2ad3a4e597f1375e78bce8a040df5c600c8d3552ef2394"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.17.0"
|
||||
path:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: path
|
||||
sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.9.1"
|
||||
source_span:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: source_span
|
||||
sha256: "254ee5351d6cb365c859e20ee823c3bb479bf4a293c22d17a9f1bf144ce86f7c"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.10.1"
|
||||
string_scanner:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: string_scanner
|
||||
sha256: "921cd31725b72fe181906c6a94d987c78e3b98c2e205b397ea399d4054872b43"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.4.1"
|
||||
term_glyph:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: term_glyph
|
||||
sha256: "7f554798625ea768a7518313e58f83891c7f5024f88e46e7182a4558850a4b8e"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.2.2"
|
||||
yaml:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: yaml
|
||||
sha256: b9da305ac7c39faa3f030eccd175340f968459dae4af175130b3fc47e40d76ce
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.1.3"
|
||||
yaml_edit:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: yaml_edit
|
||||
sha256: fb38626579fb345ad00e674e2af3a5c9b0cc4b9bfb8fd7f7ff322c7c9e62aef5
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.2.2"
|
||||
sdks:
|
||||
dart: ">=3.5.0 <4.0.0"
|
||||
13
cherrypick_cli/pubspec.yaml
Normal file
13
cherrypick_cli/pubspec.yaml
Normal file
@@ -0,0 +1,13 @@
|
||||
name: cherrypick_cli
|
||||
version: 0.1.0
|
||||
publish_to: none
|
||||
description: CLI tool for CherryPick DI ecosystem
|
||||
environment:
|
||||
sdk: ">=3.0.0 <4.0.0"
|
||||
dependencies:
|
||||
args: ^2.4.2
|
||||
yaml: ^3.1.2
|
||||
yaml_edit: ^2.1.1
|
||||
json2yaml: ^3.0.0
|
||||
executables:
|
||||
cherrypick_cli:
|
||||
@@ -1,15 +1,7 @@
|
||||
## 1.1.3-dev.1
|
||||
## 1.1.2-dev.2
|
||||
|
||||
- Update a dependency to the latest release.
|
||||
|
||||
## 1.1.3-dev.0
|
||||
|
||||
- **FIX**: update deps.
|
||||
|
||||
## 1.1.2
|
||||
|
||||
- Graduate package to a stable release. See pre-releases prior to this version for changelog entries.
|
||||
|
||||
## 1.1.2-dev.1
|
||||
|
||||
- Update a dependency to the latest release.
|
||||
|
||||
@@ -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.3-dev.1
|
||||
version: 1.1.2-dev.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: ^3.0.0-dev.1
|
||||
cherrypick: ^2.2.0-dev.2
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
||||
2
cherrypick_generator/.gitignore
vendored
2
cherrypick_generator/.gitignore
vendored
@@ -28,5 +28,3 @@ melos_cherrypick_generator.iml
|
||||
**/*.mocks.dart
|
||||
|
||||
coverage
|
||||
|
||||
pubspec_overrides.yaml
|
||||
@@ -1,6 +1,10 @@
|
||||
## 1.1.0
|
||||
## 1.1.0-dev.6
|
||||
|
||||
- Graduate package to a stable release. See pre-releases prior to this version for changelog entries.
|
||||
> 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.
|
||||
|
||||
## 1.1.0-dev.5
|
||||
|
||||
|
||||
@@ -4,6 +4,50 @@
|
||||
|
||||
---
|
||||
|
||||
### 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:**
|
||||
@@ -170,6 +214,26 @@ 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:**
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
builders:
|
||||
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"]
|
||||
module_generator:
|
||||
import: "package:cherrypick_generator/module_generator.dart"
|
||||
builder_factories: ["moduleBuilder"]
|
||||
build_extensions: {".dart": [".module.cherrypick.g.dart"]}
|
||||
auto_apply: dependents
|
||||
build_to: source
|
||||
applies_builders: ["source_gen|combining_builder"]
|
||||
|
||||
targets:
|
||||
$default:
|
||||
|
||||
110
cherrypick_generator/lib/cherrypick_custom_builders.dart
Normal file
110
cherrypick_generator/lib/cherrypick_custom_builders.dart
Normal file
@@ -0,0 +1,110 @@
|
||||
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));
|
||||
|
||||
// Check if generated code starts with formatting header
|
||||
String finalCode;
|
||||
if (generated.startsWith('// dart format width=80')) {
|
||||
// Find the end of the header (after "// GENERATED CODE - DO NOT MODIFY BY HAND")
|
||||
final lines = generated.split('\n');
|
||||
int headerEndIndex = -1;
|
||||
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
if (lines[i].startsWith('// GENERATED CODE - DO NOT MODIFY BY HAND')) {
|
||||
headerEndIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (headerEndIndex != -1) {
|
||||
// Insert part of directive after the header
|
||||
final headerLines = lines.sublist(0, headerEndIndex + 1);
|
||||
final remainingLines = lines.sublist(headerEndIndex + 1);
|
||||
|
||||
final headerPart = headerLines.join('\n');
|
||||
final remainingPart = remainingLines.join('\n');
|
||||
|
||||
// Preserve trailing newline if original had one
|
||||
final hasTrailingNewline = generated.endsWith('\n');
|
||||
finalCode = '$headerPart\n\npart of \'$partOfPath\';\n$remainingPart${hasTrailingNewline ? '' : '\n'}';
|
||||
} else {
|
||||
// Fallback: add part of at the beginning
|
||||
finalCode = "part of '$partOfPath';\n\n$generated";
|
||||
}
|
||||
} else {
|
||||
// No header, add part of at the beginning
|
||||
finalCode = "part of '$partOfPath';\n\n$generated";
|
||||
}
|
||||
|
||||
print('[CustomOutputBuilder] writing to output: \\${outputId.path}');
|
||||
await buildStep.writeAsString(outputId, finalCode);
|
||||
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);
|
||||
}
|
||||
@@ -13,12 +13,16 @@
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:analyzer/dart/constant/value.dart';
|
||||
import 'package:analyzer/dart/element/nullability_suffix.dart';
|
||||
import 'package:analyzer/dart/element/type.dart';
|
||||
|
||||
|
||||
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;
|
||||
import 'src/exceptions.dart';
|
||||
import 'src/type_parser.dart';
|
||||
import 'src/annotation_validator.dart';
|
||||
|
||||
/// InjectGenerator generates a mixin for a class marked with @injectable()
|
||||
/// and injects all fields annotated with @inject(), using CherryPick DI.
|
||||
@@ -49,34 +53,68 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
BuildStep buildStep,
|
||||
) {
|
||||
if (element is! ClassElement) {
|
||||
throw InvalidGenerationSourceError(
|
||||
'@injectable() can only be applied to classes.',
|
||||
throw CherryPickGeneratorException(
|
||||
'@injectable() can only be applied to classes',
|
||||
element: element,
|
||||
category: 'INVALID_TARGET',
|
||||
suggestion: 'Apply @injectable() to a class instead of ${element.runtimeType}',
|
||||
);
|
||||
}
|
||||
|
||||
final classElement = element;
|
||||
|
||||
try {
|
||||
// Validate class annotations
|
||||
AnnotationValidator.validateClassAnnotations(classElement);
|
||||
|
||||
return _generateInjectionCode(classElement);
|
||||
} catch (e) {
|
||||
if (e is CherryPickGeneratorException) {
|
||||
rethrow;
|
||||
}
|
||||
throw CodeGenerationException(
|
||||
'Failed to generate injection code: $e',
|
||||
element: classElement,
|
||||
suggestion: 'Check that all @inject fields have valid types and annotations',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates the injection code for a class
|
||||
String _generateInjectionCode(ClassElement classElement) {
|
||||
final className = classElement.name;
|
||||
final mixinName = '_\$$className';
|
||||
|
||||
final buffer = StringBuffer()
|
||||
..writeln('mixin $mixinName {')
|
||||
..writeln(' void _inject($className instance) {');
|
||||
|
||||
// Collect and process all @inject fields.
|
||||
// Собираем и обрабатываем все поля с @inject.
|
||||
final injectFields =
|
||||
classElement.fields.where(_isInjectField).map(_parseInjectField);
|
||||
final injectFields = classElement.fields
|
||||
.where(_isInjectField)
|
||||
.map((field) => _parseInjectField(field, classElement))
|
||||
.toList();
|
||||
|
||||
for (final parsedField in injectFields) {
|
||||
buffer.writeln(_generateInjectionLine(parsedField));
|
||||
final buffer = StringBuffer()
|
||||
..writeln('// dart format width=80')
|
||||
..writeln('// GENERATED CODE - DO NOT MODIFY BY HAND')
|
||||
..writeln()
|
||||
..writeln('// **************************************************************************')
|
||||
..writeln('// InjectGenerator')
|
||||
..writeln('// **************************************************************************')
|
||||
..writeln()
|
||||
..writeln('mixin $mixinName {');
|
||||
|
||||
if (injectFields.isEmpty) {
|
||||
// For empty classes, generate a method with empty body
|
||||
buffer.writeln(' void _inject($className instance) {}');
|
||||
} else {
|
||||
buffer.writeln(' void _inject($className instance) {');
|
||||
for (final parsedField in injectFields) {
|
||||
buffer.writeln(_generateInjectionLine(parsedField));
|
||||
}
|
||||
buffer.writeln(' }');
|
||||
}
|
||||
|
||||
buffer
|
||||
..writeln(' }')
|
||||
..writeln('}');
|
||||
buffer.writeln('}');
|
||||
|
||||
return buffer.toString();
|
||||
return '${buffer.toString()}\n';
|
||||
}
|
||||
|
||||
/// Checks if a field has the @inject annotation.
|
||||
@@ -93,50 +131,51 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
///
|
||||
/// Разбирает поле на наличие модификаторов scope/named и выясняет его тип.
|
||||
/// Возвращает [_ParsedInjectField] с информацией о внедрении.
|
||||
static _ParsedInjectField _parseInjectField(FieldElement field) {
|
||||
String? scopeName;
|
||||
String? namedValue;
|
||||
static _ParsedInjectField _parseInjectField(FieldElement field, ClassElement classElement) {
|
||||
try {
|
||||
// Validate field annotations
|
||||
AnnotationValidator.validateFieldAnnotations(field);
|
||||
|
||||
for (final meta in field.metadata) {
|
||||
final DartObject? obj = meta.computeConstantValue();
|
||||
final type = obj?.type?.getDisplayString();
|
||||
if (type == 'scope') {
|
||||
scopeName = obj?.getField('name')?.toStringValue();
|
||||
} else if (type == 'named') {
|
||||
namedValue = obj?.getField('value')?.toStringValue();
|
||||
// Parse type using improved type parser
|
||||
final parsedType = TypeParser.parseType(field.type, field);
|
||||
TypeParser.validateInjectableType(parsedType, field);
|
||||
|
||||
// Extract metadata
|
||||
String? scopeName;
|
||||
String? namedValue;
|
||||
|
||||
for (final meta in field.metadata) {
|
||||
final DartObject? obj = meta.computeConstantValue();
|
||||
final type = obj?.type?.getDisplayString();
|
||||
if (type == 'scope') {
|
||||
scopeName = obj?.getField('name')?.toStringValue();
|
||||
} else if (type == 'named') {
|
||||
namedValue = obj?.getField('value')?.toStringValue();
|
||||
}
|
||||
}
|
||||
|
||||
return _ParsedInjectField(
|
||||
fieldName: field.name,
|
||||
parsedType: parsedType,
|
||||
scopeName: scopeName,
|
||||
namedValue: namedValue,
|
||||
);
|
||||
} catch (e) {
|
||||
if (e is CherryPickGeneratorException) {
|
||||
rethrow;
|
||||
}
|
||||
throw DependencyResolutionException(
|
||||
'Failed to parse inject field "${field.name}"',
|
||||
element: field,
|
||||
suggestion: 'Check that the field type is valid and properly imported',
|
||||
context: {
|
||||
'field_name': field.name,
|
||||
'field_type': field.type.getDisplayString(),
|
||||
'class_name': classElement.name,
|
||||
'error': e.toString(),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
final DartType dartType = field.type;
|
||||
String coreTypeName;
|
||||
bool isFuture;
|
||||
|
||||
if (dartType.isDartAsyncFuture) {
|
||||
final ParameterizedType paramType = dartType as ParameterizedType;
|
||||
coreTypeName = paramType.typeArguments.first.getDisplayString();
|
||||
isFuture = true;
|
||||
} else {
|
||||
coreTypeName = dartType.getDisplayString();
|
||||
isFuture = false;
|
||||
}
|
||||
|
||||
// ***
|
||||
// Добавим определение nullable для типа (например PostRepository? или Future<PostRepository?>)
|
||||
bool isNullable = dartType.nullabilitySuffix ==
|
||||
NullabilitySuffix.question ||
|
||||
(dartType is ParameterizedType &&
|
||||
(dartType)
|
||||
.typeArguments
|
||||
.any((t) => t.nullabilitySuffix == NullabilitySuffix.question));
|
||||
|
||||
return _ParsedInjectField(
|
||||
fieldName: field.name,
|
||||
coreType: coreTypeName.replaceAll('?', ''), // удаляем "?" на всякий
|
||||
isFuture: isFuture,
|
||||
isNullable: isNullable,
|
||||
scopeName: scopeName,
|
||||
namedValue: namedValue,
|
||||
);
|
||||
}
|
||||
|
||||
/// Generates a line of code that performs the dependency injection for a field.
|
||||
@@ -145,24 +184,47 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
/// Генерирует строку кода, которая внедряет зависимость для поля.
|
||||
/// Учитывает resolve/resolveAsync, scoping и named qualifier.
|
||||
String _generateInjectionLine(_ParsedInjectField field) {
|
||||
// Используем tryResolve для nullable, иначе resolve
|
||||
final resolveMethod = field.isFuture
|
||||
? (field.isNullable
|
||||
? 'tryResolveAsync<${field.coreType}>'
|
||||
: 'resolveAsync<${field.coreType}>')
|
||||
: (field.isNullable
|
||||
? 'tryResolve<${field.coreType}>'
|
||||
: 'resolve<${field.coreType}>');
|
||||
final resolveMethod = '${field.parsedType.resolveMethodName}<${field.parsedType.codeGenType}>';
|
||||
final fieldName = field.fieldName;
|
||||
|
||||
// Build the scope call
|
||||
final openCall = (field.scopeName != null && field.scopeName!.isNotEmpty)
|
||||
? "CherryPick.openScope(scopeName: '${field.scopeName}')"
|
||||
: "CherryPick.openRootScope()";
|
||||
|
||||
final params = (field.namedValue != null && field.namedValue!.isNotEmpty)
|
||||
? "(named: '${field.namedValue}')"
|
||||
: '()';
|
||||
// Build the parameters
|
||||
final hasNamedParam = field.namedValue != null && field.namedValue!.isNotEmpty;
|
||||
final params = hasNamedParam ? "(named: '${field.namedValue}')" : '()';
|
||||
|
||||
return " instance.${field.fieldName} = $openCall.$resolveMethod$params;";
|
||||
// Create the full line
|
||||
final fullLine = " instance.$fieldName = $openCall.$resolveMethod$params;";
|
||||
|
||||
// Check if line is too long (dart format width=80, accounting for indentation)
|
||||
if (fullLine.length <= 80) {
|
||||
return fullLine;
|
||||
}
|
||||
|
||||
// Format long lines with proper line breaks
|
||||
if (hasNamedParam && field.scopeName != null && field.scopeName!.isNotEmpty) {
|
||||
// For scoped calls with named parameters, break after openScope
|
||||
return " instance.$fieldName = CherryPick.openScope(\n"
|
||||
" scopeName: '${field.scopeName}',\n"
|
||||
" ).$resolveMethod(named: '${field.namedValue}');";
|
||||
} else if (hasNamedParam) {
|
||||
// For named parameters without scope, break after the method call
|
||||
return " instance.$fieldName = $openCall.$resolveMethod(\n"
|
||||
" named: '${field.namedValue}',\n"
|
||||
" );";
|
||||
} else if (field.scopeName != null && field.scopeName!.isNotEmpty) {
|
||||
// For scoped calls without named params, break after openScope with proper parameter formatting
|
||||
return " instance.$fieldName = CherryPick.openScope(\n"
|
||||
" scopeName: '${field.scopeName}',\n"
|
||||
" ).$resolveMethod();";
|
||||
} else {
|
||||
// For simple long calls, break after openRootScope
|
||||
return " instance.$fieldName = $openCall\n"
|
||||
" .$resolveMethod();";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,12 +237,8 @@ class _ParsedInjectField {
|
||||
/// The name of the field / Имя поля.
|
||||
final String fieldName;
|
||||
|
||||
/// The base type name (T or Future<T>) / Базовый тип (T или тип из Future<T>).
|
||||
final String coreType;
|
||||
|
||||
/// True if the field type is Future<T>; false otherwise
|
||||
/// Истина, если поле — Future<T>, иначе — ложь.
|
||||
final bool isFuture;
|
||||
/// Parsed type information / Информация о типе поля.
|
||||
final ParsedType parsedType;
|
||||
|
||||
/// Optional scope annotation argument / Опциональное имя scope.
|
||||
final String? scopeName;
|
||||
@@ -188,20 +246,22 @@ class _ParsedInjectField {
|
||||
/// Optional named annotation argument / Опциональное имя named.
|
||||
final String? namedValue;
|
||||
|
||||
final bool isNullable;
|
||||
|
||||
_ParsedInjectField({
|
||||
required this.fieldName,
|
||||
required this.coreType,
|
||||
required this.isFuture,
|
||||
required this.isNullable,
|
||||
required this.parsedType,
|
||||
this.scopeName,
|
||||
this.namedValue,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return '_ParsedInjectField(fieldName: $fieldName, parsedType: $parsedType, '
|
||||
'scopeName: $scopeName, namedValue: $namedValue)';
|
||||
}
|
||||
}
|
||||
|
||||
/// Builder factory. Used by build_runner.
|
||||
///
|
||||
/// Фабрика билдера. Используется build_runner.
|
||||
Builder injectBuilder(BuilderOptions options) =>
|
||||
PartBuilder([InjectGenerator()], '.inject.cherrypick.g.dart');
|
||||
custom.injectCustomBuilder(options);
|
||||
|
||||
@@ -15,9 +15,10 @@ 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 'src/exceptions.dart';
|
||||
import 'src/annotation_validator.dart';
|
||||
import 'cherrypick_custom_builders.dart' as custom;
|
||||
/// ---------------------------------------------------------------------------
|
||||
/// ModuleGenerator for code generation of dependency-injected modules.
|
||||
///
|
||||
@@ -62,20 +63,40 @@ class ModuleGenerator extends GeneratorForAnnotation<ann.module> {
|
||||
// Only classes are supported for @module() annotation
|
||||
// Обрабатываются только классы (другие элементы — ошибка)
|
||||
if (element is! ClassElement) {
|
||||
throw InvalidGenerationSourceError(
|
||||
'@module() can only be applied to classes. / @module() может быть применён только к классам.',
|
||||
throw CherryPickGeneratorException(
|
||||
'@module() can only be applied to classes',
|
||||
element: element,
|
||||
category: 'INVALID_TARGET',
|
||||
suggestion: 'Apply @module() to a class instead of ${element.runtimeType}',
|
||||
);
|
||||
}
|
||||
|
||||
final classElement = element;
|
||||
|
||||
// Build a representation of the generated bindings based on class methods /
|
||||
// Создаёт объект, описывающий, какие биндинги нужно сгенерировать на основании методов класса
|
||||
final generatedClass = GeneratedClass.fromClassElement(classElement);
|
||||
try {
|
||||
// Validate class annotations
|
||||
AnnotationValidator.validateClassAnnotations(classElement);
|
||||
|
||||
// Generate the resulting Dart code / Генерирует итоговый Dart-код
|
||||
return generatedClass.generate();
|
||||
// Build a representation of the generated bindings based on class methods
|
||||
final generatedClass = GeneratedClass.fromClassElement(classElement);
|
||||
|
||||
// Generate the resulting Dart code
|
||||
return generatedClass.generate();
|
||||
} catch (e) {
|
||||
if (e is CherryPickGeneratorException) {
|
||||
rethrow;
|
||||
}
|
||||
throw CodeGenerationException(
|
||||
'Failed to generate module code for class "${classElement.name}"',
|
||||
element: classElement,
|
||||
suggestion: 'Check that all methods have valid @instance or @provide annotations',
|
||||
context: {
|
||||
'class_name': classElement.name,
|
||||
'method_count': classElement.methods.length,
|
||||
'error': e.toString(),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,5 +110,8 @@ class ModuleGenerator extends GeneratorForAnnotation<ann.module> {
|
||||
/// Возвращает Builder, используемый build_runner для генерации кода для всех
|
||||
/// файлов, где встречается @module().
|
||||
/// ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
Builder moduleBuilder(BuilderOptions options) =>
|
||||
PartBuilder([ModuleGenerator()], '.module.cherrypick.g.dart');
|
||||
custom.moduleCustomBuilder(options);
|
||||
@@ -58,8 +58,7 @@ class AnnotationValidator {
|
||||
throw AnnotationValidationException(
|
||||
'Method cannot have both @instance and @provide annotations',
|
||||
element: method,
|
||||
suggestion:
|
||||
'Use either @instance for direct instances or @provide for factory methods',
|
||||
suggestion: 'Use either @instance for direct instances or @provide for factory methods',
|
||||
context: {
|
||||
'method_name': method.displayName,
|
||||
'annotations': annotations,
|
||||
@@ -90,8 +89,7 @@ class AnnotationValidator {
|
||||
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',
|
||||
suggestion: 'Add @instance() for direct instances or @provide() for factory methods',
|
||||
context: {
|
||||
'method_name': method.displayName,
|
||||
'available_annotations': annotations,
|
||||
@@ -151,8 +149,7 @@ class AnnotationValidator {
|
||||
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',
|
||||
suggestion: 'Use alphanumeric characters and underscores only, starting with a letter or underscore',
|
||||
context: {
|
||||
'method_name': method.displayName,
|
||||
'named_value': namedValue,
|
||||
@@ -170,8 +167,7 @@ class AnnotationValidator {
|
||||
}
|
||||
}
|
||||
|
||||
static void _validateParamsParameter(
|
||||
ParameterElement param, MethodElement method) {
|
||||
static void _validateParamsParameter(ParameterElement param, MethodElement method) {
|
||||
// @params parameter should typically be dynamic or Map<String, dynamic>
|
||||
final paramType = param.type.getDisplayString();
|
||||
|
||||
@@ -236,8 +232,7 @@ class AnnotationValidator {
|
||||
}
|
||||
|
||||
// Check if class has public methods
|
||||
final publicMethods =
|
||||
classElement.methods.where((m) => m.isPublic).toList();
|
||||
final publicMethods = classElement.methods.where((m) => m.isPublic).toList();
|
||||
if (publicMethods.isEmpty) {
|
||||
throw AnnotationValidationException(
|
||||
'Module class must have at least one public method',
|
||||
@@ -253,8 +248,7 @@ class AnnotationValidator {
|
||||
// Validate that public methods have appropriate annotations
|
||||
for (final method in publicMethods) {
|
||||
final methodAnnotations = _getAnnotationNames(method.metadata);
|
||||
if (!methodAnnotations.contains('instance') &&
|
||||
!methodAnnotations.contains('provide')) {
|
||||
if (!methodAnnotations.contains('instance') && !methodAnnotations.contains('provide')) {
|
||||
throw AnnotationValidationException(
|
||||
'Public methods in module class must have @instance or @provide annotation',
|
||||
element: method,
|
||||
@@ -297,8 +291,7 @@ class AnnotationValidator {
|
||||
throw AnnotationValidationException(
|
||||
'Injectable fields should be final for immutability',
|
||||
element: field,
|
||||
suggestion:
|
||||
'Add final keyword to injectable field (preferably late final)',
|
||||
suggestion: 'Add final keyword to injectable field (preferably late final)',
|
||||
context: {
|
||||
'class_name': classElement.displayName,
|
||||
'field_name': field.displayName,
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
|
||||
|
||||
import 'bind_parameters_spec.dart';
|
||||
import 'metadata_utils.dart';
|
||||
import 'exceptions.dart';
|
||||
@@ -125,8 +126,8 @@ class BindSpec {
|
||||
// If there's a postfix, break after bind<Type>()
|
||||
final multilinePostfix = _generateMultilinePostfix(indent);
|
||||
return '${indentStr}bind<$returnType>()'
|
||||
'\n${' ' * (indent + 4)}$provide'
|
||||
'$multilinePostfix;';
|
||||
'\n${' ' * (indent + 4)}$provide'
|
||||
'$multilinePostfix;';
|
||||
} else {
|
||||
// No postfix, keep bind<Type>() with provide start
|
||||
return '${indentStr}bind<$returnType>()$provide;';
|
||||
@@ -136,11 +137,11 @@ class BindSpec {
|
||||
if (postfix.isNotEmpty) {
|
||||
final multilinePostfix = _generateMultilinePostfix(indent);
|
||||
return '${indentStr}bind<$returnType>()'
|
||||
'\n${' ' * (indent + 4)}$provide'
|
||||
'$multilinePostfix;';
|
||||
'\n${' ' * (indent + 4)}$provide'
|
||||
'$multilinePostfix;';
|
||||
} else {
|
||||
return '${indentStr}bind<$returnType>()'
|
||||
'\n${' ' * (indent + 4)}$provide;';
|
||||
'\n${' ' * (indent + 4)}$provide;';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -184,8 +185,7 @@ class BindSpec {
|
||||
|
||||
// Check if we need multiline formatting based on total line length
|
||||
final singleLineCall = '$methodName($argsStr)';
|
||||
final needsMultiline =
|
||||
singleLineCall.length >= 45 || argsStr.contains('\n');
|
||||
final needsMultiline = singleLineCall.length >= 45 || argsStr.contains('\n');
|
||||
|
||||
switch (bindingType) {
|
||||
case BindingType.instance:
|
||||
@@ -195,20 +195,16 @@ class BindSpec {
|
||||
case BindingType.provide:
|
||||
if (isAsyncProvide) {
|
||||
if (needsMultiline) {
|
||||
final lambdaIndent =
|
||||
(isSingleton || named != null) ? indent + 6 : indent + 2;
|
||||
final closingIndent =
|
||||
(isSingleton || named != null) ? indent + 4 : indent;
|
||||
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 {
|
||||
if (needsMultiline) {
|
||||
final lambdaIndent =
|
||||
(isSingleton || named != null) ? indent + 6 : indent + 2;
|
||||
final closingIndent =
|
||||
(isSingleton || named != null) ? indent + 4 : indent;
|
||||
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))';
|
||||
@@ -286,8 +282,7 @@ class BindSpec {
|
||||
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',
|
||||
suggestion: 'Add @instance() for direct instances or @provide() for factory methods',
|
||||
context: {
|
||||
'method_name': methodName,
|
||||
'return_type': parsedReturnType.displayString,
|
||||
@@ -295,8 +290,7 @@ class BindSpec {
|
||||
);
|
||||
}
|
||||
|
||||
final bindingType =
|
||||
hasInstance ? BindingType.instance : BindingType.provide;
|
||||
final bindingType = hasInstance ? BindingType.instance : BindingType.provide;
|
||||
|
||||
// PROHIBIT @params with @instance bindings!
|
||||
if (bindingType == BindingType.instance && hasParams) {
|
||||
@@ -313,10 +307,8 @@ class BindSpec {
|
||||
}
|
||||
|
||||
// Set async flags based on parsed type
|
||||
final isAsyncInstance =
|
||||
bindingType == BindingType.instance && parsedReturnType.isFuture;
|
||||
final isAsyncProvide =
|
||||
bindingType == BindingType.provide && parsedReturnType.isFuture;
|
||||
final isAsyncInstance = bindingType == BindingType.instance && parsedReturnType.isFuture;
|
||||
final isAsyncProvide = bindingType == BindingType.provide && parsedReturnType.isFuture;
|
||||
|
||||
return BindSpec(
|
||||
returnType: parsedReturnType.codeGenType,
|
||||
@@ -336,8 +328,7 @@ class BindSpec {
|
||||
throw CodeGenerationException(
|
||||
'Failed to create BindSpec from method "${method.displayName}"',
|
||||
element: method,
|
||||
suggestion:
|
||||
'Check that the method has valid annotations and return type',
|
||||
suggestion: 'Check that the method has valid annotations and return type',
|
||||
context: {
|
||||
'method_name': method.displayName,
|
||||
'return_type': method.returnType.getDisplayString(),
|
||||
|
||||
@@ -103,6 +103,13 @@ class GeneratedClass {
|
||||
/// -------------------------------------------------------------------------
|
||||
String generate() {
|
||||
final buffer = StringBuffer()
|
||||
..writeln('// dart format width=80')
|
||||
..writeln('// GENERATED CODE - DO NOT MODIFY BY HAND')
|
||||
..writeln()
|
||||
..writeln('// **************************************************************************')
|
||||
..writeln('// ModuleGenerator')
|
||||
..writeln('// **************************************************************************')
|
||||
..writeln()
|
||||
..writeln('final class $generatedClassName extends $className {')
|
||||
..writeln(' @override')
|
||||
..writeln(' void builder(Scope currentScope) {');
|
||||
|
||||
@@ -60,8 +60,7 @@ class TypeParser {
|
||||
);
|
||||
}
|
||||
|
||||
static ParsedType _parseFutureType(
|
||||
DartType dartType, Element context, bool isNullable) {
|
||||
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',
|
||||
@@ -85,8 +84,7 @@ class TypeParser {
|
||||
);
|
||||
}
|
||||
|
||||
static ParsedType _parseGenericType(
|
||||
ParameterizedType dartType, Element context, bool isNullable) {
|
||||
static ParsedType _parseGenericType(ParameterizedType dartType, Element context, bool isNullable) {
|
||||
final typeArguments = dartType.typeArguments
|
||||
.map((arg) => _parseTypeInternal(arg, context))
|
||||
.toList();
|
||||
@@ -193,7 +191,7 @@ class ParsedType {
|
||||
@override
|
||||
String toString() {
|
||||
return 'ParsedType(displayString: $displayString, coreType: $coreType, '
|
||||
'isNullable: $isNullable, isFuture: $isFuture, isGeneric: $isGeneric)';
|
||||
'isNullable: $isNullable, isFuture: $isFuture, isGeneric: $isGeneric)';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -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
|
||||
version: 1.1.0-dev.6
|
||||
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,12 +12,13 @@ environment:
|
||||
|
||||
# Add regular dependencies here.
|
||||
dependencies:
|
||||
cherrypick_annotations: ^1.1.0
|
||||
cherrypick_annotations: ^1.1.0-dev.1
|
||||
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
|
||||
|
||||
415
cherrypick_generator/test/annotation_validator_test.dart
Normal file
415
cherrypick_generator/test/annotation_validator_test.dart
Normal file
@@ -0,0 +1,415 @@
|
||||
import 'package:test/test.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/constant/value.dart';
|
||||
import 'package:analyzer/dart/element/type.dart';
|
||||
import 'package:analyzer/source/source.dart';
|
||||
import 'package:cherrypick_generator/src/annotation_validator.dart';
|
||||
import 'package:cherrypick_generator/src/exceptions.dart';
|
||||
|
||||
void main() {
|
||||
group('AnnotationValidator', () {
|
||||
group('validateMethodAnnotations', () {
|
||||
test('should pass for valid @instance method', () {
|
||||
final method = _createMockMethod(
|
||||
name: 'createService',
|
||||
annotations: ['instance'],
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateMethodAnnotations(method),
|
||||
returnsNormally,
|
||||
);
|
||||
});
|
||||
|
||||
test('should pass for valid @provide method', () {
|
||||
final method = _createMockMethod(
|
||||
name: 'createService',
|
||||
annotations: ['provide'],
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateMethodAnnotations(method),
|
||||
returnsNormally,
|
||||
);
|
||||
});
|
||||
|
||||
test('should throw for method with both @instance and @provide', () {
|
||||
final method = _createMockMethod(
|
||||
name: 'createService',
|
||||
annotations: ['instance', 'provide'],
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateMethodAnnotations(method),
|
||||
throwsA(isA<AnnotationValidationException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should throw for method with @params but no @provide', () {
|
||||
final method = _createMockMethod(
|
||||
name: 'createService',
|
||||
annotations: ['instance', 'params'],
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateMethodAnnotations(method),
|
||||
throwsA(isA<AnnotationValidationException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should throw for method with neither @instance nor @provide', () {
|
||||
final method = _createMockMethod(
|
||||
name: 'createService',
|
||||
annotations: ['singleton'],
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateMethodAnnotations(method),
|
||||
throwsA(isA<AnnotationValidationException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should pass for @provide method with @params', () {
|
||||
final method = _createMockMethod(
|
||||
name: 'createService',
|
||||
annotations: ['provide', 'params'],
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateMethodAnnotations(method),
|
||||
returnsNormally,
|
||||
);
|
||||
});
|
||||
|
||||
test('should pass for @singleton method', () {
|
||||
final method = _createMockMethod(
|
||||
name: 'createService',
|
||||
annotations: ['provide', 'singleton'],
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateMethodAnnotations(method),
|
||||
returnsNormally,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('validateFieldAnnotations', () {
|
||||
test('should pass for valid @inject field', () {
|
||||
final field = _createMockField(
|
||||
name: 'service',
|
||||
annotations: ['inject'],
|
||||
type: 'String',
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateFieldAnnotations(field),
|
||||
returnsNormally,
|
||||
);
|
||||
});
|
||||
|
||||
test('should throw for @inject field with void type', () {
|
||||
final field = _createMockField(
|
||||
name: 'service',
|
||||
annotations: ['inject'],
|
||||
type: 'void',
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateFieldAnnotations(field),
|
||||
throwsA(isA<AnnotationValidationException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should pass for non-inject field', () {
|
||||
final field = _createMockField(
|
||||
name: 'service',
|
||||
annotations: [],
|
||||
type: 'String',
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateFieldAnnotations(field),
|
||||
returnsNormally,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('validateClassAnnotations', () {
|
||||
test('should pass for valid @module class', () {
|
||||
final classElement = _createMockClass(
|
||||
name: 'AppModule',
|
||||
annotations: ['module'],
|
||||
methods: [
|
||||
_createMockMethod(name: 'createService', annotations: ['provide']),
|
||||
],
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateClassAnnotations(classElement),
|
||||
returnsNormally,
|
||||
);
|
||||
});
|
||||
|
||||
test('should throw for @module class with no public methods', () {
|
||||
final classElement = _createMockClass(
|
||||
name: 'AppModule',
|
||||
annotations: ['module'],
|
||||
methods: [],
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateClassAnnotations(classElement),
|
||||
throwsA(isA<AnnotationValidationException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should throw for @module class with unannotated public methods', () {
|
||||
final classElement = _createMockClass(
|
||||
name: 'AppModule',
|
||||
annotations: ['module'],
|
||||
methods: [
|
||||
_createMockMethod(name: 'createService', annotations: []),
|
||||
],
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateClassAnnotations(classElement),
|
||||
throwsA(isA<AnnotationValidationException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should pass for valid @injectable class', () {
|
||||
final classElement = _createMockClass(
|
||||
name: 'AppService',
|
||||
annotations: ['injectable'],
|
||||
fields: [
|
||||
_createMockField(name: 'dependency', annotations: ['inject'], type: 'String', isFinal: true),
|
||||
],
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateClassAnnotations(classElement),
|
||||
returnsNormally,
|
||||
);
|
||||
});
|
||||
|
||||
test('should pass for @injectable class with no inject fields', () {
|
||||
final classElement = _createMockClass(
|
||||
name: 'AppService',
|
||||
annotations: ['injectable'],
|
||||
fields: [
|
||||
_createMockField(name: 'dependency', annotations: [], type: 'String'),
|
||||
],
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateClassAnnotations(classElement),
|
||||
returnsNormally,
|
||||
);
|
||||
});
|
||||
|
||||
test('should throw for @injectable class with non-final inject fields', () {
|
||||
final classElement = _createMockClass(
|
||||
name: 'AppService',
|
||||
annotations: ['injectable'],
|
||||
fields: [
|
||||
_createMockField(
|
||||
name: 'dependency',
|
||||
annotations: ['inject'],
|
||||
type: 'String',
|
||||
isFinal: false,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateClassAnnotations(classElement),
|
||||
throwsA(isA<AnnotationValidationException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should pass for @injectable class with final inject fields', () {
|
||||
final classElement = _createMockClass(
|
||||
name: 'AppService',
|
||||
annotations: ['injectable'],
|
||||
fields: [
|
||||
_createMockField(
|
||||
name: 'dependency',
|
||||
annotations: ['inject'],
|
||||
type: 'String',
|
||||
isFinal: true,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
expect(
|
||||
() => AnnotationValidator.validateClassAnnotations(classElement),
|
||||
returnsNormally,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Mock implementations for testing
|
||||
MethodElement _createMockMethod({
|
||||
required String name,
|
||||
required List<String> annotations,
|
||||
}) {
|
||||
return _MockMethodElement(name, annotations);
|
||||
}
|
||||
|
||||
FieldElement _createMockField({
|
||||
required String name,
|
||||
required List<String> annotations,
|
||||
required String type,
|
||||
bool isFinal = false,
|
||||
}) {
|
||||
return _MockFieldElement(name, annotations, type, isFinal);
|
||||
}
|
||||
|
||||
ClassElement _createMockClass({
|
||||
required String name,
|
||||
required List<String> annotations,
|
||||
List<MethodElement> methods = const [],
|
||||
List<FieldElement> fields = const [],
|
||||
}) {
|
||||
return _MockClassElement(name, annotations, methods, fields);
|
||||
}
|
||||
|
||||
class _MockMethodElement implements MethodElement {
|
||||
final String _name;
|
||||
final List<String> _annotations;
|
||||
|
||||
_MockMethodElement(this._name, this._annotations);
|
||||
|
||||
@override
|
||||
Source get source => _MockSource();
|
||||
|
||||
@override
|
||||
String get displayName => _name;
|
||||
|
||||
@override
|
||||
String get name => _name;
|
||||
|
||||
@override
|
||||
List<ElementAnnotation> get metadata => _annotations.map((a) => _MockElementAnnotation(a)).toList();
|
||||
|
||||
@override
|
||||
bool get isPublic => true;
|
||||
|
||||
@override
|
||||
List<ParameterElement> get parameters => [];
|
||||
|
||||
@override
|
||||
DartType get returnType => _MockDartType('String');
|
||||
|
||||
@override
|
||||
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
}
|
||||
|
||||
class _MockFieldElement implements FieldElement {
|
||||
final String _name;
|
||||
final List<String> _annotations;
|
||||
final String _type;
|
||||
final bool _isFinal;
|
||||
|
||||
_MockFieldElement(this._name, this._annotations, this._type, this._isFinal);
|
||||
|
||||
@override
|
||||
Source get source => _MockSource();
|
||||
|
||||
@override
|
||||
String get displayName => _name;
|
||||
|
||||
@override
|
||||
String get name => _name;
|
||||
|
||||
@override
|
||||
List<ElementAnnotation> get metadata => _annotations.map((a) => _MockElementAnnotation(a)).toList();
|
||||
|
||||
@override
|
||||
bool get isFinal => _isFinal;
|
||||
|
||||
@override
|
||||
DartType get type => _MockDartType(_type);
|
||||
|
||||
@override
|
||||
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
}
|
||||
|
||||
class _MockClassElement implements ClassElement {
|
||||
final String _name;
|
||||
final List<String> _annotations;
|
||||
final List<MethodElement> _methods;
|
||||
final List<FieldElement> _fields;
|
||||
|
||||
_MockClassElement(this._name, this._annotations, this._methods, this._fields);
|
||||
|
||||
@override
|
||||
Source get source => _MockSource();
|
||||
|
||||
@override
|
||||
String get displayName => _name;
|
||||
|
||||
@override
|
||||
String get name => _name;
|
||||
|
||||
@override
|
||||
List<ElementAnnotation> get metadata => _annotations.map((a) => _MockElementAnnotation(a)).toList();
|
||||
|
||||
@override
|
||||
List<MethodElement> get methods => _methods;
|
||||
|
||||
@override
|
||||
List<FieldElement> get fields => _fields;
|
||||
|
||||
@override
|
||||
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
}
|
||||
|
||||
class _MockElementAnnotation implements ElementAnnotation {
|
||||
final String _type;
|
||||
|
||||
_MockElementAnnotation(this._type);
|
||||
|
||||
@override
|
||||
DartObject? computeConstantValue() {
|
||||
return _MockDartObject(_type);
|
||||
}
|
||||
|
||||
@override
|
||||
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
}
|
||||
|
||||
class _MockDartObject implements DartObject {
|
||||
final String _type;
|
||||
|
||||
_MockDartObject(this._type);
|
||||
|
||||
@override
|
||||
DartType? get type => _MockDartType(_type);
|
||||
|
||||
@override
|
||||
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
}
|
||||
|
||||
class _MockDartType implements DartType {
|
||||
final String _name;
|
||||
|
||||
_MockDartType(this._name);
|
||||
|
||||
@override
|
||||
String getDisplayString({bool withNullability = true}) => _name;
|
||||
|
||||
@override
|
||||
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
}
|
||||
class _MockSource implements Source {
|
||||
@override
|
||||
String get fullName => 'mock_source.dart';
|
||||
|
||||
@override
|
||||
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
}
|
||||
@@ -202,8 +202,9 @@ 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>();
|
||||
}
|
||||
}
|
||||
''';
|
||||
@@ -406,10 +407,9 @@ 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>>();
|
||||
}
|
||||
}
|
||||
''';
|
||||
|
||||
@@ -41,8 +41,7 @@ void main() {
|
||||
);
|
||||
|
||||
expect(
|
||||
() => TypeParser.validateInjectableType(
|
||||
parsedType, _createMockElement()),
|
||||
() => TypeParser.validateInjectableType(parsedType, _createMockElement()),
|
||||
throwsA(isA<TypeParsingException>()),
|
||||
);
|
||||
});
|
||||
@@ -58,8 +57,7 @@ void main() {
|
||||
);
|
||||
|
||||
expect(
|
||||
() => TypeParser.validateInjectableType(
|
||||
parsedType, _createMockElement()),
|
||||
() => TypeParser.validateInjectableType(parsedType, _createMockElement()),
|
||||
throwsA(isA<TypeParsingException>()),
|
||||
);
|
||||
});
|
||||
@@ -75,8 +73,7 @@ void main() {
|
||||
);
|
||||
|
||||
expect(
|
||||
() => TypeParser.validateInjectableType(
|
||||
parsedType, _createMockElement()),
|
||||
() => TypeParser.validateInjectableType(parsedType, _createMockElement()),
|
||||
returnsNormally,
|
||||
);
|
||||
});
|
||||
@@ -159,8 +156,7 @@ void main() {
|
||||
expect(parsedType.resolveMethodName, equals('resolveAsync'));
|
||||
});
|
||||
|
||||
test('should return correct resolveMethodName for nullable async types',
|
||||
() {
|
||||
test('should return correct resolveMethodName for nullable async types', () {
|
||||
final parsedType = ParsedType(
|
||||
displayString: 'Future<String?>',
|
||||
coreType: 'String',
|
||||
|
||||
@@ -1,572 +0,0 @@
|
||||
# Circular Dependency Detection
|
||||
|
||||
CherryPick provides robust circular dependency detection to prevent infinite loops and stack overflow errors in your dependency injection setup.
|
||||
|
||||
## What are Circular Dependencies?
|
||||
|
||||
Circular dependencies occur when two or more services depend on each other directly or indirectly, creating a cycle in the dependency graph.
|
||||
|
||||
### Example of circular dependencies within a scope
|
||||
|
||||
```dart
|
||||
class UserService {
|
||||
final OrderService orderService;
|
||||
UserService(this.orderService);
|
||||
}
|
||||
|
||||
class OrderService {
|
||||
final UserService userService;
|
||||
OrderService(this.userService);
|
||||
}
|
||||
```
|
||||
|
||||
### Example of circular dependencies between scopes
|
||||
|
||||
```dart
|
||||
// In parent scope
|
||||
class ParentService {
|
||||
final ChildService childService;
|
||||
ParentService(this.childService); // Gets from child scope
|
||||
}
|
||||
|
||||
// In child scope
|
||||
class ChildService {
|
||||
final ParentService parentService;
|
||||
ChildService(this.parentService); // Gets from parent scope
|
||||
}
|
||||
```
|
||||
|
||||
## Detection Types
|
||||
|
||||
### 🔍 Local Detection
|
||||
|
||||
Detects circular dependencies within a single scope. Fast and efficient.
|
||||
|
||||
### 🌐 Global Detection
|
||||
|
||||
Detects circular dependencies across the entire scope hierarchy. Slower but provides complete protection.
|
||||
|
||||
## Usage
|
||||
|
||||
### Local Detection
|
||||
|
||||
```dart
|
||||
final scope = Scope(null);
|
||||
scope.enableCycleDetection(); // Enable local detection
|
||||
|
||||
scope.installModules([
|
||||
Module((bind) {
|
||||
bind<UserService>().to((scope) => UserService(scope.resolve<OrderService>()));
|
||||
bind<OrderService>().to((scope) => OrderService(scope.resolve<UserService>()));
|
||||
}),
|
||||
]);
|
||||
|
||||
try {
|
||||
final userService = scope.resolve<UserService>(); // Will throw CircularDependencyException
|
||||
} catch (e) {
|
||||
print(e); // CircularDependencyException: Circular dependency detected
|
||||
}
|
||||
```
|
||||
|
||||
### Global Detection
|
||||
|
||||
```dart
|
||||
// Enable global detection for all scopes
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
|
||||
final rootScope = CherryPick.openGlobalSafeRootScope();
|
||||
final childScope = rootScope.openSubScope();
|
||||
|
||||
// Configure dependencies that create cross-scope cycles
|
||||
rootScope.installModules([
|
||||
Module((bind) {
|
||||
bind<ParentService>().to((scope) => ParentService(childScope.resolve<ChildService>()));
|
||||
}),
|
||||
]);
|
||||
|
||||
childScope.installModules([
|
||||
Module((bind) {
|
||||
bind<ChildService>().to((scope) => ChildService(rootScope.resolve<ParentService>()));
|
||||
}),
|
||||
]);
|
||||
|
||||
try {
|
||||
final parentService = rootScope.resolve<ParentService>(); // Will throw CircularDependencyException
|
||||
} catch (e) {
|
||||
print(e); // CircularDependencyException with detailed chain information
|
||||
}
|
||||
```
|
||||
|
||||
## CherryPick Helper API
|
||||
|
||||
### Global Settings
|
||||
|
||||
```dart
|
||||
// Enable/disable local detection globally
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
|
||||
// Enable/disable global cross-scope detection
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
CherryPick.disableGlobalCrossScopeCycleDetection();
|
||||
|
||||
// Check current settings
|
||||
bool localEnabled = CherryPick.isGlobalCycleDetectionEnabled;
|
||||
bool globalEnabled = CherryPick.isGlobalCrossScopeCycleDetectionEnabled;
|
||||
```
|
||||
|
||||
### Per-Scope Settings
|
||||
|
||||
```dart
|
||||
// Enable/disable for specific scope
|
||||
CherryPick.enableCycleDetectionForScope(scope);
|
||||
CherryPick.disableCycleDetectionForScope(scope);
|
||||
|
||||
// Enable/disable global detection for specific scope
|
||||
CherryPick.enableGlobalCycleDetectionForScope(scope);
|
||||
CherryPick.disableGlobalCycleDetectionForScope(scope);
|
||||
```
|
||||
|
||||
### Safe Scope Creation
|
||||
|
||||
```dart
|
||||
// Create scopes with detection automatically enabled
|
||||
final safeRootScope = CherryPick.openSafeRootScope(); // Local detection enabled
|
||||
final globalSafeRootScope = CherryPick.openGlobalSafeRootScope(); // Both local and global enabled
|
||||
final safeSubScope = CherryPick.openSafeSubScope(parentScope); // Inherits parent settings
|
||||
```
|
||||
|
||||
## Performance Considerations
|
||||
|
||||
| Detection Type | Overhead | Recommended Usage |
|
||||
|----------------|----------|-------------------|
|
||||
| **Local** | Minimal (~5%) | Development, Testing |
|
||||
| **Global** | Moderate (~15%) | Complex hierarchies, Critical features |
|
||||
| **Disabled** | None | Production (after testing) |
|
||||
|
||||
### Recommendations
|
||||
|
||||
- **Development**: Enable both local and global detection for maximum safety
|
||||
- **Testing**: Keep detection enabled to catch issues early
|
||||
- **Production**: Consider disabling for performance, but only after thorough testing
|
||||
|
||||
```dart
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
void configureCycleDetection() {
|
||||
if (kDebugMode) {
|
||||
// Enable full protection in debug mode
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
} else {
|
||||
// Disable in release mode for performance
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
CherryPick.disableGlobalCrossScopeCycleDetection();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Architectural Patterns
|
||||
|
||||
### Repository Pattern
|
||||
|
||||
```dart
|
||||
// ✅ Correct: Repository doesn't depend on service
|
||||
class UserRepository {
|
||||
final ApiClient apiClient;
|
||||
UserRepository(this.apiClient);
|
||||
}
|
||||
|
||||
class UserService {
|
||||
final UserRepository repository;
|
||||
UserService(this.repository);
|
||||
}
|
||||
|
||||
// ❌ Incorrect: Circular dependency
|
||||
class UserRepository {
|
||||
final UserService userService; // Don't do this!
|
||||
UserRepository(this.userService);
|
||||
}
|
||||
```
|
||||
|
||||
### Mediator Pattern
|
||||
|
||||
```dart
|
||||
// ✅ Correct: Use mediator to break cycles
|
||||
abstract class EventBus {
|
||||
void publish<T>(T event);
|
||||
Stream<T> listen<T>();
|
||||
}
|
||||
|
||||
class UserService {
|
||||
final EventBus eventBus;
|
||||
UserService(this.eventBus);
|
||||
|
||||
void createUser(User user) {
|
||||
// ... create user logic
|
||||
eventBus.publish(UserCreatedEvent(user));
|
||||
}
|
||||
}
|
||||
|
||||
class OrderService {
|
||||
final EventBus eventBus;
|
||||
OrderService(this.eventBus) {
|
||||
eventBus.listen<UserCreatedEvent>().listen(_onUserCreated);
|
||||
}
|
||||
|
||||
void _onUserCreated(UserCreatedEvent event) {
|
||||
// React to user creation without direct dependency
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Scope Hierarchy Best Practices
|
||||
|
||||
### Proper Dependency Flow
|
||||
|
||||
```dart
|
||||
// ✅ Correct: Dependencies flow downward in hierarchy
|
||||
// Root Scope: Core services
|
||||
final rootScope = CherryPick.openGlobalSafeRootScope();
|
||||
rootScope.installModules([
|
||||
Module((bind) {
|
||||
bind<DatabaseService>().singleton((scope) => DatabaseService());
|
||||
bind<ApiClient>().singleton((scope) => ApiClient());
|
||||
}),
|
||||
]);
|
||||
|
||||
// Feature Scope: Feature-specific services
|
||||
final featureScope = rootScope.openSubScope();
|
||||
featureScope.installModules([
|
||||
Module((bind) {
|
||||
bind<UserRepository>().to((scope) => UserRepository(scope.resolve<ApiClient>()));
|
||||
bind<UserService>().to((scope) => UserService(scope.resolve<UserRepository>()));
|
||||
}),
|
||||
]);
|
||||
|
||||
// UI Scope: UI-specific services
|
||||
final uiScope = featureScope.openSubScope();
|
||||
uiScope.installModules([
|
||||
Module((bind) {
|
||||
bind<UserController>().to((scope) => UserController(scope.resolve<UserService>()));
|
||||
}),
|
||||
]);
|
||||
```
|
||||
|
||||
### Avoid Cross-Scope Dependencies
|
||||
|
||||
```dart
|
||||
// ❌ Incorrect: Child scope depending on parent's specific services
|
||||
childScope.installModules([
|
||||
Module((bind) {
|
||||
bind<ChildService>().to((scope) =>
|
||||
ChildService(rootScope.resolve<ParentService>()) // Risky!
|
||||
);
|
||||
}),
|
||||
]);
|
||||
|
||||
// ✅ Correct: Use interfaces and proper dependency injection
|
||||
abstract class IParentService {
|
||||
void doSomething();
|
||||
}
|
||||
|
||||
class ParentService implements IParentService {
|
||||
void doSomething() { /* implementation */ }
|
||||
}
|
||||
|
||||
// In root scope
|
||||
rootScope.installModules([
|
||||
Module((bind) {
|
||||
bind<IParentService>().to((scope) => ParentService());
|
||||
}),
|
||||
]);
|
||||
|
||||
// In child scope - resolve through normal hierarchy
|
||||
childScope.installModules([
|
||||
Module((bind) {
|
||||
bind<ChildService>().to((scope) =>
|
||||
ChildService(scope.resolve<IParentService>()) // Safe!
|
||||
);
|
||||
}),
|
||||
]);
|
||||
```
|
||||
|
||||
## Debug Mode
|
||||
|
||||
### Resolution Chain Tracking
|
||||
|
||||
```dart
|
||||
// Enable debug mode to track resolution chains
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
|
||||
// Access current resolution chain for debugging
|
||||
print('Current resolution chain: ${scope.currentResolutionChain}');
|
||||
|
||||
// Access global resolution chain
|
||||
print('Global resolution chain: ${GlobalCycleDetector.instance.currentGlobalResolutionChain}');
|
||||
```
|
||||
|
||||
### Exception Details
|
||||
|
||||
```dart
|
||||
try {
|
||||
final service = scope.resolve<CircularService>();
|
||||
} on CircularDependencyException catch (e) {
|
||||
print('Error: ${e.message}');
|
||||
print('Dependency chain: ${e.dependencyChain.join(' -> ')}');
|
||||
|
||||
// For global detection, additional context is available
|
||||
if (e.message.contains('cross-scope')) {
|
||||
print('This is a cross-scope circular dependency');
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Testing Integration
|
||||
|
||||
### Unit Tests
|
||||
|
||||
```dart
|
||||
import 'package:test/test.dart';
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
|
||||
void main() {
|
||||
group('Circular Dependency Detection', () {
|
||||
setUp(() {
|
||||
// Enable detection for tests
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
// Clean up after tests
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
CherryPick.disableGlobalCrossScopeCycleDetection();
|
||||
});
|
||||
|
||||
test('should detect circular dependency', () {
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
|
||||
scope.installModules([
|
||||
Module((bind) {
|
||||
bind<ServiceA>().to((scope) => ServiceA(scope.resolve<ServiceB>()));
|
||||
bind<ServiceB>().to((scope) => ServiceB(scope.resolve<ServiceA>()));
|
||||
}),
|
||||
]);
|
||||
|
||||
expect(
|
||||
() => scope.resolve<ServiceA>(),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
### Integration Tests
|
||||
|
||||
```dart
|
||||
testWidgets('should handle circular dependencies in widget tree', (tester) async {
|
||||
// Enable detection
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
|
||||
await tester.pumpWidget(
|
||||
CherryPickProvider(
|
||||
create: () {
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
// Configure modules that might have cycles
|
||||
return scope;
|
||||
},
|
||||
child: MyApp(),
|
||||
),
|
||||
);
|
||||
|
||||
// Test that circular dependencies are properly handled
|
||||
expect(find.text('Error: Circular dependency detected'), findsNothing);
|
||||
});
|
||||
```
|
||||
|
||||
## Migration Guide
|
||||
|
||||
### From Version 2.1.x to 2.2.x
|
||||
|
||||
1. **Update dependencies**:
|
||||
```yaml
|
||||
dependencies:
|
||||
cherrypick: ^2.2.0
|
||||
```
|
||||
|
||||
2. **Enable detection in existing code**:
|
||||
```dart
|
||||
// Before
|
||||
final scope = Scope(null);
|
||||
|
||||
// After - with local detection
|
||||
final scope = CherryPick.openSafeRootScope();
|
||||
|
||||
// Or with global detection
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
```
|
||||
|
||||
3. **Update error handling**:
|
||||
```dart
|
||||
try {
|
||||
final service = scope.resolve<MyService>();
|
||||
} on CircularDependencyException catch (e) {
|
||||
// Handle circular dependency errors
|
||||
logger.error('Circular dependency detected: ${e.dependencyChain}');
|
||||
}
|
||||
```
|
||||
|
||||
4. **Configure for production**:
|
||||
```dart
|
||||
void main() {
|
||||
// Configure detection based on build mode
|
||||
if (kDebugMode) {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
}
|
||||
|
||||
runApp(MyApp());
|
||||
}
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
### Scope Methods
|
||||
|
||||
```dart
|
||||
class Scope {
|
||||
// Local cycle detection
|
||||
void enableCycleDetection();
|
||||
void disableCycleDetection();
|
||||
bool get isCycleDetectionEnabled;
|
||||
List<String> get currentResolutionChain;
|
||||
|
||||
// Global cycle detection
|
||||
void enableGlobalCycleDetection();
|
||||
void disableGlobalCycleDetection();
|
||||
bool get isGlobalCycleDetectionEnabled;
|
||||
}
|
||||
```
|
||||
|
||||
### CherryPick Helper Methods
|
||||
|
||||
```dart
|
||||
class CherryPick {
|
||||
// Global settings
|
||||
static void enableGlobalCycleDetection();
|
||||
static void disableGlobalCycleDetection();
|
||||
static bool get isGlobalCycleDetectionEnabled;
|
||||
|
||||
static void enableGlobalCrossScopeCycleDetection();
|
||||
static void disableGlobalCrossScopeCycleDetection();
|
||||
static bool get isGlobalCrossScopeCycleDetectionEnabled;
|
||||
|
||||
// Per-scope settings
|
||||
static void enableCycleDetectionForScope(Scope scope);
|
||||
static void disableCycleDetectionForScope(Scope scope);
|
||||
static void enableGlobalCycleDetectionForScope(Scope scope);
|
||||
static void disableGlobalCycleDetectionForScope(Scope scope);
|
||||
|
||||
// Safe scope creation
|
||||
static Scope openSafeRootScope();
|
||||
static Scope openGlobalSafeRootScope();
|
||||
static Scope openSafeSubScope(Scope parent);
|
||||
}
|
||||
```
|
||||
|
||||
### Exception Classes
|
||||
|
||||
```dart
|
||||
class CircularDependencyException implements Exception {
|
||||
final String message;
|
||||
final List<String> dependencyChain;
|
||||
|
||||
const CircularDependencyException(this.message, this.dependencyChain);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
final chain = dependencyChain.join(' -> ');
|
||||
return 'CircularDependencyException: $message\nDependency chain: $chain';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### 1. Enable Detection During Development
|
||||
|
||||
```dart
|
||||
void main() {
|
||||
if (kDebugMode) {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
}
|
||||
|
||||
runApp(MyApp());
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Use Safe Scope Creation
|
||||
|
||||
```dart
|
||||
// Instead of
|
||||
final scope = Scope(null);
|
||||
|
||||
// Use
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
```
|
||||
|
||||
### 3. Design Proper Architecture
|
||||
|
||||
- Follow single responsibility principle
|
||||
- Use interfaces to decouple dependencies
|
||||
- Implement mediator pattern for complex interactions
|
||||
- Keep dependency flow unidirectional in scope hierarchy
|
||||
|
||||
### 4. Handle Errors Gracefully
|
||||
|
||||
```dart
|
||||
T resolveSafely<T>() {
|
||||
try {
|
||||
return scope.resolve<T>();
|
||||
} on CircularDependencyException catch (e) {
|
||||
logger.error('Circular dependency detected', e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 5. Test Thoroughly
|
||||
|
||||
- Write unit tests for dependency configurations
|
||||
- Use integration tests to verify complex scenarios
|
||||
- Enable detection in test environments
|
||||
- Test both positive and negative scenarios
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Common Issues
|
||||
|
||||
1. **False Positives**: If you're getting false circular dependency errors, check if you have proper async handling in your providers.
|
||||
|
||||
2. **Performance Issues**: If global detection is too slow, consider using only local detection or disabling it in production.
|
||||
|
||||
3. **Complex Hierarchies**: For very complex scope hierarchies, consider simplifying your architecture or using more interfaces.
|
||||
|
||||
### Debug Tips
|
||||
|
||||
1. **Check Resolution Chain**: Use `scope.currentResolutionChain` to see the current dependency resolution path.
|
||||
|
||||
2. **Enable Logging**: Add logging to your providers to trace dependency resolution.
|
||||
|
||||
3. **Simplify Dependencies**: Break complex dependencies into smaller, more manageable pieces.
|
||||
|
||||
4. **Use Interfaces**: Abstract dependencies behind interfaces to reduce coupling.
|
||||
|
||||
## Conclusion
|
||||
|
||||
Circular dependency detection in CherryPick provides robust protection against infinite loops and stack overflow errors. By following the best practices and using the appropriate detection level for your use case, you can build reliable and maintainable dependency injection configurations.
|
||||
|
||||
For more information, see the [main documentation](../README.md) and [examples](../example/).
|
||||
@@ -1,572 +0,0 @@
|
||||
# Обнаружение циклических зависимостей
|
||||
|
||||
CherryPick предоставляет надежное обнаружение циклических зависимостей для предотвращения бесконечных циклов и ошибок переполнения стека в вашей настройке внедрения зависимостей.
|
||||
|
||||
## Что такое циклические зависимости?
|
||||
|
||||
Циклические зависимости возникают, когда два или более сервиса зависят друг от друга прямо или косвенно, создавая цикл в графе зависимостей.
|
||||
|
||||
### Пример циклических зависимостей в рамках скоупа
|
||||
|
||||
```dart
|
||||
class UserService {
|
||||
final OrderService orderService;
|
||||
UserService(this.orderService);
|
||||
}
|
||||
|
||||
class OrderService {
|
||||
final UserService userService;
|
||||
OrderService(this.userService);
|
||||
}
|
||||
```
|
||||
|
||||
### Пример циклических зависимостей между скоупами
|
||||
|
||||
```dart
|
||||
// В родительском скоупе
|
||||
class ParentService {
|
||||
final ChildService childService;
|
||||
ParentService(this.childService); // Получает из дочернего скоупа
|
||||
}
|
||||
|
||||
// В дочернем скоупе
|
||||
class ChildService {
|
||||
final ParentService parentService;
|
||||
ChildService(this.parentService); // Получает из родительского скоупа
|
||||
}
|
||||
```
|
||||
|
||||
## Типы обнаружения
|
||||
|
||||
### 🔍 Локальное обнаружение
|
||||
|
||||
Обнаруживает циклические зависимости в рамках одного скоупа. Быстрое и эффективное.
|
||||
|
||||
### 🌐 Глобальное обнаружение
|
||||
|
||||
Обнаруживает циклические зависимости во всей иерархии скоупов. Более медленное, но обеспечивает полную защиту.
|
||||
|
||||
## Использование
|
||||
|
||||
### Локальное обнаружение
|
||||
|
||||
```dart
|
||||
final scope = Scope(null);
|
||||
scope.enableCycleDetection(); // Включить локальное обнаружение
|
||||
|
||||
scope.installModules([
|
||||
Module((bind) {
|
||||
bind<UserService>().to((scope) => UserService(scope.resolve<OrderService>()));
|
||||
bind<OrderService>().to((scope) => OrderService(scope.resolve<UserService>()));
|
||||
}),
|
||||
]);
|
||||
|
||||
try {
|
||||
final userService = scope.resolve<UserService>(); // Выбросит CircularDependencyException
|
||||
} catch (e) {
|
||||
print(e); // CircularDependencyException: Circular dependency detected
|
||||
}
|
||||
```
|
||||
|
||||
### Глобальное обнаружение
|
||||
|
||||
```dart
|
||||
// Включить глобальное обнаружение для всех скоупов
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
|
||||
final rootScope = CherryPick.openGlobalSafeRootScope();
|
||||
final childScope = rootScope.openSubScope();
|
||||
|
||||
// Настроить зависимости, которые создают межскоуповые циклы
|
||||
rootScope.installModules([
|
||||
Module((bind) {
|
||||
bind<ParentService>().to((scope) => ParentService(childScope.resolve<ChildService>()));
|
||||
}),
|
||||
]);
|
||||
|
||||
childScope.installModules([
|
||||
Module((bind) {
|
||||
bind<ChildService>().to((scope) => ChildService(rootScope.resolve<ParentService>()));
|
||||
}),
|
||||
]);
|
||||
|
||||
try {
|
||||
final parentService = rootScope.resolve<ParentService>(); // Выбросит CircularDependencyException
|
||||
} catch (e) {
|
||||
print(e); // CircularDependencyException с детальной информацией о цепочке
|
||||
}
|
||||
```
|
||||
|
||||
## API CherryPick Helper
|
||||
|
||||
### Глобальные настройки
|
||||
|
||||
```dart
|
||||
// Включить/отключить локальное обнаружение глобально
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
|
||||
// Включить/отключить глобальное межскоуповое обнаружение
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
CherryPick.disableGlobalCrossScopeCycleDetection();
|
||||
|
||||
// Проверить текущие настройки
|
||||
bool localEnabled = CherryPick.isGlobalCycleDetectionEnabled;
|
||||
bool globalEnabled = CherryPick.isGlobalCrossScopeCycleDetectionEnabled;
|
||||
```
|
||||
|
||||
### Настройки для конкретного скоупа
|
||||
|
||||
```dart
|
||||
// Включить/отключить для конкретного скоупа
|
||||
CherryPick.enableCycleDetectionForScope(scope);
|
||||
CherryPick.disableCycleDetectionForScope(scope);
|
||||
|
||||
// Включить/отключить глобальное обнаружение для конкретного скоупа
|
||||
CherryPick.enableGlobalCycleDetectionForScope(scope);
|
||||
CherryPick.disableGlobalCycleDetectionForScope(scope);
|
||||
```
|
||||
|
||||
### Безопасное создание скоупов
|
||||
|
||||
```dart
|
||||
// Создать скоупы с автоматически включенным обнаружением
|
||||
final safeRootScope = CherryPick.openSafeRootScope(); // Локальное обнаружение включено
|
||||
final globalSafeRootScope = CherryPick.openGlobalSafeRootScope(); // Включены локальное и глобальное
|
||||
final safeSubScope = CherryPick.openSafeSubScope(parentScope); // Наследует настройки родителя
|
||||
```
|
||||
|
||||
## Соображения производительности
|
||||
|
||||
| Тип обнаружения | Накладные расходы | Рекомендуемое использование |
|
||||
|-----------------|-------------------|----------------------------|
|
||||
| **Локальное** | Минимальные (~5%) | Разработка, тестирование |
|
||||
| **Глобальное** | Умеренные (~15%) | Сложные иерархии, критические функции |
|
||||
| **Отключено** | Нет | Продакшн (после тестирования) |
|
||||
|
||||
### Рекомендации
|
||||
|
||||
- **Разработка**: Включите локальное и глобальное обнаружение для максимальной безопасности
|
||||
- **Тестирование**: Оставьте обнаружение включенным для раннего выявления проблем
|
||||
- **Продакшн**: Рассмотрите отключение для производительности, но только после тщательного тестирования
|
||||
|
||||
```dart
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
void configureCycleDetection() {
|
||||
if (kDebugMode) {
|
||||
// Включить полную защиту в режиме отладки
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
} else {
|
||||
// Отключить в релизном режиме для производительности
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
CherryPick.disableGlobalCrossScopeCycleDetection();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Архитектурные паттерны
|
||||
|
||||
### Паттерн Repository
|
||||
|
||||
```dart
|
||||
// ✅ Правильно: Repository не зависит от сервиса
|
||||
class UserRepository {
|
||||
final ApiClient apiClient;
|
||||
UserRepository(this.apiClient);
|
||||
}
|
||||
|
||||
class UserService {
|
||||
final UserRepository repository;
|
||||
UserService(this.repository);
|
||||
}
|
||||
|
||||
// ❌ Неправильно: Циклическая зависимость
|
||||
class UserRepository {
|
||||
final UserService userService; // Не делайте так!
|
||||
UserRepository(this.userService);
|
||||
}
|
||||
```
|
||||
|
||||
### Паттерн Mediator
|
||||
|
||||
```dart
|
||||
// ✅ Правильно: Используйте медиатор для разрыва циклов
|
||||
abstract class EventBus {
|
||||
void publish<T>(T event);
|
||||
Stream<T> listen<T>();
|
||||
}
|
||||
|
||||
class UserService {
|
||||
final EventBus eventBus;
|
||||
UserService(this.eventBus);
|
||||
|
||||
void createUser(User user) {
|
||||
// ... логика создания пользователя
|
||||
eventBus.publish(UserCreatedEvent(user));
|
||||
}
|
||||
}
|
||||
|
||||
class OrderService {
|
||||
final EventBus eventBus;
|
||||
OrderService(this.eventBus) {
|
||||
eventBus.listen<UserCreatedEvent>().listen(_onUserCreated);
|
||||
}
|
||||
|
||||
void _onUserCreated(UserCreatedEvent event) {
|
||||
// Реагировать на создание пользователя без прямой зависимости
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Лучшие практики иерархии скоупов
|
||||
|
||||
### Правильный поток зависимостей
|
||||
|
||||
```dart
|
||||
// ✅ Правильно: Зависимости текут вниз по иерархии
|
||||
// Корневой скоуп: Основные сервисы
|
||||
final rootScope = CherryPick.openGlobalSafeRootScope();
|
||||
rootScope.installModules([
|
||||
Module((bind) {
|
||||
bind<DatabaseService>().singleton((scope) => DatabaseService());
|
||||
bind<ApiClient>().singleton((scope) => ApiClient());
|
||||
}),
|
||||
]);
|
||||
|
||||
// Скоуп функции: Сервисы, специфичные для функции
|
||||
final featureScope = rootScope.openSubScope();
|
||||
featureScope.installModules([
|
||||
Module((bind) {
|
||||
bind<UserRepository>().to((scope) => UserRepository(scope.resolve<ApiClient>()));
|
||||
bind<UserService>().to((scope) => UserService(scope.resolve<UserRepository>()));
|
||||
}),
|
||||
]);
|
||||
|
||||
// UI скоуп: Сервисы, специфичные для UI
|
||||
final uiScope = featureScope.openSubScope();
|
||||
uiScope.installModules([
|
||||
Module((bind) {
|
||||
bind<UserController>().to((scope) => UserController(scope.resolve<UserService>()));
|
||||
}),
|
||||
]);
|
||||
```
|
||||
|
||||
### Избегайте межскоуповых зависимостей
|
||||
|
||||
```dart
|
||||
// ❌ Неправильно: Дочерний скоуп зависит от конкретных сервисов родителя
|
||||
childScope.installModules([
|
||||
Module((bind) {
|
||||
bind<ChildService>().to((scope) =>
|
||||
ChildService(rootScope.resolve<ParentService>()) // Рискованно!
|
||||
);
|
||||
}),
|
||||
]);
|
||||
|
||||
// ✅ Правильно: Используйте интерфейсы и правильное внедрение зависимостей
|
||||
abstract class IParentService {
|
||||
void doSomething();
|
||||
}
|
||||
|
||||
class ParentService implements IParentService {
|
||||
void doSomething() { /* реализация */ }
|
||||
}
|
||||
|
||||
// В корневом скоупе
|
||||
rootScope.installModules([
|
||||
Module((bind) {
|
||||
bind<IParentService>().to((scope) => ParentService());
|
||||
}),
|
||||
]);
|
||||
|
||||
// В дочернем скоупе - разрешение через обычную иерархию
|
||||
childScope.installModules([
|
||||
Module((bind) {
|
||||
bind<ChildService>().to((scope) =>
|
||||
ChildService(scope.resolve<IParentService>()) // Безопасно!
|
||||
);
|
||||
}),
|
||||
]);
|
||||
```
|
||||
|
||||
## Режим отладки
|
||||
|
||||
### Отслеживание цепочки разрешения
|
||||
|
||||
```dart
|
||||
// Включить режим отладки для отслеживания цепочек разрешения
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
|
||||
// Доступ к текущей цепочке разрешения для отладки
|
||||
print('Текущая цепочка разрешения: ${scope.currentResolutionChain}');
|
||||
|
||||
// Доступ к глобальной цепочке разрешения
|
||||
print('Глобальная цепочка разрешения: ${GlobalCycleDetector.instance.currentGlobalResolutionChain}');
|
||||
```
|
||||
|
||||
### Детали исключений
|
||||
|
||||
```dart
|
||||
try {
|
||||
final service = scope.resolve<CircularService>();
|
||||
} on CircularDependencyException catch (e) {
|
||||
print('Ошибка: ${e.message}');
|
||||
print('Цепочка зависимостей: ${e.dependencyChain.join(' -> ')}');
|
||||
|
||||
// Для глобального обнаружения доступен дополнительный контекст
|
||||
if (e.message.contains('cross-scope')) {
|
||||
print('Это межскоуповая циклическая зависимость');
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Интеграция с тестированием
|
||||
|
||||
### Модульные тесты
|
||||
|
||||
```dart
|
||||
import 'package:test/test.dart';
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
|
||||
void main() {
|
||||
group('Обнаружение циклических зависимостей', () {
|
||||
setUp(() {
|
||||
// Включить обнаружение для тестов
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
// Очистка после тестов
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
CherryPick.disableGlobalCrossScopeCycleDetection();
|
||||
});
|
||||
|
||||
test('должен обнаружить циклическую зависимость', () {
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
|
||||
scope.installModules([
|
||||
Module((bind) {
|
||||
bind<ServiceA>().to((scope) => ServiceA(scope.resolve<ServiceB>()));
|
||||
bind<ServiceB>().to((scope) => ServiceB(scope.resolve<ServiceA>()));
|
||||
}),
|
||||
]);
|
||||
|
||||
expect(
|
||||
() => scope.resolve<ServiceA>(),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
### Интеграционные тесты
|
||||
|
||||
```dart
|
||||
testWidgets('должен обрабатывать циклические зависимости в дереве виджетов', (tester) async {
|
||||
// Включить обнаружение
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
|
||||
await tester.pumpWidget(
|
||||
CherryPickProvider(
|
||||
create: () {
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
// Настроить модули, которые могут иметь циклы
|
||||
return scope;
|
||||
},
|
||||
child: MyApp(),
|
||||
),
|
||||
);
|
||||
|
||||
// Проверить, что циклические зависимости правильно обрабатываются
|
||||
expect(find.text('Ошибка: Обнаружена циклическая зависимость'), findsNothing);
|
||||
});
|
||||
```
|
||||
|
||||
## Руководство по миграции
|
||||
|
||||
### С версии 2.1.x на 2.2.x
|
||||
|
||||
1. **Обновите зависимости**:
|
||||
```yaml
|
||||
dependencies:
|
||||
cherrypick: ^2.2.0
|
||||
```
|
||||
|
||||
2. **Включите обнаружение в существующем коде**:
|
||||
```dart
|
||||
// Раньше
|
||||
final scope = Scope(null);
|
||||
|
||||
// Теперь - с локальным обнаружением
|
||||
final scope = CherryPick.openSafeRootScope();
|
||||
|
||||
// Или с глобальным обнаружением
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
```
|
||||
|
||||
3. **Обновите обработку ошибок**:
|
||||
```dart
|
||||
try {
|
||||
final service = scope.resolve<MyService>();
|
||||
} on CircularDependencyException catch (e) {
|
||||
// Обработать ошибки циклических зависимостей
|
||||
logger.error('Обнаружена циклическая зависимость: ${e.dependencyChain}');
|
||||
}
|
||||
```
|
||||
|
||||
4. **Настройте для продакшна**:
|
||||
```dart
|
||||
void main() {
|
||||
// Настроить обнаружение в зависимости от режима сборки
|
||||
if (kDebugMode) {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
}
|
||||
|
||||
runApp(MyApp());
|
||||
}
|
||||
```
|
||||
|
||||
## Справочник API
|
||||
|
||||
### Методы Scope
|
||||
|
||||
```dart
|
||||
class Scope {
|
||||
// Локальное обнаружение циклов
|
||||
void enableCycleDetection();
|
||||
void disableCycleDetection();
|
||||
bool get isCycleDetectionEnabled;
|
||||
List<String> get currentResolutionChain;
|
||||
|
||||
// Глобальное обнаружение циклов
|
||||
void enableGlobalCycleDetection();
|
||||
void disableGlobalCycleDetection();
|
||||
bool get isGlobalCycleDetectionEnabled;
|
||||
}
|
||||
```
|
||||
|
||||
### Методы CherryPick Helper
|
||||
|
||||
```dart
|
||||
class CherryPick {
|
||||
// Глобальные настройки
|
||||
static void enableGlobalCycleDetection();
|
||||
static void disableGlobalCycleDetection();
|
||||
static bool get isGlobalCycleDetectionEnabled;
|
||||
|
||||
static void enableGlobalCrossScopeCycleDetection();
|
||||
static void disableGlobalCrossScopeCycleDetection();
|
||||
static bool get isGlobalCrossScopeCycleDetectionEnabled;
|
||||
|
||||
// Настройки для конкретного скоупа
|
||||
static void enableCycleDetectionForScope(Scope scope);
|
||||
static void disableCycleDetectionForScope(Scope scope);
|
||||
static void enableGlobalCycleDetectionForScope(Scope scope);
|
||||
static void disableGlobalCycleDetectionForScope(Scope scope);
|
||||
|
||||
// Безопасное создание скоупов
|
||||
static Scope openSafeRootScope();
|
||||
static Scope openGlobalSafeRootScope();
|
||||
static Scope openSafeSubScope(Scope parent);
|
||||
}
|
||||
```
|
||||
|
||||
### Классы исключений
|
||||
|
||||
```dart
|
||||
class CircularDependencyException implements Exception {
|
||||
final String message;
|
||||
final List<String> dependencyChain;
|
||||
|
||||
const CircularDependencyException(this.message, this.dependencyChain);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
final chain = dependencyChain.join(' -> ');
|
||||
return 'CircularDependencyException: $message\nЦепочка зависимостей: $chain';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Лучшие практики
|
||||
|
||||
### 1. Включайте обнаружение во время разработки
|
||||
|
||||
```dart
|
||||
void main() {
|
||||
if (kDebugMode) {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
}
|
||||
|
||||
runApp(MyApp());
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Используйте безопасное создание скоупов
|
||||
|
||||
```dart
|
||||
// Вместо
|
||||
final scope = Scope(null);
|
||||
|
||||
// Используйте
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
```
|
||||
|
||||
### 3. Проектируйте правильную архитектуру
|
||||
|
||||
- Следуйте принципу единственной ответственности
|
||||
- Используйте интерфейсы для разделения зависимостей
|
||||
- Реализуйте паттерн медиатор для сложных взаимодействий
|
||||
- Поддерживайте однонаправленный поток зависимостей в иерархии скоупов
|
||||
|
||||
### 4. Обрабатывайте ошибки корректно
|
||||
|
||||
```dart
|
||||
T resolveSafely<T>() {
|
||||
try {
|
||||
return scope.resolve<T>();
|
||||
} on CircularDependencyException catch (e) {
|
||||
logger.error('Обнаружена циклическая зависимость', e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 5. Тестируйте тщательно
|
||||
|
||||
- Пишите модульные тесты для конфигураций зависимостей
|
||||
- Используйте интеграционные тесты для проверки сложных сценариев
|
||||
- Включайте обнаружение в тестовых средах
|
||||
- Тестируйте как положительные, так и отрицательные сценарии
|
||||
|
||||
## Устранение неполадок
|
||||
|
||||
### Распространенные проблемы
|
||||
|
||||
1. **Ложные срабатывания**: Если вы получаете ложные ошибки циклических зависимостей, проверьте правильность обработки async в ваших провайдерах.
|
||||
|
||||
2. **Проблемы производительности**: Если глобальное обнаружение слишком медленное, рассмотрите использование только локального обнаружения или отключение в продакшне.
|
||||
|
||||
3. **Сложные иерархии**: Для очень сложных иерархий скоупов рассмотрите упрощение архитектуры или использование большего количества интерфейсов.
|
||||
|
||||
### Советы по отладке
|
||||
|
||||
1. **Проверьте цепочку разрешения**: Используйте `scope.currentResolutionChain` для просмотра текущего пути разрешения зависимостей.
|
||||
|
||||
2. **Включите логирование**: Добавьте логирование в ваши провайдеры для трассировки разрешения зависимостей.
|
||||
|
||||
3. **Упростите зависимости**: Разбейте сложные зависимости на более мелкие, управляемые части.
|
||||
|
||||
4. **Используйте интерфейсы**: Абстрагируйте зависимости за интерфейсами для уменьшения связанности.
|
||||
|
||||
## Заключение
|
||||
|
||||
Обнаружение циклических зависимостей в CherryPick обеспечивает надежную защиту от бесконечных циклов и ошибок переполнения стека. Следуя лучшим практикам и используя подходящий уровень обнаружения для вашего случая использования, вы можете создавать надежные и поддерживаемые конфигурации внедрения зависимостей.
|
||||
|
||||
Для получения дополнительной информации см. [основную документацию](../README.md) и [примеры](../example/).
|
||||
@@ -75,7 +75,7 @@ Allows you to create dependencies with runtime parameters, e.g., a service for a
|
||||
bind<UserService>().toProvideWithParams((userId) => UserService(userId));
|
||||
|
||||
// Resolve:
|
||||
final userService = scope.resolve<UserService>(params: '123');
|
||||
final userService = scope.resolveWithParams<UserService>(params: '123');
|
||||
```
|
||||
|
||||
---
|
||||
@@ -379,6 +379,45 @@ 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.
|
||||
|
||||
@@ -76,7 +76,7 @@ final api = scope.resolve<ApiClient>(named: 'mock');
|
||||
bind<UserService>().toProvideWithParams((userId) => UserService(userId));
|
||||
|
||||
// Получение
|
||||
final userService = scope.resolve<UserService>(params: '123');
|
||||
final userService = scope.resolveWithParams<UserService>(params: '123');
|
||||
```
|
||||
|
||||
---
|
||||
@@ -382,6 +382,45 @@ 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 делают его особенно мощным как для небольших, так и для масштабных проектов.
|
||||
|
||||
@@ -19,7 +19,29 @@ There are two main methods for initializing a custom instance `toInstance ()` an
|
||||
|
||||
Example:
|
||||
|
||||
```dart
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 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`.
|
||||
|
||||
---
|
||||
// initializing a text string instance through a method toInstance()
|
||||
Binding<String>().toInstance("hello world");
|
||||
|
||||
|
||||
@@ -19,7 +19,29 @@ Binding - по сути это конфигуратор для пользов
|
||||
|
||||
Пример:
|
||||
|
||||
```dart
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Продвинутая настройка генерации кода
|
||||
|
||||
В файле `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`.
|
||||
|
||||
---
|
||||
// инициализация экземпляра текстовой строки через метод toInstance()
|
||||
Binding<String>().toInstance("hello world");
|
||||
|
||||
|
||||
@@ -127,28 +127,28 @@ packages:
|
||||
path: "../../cherrypick"
|
||||
relative: true
|
||||
source: path
|
||||
version: "2.2.0"
|
||||
version: "2.2.0-dev.1"
|
||||
cherrypick_annotations:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
path: "../../cherrypick_annotations"
|
||||
relative: true
|
||||
source: path
|
||||
version: "1.1.0"
|
||||
version: "1.1.0-dev.1"
|
||||
cherrypick_flutter:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
path: "../../cherrypick_flutter"
|
||||
relative: true
|
||||
source: path
|
||||
version: "1.1.2"
|
||||
version: "1.1.2-dev.1"
|
||||
cherrypick_generator:
|
||||
dependency: "direct dev"
|
||||
description:
|
||||
path: "../../cherrypick_generator"
|
||||
relative: true
|
||||
source: path
|
||||
version: "1.1.0"
|
||||
version: "1.1.0-dev.5"
|
||||
clock:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
27
examples/postly/build.yaml
Normal file
27
examples/postly/build.yaml
Normal file
@@ -0,0 +1,27 @@
|
||||
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
|
||||
@@ -7,7 +7,7 @@ import 'domain/repository/post_repository.dart';
|
||||
import 'presentation/bloc/post_bloc.dart';
|
||||
import 'router/app_router.dart';
|
||||
|
||||
part 'app.inject.cherrypick.g.dart';
|
||||
part 'generated/app.inject.cherrypick.g.dart';
|
||||
|
||||
@injectable()
|
||||
class MyApp extends StatelessWidget with _$MyApp {
|
||||
|
||||
@@ -5,7 +5,7 @@ import '../data/network/json_placeholder_api.dart';
|
||||
import '../data/post_repository_impl.dart';
|
||||
import '../domain/repository/post_repository.dart';
|
||||
|
||||
part 'app_module.module.cherrypick.g.dart';
|
||||
part '../generated/di/app_module.module.cherrypick.g.dart';
|
||||
|
||||
@module()
|
||||
abstract class AppModule extends Module {
|
||||
|
||||
@@ -1,17 +1,9 @@
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:postly/app.dart';
|
||||
import 'di/app_module.dart';
|
||||
|
||||
void main() {
|
||||
// Включаем cycle-detection только в debug/test
|
||||
if (kDebugMode) {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
}
|
||||
|
||||
// Используем safe root scope для гарантии защиты
|
||||
CherryPick.openRootScope().installModules([$AppModule()]);
|
||||
runApp(MyApp());
|
||||
}
|
||||
|
||||
@@ -151,21 +151,21 @@ packages:
|
||||
path: "../../cherrypick"
|
||||
relative: true
|
||||
source: path
|
||||
version: "2.2.0"
|
||||
version: "2.2.0-dev.1"
|
||||
cherrypick_annotations:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
path: "../../cherrypick_annotations"
|
||||
relative: true
|
||||
source: path
|
||||
version: "1.1.0"
|
||||
version: "1.1.0-dev.1"
|
||||
cherrypick_generator:
|
||||
dependency: "direct dev"
|
||||
description:
|
||||
path: "../../cherrypick_generator"
|
||||
relative: true
|
||||
source: path
|
||||
version: "1.1.0"
|
||||
version: "1.1.0-dev.5"
|
||||
clock:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
Reference in New Issue
Block a user