// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutomationTool;
using UnrealBuildTool;
using System.Xml;
using System.IO;
using EpicGames.Core;
using UnrealBuildBase;
using EpicGames.BuildGraph;
using AutomationTool.Tasks;
using Microsoft.Extensions.Logging;
using static AutomationTool.CommandUtils;
namespace AutomationTool.Tasks
{
static class StringExtensions
{
public static bool CaseInsensitiveContains(this string Text, string Value)
{
return System.Globalization.CultureInfo.InvariantCulture.CompareInfo.IndexOf(Text, Value, System.Globalization.CompareOptions.IgnoreCase) >= 0;
}
}
///
/// Parameters for a task that calls another UAT command
///
public class CommandTaskParameters
{
///
/// The command name to execute.
///
[TaskParameter]
public string Name;
///
/// Arguments to be passed to the command.
///
[TaskParameter(Optional = true)]
public string Arguments;
///
/// If non-null, instructs telemetry from the command to be merged into the telemetry for this UAT instance with the given prefix. May be an empty (non-null) string.
///
[TaskParameter(Optional = true)]
public string MergeTelemetryWithPrefix;
}
///
/// Invokes an AutomationTool child process to run the given command.
///
[TaskElement("Command", typeof(CommandTaskParameters))]
public class CommandTask : BgTaskImpl
{
///
/// Parameters for this task
///
CommandTaskParameters Parameters;
///
/// Construct a new CommandTask.
///
/// Parameters for this task
public CommandTask(CommandTaskParameters 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 Task ExecuteAsync(JobContext Job, HashSet BuildProducts, Dictionary> TagNameToFileSet)
{
// If we're merging telemetry from the child process, get a temp filename for it
FileReference TelemetryFile = null;
if (Parameters.MergeTelemetryWithPrefix != null)
{
TelemetryFile = FileReference.Combine(Unreal.RootDirectory, "Engine", "Intermediate", "UAT", "Telemetry.json");
DirectoryReference.CreateDirectory(TelemetryFile.Directory);
}
// Run the command
StringBuilder CommandLine = new StringBuilder();
if (Parameters.Arguments == null || (!Parameters.Arguments.CaseInsensitiveContains("-p4") && !Parameters.Arguments.CaseInsensitiveContains("-nop4")))
{
CommandLine.AppendFormat("{0} ", CommandUtils.P4Enabled ? "-p4" : "-nop4");
}
if (Parameters.Arguments == null || (!Parameters.Arguments.CaseInsensitiveContains("-submit") && !Parameters.Arguments.CaseInsensitiveContains("-nosubmit")))
{
if(GlobalCommandLine.Submit)
{
CommandLine.Append("-submit ");
}
if(GlobalCommandLine.NoSubmit)
{
CommandLine.Append("-nosubmit ");
}
}
if (Parameters.Arguments == null || !Parameters.Arguments.CaseInsensitiveContains("-uselocalbuildstorage"))
{
if (GlobalCommandLine.UseLocalBuildStorage)
{
CommandLine.Append("-uselocalbuildstorage ");
}
}
CommandLine.Append("-NoCompile ");
CommandLine.Append(Parameters.Name);
if (!String.IsNullOrEmpty(Parameters.Arguments))
{
CommandLine.AppendFormat(" {0}", Parameters.Arguments);
}
if (TelemetryFile != null)
{
CommandLine.AppendFormat(" -Telemetry={0}", CommandUtils.MakePathSafeToUseWithCommandLine(TelemetryFile.FullName));
}
CommandUtils.RunUAT(CommandUtils.CmdEnv, CommandLine.ToString(), Identifier: Parameters.Name);
// Merge in any new telemetry data that was produced
if (TelemetryFile != null && FileReference.Exists(TelemetryFile))
{
Logger.LogDebug("Merging telemetry from {TelemetryFile}", TelemetryFile);
TelemetryData NewTelemetry;
if (TelemetryData.TryRead(TelemetryFile, out NewTelemetry))
{
CommandUtils.Telemetry.Merge(Parameters.MergeTelemetryWithPrefix, NewTelemetry);
}
else
{
Logger.LogWarning("Unable to read UAT telemetry file from {TelemetryFile}", TelemetryFile);
}
}
return Task.CompletedTask;
}
///
/// Gets the name of this trace
///
/// Name of the trace
public override string GetTraceName()
{
return String.Format("{0}.{1}", base.GetTraceName(), Parameters.Name.ToLowerInvariant());
}
///
/// 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()
{
yield break;
}
///
/// Find all the tags which are modified by this task
///
/// The tag names which are modified by this task
public override IEnumerable FindProducedTagNames()
{
yield break;
}
}
public static partial class StandardTasks
{
///
/// Runs another UAT command
///
/// The execution state
/// Name of the command to run
/// Arguments for the command
/// If non-null, instructs telemetry from the command to be merged into the telemetry for this UAT instance with the given prefix. May be an empty (non-null) string.
public static async Task CommandAsync(this BgContext State, string Name, string Arguments = null, string MergeTelemetryWithPrefix = null)
{
CommandTaskParameters Parameters = new CommandTaskParameters();
Parameters.Name = Name;
Parameters.Arguments = Arguments ?? Parameters.Arguments;
Parameters.MergeTelemetryWithPrefix = MergeTelemetryWithPrefix ?? Parameters.MergeTelemetryWithPrefix;
await ExecuteAsync(new CommandTask(Parameters));
}
}
}