99 lines
No EOL
3.3 KiB
C#
99 lines
No EOL
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using Packager.Parameters;
|
|
|
|
namespace Packager.Tasks
|
|
{
|
|
/// <summary>
|
|
/// Let C be a collection of N paths to some files, and let K be an strictly positive integer.
|
|
/// This task will make K groups of file paths from C, and, for each group, merge the content of the files into one new file.
|
|
/// In the end, this task produce K files, and the paths to theses files will be the output.
|
|
/// </summary>
|
|
public class MergeFileList : Task
|
|
{
|
|
/// <summary>
|
|
/// Collection of paths.
|
|
/// </summary>
|
|
[Input(nameof(FilePaths), IOCardinality.Multi, IOType.File)]
|
|
public ReferenceList FilePaths { get; set; }
|
|
|
|
/// <summary>
|
|
/// Number of groups to make.
|
|
/// </summary>
|
|
[Input(nameof(GroupsCount), IOCardinality.Single, IOType.String)]
|
|
public SingleReference GroupsCount { get; set; }
|
|
|
|
/// <summary>
|
|
/// Collection of paths to files containing the merge of each group.
|
|
/// </summary>
|
|
[Output(nameof(MergedFilePaths), IOType.File)]
|
|
public ReferenceList MergedFilePaths = new ReferenceList();
|
|
|
|
/// <summary>
|
|
/// Collection of names of each grouping.
|
|
/// </summary>
|
|
[Output(nameof(GroupNames), IOType.String)]
|
|
public ReferenceList GroupNames = new ReferenceList();
|
|
|
|
public MergeFileList()
|
|
{
|
|
FilePaths = new ReferenceList();
|
|
GroupsCount = new SingleReference();
|
|
}
|
|
|
|
protected override void StartInternal(BuildConfiguration profile, cookerWrapper cooker)
|
|
{
|
|
var filePaths = FilePaths.ToList();
|
|
|
|
try
|
|
{
|
|
var numberOfMaps = int.Parse(GroupsCount.Value);
|
|
|
|
var bundleFileList = Chunkify(filePaths, numberOfMaps);
|
|
|
|
var bundleIndex = 0;
|
|
foreach (var bundleFile in bundleFileList)
|
|
{
|
|
var outputFile = Path.Combine(TaskOutputFolder, $"Bundle_{bundleIndex}.txt");
|
|
File.WriteAllLines(outputFile, GetContent(bundleFile));
|
|
|
|
MergedFilePaths.Add(Path.Combine(TaskOutputFolder, outputFile));
|
|
GroupNames.Add($"Bundle_{bundleIndex}");
|
|
|
|
bundleIndex++;
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogMessage(true, exception.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Splits a collection into multiple ones of a specific size
|
|
/// </summary>
|
|
/// <param name="originalList"></param>
|
|
/// <param name="chunkSize"></param>
|
|
/// <returns></returns>
|
|
public static IEnumerable<List<T>> Chunkify<T>(List<T> originalList, int chunkSize)
|
|
{
|
|
var cur = 0;
|
|
var total = originalList.Count;
|
|
|
|
var listOfList = new List<List<T>>(total / chunkSize + 1);
|
|
while (cur < total)
|
|
{
|
|
var nextSize = Math.Min(chunkSize, total - cur);
|
|
var list = new List<T>(nextSize);
|
|
list.AddRange(originalList.GetRange(cur, nextSize));
|
|
listOfList.Add(list);
|
|
cur += nextSize;
|
|
}
|
|
|
|
return listOfList;
|
|
}
|
|
|
|
}
|
|
} |