Exemple #1
0
        public override async Task <ApplicationBundleInfo> Build()
        {
            OnBundlingStart();
            BuildStopWatch.Reset();
            BuildStopWatch.Start();
            var bundleInfo = new ApplicationBundleInfo();

            bundleInfo.OutputFilePath = Path.Combine(Settings.OutputCssDirectory, "bundle.css");

            if (_fileManager.TempCssFileExists())
            {
                await _fileManager.ClearTmpCssFileAsync();
            }
            else
            {
                await _fileManager.CreateTmpCssFileAsync();
            }

            ProjectAnalyzingResult projAnalyzeRes = _projAnalyzer.Analyze(Settings.ProjectFilePath);

            foreach (var pckgRef in projAnalyzeRes.PackageReferences.Where(x => !x.Name.StartsWith("System") && !x.Name.StartsWith("Microsoft")))
            {
                /* Extract styles from assembly */
                string mainAsmPath    = NugetHelper.MakeAssemblyPath(pckgRef.Name, pckgRef.Version, "netstandard2.0");
                var    mainStylesheet = CssParser.Parse(BundleHelper.GetStylesFromAssembly(mainAsmPath));
                if (BundleHelper.HasIsolatedCss(mainAsmPath))
                {
                    BundleHelper.AppendStylesToFile(_fileManager.TempStylesFilePath, mainStylesheet);
                }

                /* Extract styles from assembly references */
                AssemblyAnalyzingResult asmAnalyzeRes = _asmAnalyzer.Analyze(mainAsmPath);
                foreach (string asmPath in asmAnalyzeRes.AssemblyPaths)
                {
                    if (BundleHelper.HasIsolatedCss(asmPath))
                    {
                        var stylesheet = CssParser.Parse(BundleHelper.GetStylesFromAssembly(asmPath));
                        BundleHelper.AppendStylesToFile(_fileManager.TempStylesFilePath, stylesheet);
                    }
                }
            }
            foreach (var @ref in projAnalyzeRes.References)
            {
                string mainAsmPath = @ref.HintPath;
                if (@ref.HintPath.StartsWith("..\\"))
                {
                    mainAsmPath = Path.Combine(Settings.ProjectDirectory, mainAsmPath);
                }

                /* Extract styles from assembly */
                if (BundleHelper.HasIsolatedCss(mainAsmPath))
                {
                    var mainStylesheet = CssParser.Parse(BundleHelper.GetStylesFromAssembly(mainAsmPath));
                    BundleHelper.AppendStylesToFile(_fileManager.TempStylesFilePath, mainStylesheet);
                }

                /* Extract styles from assembly references */
                AssemblyAnalyzingResult asmAnalyzeRes = _asmAnalyzer.Analyze(mainAsmPath);
                foreach (string asmPath in asmAnalyzeRes.AssemblyPaths)
                {
                    Stylesheet stylesheet = CssParser.Parse(BundleHelper.GetStylesFromAssembly(asmPath));
                    if (BundleHelper.HasIsolatedCss(asmPath))
                    {
                        BundleHelper.AppendStylesToFile(_fileManager.TempStylesFilePath, stylesheet);
                    }
                }
            }

            string pagesDirPath  = Path.Combine(Settings.ProjectDirectory, "Pages");
            string sharedDirPath = Path.Combine(Settings.ProjectDirectory, "Shared");

            BuildByDirectory(pagesDirPath, bundleInfo);
            BuildByDirectory(sharedDirPath, bundleInfo);

            File.WriteAllText(bundleInfo.OutputFilePath, File.ReadAllText(_fileManager.TempStylesFilePath)); // generating css bundle
            BuildStopWatch.Stop();
            bundleInfo.BundlingTime = BuildStopWatch.Elapsed;

            if (bundleInfo.Succeed)
            {
                OnBundlingEnd(bundleInfo);
            }
            else
            {
                OnBundlingError(bundleInfo);
            }

            return(bundleInfo);
        }