Ejemplo n.º 1
0
        public static string CreateChangeList(ChangeSummary changes)
        {
            var changeList = "";

            if (changes.IndentationStyleChange == IndentationStyleChange.ToSpaces)
            {
                changeList += "* Converted tabs to spaces" + Environment.NewLine;
            }
            if (changes.IndentationStyleChange == IndentationStyleChange.ToTabs)
            {
                changeList += "* Converted spaces to tabs" + Environment.NewLine;
            }
            if (changes.LineEndingStyleChange == LineEndingStyleChange.ToCRLF)
            {
                changeList += "* Converted LF to CRLF" + Environment.NewLine;
            }
            if (changes.LineEndingStyleChange == LineEndingStyleChange.ToLF)
            {
                changeList += "* Converted CRLF to LF" + Environment.NewLine;
            }
            if (changes.TrimTrailingLines)
            {
                changeList += "* Removed trailing newlines" + Environment.NewLine;
            }
            if (changes.TrimTrailingWhitespace)
            {
                changeList += "* Removed trailing whitespace" + Environment.NewLine;
            }
            return(changeList);
        }
Ejemplo n.º 2
0
        public static string GenerateSummary(Project project, ChangeSummary changes)
        {
            var reader = new StreamReader(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("QualityEnforcer.SummaryTemplate.txt"));
            var template = reader.ReadToEnd();

            reader.Close();

            var changeList = CreateChangeList(changes);

            template = template.Replace("{changes}", changeList);

            return(template);
        }
Ejemplo n.º 3
0
        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);
        }