/// <inheritdoc />
        public CompilationResult Compile([NotNull] RelativeFileInfo fileInfo, [NotNull] string compilationContent)
        {
            var assemblyName        = Path.GetRandomFileName();
            var compilationSettings = _compilerOptionsProvider.GetCompilationSettings(_environment);
            var syntaxTree          = SyntaxTreeGenerator.Generate(compilationContent,
                                                                   assemblyName,
                                                                   compilationSettings);
            var references = _applicationReferences.Value;

            var compilationOptions = compilationSettings.CompilationOptions
                                     .WithOutputKind(OutputKind.DynamicallyLinkedLibrary);

            var compilation = CSharpCompilation.Create(assemblyName,
                                                       options: compilationOptions,
                                                       syntaxTrees: new[] { syntaxTree },
                                                       references: references);

            using (var ms = new MemoryStream()) {
                using (var pdb = new MemoryStream()) {
                    EmitResult result;

                    if (_supportsPdbGeneration.Value)
                    {
                        result = compilation.Emit(ms, pdbStream: pdb);
                    }
                    else
                    {
                        result = compilation.Emit(ms);
                    }

                    if (!result.Success)
                    {
                        return(GetCompilationFailedResult(
                                   fileInfo.RelativePath,
                                   compilationContent,
                                   assemblyName,
                                   result.Diagnostics));
                    }

                    Assembly assembly;
                    ms.Seek(0, SeekOrigin.Begin);

                    if (_supportsPdbGeneration.Value)
                    {
                        pdb.Seek(0, SeekOrigin.Begin);
                        assembly = _loader.LoadStream(ms, pdb);
                    }
                    else
                    {
                        assembly = _loader.LoadStream(ms, assemblySymbols: null);
                    }

                    var type = assembly.GetExportedTypes()
                               .First(t => t.Name.StartsWith(_classPrefix, StringComparison.Ordinal));

                    return(UncachedCompilationResult.Successful(type, compilationContent));
                }
            }
        }
Exemplo n.º 2
0
        public virtual void CompileViews()
        {
            var result = CreateFileInfoCollection();

            if (result != null)
            {
                var generatedCode = RazorFileInfoCollectionGenerator.GenerateCode(result);
                var syntaxTree    = CSharpSyntaxTree.ParseText(
                    generatedCode,
                    SyntaxTreeGenerator.GetParseOptions(CompilationSettings));
                CompileContext.Compilation = CompileContext.Compilation.AddSyntaxTrees(syntaxTree);
            }
        }
Exemplo n.º 3
0
        protected virtual PrecompilationCacheEntry GetCacheEntry(RelativeFileInfo fileInfo)
        {
            if (fileInfo == null)
            {
                throw new ArgumentNullException(nameof(fileInfo));
            }

            using (var stream = fileInfo.FileInfo.CreateReadStream())
            {
                using (var host = GetRazorHost())
                {
                    var results = host.GenerateCode(fileInfo.RelativePath, stream);

                    if (results.Success)
                    {
                        var syntaxTree = SyntaxTreeGenerator.Generate(
                            results.GeneratedCode,
                            fileInfo.FileInfo.PhysicalPath,
                            CompilationSettings);
                        var fullTypeName = results.GetMainClassName(host, syntaxTree);

                        if (fullTypeName != null)
                        {
                            var razorFileInfo = new RazorFileInfo
                            {
                                RelativePath = fileInfo.RelativePath,
                                FullTypeName = fullTypeName
                            };

                            return(new PrecompilationCacheEntry(razorFileInfo, syntaxTree));
                        }
                    }
                    else
                    {
                        var diagnostics = results
                                          .ParserErrors
                                          .Select(error => error.ToDiagnostics(fileInfo.FileInfo.PhysicalPath))
                                          .ToList();

                        return(new PrecompilationCacheEntry(diagnostics));
                    }
                }
            }

            return(null);
        }
Exemplo n.º 4
0
        protected virtual PrecompilationCacheEntry GetCacheEntry([NotNull] RelativeFileInfo fileInfo)
        {
            using (var stream = fileInfo.FileInfo.CreateReadStream())
            {
                var host    = GetRazorHost();
                var results = host.GenerateCode(fileInfo.RelativePath, stream);

                if (results.Success)
                {
                    var syntaxTree = SyntaxTreeGenerator.Generate(results.GeneratedCode,
                                                                  fileInfo.FileInfo.PhysicalPath,
                                                                  CompilationSettings);
                    var fullTypeName = results.GetMainClassName(host, syntaxTree);

                    if (fullTypeName != null)
                    {
                        var hashAlgorithmVersion = RazorFileHash.HashAlgorithmVersion1;
                        var hash          = RazorFileHash.GetHash(fileInfo.FileInfo, hashAlgorithmVersion);
                        var razorFileInfo = new RazorFileInfo
                        {
                            RelativePath         = fileInfo.RelativePath,
                            LastModified         = fileInfo.FileInfo.LastModified,
                            Length               = fileInfo.FileInfo.Length,
                            FullTypeName         = fullTypeName,
                            Hash                 = hash,
                            HashAlgorithmVersion = hashAlgorithmVersion
                        };

                        return(new PrecompilationCacheEntry(razorFileInfo, syntaxTree));
                    }
                }
                else
                {
                    var diagnostics = results.ParserErrors
                                      .Select(error => error.ToDiagnostics(fileInfo.FileInfo.PhysicalPath))
                                      .ToList();

                    return(new PrecompilationCacheEntry(diagnostics));
                }
            }

            return(null);
        }
Exemplo n.º 5
0
        public virtual SyntaxTree GenerateCollection()
        {
            var builder = new StringBuilder();

            builder.AppendFormat(CultureInfo.InvariantCulture,
                                 TopFormat,
                                 RazorFileInfoCollection.AssemblyResourceName,
                                 RazorFileInfoCollection.SymbolsResourceName);

            foreach (var fileInfo in RazorFileInfoCollection.FileInfos)
            {
                var perFileEntry = GenerateFile(fileInfo);
                builder.Append(perFileEntry);
            }

            builder.Append(Bottom);

            var sourceCode = builder.ToString();
            var syntaxTree = SyntaxTreeGenerator.Generate(sourceCode,
                                                          "__AUTO__GeneratedViewsCollection.cs",
                                                          CompilationSettings);

            return(syntaxTree);
        }