Example #1
0
        /// <summary>
        /// Get the lines to write for the given file based on this.NewComments and replace old comments if <paramref name="replaceOldComments"/> == true
        /// </summary>
        /// <param name="path">The file to remove comments from.</param>
        /// <param name="replaceOldComments">Whether or not to replace old comments in the file.</param>
        /// <returns>Returns a list of lines to write.</returns>
        private List <string> DetermineRewriteLines(string path, bool replaceOldComments)
        {
            if (!replaceOldComments && HasHeaderComments(path))
            {
                Debug.WriteLine("File already has comments, ignoring: " + path);
                return(null);
            }

            var linesToKeep = File.ReadAllLines(path).ToList();

            if (replaceOldComments)
            {
                // This should make it remove everything until it runs into something in NonCommentIndicators
                while (linesToKeep.Count > 0)
                {
                    // Trim in case of unusual whitespace style
                    if (NonCommentIndicators.Any(s => linesToKeep[0].Trim().StartsWith(s)))
                    {
                        break;
                    }
                    else
                    {
                        linesToKeep.RemoveAt(0);
                    }
                }
            }

            linesToKeep.InsertRange(0, NewComments);

            return(linesToKeep);
        }
Example #2
0
        /// <summary>
        /// Checks if a given file has comments at the top of a file already.
        /// </summary>
        /// <param name="path">The file to check for header comments.</param>
        /// <returns>Returns true if the file already has comments at the top, false if not.</returns>
        private bool HasHeaderComments(string path)
        {
            var lines = File.ReadAllLines(path);

            foreach (string line in lines)
            {
                if (NonCommentIndicators.Any(s => line.Trim().StartsWith(s)))
                {
                    break;
                }
                else
                {
                    return(true);
                }
            }
            return(false);
        }