// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Xml;
using EpicGames.Core;
using UnrealBuildTool;
namespace AutomationTool.Tasks
{
///
/// Parameters for a ModifyConfig task
///
public class ModifyConfigTaskParameters
{
///
/// Path to the config file
///
[TaskParameter(ValidationType = TaskParameterValidationType.FileSpec)]
public string File { get; set; }
///
/// The section name to modify
///
[TaskParameter]
public string Section { get; set; }
///
/// The property name to set
///
[TaskParameter]
public string Key { get; set; }
///
/// The property value to set
///
[TaskParameter]
public string Value { get; set; }
///
/// Tag to be applied to the extracted files
///
[TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.TagList)]
public string Tag { get; set; }
}
///
/// Modifies a config file
///
[TaskElement("ModifyConfig", typeof(ModifyConfigTaskParameters))]
public class ModifyConfigTask : BgTaskImpl
{
readonly ModifyConfigTaskParameters _parameters;
///
/// Constructor
///
/// Parameters for this task
public ModifyConfigTask(ModifyConfigTaskParameters 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)
{
FileReference configFileLocation = ResolveFile(_parameters.File);
ConfigFile configFile;
if (FileReference.Exists(configFileLocation))
{
configFile = new ConfigFile(configFileLocation);
}
else
{
configFile = new ConfigFile();
}
ConfigFileSection section = configFile.FindOrAddSection(_parameters.Section);
section.Lines.RemoveAll(x => String.Equals(x.Key, _parameters.Key, StringComparison.OrdinalIgnoreCase));
section.Lines.Add(new ConfigLine(ConfigLineAction.Set, _parameters.Key, _parameters.Value));
FileReference.MakeWriteable(configFileLocation);
configFile.Write(configFileLocation);
// Apply the optional tag to the produced archive
foreach (string tagName in FindTagNamesFromList(_parameters.Tag))
{
FindOrAddTagSet(tagNameToFileSet, tagName).Add(configFileLocation);
}
// Add the archive to the set of build products
buildProducts.Add(configFileLocation);
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()
{
return FindTagNamesFromFilespec(_parameters.File);
}
///
/// 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);
}
}
}