-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
347 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
database/database.dart |
14 changes: 14 additions & 0 deletions
14
_tests_/database/migrations/2024_04_21_003612_create_users_table.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'package:yaroorm/yaroorm.dart'; | ||
import 'package:yaroorm_tests/src/models.dart'; | ||
|
||
class AddUsersTable extends Migration { | ||
@override | ||
void up(List<Schema> schemas) { | ||
schemas.add(UserSchema); | ||
} | ||
|
||
@override | ||
void down(List<Schema> schemas) { | ||
schemas.add(Schema.dropIfExists(UserSchema)); | ||
} | ||
} |
15 changes: 1 addition & 14 deletions
15
...test/integration/fixtures/migrations.dart → ...2024_04_21_003650_create_posts_table.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,53 @@ | ||
import 'dart:io'; | ||
import 'package:analyzer/dart/analysis/analysis_context_collection.dart'; | ||
import 'package:analyzer/dart/analysis/results.dart'; | ||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:analyzer/file_system/physical_file_system.dart'; | ||
import 'package:path/path.dart' as path; | ||
import 'package:source_gen/source_gen.dart'; | ||
import 'package:yaroorm/src/cli/logger.dart'; | ||
import 'package:yaroorm/src/cli/commands/init_orm_command.dart'; | ||
|
||
import 'package:yaroorm/src/migration.dart' show Migration; | ||
import 'package:yaroorm/src/database/entity/entity.dart' show Entity; | ||
const _migratorFileContent = ''' | ||
import 'package:yaroorm/src/cli/orm.dart'; | ||
import 'package:yaroorm/yaroorm.dart'; | ||
import '../../database/database.dart'; | ||
void main(List<String> args) async { | ||
final migratorDirectoryPath = migratorPath; | ||
initializeORM(); | ||
await OrmCLIRunner.start(args); | ||
} | ||
'''; | ||
|
||
void main(List<String> args) async { | ||
final migratorDirectoryPath = path.join(Directory.current.path, '.dart_tool', 'yaroorm'); | ||
final dir = Directory(migratorDirectoryPath); | ||
if (!dir.existsSync()) dir.createSync(); | ||
|
||
final dartFile = path.join(migratorDirectoryPath, 'migrator.dart'); | ||
final aotFilePath = path.join(migratorDirectoryPath, 'migrator'); | ||
final kernelFilePath = path.join(migratorDirectoryPath, 'migrator_kernel'); | ||
|
||
if (args.isNotEmpty && args[0] == 'init') { | ||
await _initOrmInProject(Directory.current); | ||
exit(0); | ||
} | ||
final migratorFile = File(dartFile); | ||
final kernelFile = File(kernelFilePath); | ||
|
||
if (!File(dartFile).existsSync()) { | ||
logger.err('🗙 Migrator file does not exist'); | ||
exit(0); | ||
final isInitCommand = args.isNotEmpty && args[0] == InitializeOrmCommand.commandName; | ||
if (isInitCommand && kernelFile.existsSync()) { | ||
kernelFile.delete(); | ||
} | ||
|
||
final aotFile = File(aotFilePath); | ||
if (!aotFile.existsSync()) { | ||
/// TODO(codekeyz): add checksum check for invalidating aot | ||
Process.start('dart', ['compile', 'exe', dartFile, '-o', aotFilePath], mode: ProcessStartMode.detached); | ||
if (!migratorFile.existsSync()) { | ||
await migratorFile.writeAsString(_migratorFileContent); | ||
} | ||
|
||
late Process process; | ||
|
||
if (aotFile.existsSync()) { | ||
process = await Process.start(aotFilePath, args); | ||
if (kernelFile.existsSync()) { | ||
process = await Process.start('dart', ['run', kernelFilePath, ...args]); | ||
} else { | ||
process = await Process.start('dart', ['run', dartFile, ...args]); | ||
} | ||
|
||
stdout.addStream(process.stdout); | ||
stderr.addStream(process.stderr); | ||
} | ||
|
||
String get migratorPath { | ||
return path.join(Directory.current.path, '.dart_tool', 'yaroorm/bin'); | ||
} | ||
|
||
Future<void> _initOrmInProject(Directory workingDir) async { | ||
// final progress = logger.progress('Initializing ORM in project'); | ||
|
||
final collection = AnalysisContextCollection( | ||
includedPaths: [workingDir.absolute.path], | ||
resourceProvider: PhysicalResourceProvider.INSTANCE, | ||
); | ||
|
||
List<Item> migrations = []; | ||
List<Item> entities = []; | ||
|
||
await for (final (library, _, _) in _libraries(collection)) { | ||
final result = _validateLibrary(library, library.element.identifier); | ||
if (result == null) continue; | ||
|
||
if (result.migrations != null) { | ||
migrations.add(result.migrations!); | ||
} | ||
|
||
if (result.entityClasses != null) { | ||
entities.add(result.entityClasses!); | ||
} | ||
} | ||
|
||
print(migrations.map((e) => (e.elements.map((e) => e.name), e.path))); | ||
print(entities.map((e) => (e.elements.map((e) => e.name), e.path))); | ||
} | ||
|
||
TypeChecker _typeChecker(Type type) => TypeChecker.fromRuntime(type); | ||
|
||
class Item { | ||
final Iterable<ClassElement> elements; | ||
final String path; | ||
|
||
const Item(this.elements, this.path); | ||
} | ||
|
||
({Item? migrations, Item? entityClasses})? _validateLibrary(ResolvedLibraryResult library, String identifier) { | ||
final classElements = library.element.topLevelElements | ||
.where((e) => !e.isPrivate && e is ClassElement && e.supertype != null && !e.isAbstract) | ||
.toList() | ||
.cast<ClassElement>(); | ||
|
||
if (classElements.isEmpty) return null; | ||
|
||
final migrationClasses = classElements.where((element) => _typeChecker(Migration).isExactlyType(element.supertype!)); | ||
final entityClasses = classElements.where((element) => _typeChecker(Entity).isExactlyType(element.supertype!)); | ||
|
||
return ( | ||
migrations: migrationClasses.isEmpty ? null : Item(migrationClasses, identifier), | ||
entityClasses: entityClasses.isEmpty ? null : Item(entityClasses, identifier), | ||
); | ||
} | ||
|
||
Stream<(ResolvedLibraryResult, String, String)> _libraries(AnalysisContextCollection collection) async* { | ||
for (var context in collection.contexts) { | ||
var analyzedFiles = context.contextRoot.analyzedFiles().toList(); | ||
analyzedFiles.sort(); | ||
final analyzedDartFiles = analyzedFiles.where((path) => path.endsWith('.dart') && !path.endsWith('_test.dart')); | ||
for (final filePath in analyzedDartFiles) { | ||
final library = await context.currentSession.getResolvedLibrary(filePath); | ||
if (library is ResolvedLibraryResult) { | ||
yield (library, filePath, context.contextRoot.root.path); | ||
} | ||
} | ||
if (!isInitCommand && !kernelFile.existsSync()) { | ||
/// TODO(codekeyz): add checksum check for invalidating kernel snapshot | ||
Process.start('dart', ['compile', 'kernel', dartFile, '-o', kernelFilePath], mode: ProcessStartMode.detached); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import 'package:build/build.dart'; | ||
|
||
import 'src/generator.dart'; | ||
import 'src/builder/generator.dart'; | ||
|
||
/// Builds generators for `build_runner` to run | ||
Builder yaroormBuilder(BuilderOptions options) => generatorFactoryBuilder(options); |
Oops, something went wrong.