using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using UnrealBuildTool;
namespace AutomationTool.Tasks
{
///
/// Parameters for the submit task
///
public class SubmitTaskParameters
{
///
/// The description for the submitted changelist
///
[TaskParameter]
public string Description;
///
/// The files to submit
///
[TaskParameter]
public string Files;
///
/// The filetype for the submitted files
///
[TaskParameter(Optional = true)]
public string FileType;
///
/// The workspace name. If specified, a new workspace will be created using the given stream and root directory to submit the files.
///
[TaskParameter(Optional=true)]
public string Workspace;
///
/// The stream for the workspace. If not specified, defaults to the current stream.
///
[TaskParameter(Optional = true)]
public string Stream;
///
/// Root directory for the stream. If not specified, defaults to the current root directory.
///
[TaskParameter(Optional = true)]
public string RootDir;
}
///
/// Task which submits a the version files in the current branch
///
[TaskElement("Submit", typeof(SubmitTaskParameters))]
public class SubmitTask : CustomTask
{
///
/// Parameters for the task
///
SubmitTaskParameters Parameters;
///
/// Construct a version task
///
/// Parameters for this task
public SubmitTask(SubmitTaskParameters 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
/// True if the task succeeded
public override bool Execute(JobContext Job, HashSet BuildProducts, Dictionary> TagNameToFileSet)
{
HashSet Files = ResolveFilespec(CommandUtils.RootDirectory, Parameters.Files, TagNameToFileSet);
if (CommandUtils.AllowSubmit && Files.Count > 0)
{
// Get the connection that we're going to submit with
P4Connection SubmitP4 = CommandUtils.P4;
if (Parameters.Workspace != null)
{
// Create a brand new workspace
P4ClientInfo Client = new P4ClientInfo();
Client.Owner = Environment.UserName;
Client.Host = Environment.MachineName;
Client.Stream = Parameters.Stream ?? CommandUtils.P4Env.BuildRootP4;
Client.RootPath = Parameters.RootDir ?? CommandUtils.RootDirectory.FullName;
Client.Name = Parameters.Workspace;
Client.Options = P4ClientOption.NoAllWrite | P4ClientOption.Clobber | P4ClientOption.NoCompress | P4ClientOption.Unlocked | P4ClientOption.NoModTime | P4ClientOption.RmDir;
Client.LineEnd = P4LineEnd.Local;
CommandUtils.P4.CreateClient(Client, AllowSpew: false);
// Create a new connection for it
SubmitP4 = new P4Connection(Client.Owner, Client.Name);
}
// Get the latest version of it
int NewCL = SubmitP4.CreateChange(Description: Parameters.Description);
foreach(FileReference File in Files)
{
SubmitP4.Revert(String.Format("-k \"{0}\"", File.FullName));
SubmitP4.Sync(String.Format("-k \"{0}\"", File.FullName), AllowSpew: false);
SubmitP4.Add(NewCL, String.Format("\"{0}\"", File.FullName));
SubmitP4.Edit(NewCL, String.Format("\"{0}\"", File.FullName));
if (Parameters.FileType != null)
{
SubmitP4.P4(String.Format("reopen -t \"{0}\" \"{1}\"", Parameters.FileType, File.FullName), AllowSpew: false);
}
}
// Submit it
int SubmittedCL;
SubmitP4.Submit(NewCL, out SubmittedCL);
if (SubmittedCL <= 0)
{
throw new AutomationException("Submit failed.");
}
CommandUtils.Log("Submitted in changelist {0}", SubmittedCL);
}
return true;
}
///
/// 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 FindTagNamesFromFilespec(Parameters.Files);
}
///
/// 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;
}
}
}