Example #1
0
        private static CompilationSettings ToCompilationSettings(CommonCompilerOptions compilerOptions,
                                                                 NuGetFramework targetFramework,
                                                                 string projectDirectory)
        {
            var options = GetCompilationOptions(compilerOptions, projectDirectory);

            options = options.WithSpecificDiagnosticOptions(compilerOptions.SuppressWarnings.ToDictionary(
                                                                suppress => suppress, _ => ReportDiagnostic.Suppress));

            AssemblyIdentityComparer assemblyIdentityComparer =
                targetFramework.IsDesktop() ?
                DesktopAssemblyIdentityComparer.Default :
                null;

            options = options.WithAssemblyIdentityComparer(assemblyIdentityComparer);

            LanguageVersion languageVersion;

            if (!Enum.TryParse <LanguageVersion>(value: compilerOptions.LanguageVersion,
                                                 ignoreCase: true,
                                                 result: out languageVersion))
            {
                languageVersion = LanguageVersion.CSharp6;
            }

            var settings = new CompilationSettings
            {
                LanguageVersion    = languageVersion,
                Defines            = compilerOptions.Defines ?? Enumerable.Empty <string>(),
                CompilationOptions = options
            };

            return(settings);
        }
Example #2
0
        public RazorPreCompiler(
            BeforeCompileContext compileContext,
            IFileProvider fileProvider,
            IMemoryCache precompilationCache)
        {
            if (compileContext == null)
            {
                throw new ArgumentNullException(nameof(compileContext));
            }

            if (fileProvider == null)
            {
                throw new ArgumentNullException(nameof(fileProvider));
            }

            if (precompilationCache == null)
            {
                throw new ArgumentNullException(nameof(precompilationCache));
            }

            CompileContext = compileContext;
            FileProvider   = fileProvider;
            // There should always be a syntax tree even if there are no files (we generate one)
            Debug.Assert(compileContext.Compilation.SyntaxTrees.Length > 0);
            var defines = compileContext.Compilation.SyntaxTrees[0].Options.PreprocessorSymbolNames;

            CompilationSettings = new CompilationSettings
            {
                CompilationOptions = compileContext.Compilation.Options,
                Defines            = defines,
                LanguageVersion    = compileContext.Compilation.LanguageVersion
            };
            PreCompilationCache   = precompilationCache;
            TagHelperTypeResolver = new PrecompilationTagHelperTypeResolver(CompileContext.Compilation);
        }
Example #3
0
        public static SyntaxTree Generate(
            string text,
            string path,
            CompilationSettings compilationSettings)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (compilationSettings == null)
            {
                throw new ArgumentNullException(nameof(compilationSettings));
            }

            var sourceText = SourceText.From(text, Encoding.UTF8);
            var syntaxTree = CSharpSyntaxTree.ParseText(sourceText,
                                                        path: path,
                                                        options: GetParseOptions(compilationSettings));

            return(syntaxTree);
        }
Example #4
0
 public RazorPreCompiler([NotNull] IServiceProvider designTimeServiceProvider,
                         [NotNull] IMemoryCache precompilationCache,
                         [NotNull] CompilationSettings compilationSettings) :
     this(designTimeServiceProvider,
          designTimeServiceProvider.GetRequiredService <IOptions <RazorViewEngineOptions> >(),
          precompilationCache,
          compilationSettings)
 {
 }
Example #5
0
 public RazorPreCompiler([NotNull] IServiceProvider designTimeServiceProvider,
                         [NotNull] IOptions <RazorViewEngineOptions> optionsAccessor,
                         [NotNull] IMemoryCache precompilationCache,
                         [NotNull] CompilationSettings compilationSettings)
 {
     _serviceProvider    = designTimeServiceProvider;
     _fileProvider       = optionsAccessor.Options.FileProvider;
     CompilationSettings = compilationSettings;
     PreCompilationCache = precompilationCache;
 }
Example #6
0
        public static SyntaxTree Generate([NotNull] string text,
                                          [NotNull] string path,
                                          [NotNull] CompilationSettings compilationSettings)
        {
            var sourceText = SourceText.From(text, Encoding.UTF8);
            var syntaxTree = CSharpSyntaxTree.ParseText(sourceText,
                                                        path: path,
                                                        options: GetParseOptions(compilationSettings));

            return(syntaxTree);
        }
Example #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CompilerContext"/> class with a parent context
 /// </summary>
 /// <param name="view">The type for the context</param>
 /// <param name="sourceData">The source expression for the type</param>
 /// <param name="compilerSettings">The compilation settings</param>
 /// <param name="partialLoader">A loader for partial templates</param>
 /// <param name="settings">The compilation settings for the compiler</param>
 /// <param name="parentContext">The parent context for the new context</param>
 public CompilerContext(Type view, Expression sourceData, CompilerSettings compilerSettings, IStubbleLoader partialLoader, CompilationSettings settings, CompilerContext parentContext)
     : base(partialLoader, parentContext)
 {
     CompilerSettings    = compilerSettings;
     CompilationSettings = settings;
     View       = view;
     SourceData = sourceData;
     cache      = new Dictionary <string, Expression>()
     {
         { ".", sourceData }
     };
 }
        public static SyntaxTree Generate(
            string text,
            string path,
            CompilationSettings compilationSettings)
        {
            if (compilationSettings == null)
            {
                throw new ArgumentNullException(nameof(compilationSettings));
            }

            return(Generate(text, path, GetParseOptions(compilationSettings)));
        }
Example #9
0
 public RazorPreCompiler(
     [NotNull] BeforeCompileContext compileContext,
     [NotNull] IAssemblyLoadContextAccessor loadContextAccessor,
     [NotNull] IFileProvider fileProvider,
     [NotNull] IMemoryCache precompilationCache,
     [NotNull] CompilationSettings compilationSettings)
 {
     CompileContext        = compileContext;
     LoadContext           = loadContextAccessor.GetLoadContext(GetType().GetTypeInfo().Assembly);
     FileProvider          = fileProvider;
     CompilationSettings   = compilationSettings;
     PreCompilationCache   = precompilationCache;
     TagHelperTypeResolver = new PrecompilationTagHelperTypeResolver(CompileContext, LoadContext);
 }
Example #10
0
        public async Task CompilationRendererSpecTest_Async(SpecTest data)
        {
            OutputStream.WriteLine(data.Name);
            var settings = CompilationSettings.GetDefaultRenderSettings();

            settings.CultureInfo = data.CultureInfo ?? settings.CultureInfo;

            var stubble = new StubbleCompilationRenderer();
            var output  = await(data.Partials != null ? stubble.CompileAsync(data.Template, data.Data, data.Partials, settings) : stubble.CompileAsync(data.Template, data.Data, settings));

            var outputResult = output(data.Data);

            OutputStream.WriteLine("Expected \"{0}\", Actual \"{1}\"", data.Expected, outputResult);
            Assert.Equal(data.Expected, outputResult);
        }
        public async Task You_Should_Be_Able_To_Compile_With_An_Example_Object_With_Settings()
        {
            var stubble  = new StubbleCompilationRenderer();
            var settings = new CompilationSettings
            {
                ThrowOnDataMiss = true
            };

            var input = new ExampleClass {
                Foo = "Bar"
            };
            var func      = stubble.Compile("{{Foo}}", input, settings);
            var funcAsync = await stubble.CompileAsync("{{Foo}}", input, settings);

            Assert.Equal("Bar", func(input));
            Assert.Equal("Bar", funcAsync(input));
        }
        public async Task You_Should_Be_Able_To_Compile_Without_An_Example_Object_With_Partials_And_Settings()
        {
            var stubble  = new StubbleCompilationRenderer();
            var settings = new CompilationSettings
            {
                ThrowOnDataMiss = true
            };
            var partials = new Dictionary <string, string>
            {
                { "Partial", "{{Foo}}" }
            };

            var input = new ExampleClass {
                Foo = "Bar"
            };
            var func      = stubble.Compile <ExampleClass>("{{> Partial}}", partials, settings);
            var funcAsync = await stubble.CompileAsync <ExampleClass>("{{> Partial}}", partials, settings);

            Assert.Equal("Bar", func(input));
            Assert.Equal("Bar", funcAsync(input));
        }
Example #13
0
        private static CompilationSettings ToCompilationSettings(CommonCompilerOptions compilerOptions,
                                                                 NuGetFramework targetFramework,
                                                                 string projectDirectory)
        {
            var options = GetCompilationOptions(compilerOptions, projectDirectory);

            // Disable 1702 until roslyn turns this off by default
            options = options.WithSpecificDiagnosticOptions(new Dictionary <string, ReportDiagnostic>
            {
                { "CS1701", ReportDiagnostic.Suppress }, // Binding redirects
                { "CS1702", ReportDiagnostic.Suppress },
                { "CS1705", ReportDiagnostic.Suppress }
            });

            AssemblyIdentityComparer assemblyIdentityComparer =
                targetFramework.IsDesktop() ?
                DesktopAssemblyIdentityComparer.Default :
                null;

            options = options.WithAssemblyIdentityComparer(assemblyIdentityComparer);

            LanguageVersion languageVersion;

            if (!Enum.TryParse <LanguageVersion>(value: compilerOptions.LanguageVersion,
                                                 ignoreCase: true,
                                                 result: out languageVersion))
            {
                languageVersion = LanguageVersion.CSharp6;
            }

            var settings = new CompilationSettings
            {
                LanguageVersion    = languageVersion,
                Defines            = compilerOptions.Defines ?? Enumerable.Empty <string>(),
                CompilationOptions = options
            };

            return(settings);
        }
 public RazorFileInfoCollectionGenerator([NotNull] RazorFileInfoCollection fileInfoCollection,
                                         [NotNull] CompilationSettings compilationSettings)
 {
     RazorFileInfoCollection = fileInfoCollection;
     CompilationSettings     = compilationSettings;
 }
Example #15
0
 /// <summary>
 /// Compiles the template to a function and some partials
 /// </summary>
 /// <typeparam name="T">The type of data to accept</typeparam>
 /// <param name="template">The template to compile</param>
 /// <param name="partials">The partials to use when compiling</param>
 /// <param name="settings">The settings to use for the compilation</param>
 /// <returns>The function to render the template with</returns>
 public ValueTask <Func <T, string> > CompileAsync <T>(string template, IDictionary <string, string> partials, CompilationSettings settings)
 {
     return(CompileAsync <T>(template, typeof(T), partials, settings));
 }
Example #16
0
 /// <summary>
 /// Compiles the template to a function accepting a type of data
 /// </summary>
 /// <typeparam name="T">The type of data to accept</typeparam>
 /// <param name="template">The template to compile</param>
 /// <param name="view">A view on the data the compiled template will accept</param>
 /// <param name="settings">The settings to use for the compilation</param>
 /// <returns>The function to render the template with</returns>
 public ValueTask <Func <T, string> > CompileAsync <T>(string template, T view, CompilationSettings settings)
 {
     return(CompileAsync <T>(template, view.GetType(), null, settings));
 }
Example #17
0
 /// <summary>
 /// Compiles the template to a function accepting a type of data and some partials
 /// </summary>
 /// <typeparam name="T">The type of data to accept</typeparam>
 /// <param name="template">The template to compile</param>
 /// <param name="view">A view on the data the compiled template will accept</param>
 /// <param name="partials">The partials to use when compiling</param>
 /// <param name="settings">The settings to use for the compilation</param>
 /// <returns>The function to render the template with</returns>
 public ValueTask <Func <T, string> > CompileAsync <T>(string template, T view, IDictionary <string, string> partials, CompilationSettings settings)
 {
     return(CompileAsync <T>(template, view.GetType(), partials, settings));
 }
Example #18
0
        private async ValueTask <Func <T, string> > CompileAsync <T>(string template, Type viewType, IDictionary <string, string> partials, CompilationSettings settings)
        {
            var loadedTemplate = await CompilerSettings.TemplateLoader.LoadAsync(template);

            if (loadedTemplate == null)
            {
                throw new UnknownTemplateException("No template was found with the name '" + template + "'");
            }

            var document = CompilerSettings.Parser.Parse(loadedTemplate, CompilerSettings.DefaultTags, pipeline: CompilerSettings.ParserPipeline);
            var renderer = new CompilationRenderer <T>(CompilerSettings.RendererPipeline, CompilerSettings.MaxRecursionDepth);

            var compilationContext = BuildCompilerContext(viewType, partials, settings);

            return(await renderer.CompileAsync(document, compilationContext) as Func <T, string>);
        }
Example #19
0
 /// <summary>
 /// Compiles the template to a function accepting a type of data
 /// </summary>
 /// <typeparam name="T">The type of data to accept</typeparam>
 /// <param name="template">The template to compile</param>
 /// <param name="view">A view on the data the compiled template will accept</param>
 /// <param name="settings">The settings to configure the compilation with</param>
 /// <returns>The function to render the template with</returns>
 public Func <T, string> Compile <T>(string template, T view, CompilationSettings settings)
 {
     return(Compile <T>(template, view.GetType(), null, settings));
 }
Example #20
0
 public RazorFileInfoCollectionGenerator([NotNull] IEnumerable <RazorFileInfo> fileInfos,
                                         [NotNull] CompilationSettings compilationSettings)
 {
     FileInfos           = fileInfos;
     CompilationSettings = compilationSettings;
 }
Example #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CompilerContext"/> class.
 /// </summary>
 /// <param name="view">The type for the context</param>
 /// <param name="sourceData">The source expression for the type</param>
 /// <param name="compilerSettings">The compilation settings</param>
 /// <param name="partialLoader">A loader for partial templates</param>
 /// <param name="settings">The compilation settings for the compiler</param>
 public CompilerContext(Type view, Expression sourceData, CompilerSettings compilerSettings, IStubbleLoader partialLoader, CompilationSettings settings)
     : this(view, sourceData, compilerSettings, partialLoader, settings, null)
 {
 }
Example #22
0
 public static CSharpParseOptions GetParseOptions(CompilationSettings compilationSettings)
 {
     return(new CSharpParseOptions(
                languageVersion: compilationSettings.LanguageVersion,
                preprocessorSymbols: compilationSettings.Defines));
 }
Example #23
0
 /// <summary>
 /// Compiles the template to a function
 /// </summary>
 /// <typeparam name="T">The type of data to accept</typeparam>
 /// <param name="template">The template to compile</param>
 /// <param name="settings">The settings to use for the compilation</param>
 /// <returns>The function to render the template with</returns>
 public ValueTask <Func <T, string> > CompileAsync <T>(string template, CompilationSettings settings)
 {
     return(CompileAsync <T>(template, typeof(T), null, settings));
 }
Example #24
0
        private static CompilationSettings ToCompilationSettings(CommonCompilerOptions compilerOptions,
                                                                 NuGetFramework targetFramework,
                                                                 string projectDirectory)
        {
            var options = GetCompilationOptions(compilerOptions, projectDirectory);

            options = options.WithSpecificDiagnosticOptions(compilerOptions.SuppressWarnings.ToDictionary(
                suppress => suppress, _ => ReportDiagnostic.Suppress));

            AssemblyIdentityComparer assemblyIdentityComparer =
                targetFramework.IsDesktop() ?
                DesktopAssemblyIdentityComparer.Default :
                null;

            options = options.WithAssemblyIdentityComparer(assemblyIdentityComparer);

            LanguageVersion languageVersion;
            if (!Enum.TryParse<LanguageVersion>(value: compilerOptions.LanguageVersion,
                                                ignoreCase: true,
                                                result: out languageVersion))
            {
                languageVersion = LanguageVersion.CSharp6;
            }

            var settings = new CompilationSettings
            {
                LanguageVersion = languageVersion,
                Defines = compilerOptions.Defines ?? Enumerable.Empty<string>(),
                CompilationOptions = options
            };

            return settings;
        }
Example #25
0
        private CompilerContext BuildCompilerContext(Type viewType, IDictionary <string, string> partials, CompilationSettings settings)
        {
            var partialsLoader = CompilerSettings.PartialTemplateLoader;

            if (partials != null && partials.Keys.Count > 0)
            {
                partialsLoader = new CompositeLoader(new DictionaryLoader(partials), CompilerSettings.PartialTemplateLoader);
            }

            return(new CompilerContext(viewType, Expression.Parameter(viewType, "src"), CompilerSettings, partialsLoader, settings ?? CompilerSettings.CompilationSettings));
        }
 public LanguageServiceHost(CompilationSettings settings, ILogger logger)
     : this(logger)
 {
     this.settings = settings;
 }
Example #27
0
 /// <summary>
 /// Compiles the template to a function
 /// </summary>
 /// <typeparam name="T">The type of data to accept</typeparam>
 /// <param name="template">The template to compile</param>
 /// <param name="settings">The settings to configure the compilation with</param>
 /// <returns>The function to render the template with</returns>
 public Func <T, string> Compile <T>(string template, CompilationSettings settings)
 {
     return(Compile <T>(template, typeof(T), null, settings) as Func <T, string>);
 }
Example #28
0
        private static CompilationSettings ToCompilationSettings(CommonCompilerOptions compilerOptions,
                                                                 NuGetFramework targetFramework,
                                                                 string projectDirectory)
        {
            var options = GetCompilationOptions(compilerOptions, projectDirectory);

            // Disable 1702 until roslyn turns this off by default
            options = options.WithSpecificDiagnosticOptions(new Dictionary<string, ReportDiagnostic>
            {
                { "CS1701", ReportDiagnostic.Suppress }, // Binding redirects
                { "CS1702", ReportDiagnostic.Suppress },
                { "CS1705", ReportDiagnostic.Suppress }
            });

            AssemblyIdentityComparer assemblyIdentityComparer =
                targetFramework.IsDesktop() ?
                DesktopAssemblyIdentityComparer.Default :
                null;

            options = options.WithAssemblyIdentityComparer(assemblyIdentityComparer);

            LanguageVersion languageVersion;
            if (!Enum.TryParse<LanguageVersion>(value: compilerOptions.LanguageVersion,
                                                ignoreCase: true,
                                                result: out languageVersion))
            {
                languageVersion = LanguageVersion.CSharp6;
            }

            var settings = new CompilationSettings
            {
                LanguageVersion = languageVersion,
                Defines = compilerOptions.Defines ?? Enumerable.Empty<string>(),
                CompilationOptions = options
            };

            return settings;
        }
Example #29
0
 /// <summary>
 /// Compiles the template to a function and some partials
 /// </summary>
 /// <typeparam name="T">The type of data to accept</typeparam>
 /// <param name="template">The template to compile</param>
 /// <param name="partials">The partials to use when compiling</param>
 /// <param name="settings">The settings to configure the compilation with</param>
 /// <returns>The function to render the template with</returns>
 public Func <T, string> Compile <T>(string template, IDictionary <string, string> partials, CompilationSettings settings)
 {
     return(Compile <T>(template, typeof(T), partials, settings) as Func <T, string>);
 }