// Copyright Epic Games, Inc. All Rights Reserved.
using EpicGames.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using AutomationTool;
using UnrealBuildBase;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace AutomationTool.Tasks
{
///
/// Parameters for a Docker-Compose task
///
public class DockerComposeUpTaskParameters
{
///
/// Path to the docker-compose file
///
[TaskParameter]
public string File;
///
/// Arguments for the command
///
[TaskParameter(Optional = true)]
public string Arguments;
}
///
/// Spawns Docker and waits for it to complete.
///
[TaskElement("Docker-Compose-Up", typeof(DockerComposeUpTaskParameters))]
public class DockerComposeUpTask : SpawnTaskBase
{
///
/// Parameters for this task
///
DockerComposeUpTaskParameters Parameters;
///
/// Construct a Docker-Compose task
///
/// Parameters for the task
public DockerComposeUpTask(DockerComposeUpTaskParameters 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 LogFile = FileReference.Combine(Unreal.RootDirectory, "Engine/Programs/AutomationTool/Saved/Logs/docker-compose-logs.txt");
string[] Lines =
{
$"docker-compose -f {Parameters.File.QuoteArgument()} logs --no-color > {LogFile.FullName.QuoteArgument()}",
$"docker-compose -f {Parameters.File.QuoteArgument()} down"
};
await AddCleanupCommandsAsync(Lines);
StringBuilder Arguments = new StringBuilder("--ansi never ");
if (!String.IsNullOrEmpty(Parameters.File))
{
Arguments.Append($"--file {Parameters.File.QuoteArgument()} ");
}
Arguments.Append("up --detach");
if (!String.IsNullOrEmpty(Parameters.Arguments))
{
Arguments.Append($" {Parameters.Arguments}");
}
Logger.LogInformation("Running docker compose {Arguments}", Arguments.ToString());
using (LogIndentScope Scope = new LogIndentScope(" "))
{
await SpawnTaskBase.ExecuteAsync("docker-compose", Arguments.ToString());
}
}
///
/// 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()
{
return 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()
{
return Enumerable.Empty();
}
}
}