JD2022-TU1/main/tools/apps/Packager/Tasks/MergeMapDependenciesTask.cs

77 lines
No EOL
2.8 KiB
C#

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
{
/// <summary>
/// Collection of paths.
/// </summary>
[Input(nameof(FilePaths), IOCardinality.Multi, IOType.File)]
public ReferenceList FilePaths { get; set; }
/// <summary>
/// Collection of paths to files containing the dependencies of each map.
/// </summary>
[Output(nameof(MergedMapDependenciesFilePaths), IOType.File)]
public ReferenceList MergedMapDependenciesFilePaths = new ReferenceList();
public MergeMapDependenciesTask()
{
FilePaths = new ReferenceList();
}
protected override void StartInternal(BuildConfiguration profile, cookerWrapper cooker)
{
Dictionary<string, HashSet<string> > mapsDependencies = new Dictionary<string, HashSet<string>>();
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<string>());
}
mapsDependencies[mapName].Add(filePath);
}
foreach (string mapName in mapsDependencies.Keys)
{
HashSet<string> mapFiles = mapsDependencies[mapName];
IEnumerable<string> 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);
}
}
}
}