Ejemplo n.º 1
0
        private static TestRazorViewCompiler GetViewCompiler(
            TestFileProvider fileProvider                   = null,
            RazorReferenceManager referenceManager          = null,
            IList <CompiledViewDescriptor> precompiledViews = null,
            CSharpCompiler csharpCompiler                   = null)
        {
            fileProvider = fileProvider ?? new TestFileProvider();
            var options = Options.Create(new MvcRazorRuntimeCompilationOptions
            {
                FileProviders = { fileProvider }
            });
            var compilationFileProvider = new RuntimeCompilationFileProvider(options);


            referenceManager = referenceManager ?? CreateReferenceManager();
            precompiledViews = precompiledViews ?? Array.Empty <CompiledViewDescriptor>();

            var hostingEnvironment = Mock.Of <IWebHostEnvironment>(e => e.ContentRootPath == "BasePath");
            var fileSystem         = new FileProviderRazorProjectFileSystem(compilationFileProvider, hostingEnvironment);
            var projectEngine      = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem, builder =>
            {
                RazorExtensions.Register(builder);
            });

            csharpCompiler = csharpCompiler ?? new CSharpCompiler(referenceManager, hostingEnvironment);

            return(new TestRazorViewCompiler(
                       fileProvider,
                       projectEngine,
                       csharpCompiler,
                       precompiledViews));
        }
Ejemplo n.º 2
0
        public FileProviderRazorProjectFileSystem(RuntimeCompilationFileProvider fileProvider, IWebHostEnvironment hostingEnvironment)
        {
            if (fileProvider == null)
            {
                throw new ArgumentNullException(nameof(fileProvider));
            }

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

            _fileProvider       = fileProvider;
            _hostingEnvironment = hostingEnvironment;
        }
        private static FileProviderRazorProjectFileSystem GetRazorProjectFileSystem(
            TestFileProvider fileProvider,
            string contentRootPath = "BasePath")
        {
            var options = Options.Create(new MvcRazorRuntimeCompilationOptions
            {
                FileProviders = { fileProvider }
            });
            var compilationFileProvider = new RuntimeCompilationFileProvider(options);
            var fileSystem = new FileProviderRazorProjectFileSystem(
                compilationFileProvider,
                Mock.Of <IWebHostEnvironment>(e => e.ContentRootPath == contentRootPath));

            return(fileSystem);
        }
        public RuntimeViewCompilerProvider(
            ApplicationPartManager applicationPartManager,
            RazorProjectEngine razorProjectEngine,
            RuntimeCompilationFileProvider fileProvider,
            CSharpCompiler csharpCompiler,
            ILoggerFactory loggerFactory)
        {
            _applicationPartManager = applicationPartManager;
            _razorProjectEngine     = razorProjectEngine;
            _csharpCompiler         = csharpCompiler;
            _fileProvider           = fileProvider;

            _logger         = loggerFactory.CreateLogger <RuntimeViewCompiler>();
            _createCompiler = CreateCompiler;
        }
        public PageActionDescriptorChangeProvider(
            RazorProjectEngine projectEngine,
            RuntimeCompilationFileProvider fileProvider,
            IOptions <RazorPagesOptions> razorPagesOptions)
        {
            if (projectEngine == null)
            {
                throw new ArgumentNullException(nameof(projectEngine));
            }

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

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

            _fileProvider = fileProvider;

            var rootDirectory = razorPagesOptions.Value.RootDirectory;

            Debug.Assert(!string.IsNullOrEmpty(rootDirectory));
            rootDirectory = rootDirectory.TrimEnd('/');

            // Search pattern that matches all cshtml files under the Pages RootDirectory
            var pagesRootSearchPattern = rootDirectory + "/**/*.cshtml";

            // Search pattern that matches all cshtml files under the Pages AreaRootDirectory
            var areaRootSearchPattern = "/Areas/**/*.cshtml";

            _searchPatterns = new[]
            {
                pagesRootSearchPattern,
                areaRootSearchPattern
            };

            // pagesRootSearchPattern will miss _ViewImports outside the RootDirectory despite these influencing
            // compilation. e.g. when RootDirectory = /Dir1/Dir2, the search pattern will ignore changes to
            // [/_ViewImports.cshtml, /Dir1/_ViewImports.cshtml]. We need to additionally account for these.
            var importFeatures  = projectEngine.ProjectFeatures.OfType <IImportProjectFeature>().ToArray();
            var fileAtPagesRoot = projectEngine.FileSystem.GetItem(rootDirectory + "/Index.cshtml", fileKind: null);

            _additionalFilesToTrack = GetImports(importFeatures, fileAtPagesRoot);
        }
Ejemplo n.º 6
0
        public void GetFileProvider_ThrowsIfNoConfiguredFileProviders()
        {
            // Arrange
            var expected =
                $"'{typeof(MvcRazorRuntimeCompilationOptions).FullName}.{nameof(MvcRazorRuntimeCompilationOptions.FileProviders)}' must " +
                $"not be empty. At least one '{typeof(IFileProvider).FullName}' is required to locate a view for " +
                "rendering.";
            var options = Options.Create(new MvcRazorRuntimeCompilationOptions());

            var fileProvider = new RuntimeCompilationFileProvider(options);

            // Act & Assert
            var exception = Assert.Throws <InvalidOperationException>(
                () => fileProvider.FileProvider);

            Assert.Equal(expected, exception.Message);
        }