private void BuildByDirectory(string dirName, ApplicationBundleInfo curBundleInfo) { try { foreach (FileInfo cssFile in new DirectoryInfo(dirName).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); curBundleInfo.CombinedFilesCount++; } curBundleInfo.Succeed = true; } catch (IOException ex) { curBundleInfo.Errors.Add(new Error("IOException", ex.Message)); } catch (InvalidCssSyntaxException ex) { curBundleInfo.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))); } }
public string GetNamespaceFromFile(string projName, string relativeFilePath, RazorDirectory razorDir, RazorFileExtension razorExt) { string projPath = GetProjectPath(projName); string razorDirPath = GetPathByEnum(razorDir); string fullFilePath = Path.Combine(projPath, razorDirPath, relativeFilePath); fullFilePath = fullFilePath + "." + GetExtensionByEnum(razorExt); SyntaxTree tree = CSharpSyntaxTree.ParseText(File.ReadAllText(fullFilePath)); var root = tree.GetCompilationUnitRoot(); var @namespace = (NamespaceDeclarationSyntax)root.Members[0]; return(@namespace.Name.ToString()); }
/// <summary> /// Finding file in directory and return file if found or null if not /// </summary> /// <param name="projName">project path</param> /// <param name="relativeFilePath">relative file path</param> /// <param name="razorDir">razor directory</param> /// <param name="razorExt">razor file extension</param> /// <returns>Found file path</returns> public RazorFile FindFile(string projName, string relativeFilePath, RazorDirectory razorDir, RazorFileExtension razorExt) { string projPath = GetProjectPath(projName); string razorDirPath = GetPathByEnum(razorDir); string fullFilePath = Path.Combine(projPath, razorDirPath, relativeFilePath); string extension = GetExtensionByEnum(razorExt); fullFilePath = fullFilePath + "." + extension; if (File.Exists(fullFilePath)) { return(new RazorFile() { SystemFile = new FileInfo(fullFilePath), FileLocation = razorDir, RazorExtension = razorExt, FullExtension = extension }); } return(null); }
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); }
public static string GetPathByEnum(RazorDirectory razorDir) => razorDir switch {