// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved. using AutomationTool; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Xml; using Tools.DotNETCommon; using UnrealBuildTool; namespace BuildGraph.Tasks { /// /// Parameters for a copy task /// public class RenameTaskParameters { /// /// The file or files to rename /// [TaskParameter(ValidationType = TaskParameterValidationType.FileSpec)] public string Files; /// /// The current file name, or pattern to match (eg. *.txt). Should not include any path separators. /// [TaskParameter(Optional = true)] public string From; /// /// The new name for the file(s). Should not include any path separators. /// [TaskParameter] public string To; /// /// Tag to be applied to the renamed files /// [TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.TagList)] public string Tag; } /// /// Renames a file, or group of files. /// [TaskElement("Rename", typeof(RenameTaskParameters))] public class RenameTask : CustomTask { /// /// Parameters for this task /// RenameTaskParameters Parameters; /// /// Constructor /// /// Parameters for this task public RenameTask(RenameTaskParameters 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 void Execute(JobContext Job, HashSet BuildProducts, Dictionary> TagNameToFileSet) { // Get the pattern to match against. If it's a simple pattern (eg. *.cpp, Engine/Build/...), automatically infer the source wildcard string FromPattern = Parameters.From; if (FromPattern == null) { List Patterns = SplitDelimitedList(Parameters.Files); if (Patterns.Count != 1 || Patterns[0].StartsWith("#")) { throw new AutomationException("Missing 'From' attribute specifying pattern to match source files against"); } FromPattern = Patterns[0]; int SlashIdx = FromPattern.LastIndexOfAny(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }); if (SlashIdx != -1) { FromPattern = FromPattern.Substring(SlashIdx + 1); } if (FromPattern.StartsWith("...")) { FromPattern = "*" + FromPattern.Substring(3); } } // Convert the source pattern into a regex string EscapedFromPattern = "^" + Regex.Escape(FromPattern) + "$"; EscapedFromPattern = EscapedFromPattern.Replace("\\*", "(.*)"); EscapedFromPattern = EscapedFromPattern.Replace("\\?", "(.)"); Regex FromRegex = new Regex(EscapedFromPattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); // Split the output pattern into fragments that we can insert captures between string[] FromFragments = FromPattern.Split('*', '?'); string[] ToFragments = Parameters.To.Split('*', '?'); if(FromFragments.Length < ToFragments.Length) { throw new AutomationException("Too few capture groups in source pattern '{0}' to rename to '{1}'", FromPattern, Parameters.To); } // Find the input files HashSet InputFiles = ResolveFilespec(CommandUtils.RootDirectory, Parameters.Files, TagNameToFileSet); // Find all the corresponding output files Dictionary RenameFiles = new Dictionary(); foreach (FileReference InputFile in InputFiles) { Match Match = FromRegex.Match(InputFile.GetFileName()); if (Match.Success) { StringBuilder OutputName = new StringBuilder(ToFragments[0]); for (int Idx = 1; Idx < ToFragments.Length; Idx++) { OutputName.Append(Match.Groups[Idx].Value); OutputName.Append(ToFragments[Idx]); } RenameFiles[InputFile] = FileReference.Combine(InputFile.Directory, OutputName.ToString()); } } // Print out everything we're going to do foreach(KeyValuePair Pair in RenameFiles) { CommandUtils.RenameFile(Pair.Key.FullName, Pair.Value.FullName, true); } // Add the build product BuildProducts.UnionWith(RenameFiles.Values); // Apply the optional output tag to them foreach(string TagName in FindTagNamesFromList(Parameters.Tag)) { FindOrAddTagSet(TagNameToFileSet, TagName).UnionWith(RenameFiles.Values); } } /// /// 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() { return FindTagNamesFromList(Parameters.Tag); } } }