From a3d40fdc2b45ed2c7e631a26edd725ae5497e7e5 Mon Sep 17 00:00:00 2001 From: chylex <contact@chylex.com> Date: Sun, 15 Apr 2018 18:08:03 +0200 Subject: [PATCH] Push a quick utility to detect unused selectors and classes --- Resources/Utilities/CompareStylesheets.cs | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Resources/Utilities/CompareStylesheets.cs diff --git a/Resources/Utilities/CompareStylesheets.cs b/Resources/Utilities/CompareStylesheets.cs new file mode 100644 index 00000000..8dbd9454 --- /dev/null +++ b/Resources/Utilities/CompareStylesheets.cs @@ -0,0 +1,26 @@ +using System.Text.RegularExpressions; + +HashSet<string> ReadSelectors(string file){ + return new HashSet<string>( + File.ReadAllLines(file) + .Where(line => line.Contains('{')) + .Select(line => line.Substring(0, line.IndexOf('{')).Trim()) + .SelectMany(lines => lines.Split(new char[]{ ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)) + ); +} + +HashSet<string> ExtractClasses(HashSet<string> selectors){ + return new HashSet<string>( + selectors.SelectMany(selector => Regex.Matches(selector, @"\.[a-zA-Z0-9_-]+").Cast<Match>().Select(match => match.Value)) + ); +} + +void PrintAll(IEnumerable<string> data){ + foreach(string line in data){ + Print(line); + } +} + +void PrintMissing(HashSet<string> all, HashSet<string> subset){ + PrintAll(subset.Where(ele => !all.Contains(ele))); +}