コード例 #1
0
        public virtual async Task <TemplateResult> RunTemplateAsync([NotNull] string content,
                                                                    [NotNull] dynamic templateModel, CancellationToken cancellationToken = default(CancellationToken))
        {
            Check.NotNull(content, nameof(content));
            Check.NotNull(templateModel, nameof(templateModel));

            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)
                    });
                }

                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;
                    razorTemplate.ModelUtilities  = _modelUtilities;
                    razorTemplate.CSharpUtilities = _csharpUtiliies;
                    //ToDo: If there are errors executing the code, they are missed here.
                    result = await razorTemplate.ExecuteTemplateAsync(cancellationToken);
                }

                return(new TemplateResult
                {
                    GeneratedText = result,
                    ProcessingException = null
                });
            }
        }
コード例 #2
0
        public virtual List <MetadataReference> GetReferences()
        {
            var provider = new MetadataReferencesProvider();

            NamedReferences.ForEach(name => provider.AddReferenceFromName(name));
            References.ForEach(r => provider.Add(r));
            return(provider.GetApplicationReferences());
        }
コード例 #3
0
        private List <MetadataReference> SetupMetadataReferencesForCompilationOfGeneratedCode(
            MetadataReferencesProvider metadataReferencesProvider)
        {
            metadataReferencesProvider.AddReferenceFromName("EntityFramework.Core");
            metadataReferencesProvider.AddReferenceFromName("EntityFramework.Relational");
            metadataReferencesProvider.AddReferenceFromName("EntityFramework.SqlServer");

#if DNXCORE50 || NETCORE50
            metadataReferencesProvider.AddReferenceFromName("System.Data.Common");
            metadataReferencesProvider.AddReferenceFromName("System.Linq.Expressions");
            metadataReferencesProvider.AddReferenceFromName("System.Reflection");

            return(metadataReferencesProvider.GetApplicationReferences());
#else
            var metadataReferences = metadataReferencesProvider.GetApplicationReferences();
            metadataReferences.Add(MetadataReference.CreateFromFile(
                                       Assembly.Load(new AssemblyName(
                                                         "System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")).Location));

            return(metadataReferences);
#endif
        }
コード例 #4
0
        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
                });
            }
        }