// Copyright Epic Games, Inc. All Rights Reserved.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using System.Xml;
using EpicGames.Core;
using Microsoft.Extensions.Logging;
namespace AutomationTool.Tasks
{
///
/// Parameters for a .
///
public class HordeCreateReportTaskParameters
{
///
/// Name for the report
///
[TaskParameter]
public string Name { get; set; }
///
/// Where to display the report
///
[TaskParameter]
public string Scope { get; set; }
///
/// Where to show the report
///
[TaskParameter]
public string Placement { get; set; }
///
/// Text to be displayed
///
[TaskParameter]
public string Text { get; set; }
}
///
/// Creates a Horde report file, which will be displayed on the dashboard with any job running this task.
///
[TaskElement("Horde-CreateReport", typeof(HordeCreateReportTaskParameters))]
public class HordeCreateReportTask : BgTaskImpl
{
///
/// Parameters for this task.
///
readonly HordeCreateReportTaskParameters _parameters;
///
/// Constructor.
///
/// Parameters for this task.
public HordeCreateReportTask(HordeCreateReportTaskParameters parameters)
{
_parameters = parameters;
}
///
/// ExecuteAsync 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)
{
FileReference reportTextFile = FileReference.Combine(new DirectoryReference(CommandUtils.CmdEnv.LogFolder), $"{_parameters.Name}.md");
await FileReference.WriteAllTextAsync(reportTextFile, _parameters.Text);
FileReference reportJsonFile = FileReference.Combine(new DirectoryReference(CommandUtils.CmdEnv.LogFolder), $"{_parameters.Name}.report.json");
using (FileStream reportJsonStream = FileReference.Open(reportJsonFile, FileMode.Create, FileAccess.Write, FileShare.Read))
{
using (Utf8JsonWriter writer = new Utf8JsonWriter(reportJsonStream))
{
writer.WriteStartObject();
writer.WriteString("scope", _parameters.Scope);
writer.WriteString("name", _parameters.Name);
writer.WriteString("placement", _parameters.Placement);
writer.WriteString("fileName", reportTextFile.GetFileName());
writer.WriteEndObject();
}
}
Logger.LogInformation("Written report to {TextFile} and {JsonFile}: \"{Text}\"", reportTextFile, reportJsonFile, _parameters.Text);
}
///
/// 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() => Enumerable.Empty();
///
/// Find all the tags which are modified by this task
///
/// The tag names which are modified by this task
public override IEnumerable FindProducedTagNames() => Enumerable.Empty();
}
}