// Copyright Epic Games, Inc. All Rights Reserved.
using AutomationTool;
using EpicGames.BuildGraph;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using EpicGames.Core;
using UnrealBuildBase;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using static AutomationTool.CommandUtils;
namespace AutomationTool.Tasks
{
///
/// Parameters for a .
///
public class WriteTextFileTaskParameters
{
///
/// Path to the file to write.
///
[TaskParameter]
public FileReference File { get; set; }
///
/// Optional, whether or not to append to the file rather than overwrite.
///
[TaskParameter(Optional = true)]
public bool Append { get; set; }
///
/// The text to write to the file.
///
[TaskParameter(Optional = true)]
public string Text { get; set; }
///
/// If specified, causes the given list of files to be printed after the given message.
///
[TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.FileSpec)]
public string Files { get; set; }
///
/// Tag to be applied to build products of this task.
///
[TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.TagList)]
public string Tag { get; set; }
}
///
/// Writes text to a file.
///
[TaskElement("WriteTextFile", typeof(WriteTextFileTaskParameters))]
public class WriteTextFileTask : BgTaskImpl
{
///
/// 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 async Task ExecuteAsync(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)
{
Logger.LogInformation("{Text}", string.Format("Appending text to file '{0}': {1}", Parameters.File, FileText));
await FileReference.AppendAllTextAsync(Parameters.File, Environment.NewLine + FileText);
}
else
{
Logger.LogInformation("{Text}", string.Format("Writing text to file '{0}': {1}", Parameters.File, FileText));
await FileReference.WriteAllTextAsync(Parameters.File, FileText);
}
// Apply the optional tag to the build products
foreach (string TagName in FindTagNamesFromList(Parameters.Tag))
{
FindOrAddTagSet(TagNameToFileSet, TagName).Add(Parameters.File);
}
// Add them to the set of build products
BuildProducts.Add(Parameters.File);
}
///
/// 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);
}
}
}