generate method
Generate a model from a schema
schema
- The schema as a Map<String, dynamic>
Implementation
void generate(Map<String, dynamic> schema) {
try {
// Validate input schema
if (schema.isEmpty) {
throw const SchemaValidationException('Schema cannot be empty');
}
// Create output directory with proper error handling
final dir = Directory(outputDir);
if (!dir.existsSync()) {
try {
dir.createSync(recursive: true);
_logger.info('Created output directory: $outputDir');
} catch (e) {
throw FileOperationException(
'Could not create output directory: $outputDir',
e,
);
}
}
// Generate models with individual error handling
for (final entry in schema.entries) {
try {
_generateModel(entry.key, entry.value, schema);
} catch (e) {
if (e is ApiGenException) {
rethrow;
}
throw CodeGenerationException(
'Failed to generate legacy model: ${entry.key}',
e,
);
}
}
_logger.info('Successfully generated ${schema.length} legacy models');
} on ApiGenException catch (e, st) {
_logger.error('ApiGenException: ${e.message}', e, st);
rethrow;
} catch (e, st) {
final ex = CodeGenerationException(
'Unknown error in legacy model generation',
e,
);
_logger.error('Unknown error: $e', e, st);
throw ex;
}
}