// Copyright Epic Games, Inc. All Rights Reserved.
using AutomationTool;
using AutomationTool.Tasks;
using EpicGames.BuildGraph;
using EpicGames.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using UnrealBuildBase;
namespace AutomationTool.Tasks
{
///
/// Parameters for a DotNet task
///
public class DotNetTaskParameters
{
///
/// Docker command line arguments
///
[TaskParameter(Optional = true)]
public string Arguments;
///
/// Base directory for running the command
///
[TaskParameter(Optional = true)]
public string BaseDir;
///
/// Environment variables to set
///
[TaskParameter(Optional = true)]
public string Environment;
///
/// File to read environment variables from
///
[TaskParameter(Optional = true)]
public string EnvironmentFile;
///
/// The minimum exit code, which is treated as an error.
///
[TaskParameter(Optional = true)]
public int ErrorLevel = 1;
///
/// Override path to dotnet executable
///
[TaskParameter(Optional = true)]
public FileReference DotNetPath;
}
///
/// Spawns Docker and waits for it to complete.
///
[TaskElement("DotNet", typeof(DotNetTaskParameters))]
public class DotNetTask : SpawnTaskBase
{
///
/// Parameters for this task
///
DotNetTaskParameters Parameters;
///
/// Construct a Docker task
///
/// Parameters for the task
public DotNetTask(DotNetTaskParameters 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)
{
FileReference DotNetFile = Parameters.DotNetPath == null ? Unreal.DotnetPath : Parameters.DotNetPath;
if(!FileReference.Exists(DotNetFile))
{
throw new AutomationException("DotNet is missing from {0}", DotNetFile);
}
IProcessResult Result = await ExecuteAsync(DotNetFile.FullName, Parameters.Arguments, WorkingDir: Parameters.BaseDir, EnvVars: ParseEnvVars(Parameters.Environment, Parameters.EnvironmentFile));
if (Result.ExitCode < 0 || Result.ExitCode >= Parameters.ErrorLevel)
{
throw new AutomationException("Docker terminated with an exit code indicating an error ({0})", Result.ExitCode);
}
}
///
/// 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 a command using dotnet.
///
/// Command-line arguments.
/// Base directory for running the command.
/// Environment variables to set.
/// File to read environment variables from.
/// The minimum exit code, which is treated as an error.
/// Override path to dotnet executable.
public static async Task DotNetAsync(string Arguments = null, DirectoryReference BaseDir = null, string Environment = null, FileReference EnvironmentFile = null, int ErrorLevel = 1, FileReference DotNetPath = null)
{
DotNetTaskParameters Parameters = new DotNetTaskParameters();
Parameters.Arguments = Arguments;
Parameters.BaseDir = BaseDir?.FullName;
Parameters.Environment = Environment;
Parameters.EnvironmentFile = EnvironmentFile?.FullName;
Parameters.ErrorLevel = ErrorLevel;
Parameters.DotNetPath = DotNetPath;
await ExecuteAsync(new DotNetTask(Parameters));
}
}
}