// Copyright Epic Games, Inc. All Rights Reserved. using AutomationTool; using EpicGames.BuildGraph; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using EpicGames.Core; using UnrealBuildTool; using UnrealBuildBase; using Microsoft.Extensions.Logging; using static AutomationTool.CommandUtils; namespace AutomationTool.Tasks { /// /// Parameters for a move task /// public class MoveTaskParameters { /// /// Optional filter to be applied to the list of input files. /// [TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.FileSpec)] public string Files; /// /// The pattern(s) to copy from (for example, Engine/*.txt). /// [TaskParameter(ValidationType = TaskParameterValidationType.FileSpec)] public string From; /// /// The directory to copy to. /// [TaskParameter(ValidationType = TaskParameterValidationType.FileSpec)] public string To; /// /// Optionally if files should be overwritten, defaults to false. /// [TaskParameter(Optional = true)] public bool Overwrite = false; /// /// Tag to be applied to build products of this task. /// [TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.TagList)] public string Tag; /// /// Whether or not to throw an error if no files were found to copy /// [TaskParameter(Optional = true)] public bool ErrorIfNotFound = false; } /// /// Moves files from one directory to another. /// [TaskElement("Move", typeof(MoveTaskParameters))] public class MoveTask : BgTaskImpl { /// /// Parameters for this task /// MoveTaskParameters Parameters; /// /// Constructor /// /// Parameters for this task public MoveTask(MoveTaskParameters 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 Task ExecuteAsync(JobContext Job, HashSet BuildProducts, Dictionary> TagNameToFileSet) { // Parse all the source patterns FilePattern SourcePattern = new FilePattern(Unreal.RootDirectory, Parameters.From); // Parse the target pattern FilePattern TargetPattern = new FilePattern(Unreal.RootDirectory, Parameters.To); // Apply the filter to the source files HashSet Files = null; if(!String.IsNullOrEmpty(Parameters.Files)) { SourcePattern = SourcePattern.AsDirectoryPattern(); Files = ResolveFilespec(SourcePattern.BaseDirectory, Parameters.Files, TagNameToFileSet); } // Build the file mapping Dictionary TargetFileToSourceFile = FilePattern.CreateMapping(Files, ref SourcePattern, ref TargetPattern); // Check we got some files if(TargetFileToSourceFile.Count == 0) { if (Parameters.ErrorIfNotFound) { Logger.LogError("No files found matching '{SourcePattern}'", SourcePattern); } else { Logger.LogInformation("No files found matching '{SourcePattern}'", SourcePattern); } return Task.CompletedTask; } // Copy them all Logger.LogInformation("Moving {Arg0} file{Arg1} from {Arg2} to {Arg3}...", TargetFileToSourceFile.Count, (TargetFileToSourceFile.Count == 1)? "" : "s", SourcePattern.BaseDirectory, TargetPattern.BaseDirectory); CommandUtils.ParallelMoveFiles(TargetFileToSourceFile.Select(x => new KeyValuePair(x.Value, x.Key)), Parameters.Overwrite); // Update the list of build products BuildProducts.UnionWith(TargetFileToSourceFile.Keys); // Apply the optional output tag to them foreach(string TagName in FindTagNamesFromList(Parameters.Tag)) { FindOrAddTagSet(TagNameToFileSet, TagName).UnionWith(TargetFileToSourceFile.Keys); } 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() { foreach(string TagName in FindTagNamesFromFilespec(Parameters.Files)) { yield return TagName; } } /// /// Find all the tags which are modified by this task /// /// The tag names which are modified by this task public override IEnumerable FindProducedTagNames() { return FindTagNamesFromList(Parameters.Tag); } } }