public virtual async Task<TemplateResult> RunTemplateAsync(string content, dynamic templateModel, IDatabaseMetadataModelProvider provider, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); var host = new RazorTemplatingHost(typeof(RazorReverseEngineeringBase)); var engine = new RazorTemplateEngine(host); using (var reader = new StringReader(content)) { var generatorResults = engine.GenerateCode(reader); if (!generatorResults.Success) { var messages = generatorResults.ParserErrors.Select(e => e.Message); return new TemplateResult { GeneratedText = string.Empty, ProcessingException = new TemplateProcessingException(messages) }; } provider.AddReferencesForTemplates(_metadataReferencesProvider); var references = _metadataReferencesProvider.GetApplicationReferences(); var templateResult = _compilationService.Compile( new List<string> { generatorResults.GeneratedCode }, references); if (templateResult.Messages.Any()) { return new TemplateResult { GeneratedText = string.Empty, ProcessingException = new TemplateProcessingException(templateResult.Messages) }; } var compiledObject = Activator.CreateInstance(templateResult.CompiledType); var razorTemplate = compiledObject as RazorReverseEngineeringBase; var result = String.Empty; if (razorTemplate != null) { razorTemplate.Model = templateModel; //ToDo: If there are errors executing the code, they are missed here. result = await razorTemplate.ExecuteTemplateAsync(cancellationToken); } return new TemplateResult { GeneratedText = result, ProcessingException = null }; } }
public virtual IModel GetMetadataModel( [NotNull] IDatabaseMetadataModelProvider provider, [NotNull] ReverseEngineeringConfiguration configuration) { Check.NotNull(provider, nameof(provider)); Check.NotNull(configuration, nameof(configuration)); var metadataModel = provider .GenerateMetadataModel(configuration.ConnectionString); if (metadataModel == null) { throw new InvalidOperationException( Strings.ProviderReturnedNullModel( provider.GetType().FullName, configuration.ConnectionString)); } return(metadataModel); }
public ReverseEngineeringGenerator( [NotNull] ILoggerFactory loggerFactory, [NotNull] IFileService fileService, [NotNull] ModelUtilities modelUtilities, [NotNull] IDatabaseMetadataModelProvider metadataModelProvider, [NotNull] ModelConfigurationFactory modelConfigurationFactory, [NotNull] CodeWriter codeWriter) { Check.NotNull(loggerFactory, nameof(loggerFactory)); Check.NotNull(fileService, nameof(fileService)); Check.NotNull(modelUtilities, nameof(modelUtilities)); Check.NotNull(metadataModelProvider, nameof(metadataModelProvider)); Check.NotNull(modelConfigurationFactory, nameof(modelConfigurationFactory)); Check.NotNull(codeWriter, nameof(codeWriter)); Logger = loggerFactory.CreateCommandsLogger(); _provider = metadataModelProvider; _modelConfigurationFactory = modelConfigurationFactory; CodeWriter = codeWriter; }
public virtual async Task <TemplateResult> RunTemplateAsync(string content, dynamic templateModel, IDatabaseMetadataModelProvider provider, CancellationToken cancellationToken = default(CancellationToken)) { RazorTemplatingHost host = new RazorTemplatingHost(typeof(RazorReverseEngineeringBase)); RazorTemplateEngine engine = new RazorTemplateEngine(host); using (var reader = new StringReader(content)) { var generatorResults = engine.GenerateCode(reader); if (!generatorResults.Success) { var messages = generatorResults.ParserErrors.Select(e => e.Message); return(new TemplateResult() { GeneratedText = string.Empty, ProcessingException = new TemplateProcessingException(messages, generatorResults.GeneratedCode) }); } provider.AddReferencesForTemplates(_metadataReferencesProvider); var references = _metadataReferencesProvider.GetApplicationReferences(); var templateResult = _compilationService.Compile(generatorResults.GeneratedCode, references); if (templateResult.Messages.Any()) { return(new TemplateResult() { GeneratedText = string.Empty, ProcessingException = new TemplateProcessingException(templateResult.Messages, generatorResults.GeneratedCode) }); } var compiledObject = Activator.CreateInstance(templateResult.CompiledType); var razorTemplate = compiledObject as RazorReverseEngineeringBase; string result = String.Empty; if (razorTemplate != null) { razorTemplate.Model = templateModel; //ToDo: If there are errors executing the code, they are missed here. result = await razorTemplate.ExecuteTemplateAsync(); } return(new TemplateResult() { GeneratedText = result, ProcessingException = null }); } }
public virtual string GetEntityTypeTemplateFileName([NotNull] IDatabaseMetadataModelProvider provider) { Check.NotNull(provider, nameof(provider)); return(provider.GetType().GetTypeInfo().Assembly.GetName().Name + "." + EntityTypeTemplateFileName); }