Ejemplo n.º 1
0
        public override async Task <ComponentBundleInfo> Build()
        {
            OnBundlingStart();
            BuildStopWatch.Reset();
            BuildStopWatch.Start();
            var bundleInfo = new ComponentBundleInfo();

            bundleInfo.PathToBundledAssembly = Settings.AssemblyPath;

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

            try
            {
                // get files in project directory filtered by ".razor.css"
                foreach (FileInfo cssFile in new DirectoryInfo(Settings.ProjectDirectory).GetFiles(Settings.CssRazorSearchPattern, SearchOption.AllDirectories))
                {
                    var stylesheet = CssParser.Parse(File.ReadAllText(cssFile.FullName));
                    var children   = (List <IStylesheetNode>)stylesheet.Children;
                    // if contains zero styles
                    if (children.Count == 0)
                    {
                        continue;
                    }

                    string relativeCssPath = Path.GetRelativePath(Settings.ProjectDirectory, cssFile.FullName);
                    string cssFileName     = Path.GetFileNameWithoutExtension(relativeCssPath);

                    RazorDirectory     razorDir = RazorDirectory.Razor;
                    RazorFileExtension razorExt = RazorFileExtension.GenCsharp;

                    RazorFile razorFile  = _razorEngine.FindFile(Settings.ProjectName, cssFileName, razorDir, razorExt);
                    string    @namespace = _razorEngine.GetNamespaceFromFile(Settings.ProjectName, cssFileName, razorDir, razorExt);

                    foreach (IStyleRule rule in children)
                    {
                        if (rule.Type != RuleType.Style)
                        {
                            continue;
                        }

                        if (rule.SelectorText == null)
                        {
                            TextRange range = rule.Owner.StylesheetText.Range;
                            throw new InvalidCssSyntaxException(cssFile.FullName, range.End.Line, range.End.Column);
                        }

                        if (razorFile != null)
                        {
                            rule.SelectorText = BundleHelper.BuildCssClasses(@namespace, cssFileName.Replace(".razor", ""), rule.SelectorText);
                        }
                    }
                    BundleHelper.AppendStylesToFile(_fileManager.TempStylesFilePath, stylesheet);
                    bundleInfo.CombinedFilesCount++;
                }
                BundleHelper.AddStylesToAssembly(Settings.AssemblyPath, File.ReadAllText(_fileManager.TempStylesFilePath));
                bundleInfo.Succeed = true;
            }
            catch (IOException ex)
            {
                bundleInfo.Errors.Add(new Error("IOException", ex.Message));
            }
            catch (InvalidCssSyntaxException ex)
            {
                bundleInfo.Errors.Add(new Error("CssSyntax", string.Format("invalid css syntax in file by path: {0};\nLine: {1}, Column: {2}", ex.FilePath, ex.Line, ex.Column)));
            }
            BuildStopWatch.Stop();
            bundleInfo.BundlingTime = BuildStopWatch.Elapsed;

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

            return(bundleInfo);
        }
Ejemplo n.º 2
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);
        }