示例#1
0
        /// <inheritdoc />
        public CompilationResult Compile([NotNull] RelativeFileInfo file)
        {
            GeneratorResults results;

            using (var inputStream = file.FileInfo.CreateReadStream())
            {
                results = _razorHost.GenerateCode(file.RelativePath, inputStream);
            }

            if (!results.Success)
            {
                return(GetCompilationFailedResult(file, results.ParserErrors));
            }

            return(_compilationService.Compile(file, results.GeneratedCode));
        }
示例#2
0
        /// <inheritdoc />
        public CompilationResult Compile([NotNull] RelativeFileInfo file)
        {
            GeneratorResults results;

            using (var inputStream = file.FileInfo.CreateReadStream())
            {
                results = _razorHost.GenerateCode(
                    file.RelativePath, inputStream);
            }

            if (!results.Success)
            {
                var messages = results.ParserErrors.Select(e => new CompilationMessage(e.Message));
                return(CompilationResult.Failed(file.FileInfo, results.GeneratedCode, messages));
            }

            return(_compilationService.Compile(file.FileInfo, results.GeneratedCode));
        }
示例#3
0
        internal CompilationResult CompileCore(IFileInfo file)
        {
            GeneratorResults results;

            using (var inputStream = file.CreateReadStream())
            {
                Contract.Assert(file.PhysicalPath.StartsWith(_appRoot, StringComparison.OrdinalIgnoreCase));
                var rootRelativePath = file.PhysicalPath.Substring(_appRoot.Length);
                results = _razorHost.GenerateCode(rootRelativePath, inputStream);
            }

            if (!results.Success)
            {
                var messages = results.ParserErrors.Select(e => new CompilationMessage(e.Message));
                return(CompilationResult.Failed(file, results.GeneratedCode, messages));
            }

            return(_baseCompilationService.Compile(file, results.GeneratedCode));
        }
示例#4
0
        protected virtual RazorFileInfo ParseView([NotNull] RelativeFileInfo fileInfo,
                                                  [NotNull] IBeforeCompileContext context,
                                                  [NotNull] CSharpParseOptions options)
        {
            using (var stream = fileInfo.FileInfo.CreateReadStream())
            {
                var results = _host.GenerateCode(fileInfo.RelativePath, stream);

                foreach (var parserError in results.ParserErrors)
                {
                    var diagnostic = parserError.ToDiagnostics(fileInfo.FileInfo.PhysicalPath);
                    context.Diagnostics.Add(diagnostic);
                }

                var generatedCode = results.GeneratedCode;

                if (generatedCode != null)
                {
                    var syntaxTree   = SyntaxTreeGenerator.Generate(generatedCode, fileInfo.FileInfo.PhysicalPath, options);
                    var fullTypeName = results.GetMainClassName(_host, syntaxTree);

                    if (fullTypeName != null)
                    {
                        context.CSharpCompilation = context.CSharpCompilation.AddSyntaxTrees(syntaxTree);

                        var hash = RazorFileHash.GetHash(fileInfo.FileInfo);

                        return(new RazorFileInfo()
                        {
                            FullTypeName = fullTypeName,
                            RelativePath = fileInfo.RelativePath,
                            LastModified = fileInfo.FileInfo.LastModified,
                            Length = fileInfo.FileInfo.Length,
                            Hash = hash,
                        });
                    }
                }
            }

            return(null);
        }
示例#5
0
        /// <inheritdoc />
        public CompilationResult Compile([NotNull] RelativeFileInfo file)
        {
            GeneratorResults results;

            using (var inputStream = file.FileInfo.CreateReadStream())
            {
                results = _razorHost.GenerateCode(
                    file.RelativePath, inputStream);
            }

            if (!results.Success)
            {
                var messages = results.ParserErrors
                               .Select(parseError => new RazorCompilationMessage(parseError, file.RelativePath));
                var failure = new RazorCompilationFailure(
                    file.RelativePath,
                    ReadFileContentsSafely(file.FileInfo),
                    messages);

                return(CompilationResult.Failed(failure));
            }

            return(_compilationService.Compile(file, results.GeneratedCode));
        }
示例#6
0
 /// <summary>
 /// Generate code for the Razor file at <paramref name="relativePath"/> with content
 /// <paramref name="inputStream"/>.
 /// </summary>
 /// <param name="relativePath">
 /// The path of the Razor file relative to the root of the application. Used to generate line pragmas and
 /// calculate the class name of the generated type.
 /// </param>
 /// <param name="inputStream">A <see cref="Stream"/> that contains the Razor content.</param>
 /// <returns>A <see cref="GeneratorResults"/> instance containing results of code generation.</returns>
 protected virtual GeneratorResults GenerateCode(string relativePath, Stream inputStream)
 {
     return(_razorHost.GenerateCode(relativePath, inputStream));
 }