// Copyright Epic Games, Inc. All Rights Reserved. using AutomationTool; using System; using System.Collections.Generic; using System.Linq; using System.Xml; using EpicGames.Core; using UnrealBuildBase; namespace BuildGraph.Tasks { /// /// Parameters for a . /// public class WriteTextFileTaskParameters { /// /// Path to the file to write. /// [TaskParameter] public FileReference File; /// /// Optional, whether or not to append to the file rather than overwrite. /// [TaskParameter(Optional = true)] public bool Append; /// /// The text to write to the file. /// [TaskParameter(Optional = true)] public string Text; /// /// If specified, causes the given list of files to be printed after the given message. /// [TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.FileSpec)] public string Files; /// /// Tag to be applied to build products of this task. /// [TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.TagList)] public string Tag; } /// /// Writes text to a file. /// [TaskElement("WriteTextFile", typeof(WriteTextFileTaskParameters))] public class WriteTextFileTask : CustomTask { /// /// Parameters for this task. /// WriteTextFileTaskParameters Parameters; /// /// Constructor. /// /// Parameters for this task. public WriteTextFileTask(WriteTextFileTaskParameters InParameters) { Parameters = InParameters; } /// /// Execute the task. /// /// Information about the current job. /// Set of build products produced by this node. /// Mapping from tag names to the set of files they include. public override void Execute(JobContext Job, HashSet BuildProducts, Dictionary> TagNameToFileSet) { string FileText = Parameters.Text; // If any files or tagsets are provided, add them to the text output. if (!String.IsNullOrEmpty(Parameters.Files)) { if (!string.IsNullOrWhiteSpace(FileText)) { FileText += Environment.NewLine; } HashSet Files = ResolveFilespec(Unreal.RootDirectory, Parameters.Files, TagNameToFileSet); if (Files.Any()) { FileText += string.Join(Environment.NewLine, Files.Select(f => f.FullName)); } } // Make sure output folder exists. if (!DirectoryReference.Exists(Parameters.File.Directory)) { DirectoryReference.CreateDirectory(Parameters.File.Directory); } if (Parameters.Append) { CommandUtils.LogInformation(string.Format("Appending text to file '{0}': {1}", Parameters.File, FileText)); FileReference.AppendAllText(Parameters.File, Environment.NewLine + FileText); } else { CommandUtils.LogInformation(string.Format("Writing text to file '{0}': {1}", Parameters.File, FileText)); FileReference.WriteAllText(Parameters.File, FileText); } } /// /// Output this task out to an XML writer. /// public override void Write(XmlWriter Writer) { Write(Writer, Parameters); } /// /// Find all the tags which are used as inputs to this task /// /// The tag names which are read by this task public override IEnumerable FindConsumedTagNames() { foreach(string TagName in FindTagNamesFromFilespec(Parameters.Files)) { yield return TagName; } } /// /// Find all the tags which are modified by this task /// /// The tag names which are modified by this task public override IEnumerable FindProducedTagNames() { return FindTagNamesFromList(Parameters.Tag); } } }