diff --git a/src/Tasks/FileIO/WriteLinesToFile.cs b/src/Tasks/FileIO/WriteLinesToFile.cs index 57691f39db2..5c3f7e5943f 100644 --- a/src/Tasks/FileIO/WriteLinesToFile.cs +++ b/src/Tasks/FileIO/WriteLinesToFile.cs @@ -60,101 +60,100 @@ public class WriteLinesToFile : TaskExtension, IIncrementalTask [Obsolete] public bool CanBeIncremental => WriteOnlyWhenDifferent; - /// - /// Execute the task. - /// - /// + /// public override bool Execute() { bool success = true; - if (File != null) + if (File == null) + { + return success; + } + + string filePath = FileUtilities.NormalizePath(File.ItemSpec); + + string contentsAsString = string.Empty; + + if (Lines != null && Lines.Length > 0) { - // do not return if Lines is null, because we may - // want to delete the file in that case - StringBuilder buffer = new StringBuilder(); - if (Lines != null) + StringBuilder buffer = new StringBuilder(capacity: Lines.Length * 64); + + foreach (ITaskItem line in Lines) { - foreach (ITaskItem line in Lines) - { - buffer.AppendLine(line.ItemSpec); - } + buffer.AppendLine(line.ItemSpec); } - Encoding encoding = s_defaultEncoding; - if (Encoding != null) + contentsAsString = buffer.ToString(); + } + + Encoding encoding = s_defaultEncoding; + if (Encoding != null) + { + try { - try - { - encoding = System.Text.Encoding.GetEncoding(Encoding); - } - catch (ArgumentException) - { - Log.LogErrorWithCodeFromResources("General.InvalidValue", "Encoding", "WriteLinesToFile"); - return false; - } + encoding = System.Text.Encoding.GetEncoding(Encoding); + } + catch (ArgumentException) + { + Log.LogErrorWithCodeFromResources("General.InvalidValue", "Encoding", "WriteLinesToFile"); + return false; } + } - try + try + { + Directory.CreateDirectory(Path.GetDirectoryName(filePath)); + + if (Overwrite) { - var directoryPath = Path.GetDirectoryName(FileUtilities.NormalizePath(File.ItemSpec)); - if (Overwrite) + // When WriteOnlyWhenDifferent is set, read the file and if they're the same return. + if (WriteOnlyWhenDifferent) { - Directory.CreateDirectory(directoryPath); - string contentsAsString = buffer.ToString(); - - // When WriteOnlyWhenDifferent is set, read the file and if they're the same return. - if (WriteOnlyWhenDifferent) + MSBuildEventSource.Log.WriteLinesToFileUpToDateStart(); + try { - MSBuildEventSource.Log.WriteLinesToFileUpToDateStart(); - try + if (FileUtilities.FileExistsNoThrow(filePath)) { - if (FileUtilities.FileExistsNoThrow(File.ItemSpec)) + string existingContents = FileSystems.Default.ReadFileAllText(filePath); + + if (existingContents.Equals(contentsAsString)) { - string existingContents = FileSystems.Default.ReadFileAllText(File.ItemSpec); - if (existingContents.Length == buffer.Length) - { - if (existingContents.Equals(contentsAsString)) - { - Log.LogMessageFromResources(MessageImportance.Low, "WriteLinesToFile.SkippingUnchangedFile", File.ItemSpec); - MSBuildEventSource.Log.WriteLinesToFileUpToDateStop(File.ItemSpec, true); - return true; - } - else if (FailIfNotIncremental) - { - Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorReadingFile", File.ItemSpec); - return false; - } - } + Log.LogMessageFromResources(MessageImportance.Low, "WriteLinesToFile.SkippingUnchangedFile", filePath); + MSBuildEventSource.Log.WriteLinesToFileUpToDateStop(filePath, true); + return true; + } + else if (FailIfNotIncremental) + { + Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorReadingFile", filePath); + return false; } } - catch (IOException) - { - Log.LogMessageFromResources(MessageImportance.Low, "WriteLinesToFile.ErrorReadingFile", File.ItemSpec); - } - MSBuildEventSource.Log.WriteLinesToFileUpToDateStop(File.ItemSpec, false); } - - System.IO.File.WriteAllText(File.ItemSpec, contentsAsString, encoding); - } - else - { - if (WriteOnlyWhenDifferent) + catch (IOException) { - Log.LogMessageFromResources(MessageImportance.Normal, "WriteLinesToFile.UnusedWriteOnlyWhenDifferent", File.ItemSpec); + Log.LogMessageFromResources(MessageImportance.Low, "WriteLinesToFile.ErrorReadingFile", filePath); } - - Directory.CreateDirectory(directoryPath); - System.IO.File.AppendAllText(File.ItemSpec, buffer.ToString(), encoding); + MSBuildEventSource.Log.WriteLinesToFileUpToDateStop(filePath, false); } + + System.IO.File.WriteAllText(filePath, contentsAsString, encoding); } - catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e)) + else { - string lockedFileMessage = LockCheck.GetLockedFileMessage(File.ItemSpec); - Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorOrWarning", File.ItemSpec, e.Message, lockedFileMessage); - success = false; + if (WriteOnlyWhenDifferent) + { + Log.LogMessageFromResources(MessageImportance.Normal, "WriteLinesToFile.UnusedWriteOnlyWhenDifferent", filePath); + } + + System.IO.File.AppendAllText(filePath, contentsAsString, encoding); } } + catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e)) + { + string lockedFileMessage = LockCheck.GetLockedFileMessage(filePath); + Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorOrWarning", filePath, e.Message, lockedFileMessage); + success = false; + } return success; }