using System; using System.Collections.Generic; using System.IO; using System.Linq; using Packager.Parameters; using Ubisoft.P4API.Exceptions; namespace Packager.Tasks { public class MergeMapDependenciesTask : Task { /// /// Collection of paths. /// [Input(nameof(FilePaths), IOCardinality.Multi, IOType.File)] public ReferenceList FilePaths { get; set; } /// /// Collection of paths to files containing the dependencies of each map. /// [Output(nameof(MergedMapDependenciesFilePaths), IOType.File)] public ReferenceList MergedMapDependenciesFilePaths = new ReferenceList(); public MergeMapDependenciesTask() { FilePaths = new ReferenceList(); } protected override void StartInternal(BuildConfiguration profile, cookerWrapper cooker) { Dictionary > mapsDependencies = new Dictionary>(); foreach (string filePath in FilePaths) { if (!File.Exists(filePath)) { throw new FileNotFoundException($"Input file was not found at \"{filePath}\"."); } string fileName = Path.GetFileName(filePath) ?? string.Empty; int underscoreIndex = fileName.IndexOf('_'); if (underscoreIndex < 0) { throw new ArgumentException($"Unable to find character '_' in filename '{fileName}' from path \"{filePath}\"."); } string mapName = fileName.Remove(underscoreIndex); Logger.LogInfo($"File \'{filePath}\' belongs to map \'{mapName}\'."); if (!mapsDependencies.ContainsKey(mapName)) { mapsDependencies.Add(mapName, new HashSet()); } mapsDependencies[mapName].Add(filePath); } foreach (string mapName in mapsDependencies.Keys) { HashSet mapFiles = mapsDependencies[mapName]; IEnumerable filesLines = mapFiles.SelectMany(File.ReadAllLines).Where(line => !string.IsNullOrWhiteSpace(line)); string mergedFileContent = string.Join(Environment.NewLine, filesLines); string mergedFilename = $"{mapName}_merged_dependencies.txt"; string mergedFilePath = Path.Combine(TaskOutputFolder, mergedFilename); File.WriteAllText(mergedFilePath, mergedFileContent); Logger.LogInfo($"Wrote {mapName} dependencies to \'{mergedFilePath}\'."); MergedMapDependenciesFilePaths.Add(mergedFilePath); } } } }