// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Xml; using EpicGames.Core; using Microsoft.Extensions.Logging; using UnrealBuildBase; 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 { get; set; } /// /// The pattern(s) to copy from (for example, Engine/*.txt). /// [TaskParameter(ValidationType = TaskParameterValidationType.FileSpec)] public string From { get; set; } /// /// The directory to copy to. /// [TaskParameter(ValidationType = TaskParameterValidationType.FileSpec)] public string To { get; set; } /// /// Optionally if files should be overwritten, defaults to false. /// [TaskParameter(Optional = true)] public bool Overwrite { get; set; } = false; /// /// Tag to be applied to build products of this task. /// [TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.TagList)] public string Tag { get; set; } /// /// Whether or not to throw an error if no files were found to copy /// [TaskParameter(Optional = true)] public bool ErrorIfNotFound { get; set; } = false; } /// /// Moves files from one directory to another. /// [TaskElement("Move", typeof(MoveTaskParameters))] public class MoveTask : BgTaskImpl { readonly MoveTaskParameters _parameters; /// /// Constructor /// /// Parameters for this task public MoveTask(MoveTaskParameters 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 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; try { targetFileToSourceFile = FilePattern.CreateMapping(files, ref sourcePattern, ref targetPattern); // Check we got some files if (targetFileToSourceFile.Count == 0) { if (_parameters.ErrorIfNotFound) { throw new AutomationException("No files found matching '{0}'", 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); } } catch (FilePatternSourceFileMissingException ex) { if (_parameters.ErrorIfNotFound) { throw new AutomationException(ex, "Error while trying to create file pattern match for '{0}': {1}", sourcePattern, ex.Message); } else { Logger.LogInformation(ex, "Error while trying to create file pattern match for '{SourcePattern}': {Message}", sourcePattern, ex.Message); } } 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); } } }