// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Xml;
using EpicGames.Core;
namespace AutomationTool.Tasks
{
///
/// Parameters for a Docker task
///
public class DockerTaskParameters
{
///
/// Docker command line arguments
///
[TaskParameter]
public string Arguments { get; set; }
///
/// Environment variables to set
///
[TaskParameter(Optional = true)]
public string Environment { get; set; }
///
/// File to read environment variables from
///
[TaskParameter(Optional = true)]
public string EnvironmentFile { get; set; }
///
/// Base directory for running the command
///
[TaskParameter(Optional = true)]
public string WorkingDir { get; set; }
}
///
/// Spawns Docker and waits for it to complete.
///
[TaskElement("Docker", typeof(DockerTaskParameters))]
public class DockerTask : SpawnTaskBase
{
///
/// Parameters for this task
///
readonly DockerTaskParameters _parameters;
///
/// Construct a Docker task
///
/// Parameters for the task
public DockerTask(DockerTaskParameters 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)
{
await ExecuteAsync(GetDockerExecutablePath(), _parameters.Arguments, envVars: ParseEnvVars(_parameters.Environment, _parameters.EnvironmentFile), workingDir: _parameters.WorkingDir);
}
///
/// 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;
}
///
/// Resolve path to Docker executable by using the optional env var "UE_DOCKER_EXEC_PATH"
/// Will default to "docker" if not set. Allows supporting alternative Docker implementations such as Podman.
///
/// Path to Docker executable
public static string GetDockerExecutablePath()
{
return Environment.GetEnvironmentVariable("UE_DOCKER_EXEC_PATH") ?? "docker";
}
}
}