Exemple #1
0
        private static Type GetAppComponentType(CustomAssemblyLoader assemblyLoader, string typeNameOfRootComponent, Assembly appAssembly)
        {
            var rootComponentAssembly = appAssembly;

            var rootComponentTypeNameParts = typeNameOfRootComponent.Split(',');
            var rootComponentTypeName      = rootComponentTypeNameParts[0].Trim();
            var rootComponentAssemblyName  = rootComponentTypeNameParts.Length > 1 ? rootComponentTypeNameParts[1].Trim() : "";

            if (rootComponentAssemblyName != "")
            {
                rootComponentAssembly = assemblyLoader.LoadAssembly(rootComponentAssemblyName);
                if (rootComponentAssembly == null)
                {
                    throw new ArgumentException($"The assembly that has component type \"{typeNameOfRootComponent}\" colud not load.");
                }
            }

            var appComponentType = rootComponentAssembly.GetType(rootComponentTypeName);

            if (appComponentType == null)
            {
                var assemblies = appAssembly.GetReferencedAssemblies()
                                 .Where(asmname => !string.IsNullOrEmpty(asmname.Name))
                                 .Where(asmname => !asmname.Name !.StartsWith("Microsoft."))
                                 .Where(asmname => !asmname.Name !.StartsWith("System."))
                                 .Select(asmname => assemblyLoader.LoadAssembly(asmname.Name !))
                                 .Where(asm => asm != null)
                                 .Prepend(appAssembly) as IEnumerable <Assembly>;

                appComponentType = assemblies
                                   .SelectMany(asm => asm.GetTypes())
                                   .Where(t => t.Name == "App")
                                   .Where(t => t.IsSubclassOf(typeof(ComponentBase)))
                                   .FirstOrDefault();
            }

            if (appComponentType == null)
            {
                throw new ArgumentException($"The component type \"{typeNameOfRootComponent}\" was not found.");
            }
            return(appComponentType);
        }
Exemple #2
0
        internal static BlazorWasmPrerenderingOptions BuildPrerenderingOptions(CustomAssemblyLoader assemblyLoader, CommandLineOptions commandLineOptions)
        {
            if (string.IsNullOrEmpty(commandLineOptions.IntermediateDir))
            {
                throw new ArgumentException("The -i|--intermediatedir parameter is required.");
            }
            if (string.IsNullOrEmpty(commandLineOptions.PublishedDir))
            {
                throw new ArgumentException("The -p|--publisheddir parameter is required.");
            }
            if (string.IsNullOrEmpty(commandLineOptions.AssemblyName))
            {
                throw new ArgumentException("The -a|--assemblyname parameter is required.");
            }
            if (string.IsNullOrEmpty(commandLineOptions.TypeNameOfRootComponent))
            {
                throw new ArgumentException("The -t|--typenameofrootcomponent parameter is required.");
            }
            if (string.IsNullOrEmpty(commandLineOptions.SelectorOfRootComponent))
            {
                throw new ArgumentException("The --selectorofrootcomponent parameter is required.");
            }
#if ENABLE_HEADOUTLET
            if (string.IsNullOrEmpty(commandLineOptions.SelectorOfHeadOutletComponent))
            {
                throw new ArgumentException("The --selectorofheadoutletcomponent parameter is required.");
            }
#endif
            if (string.IsNullOrEmpty(commandLineOptions.FrameworkName))
            {
                throw new ArgumentException("The -f|--frameworkname parameter is required.");
            }
            if (commandLineOptions.RenderMode != RenderMode.Static && commandLineOptions.RenderMode != RenderMode.WebAssemblyPrerendered)
            {
                throw new ArgumentException($"The -r|--rendermode parameter value \"{commandLineOptions.RenderMode}\" is not supported. (Only \"Static\" and \"WebAssemblyPrerendered\" are supported.)");
            }

            var webRootPath  = Path.Combine(commandLineOptions.PublishedDir, "wwwroot");
            var frameworkDir = Path.Combine(webRootPath, "_framework");

            var middlewarePackages = MiddlewarePackageReference.Build(folderToScan: frameworkDir, commandLineOptions.MiddlewarePackages);
            var middlewareDllsDir  = PrepareMiddlewareDlls(middlewarePackages, commandLineOptions.IntermediateDir, commandLineOptions.FrameworkName);
            SetupCustomAssemblyLoader(assemblyLoader, webRootPath, middlewareDllsDir);

            var appAssembly = assemblyLoader.LoadAssembly(commandLineOptions.AssemblyName);
            if (appAssembly == null)
            {
                throw new ArgumentException($"The application assembly \"{commandLineOptions.AssemblyName}\" colud not load.");
            }
            var appComponentType = GetAppComponentType(assemblyLoader, commandLineOptions.TypeNameOfRootComponent, appAssembly);

            var indexHtmlPath           = Path.Combine(webRootPath, "index.html");
            var enableGZipCompression   = File.Exists(indexHtmlPath + ".gz");
            var enableBrotliCompression = File.Exists(indexHtmlPath + ".br");

            var htmlFragment = IndexHtmlFragments.Load(indexHtmlPath, commandLineOptions.SelectorOfRootComponent, commandLineOptions.SelectorOfHeadOutletComponent, commandLineOptions.DeleteLoadingContents);
            var options      = new BlazorWasmPrerenderingOptions
            {
                WebRootPath         = webRootPath,
                ApplicationAssembly = appAssembly,

                RootComponentType = appComponentType,
#if ENABLE_HEADOUTLET
                HeadOutletComponentType = typeof(Microsoft.AspNetCore.Components.Web.HeadOutlet),
#endif
                RenderMode            = commandLineOptions.RenderMode,
                IndexHtmlFragments    = htmlFragment,
                DeleteLoadingContents = commandLineOptions.DeleteLoadingContents,

                EnableGZipCompression   = enableGZipCompression,
                EnableBrotliCompression = enableBrotliCompression,
                MiddlewarePackages      = middlewarePackages
            };
            return(options);
        }