示例#1
0
        public override bool Equals(object obj)
        {
            NamespaceCollection other = obj as NamespaceCollection;

            return(other != null && _namespaces.SequenceEqual(other._namespaces));
        }
示例#2
0
        public RazorCompiler(CompilationParameters parameters, IExecutionContext context)
        {
            _namespaces = parameters.Namespaces;

            // Calculate the base page type
            Type   basePageType  = parameters.BasePageType ?? typeof(StatiqRazorPage <>);
            string baseClassName = basePageType.FullName;
            int    tickIndex     = baseClassName.IndexOf('`');

            if (tickIndex > 0)
            {
                baseClassName = baseClassName.Substring(0, tickIndex);
            }
            _baseType = basePageType.IsGenericTypeDefinition ? $"{baseClassName}<TModel>" : baseClassName;

            // Create the service collection that MVC needs and add default MVC services
            ServiceCollection serviceCollection = new ServiceCollection();

            // Register some of our own types
            serviceCollection
            .AddSingleton(parameters.FileSystem)
            .AddSingleton <FileSystemFileProvider>()
            .AddSingleton(context.GetRequiredService <ILoggerFactory>())
            .AddSingleton <DiagnosticSource, SilentDiagnosticSource>()
            .AddSingleton <IHostingEnvironment, HostingEnvironment>()
            .AddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>()
            .AddSingleton <IRazorViewEngineFileProviderAccessor, DefaultRazorViewEngineFileProviderAccessor>()
            .AddSingleton <IViewCompilerProvider, StatiqRazorViewCompilerProvider>()
            .AddSingleton <StatiqRazorProjectFileSystem>()
            .AddSingleton <RazorProjectFileSystem, StatiqRazorProjectFileSystem>()
            .AddSingleton(x =>
                          RazorProjectEngine.Create(
                              RazorConfiguration.Default,
                              x.GetRequiredService <RazorProjectFileSystem>(),
                              b =>
            {
                // See MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngineServices(IServiceCollection)
                RazorExtensions.Register(b);
                b.Features.Add(x.GetRequiredService <LazyMetadataReferenceFeature>());            // Lazily calls the MetadataReferenceFeatureProvider
                b.Features.Add(new CompilationTagHelperFeature());
                b.Features.Add(new DefaultTagHelperDescriptorProvider());
                b.Features.Add(new ViewComponentTagHelperDescriptorProvider());

                // We need to register a new document classifier phase because builder.SetBaseType() (which uses builder.ConfigureClass())
                // use the DefaultRazorDocumentClassifierPhase which stops applying document classifier passes after DocumentIntermediateNode.DocumentKind is set
                // (which gets set by the Razor document classifier passes registered in RazorExtensions.Register())
                // Also need to add it just after the DocumentClassifierPhase, otherwise it'll miss the C# lowering phase
                b.Phases.Insert(
                    b.Phases.IndexOf(b.Phases.OfType <IRazorDocumentClassifierPhase>().Last()) + 1,
                    new StatiqDocumentPhase(_baseType, _namespaces));
            }));

            // Register the view location expander
            serviceCollection.Configure <RazorViewEngineOptions>(x => x.ViewLocationExpanders.Add(new ViewLocationExpander()));

            // Add the default services _after_ adding our own
            // (most default registration use .TryAdd...() so they skip already registered types)
            IMvcCoreBuilder builder = serviceCollection
                                      .AddMvcCore()
                                      .AddRazorViewEngine();

            // Get and register MetadataReferences
            builder.PartManager.FeatureProviders.Add(
                new MetadataReferenceFeatureProvider(parameters.DynamicAssemblies));

            IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

            _serviceScopeFactory = serviceProvider.GetRequiredService <IServiceScopeFactory>();
        }
示例#3
0
 public StatiqDocumentPhase(string baseType, NamespaceCollection namespaces)
 {
     _baseType   = baseType;
     _namespaces = namespaces;
 }