public async Task DeleteComment(string commentName, DeleteCommentMode mode) { foreach (string filePath in await this.fileSystemService.DirectoryGetAllFiles(this.projectDirectoryPath)) { await this.DeleteCommentInternal(commentName, filePath, mode); } }
public async Task DeleteCommentTest( DeleteCommentMode 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.DeleteComment(commentName, mode, "File" + fileExtension); // Assert fileSystemServiceMock.VerifyAll(); }
public async Task DeleteCommentTest( DeleteCommentMode 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.DeleteComment(commentName, mode, "File" + fileExtension); // Assert fileSystemServiceMock.VerifyAll(); }
public async Task DeleteComment(string commentName, DeleteCommentMode mode) { foreach (string filePath in await this.fileSystemService.DirectoryGetAllFiles(this.projectDirectoryPath)) { await this.DeleteCommentInternal(commentName, filePath, mode); } }
public async Task DeleteComment(string commentName, DeleteCommentMode mode, string relativeFilePath) { string filePath = Path.Combine(this.projectDirectoryPath, relativeFilePath); if (this.fileSystemService.FileExists(filePath)) { await this.DeleteCommentInternal(commentName, filePath, mode); } }
public async Task DeleteComment(string commentName, DeleteCommentMode mode, string relativeFilePath) { string filePath = Path.Combine(this.projectDirectoryPath, relativeFilePath); if (this.fileSystemService.FileExists(filePath)) { await this.DeleteCommentInternal(commentName, filePath, mode); } }
private static IEnumerable <string> GetOutputLines(DeleteCommentMode deleteCommentMode, string commentStart, string commentEnd = null) { if (commentEnd != null) { commentEnd = " " + commentEnd; } if (deleteCommentMode == DeleteCommentMode.StartEndComment) { return(new string[] { $" {commentStart} Blah Blah Blah{commentEnd} ", $" {commentStart} Blah Blah Blah{commentEnd} ", $" {commentStart} Blah Blah Blah{commentEnd} " }); } else if (deleteCommentMode == DeleteCommentMode.StartEndCommentAndCode) { return(new string[] { $" {commentStart} Blah Blah Blah{commentEnd} ", $" {commentStart} Blah Blah Blah{commentEnd} " }); } else // if (deleteCommentMode == DeleteCommentMode.StartEndCommentAndUncommentCode) { string startSpacer = new string(' ', commentStart.Length); string endSpacer = new string(' ', commentEnd == null ? 0 : 1); return(new string[] { $" {commentStart} Blah Blah Blah{commentEnd} ", startSpacer + $" Blah Blah Blah " + endSpacer, $" {commentStart} Blah Blah Blah{commentEnd} " }); } }
private static IEnumerable<string> GetOutputLines(DeleteCommentMode deleteCommentMode, string commentStart, string commentEnd = null) { if (commentEnd != null) { commentEnd = " " + commentEnd; } if (deleteCommentMode == DeleteCommentMode.StartEndComment) { return new string[] { $" {commentStart} Blah Blah Blah{commentEnd} ", $" {commentStart} Blah Blah Blah{commentEnd} ", $" {commentStart} Blah Blah Blah{commentEnd} " }; } else if (deleteCommentMode == DeleteCommentMode.StartEndCommentAndCode) { return new string[] { $" {commentStart} Blah Blah Blah{commentEnd} ", $" {commentStart} Blah Blah Blah{commentEnd} " }; } else // if (deleteCommentMode == DeleteCommentMode.StartEndCommentAndUncommentCode) { string startSpacer = new string(' ', commentStart.Length); string endSpacer = new string(' ', commentEnd == null ? 0 : 1); return new string[] { $" {commentStart} Blah Blah Blah{commentEnd} ", startSpacer + $" Blah Blah Blah " + endSpacer, $" {commentStart} Blah Blah Blah{commentEnd} " }; } }
private async Task DeleteCommentInternal(string commentName, string filePath, DeleteCommentMode mode) { 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 != DeleteCommentMode.StartEndCommentAndCode) { string newLine = line; if (mode == DeleteCommentMode.StartEndCommentAndUncommentCode) { if (newLine.TrimStart().StartsWith(comment.Start)) { int commentIndex = newLine.IndexOf(comment.Start); newLine = newLine.Substring(0, commentIndex) + new string(' ', comment.Start.Length) + 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 DeleteCommentInternal(string commentName, string filePath, DeleteCommentMode mode) { 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 != DeleteCommentMode.StartEndCommentAndCode) { string newLine = line; if (mode == DeleteCommentMode.StartEndCommentAndUncommentCode) { if (newLine.TrimStart().StartsWith(comment.Start)) { int commentIndex = newLine.IndexOf(comment.Start); newLine = newLine.Substring(0, commentIndex) + new string(' ', comment.Start.Length) + 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); }