// Copyright Epic Games, Inc. All Rights Reserved.
using EpicGames.Core;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using UnrealBuildBase;
using Microsoft.Extensions.Logging;
namespace AutomationTool.Tasks
{
///
/// Parameters for a Git-Checkout task
///
public class GitCloneTaskParameters
{
///
/// Directory for the repository
///
[TaskParameter]
public string Dir;
///
/// The remote to add
///
[TaskParameter(Optional = true)]
public string Remote;
///
/// The branch to check out on the remote
///
[TaskParameter]
public string Branch;
///
/// Configuration file for the repo. This can be used to set up a remote to be fetched and/or provide credentials.
///
[TaskParameter(Optional = true)]
public string ConfigFile;
}
///
/// Clones a Git repository into a local path.
///
[TaskElement("Git-Clone", typeof(GitCloneTaskParameters))]
public class GitCloneTask : BgTaskImpl
{
///
/// Parameters for this task
///
GitCloneTaskParameters Parameters;
///
/// Construct a Git task
///
/// Parameters for the task
public GitCloneTask(GitCloneTaskParameters 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 GitExe = CommandUtils.FindToolInPath("git");
if(GitExe == null)
{
throw new AutomationException("Unable to find path to Git. Check you have it installed, and it is on your PATH.");
}
DirectoryReference Dir = ResolveDirectory(Parameters.Dir);
Logger.LogInformation("Cloning Git repository into {Dir}", Parameters.Dir);
using (LogIndentScope Scope = new LogIndentScope(" "))
{
DirectoryReference GitDir = DirectoryReference.Combine(Dir, ".git");
if (!FileReference.Exists(FileReference.Combine(GitDir, "HEAD")))
{
await RunGitAsync(GitExe, $"init \"{Dir}\"", Unreal.RootDirectory);
}
if (Parameters.ConfigFile != null)
{
CommandUtils.CopyFile(Parameters.ConfigFile, FileReference.Combine(GitDir, "config").FullName);
}
if (Parameters.Remote != null)
{
await RunGitAsync(GitExe, $"remote add origin {Parameters.Remote}", Dir);
}
await RunGitAsync(GitExe, "clean -dxf", Dir);
await RunGitAsync(GitExe, "fetch --all", Dir);
await RunGitAsync(GitExe, $"reset --hard {Parameters.Branch}", Dir);
}
}
///
/// Runs a git command
///
///
///
///
Task RunGitAsync(FileReference ToolFile, string Arguments, DirectoryReference WorkingDir)
{
IProcessResult Result = CommandUtils.Run(ToolFile.FullName, Arguments, WorkingDir: WorkingDir.FullName);
if (Result.ExitCode != 0)
{
throw new AutomationException("Git terminated with an exit code indicating an error ({0})", Result.ExitCode);
}
return Task.CompletedTask;
}
///
/// 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;
}
}
}