public static QualityRules Parse(string contents) { QualityRules rules = new QualityRules(); var lines = contents.Replace("\r", "").Split('\n'); foreach (var line in lines) { if (line.StartsWith("* ")) { // Attempt to interpret as a rule string key = line.Substring(2); string value = null; if (key.Contains(":")) { value = key.Substring(key.IndexOf(":") + 1).Trim().ToLower(); key = key.Remove(key.IndexOf(":")); } key = key.Trim().ToLower(); switch (key) { case "line endings": if (value == null) break; if (value == "crlf") rules.LineEndings = LineEndingStyle.CRLF; if (value == "lf") rules.LineEndings = LineEndingStyle.LF; break; case "indentation": if (value == null) break; if (value == "tabs") rules.Indentation = IndentationStyle.Tabs; int spaces; if (value.EndsWith("spaces") && int.TryParse( value.Remove(value.IndexOf(' ')), out spaces)) { rules.Indentation = IndentationStyle.Spaces; rules.NumberOfSpaces = spaces; } break; case "trim trailing lines": rules.TrimTrailingLines = true; break; case "trim trailing whitespace": rules.TrimTrailingWhitespace = true; break; } } } return rules; }
public static QualityRules Parse(string contents) { QualityRules rules = new QualityRules(); var lines = contents.Replace("\r", "").Split('\n'); foreach (var line in lines) { if (line.StartsWith("* ")) { // Attempt to interpret as a rule string key = line.Substring(2); string value = null; if (key.Contains(":")) { value = key.Substring(key.IndexOf(":") + 1).Trim().ToLower(); key = key.Remove(key.IndexOf(":")); } key = key.Trim().ToLower(); switch (key) { case "line endings": if (value == null) { break; } if (value == "crlf") { rules.LineEndings = LineEndingStyle.CRLF; } if (value == "lf") { rules.LineEndings = LineEndingStyle.LF; } break; case "indentation": if (value == null) { break; } if (value == "tabs") { rules.Indentation = IndentationStyle.Tabs; } int spaces; if (value.EndsWith("spaces") && int.TryParse( value.Remove(value.IndexOf(' ')), out spaces)) { rules.Indentation = IndentationStyle.Spaces; rules.NumberOfSpaces = spaces; } break; case "trim trailing lines": rules.TrimTrailingLines = true; break; case "trim trailing whitespace": rules.TrimTrailingWhitespace = true; break; } } } return(rules); }
public static QualityRules FromFile(string file) { var text = File.ReadAllText(file); return(QualityRules.Parse(text)); }
public static void Main(string[] args) { string directory = null; string analysis = null; string summary = null; string rules = null; for (int i = 0; i < args.Length; i++) { string arg = args[i]; if (arg.StartsWith("-")) { switch (arg) { case "--analysis": analysis = args[++i]; break; case "--summary": summary = args[++i]; break; case "--rules": rules = args[++i]; break; default: Console.WriteLine("Invalid parameters. Use QualityEnforcer.exe --help for more information."); return; } } else { if (directory == null) { directory = arg; } else { Console.WriteLine("Invalid parameters. Use QualityEnforcer.exe --help for more information."); return; } } } if (directory == null) { Console.WriteLine("Invalid parameters. Use QualityEnforcer.exe --help for more information."); return; } var project = Enforcer.AnalyzeDirectory(directory); if (analysis != null) { File.WriteAllText(analysis, GenerateAnalysis(project)); return; } else { // Enforce style QualityRules qualityRules = new QualityRules(); if (rules != null) { qualityRules = QualityRules.FromFile(rules); } else if (File.Exists(Path.Combine(directory, "CONTRIBUTING.md"))) { qualityRules = QualityRules.FromFile(Path.Combine(directory, "CONTRIBUTING.md")); } var changes = Enforcer.EnforceQuality(project, qualityRules); if (summary != null) { File.WriteAllText(summary, GenerateSummary(project, changes)); } } }
public static ChangeSummary EnforceQuality(Project project, QualityRules rules) { ChangeSummary summary = new ChangeSummary(); string indent, lineEnding; // Set up indent and line ending stles if (rules.Indentation == IndentationStyle.Detect) { if (project.Indentation == IndentationStyle.Spaces) { indent = ""; int spaces = project.NumberOfSpaces; if (rules.NumberOfSpaces != null) { spaces = rules.NumberOfSpaces.Value; } for (int i = 0; i < spaces; i++) { indent += " "; } } else { indent = "\t"; } } else if (rules.Indentation == IndentationStyle.Spaces) { indent = ""; for (int i = 0; i < rules.NumberOfSpaces; i++) { indent += " "; } project.Indentation = IndentationStyle.Spaces; } else { indent = "\t"; project.Indentation = IndentationStyle.Tabs; } if (rules.LineEndings == LineEndingStyle.Detect) { if (project.LineEndings == LineEndingStyle.CRLF) { lineEnding = "\r\n"; } else { lineEnding = "\n"; } } else if (rules.LineEndings == LineEndingStyle.CRLF) { lineEnding = "\r\n"; project.LineEndings = LineEndingStyle.CRLF; } else { lineEnding = "\n"; project.LineEndings = LineEndingStyle.LF; } // Apply changes foreach (var file in project.CodeFiles) { var map = FileMap.GenerateMap(file); // Detect changes if (map.LFUsed != map.CRLFUsed && summary.LineEndingStyleChange == LineEndingStyleChange.None) { if (project.LineEndings == LineEndingStyle.CRLF) { summary.LineEndingStyleChange = LineEndingStyleChange.ToCRLF; } else { summary.LineEndingStyleChange = LineEndingStyleChange.ToLF; } } if (map.TabsUsed != map.SpacesUsed && summary.IndentationStyleChange == IndentationStyleChange.None) { if (project.Indentation == IndentationStyle.Spaces) { summary.IndentationStyleChange = IndentationStyleChange.ToSpaces; } else { summary.IndentationStyleChange = IndentationStyleChange.ToTabs; } } // Create new file based on old File.Delete(file); var writer = new StreamWriter(file); int end = map.Lines.Length; if (rules.TrimTrailingLines) { if (map.TrailingLines != 0) { summary.TrimTrailingLines = true; end -= map.TrailingLines; } } for (int i = 0; i < end; i++) { // Reconstruct file var line = map.Lines[i]; if (rules.TrimTrailingWhitespace) { if (summary.TrimTrailingWhitespace == false) { summary.TrimTrailingWhitespace = line.EndsWith(" ") || line.EndsWith("\t"); } line = line.TrimEnd(' ', '\t'); } line = line.TrimStart(' ', '\t'); for (int j = 0; j < map.Indentation[i]; j++) { line = indent + line; } if (i != end - 1) { writer.Write(line + lineEnding); } else { writer.Write(line); } } writer.Close(); } return(summary); }