public async Task EditCommentByPattern(string commentName, EditCommentMode mode, string searchPattern)
 {
     foreach (string filePath in await this.fileSystemService.DirectoryGetAllFiles(this.projectDirectoryPath, searchPattern))
     {
         await this.EditCommentInternal(commentName, mode, filePath);
     }
 }
        public async Task EditCommentTest(
            EditCommentMode mode,
            string fileExtension,
            string commentStart,
            string commentEnd = null)
        {
            // Arrange
            string commentName = "CommentName";
            string filePath = @"C:\Project\File" + fileExtension;
            Mock<IFileSystemService> fileSystemServiceMock = new Mock<IFileSystemService>(MockBehavior.Strict);
            fileSystemServiceMock
                .Setup(x => x.FileExists(It.Is<string>(y => string.Equals(y, filePath))))
                .Returns(true);
            fileSystemServiceMock
                .Setup(x => x.FileReadAllLines(It.Is<string>(y => string.Equals(y, filePath))))
                .Returns(Task.FromResult(GetInputLines(commentName, commentStart, commentEnd)));
            fileSystemServiceMock
                .Setup(x => x.FileWriteAllLines(
                    It.Is<string>(y => string.Equals(y, filePath)),
                    It.Is<IEnumerable<string>>(y => Enumerable.SequenceEqual(y, GetOutputLines(mode, commentStart, commentEnd)))))
                .Returns(Task.FromResult<object>(null));
            IProjectService projectService = new ProjectService(fileSystemServiceMock.Object, @"C:\Project\Project.xproj");

            // Act
            await projectService.EditComment(commentName, mode, "File" + fileExtension);

            // Assert
            fileSystemServiceMock.VerifyAll();
        }
Пример #3
0
        public async Task EditCommentTest(
            EditCommentMode mode,
            string fileExtension,
            string commentStart,
            string commentEnd = null)
        {
            // Arrange
            string commentName = "CommentName";
            string filePath    = @"C:\Project\File" + fileExtension;
            Mock <IFileSystemService> fileSystemServiceMock = new Mock <IFileSystemService>(MockBehavior.Strict);

            fileSystemServiceMock
            .Setup(x => x.FileExists(It.Is <string>(y => string.Equals(y, filePath))))
            .Returns(true);
            var inputLines  = GetInputLines(commentName, commentStart, commentEnd);
            var outputLines = GetOutputLines(mode, commentStart, commentEnd);

            fileSystemServiceMock
            .Setup(x => x.FileReadAllLines(It.Is <string>(y => string.Equals(y, filePath))))
            .Returns(Task.FromResult(inputLines));
            fileSystemServiceMock
            .Setup(x => x.FileWriteAllLines(
                       It.Is <string>(y => string.Equals(y, filePath)),
                       It.Is <IEnumerable <string> >(y => Enumerable.SequenceEqual(y, outputLines))));
            IProjectService projectService = new ProjectService(fileSystemServiceMock.Object, @"C:\Project\Project.xproj");

            // Act
            await projectService.EditCommentInFile(commentName, mode, "File" + fileExtension);

            // Assert
            fileSystemServiceMock.VerifyAll();
        }
Пример #4
0
 public async Task EditCommentByPattern(string commentName, EditCommentMode mode, string searchPattern)
 {
     foreach (string filePath in await this.fileSystemService.DirectoryGetAllFiles(this.projectDirectoryPath, searchPattern))
     {
         await this.EditCommentInternal(commentName, mode, filePath);
     }
 }
        private static IEnumerable<string> GetOutputLines(EditCommentMode mode, string commentStart, string commentEnd = null)
        {
            if (commentEnd != null)
            {
                commentEnd = " " + commentEnd;
            }

            if (mode == EditCommentMode.LeaveCodeUnchanged)
            {
                return new string[]
                {
                    $"   {commentStart} Blah Blah Blah{commentEnd}    ",
                    $"   {commentStart} Blah Blah Blah{commentEnd}    ",
                    $"   {commentStart} Blah Blah Blah{commentEnd}    "
                };
            }
            else if (mode == EditCommentMode.DeleteCode)
            {
                return new string[]
                {
                    $"   {commentStart} Blah Blah Blah{commentEnd}    ",
                    $"   {commentStart} Blah Blah Blah{commentEnd}    "
                };
            }
            else // if (deleteCommentMode == DeleteCommentMode.StartEndCommentAndUncommentCode)
            {
                string endSpacer = new string(' ', commentEnd == null ? 0 : 1);
                return new string[]
                {
                    $"   {commentStart} Blah Blah Blah{commentEnd}    ",
                    $"    Blah Blah Blah    " + endSpacer,
                    $"   {commentStart} Blah Blah Blah{commentEnd}    "
                };
            }
        }
Пример #6
0
        private static IEnumerable <string> GetOutputLines(EditCommentMode mode, string commentStart, string commentEnd = null)
        {
            if (commentEnd != null)
            {
                commentEnd = " " + commentEnd;
            }

            if (mode == EditCommentMode.LeaveCodeUnchanged)
            {
                return(new string[]
                {
                    $"   {commentStart} Blah Blah Blah{commentEnd}    ",
                    $"   {commentStart} Blah Blah Blah{commentEnd}    ",
                    $"   {commentStart} Blah Blah Blah{commentEnd}    "
                });
            }
            else if (mode == EditCommentMode.DeleteCode)
            {
                return(new string[]
                {
                    $"   {commentStart} Blah Blah Blah{commentEnd}    ",
                    $"   {commentStart} Blah Blah Blah{commentEnd}    "
                });
            }
            else // if (deleteCommentMode == DeleteCommentMode.StartEndCommentAndUncommentCode)
            {
                string endSpacer = new string(' ', commentEnd == null ? 0 : 1);
                return(new string[]
                {
                    $"   {commentStart} Blah Blah Blah{commentEnd}    ",
                    $"    Blah Blah Blah    " + endSpacer,
                    $"   {commentStart} Blah Blah Blah{commentEnd}    "
                });
            }
        }
 public async Task EditCommentInFile(string commentName, EditCommentMode mode, string relativeFilePath)
 {
     string filePath = Path.Combine(this.projectDirectoryPath, relativeFilePath);
     if (this.fileSystemService.FileExists(filePath))
     {
         await this.EditCommentInternal(commentName, mode, filePath);
     }
 }
Пример #8
0
 public async Task EditCommentInFile(string commentName, EditCommentMode mode, string relativeFilePath)
 {
     string filePath = Path.Combine(this.ProjectDirectoryPath, relativeFilePath);
     if (this.fileSystemService.FileExists(filePath))
     {
         await this.EditCommentInternal(commentName, mode, filePath);
     }
 }
Пример #9
0
        private async Task EditCommentInternal(string commentName, EditCommentMode mode, string filePath)
        {
            string fileExtension = Path.GetExtension(filePath);
            Comment[] comments = Comment.GetComments(fileExtension);

            if (comments.Length > 0)
            {
                string[] lines = await this.fileSystemService.FileReadAllLines(filePath);

                foreach (Comment comment in comments)
                {
                    NamedComment namedComment = new NamedComment(commentName, comment);
                    lines = EditCommentInternal(lines, namedComment, mode);
                }

                this.fileSystemService.FileWriteAllLines(filePath, lines);
            }
        }
        private string[] EditCommentInternal(string[] lines, NamedComment namedComment, EditCommentMode mode)
        {
            List<string> newLines = new List<string>(lines.Length);
            
            bool isUncommenting = false;
            foreach (string line in lines)
            {
                if (isUncommenting)
                {
                    if (line.Contains(namedComment.End))
                    {
                        isUncommenting = false;
                    }
                    else if (mode != EditCommentMode.DeleteCode)
                    {
                        string newLine = line;

                        if (mode == EditCommentMode.UncommentCode)
                        {
                            if (newLine.TrimStart().StartsWith(namedComment.Comment.Start))
                            {
                                int commentIndex = newLine.IndexOf(namedComment.Comment.Start);
                                newLine = newLine.Substring(0, commentIndex) +
                                    (newLine[commentIndex + namedComment.Comment.Start.Length] == ' ' ?
                                    newLine.Substring(commentIndex + namedComment.Comment.Start.Length + 1) :
                                    newLine.Substring(commentIndex + namedComment.Comment.Start.Length));
                            }

                            if (namedComment.Comment.HasEnd && newLine.TrimEnd().EndsWith(namedComment.Comment.End))
                            {
                                int commentIndex = newLine.LastIndexOf(namedComment.Comment.End);
                                newLine = newLine.Substring(0, commentIndex) +
                                    newLine.Substring(commentIndex + namedComment.Comment.End.Length);
                            }
                        }

                        newLines.Add(newLine);
                    }
                }
                else if (line.Contains(namedComment.Start))
                {
                    isUncommenting = true;
                }
                else
                {
                    newLines.Add(line);
                }
            }

            return newLines.ToArray();
        }
        private async Task EditCommentInternal(string commentName, EditCommentMode mode, string filePath)
        {
            string fileExtension = Path.GetExtension(filePath);
            Comment[] comments = Comment.GetComments(fileExtension);

            if (comments.Length > 0)
            {
                string[] lines = await this.fileSystemService.FileReadAllLines(filePath);

                foreach (Comment comment in comments)
                {
                    NamedComment namedComment = new NamedComment(commentName, comment);
                    lines = EditCommentInternal(lines, namedComment, mode);
                }

                await this.fileSystemService.FileWriteAllLines(filePath, lines);
            }
        }
Пример #12
0
        private string[] EditCommentInternal(string[] lines, NamedComment namedComment, EditCommentMode mode)
        {
            List <string> newLines = new List <string>(lines.Length);

            bool isUncommenting = false;

            foreach (string line in lines)
            {
                if (isUncommenting)
                {
                    if (line.Contains(namedComment.End))
                    {
                        isUncommenting = false;
                    }
                    else if (mode != EditCommentMode.DeleteCode)
                    {
                        string newLine = line;

                        if (mode == EditCommentMode.UncommentCode)
                        {
                            if (newLine.TrimStart().StartsWith(namedComment.Comment.Start))
                            {
                                int commentIndex = newLine.IndexOf(namedComment.Comment.Start);
                                newLine = newLine.Substring(0, commentIndex) +
                                          (newLine[commentIndex + namedComment.Comment.Start.Length] == ' ' ?
                                           newLine.Substring(commentIndex + namedComment.Comment.Start.Length + 1) :
                                           newLine.Substring(commentIndex + namedComment.Comment.Start.Length));
                            }

                            if (namedComment.Comment.HasEnd && newLine.TrimEnd().EndsWith(namedComment.Comment.End))
                            {
                                int commentIndex = newLine.LastIndexOf(namedComment.Comment.End);
                                newLine = newLine.Substring(0, commentIndex) +
                                          newLine.Substring(commentIndex + namedComment.Comment.End.Length);
                            }
                        }

                        newLines.Add(newLine);
                    }
                }
                else if (line.Contains(namedComment.Start))
                {
                    isUncommenting = true;
                }
                else
                {
                    newLines.Add(line);
                }
            }

            if (isUncommenting)
            {
                throw new InvalidOperationException($"No end comment was found. End:<{namedComment.End}>.");
            }

            return(newLines.ToArray());
        }
        private async Task EditCommentInternal(string commentName, EditCommentMode mode, string filePath)
        {
            string  fileExtension = Path.GetExtension(filePath);
            Comment comment       = Comment.GetComment(fileExtension);

            if (comment == null)
            {
                // We don't support this file extension.
                return;
            }

            NamedComment namedComment = new NamedComment(commentName, comment);

            string[] lines = await this.fileSystemService.FileReadAllLines(filePath);

            List <string> newLines = new List <string>(lines.Length);

            bool isUncommenting = false;

            foreach (string line in lines)
            {
                if (isUncommenting)
                {
                    if (line.Contains(namedComment.End))
                    {
                        isUncommenting = false;
                    }
                    else if (mode != EditCommentMode.DeleteCode)
                    {
                        string newLine = line;

                        if (mode == EditCommentMode.UncommentCode)
                        {
                            if (newLine.TrimStart().StartsWith(comment.Start))
                            {
                                int commentIndex = newLine.IndexOf(comment.Start);
                                newLine = newLine.Substring(0, commentIndex) +
                                          (newLine[commentIndex + comment.Start.Length] == ' ' ?
                                           newLine.Substring(commentIndex + comment.Start.Length + 1) :
                                           newLine.Substring(commentIndex + comment.Start.Length));
                            }

                            if (comment.HasEnd && newLine.TrimEnd().EndsWith(comment.End))
                            {
                                int commentIndex = newLine.LastIndexOf(comment.End);
                                newLine = newLine.Substring(0, commentIndex) +
                                          newLine.Substring(commentIndex + comment.End.Length);
                            }
                        }

                        newLines.Add(newLine);
                    }
                }
                else if (line.Contains(namedComment.Start))
                {
                    isUncommenting = true;
                }
                else
                {
                    newLines.Add(line);
                }
            }

            await this.fileSystemService.FileWriteAllLines(filePath, newLines);
        }
        private async Task EditCommentInternal(string commentName, EditCommentMode mode, string filePath)
        {
            string fileExtension = Path.GetExtension(filePath);
            Comment comment = Comment.GetComment(fileExtension);

            if (comment == null)
            {
                // We don't support this file extension.
                return;
            }

            NamedComment namedComment = new NamedComment(commentName, comment);

            string[] lines = await this.fileSystemService.FileReadAllLines(filePath);
            List<string> newLines = new List<string>(lines.Length);

            bool isUncommenting = false;
            foreach (string line in lines)
            {
                if (isUncommenting)
                {
                    if (line.Contains(namedComment.End))
                    {
                        isUncommenting = false;
                    }
                    else if (mode != EditCommentMode.DeleteCode)
                    {
                        string newLine = line;

                        if (mode == EditCommentMode.UncommentCode)
                        {
                            if (newLine.TrimStart().StartsWith(comment.Start))
                            {
                                int commentIndex = newLine.IndexOf(comment.Start);
                                newLine = newLine.Substring(0, commentIndex) +
                                    (newLine[commentIndex + comment.Start.Length] == ' ' ?
                                    newLine.Substring(commentIndex + comment.Start.Length + 1) :
                                    newLine.Substring(commentIndex + comment.Start.Length));
                            }

                            if (comment.HasEnd && newLine.TrimEnd().EndsWith(comment.End))
                            {
                                int commentIndex = newLine.LastIndexOf(comment.End);
                                newLine = newLine.Substring(0, commentIndex) +
                                    newLine.Substring(commentIndex + comment.End.Length);
                            }
                        }

                        newLines.Add(newLine);
                    }
                }
                else if (line.Contains(namedComment.Start))
                {
                    isUncommenting = true;
                }
                else
                {
                    newLines.Add(line);
                }
            }

            await this.fileSystemService.FileWriteAllLines(filePath, newLines);
        }