// Copyright Epic Games, Inc. All Rights Reserved.
using System.Collections.Generic;
using System.Linq;
using EpicGames.BuildGraph.Expressions;
using EpicGames.Core;
namespace EpicGames.BuildGraph
{
///
/// Context for executing a node method
///
public abstract class BgContext
{
///
/// The stream executing the current build
///
public abstract string Stream { get; }
///
/// Changelist being built
///
public abstract int Change { get; }
///
/// The code changelist currently being built
///
public abstract int CodeChange { get; }
///
/// Version number for the engine
///
public abstract (int Major, int Minor, int Patch) EngineVersion { get; }
///
/// Whether this machine is a builder
///
public abstract bool IsBuildMachine { get; }
///
/// All outputs for the node
///
public HashSet BuildProducts { get; } = new HashSet();
readonly Dictionary _tagNameToFileSet;
///
/// Constructor
///
protected BgContext(Dictionary tagNameToFileSet)
{
_tagNameToFileSet = tagNameToFileSet;
}
///
/// Resolve a boolean expression to a value
///
/// The boolean expression
/// Value of the expression
public bool Get(BgBool expr) => ((BgBoolConstantExpr)expr).Value;
///
/// Resolve an integer expression to a value
///
/// The integer expression
/// Value of the expression
public int Get(BgInt expr) => ((BgIntConstantExpr)expr).Value;
///
/// Resolve a string expression to a value
///
/// The string expression
/// Value of the expression
public string Get(BgString expr) => ((BgStringConstantExpr)expr).Value;
///
/// Resolves an enum expression to a value
///
/// The enum type
/// Enum expression
/// The enum value
public TEnum Get(BgEnum expr) where TEnum : struct => ((BgEnumConstantExpr)expr).Value;
///
/// Resolve a list of enums to a value
///
/// The enum type
/// Enum expression
/// The enum value
public List Get(BgList> expr) where TEnum : struct => ((BgListConstantExpr>)expr).Value.Select(x => ((BgEnumConstantExpr)x).Value).ToList();
///
/// Resolve a list of strings
///
/// List expression
///
public List Get(BgList expr) => (((BgListConstantExpr)expr).Value).ConvertAll(x => ((BgStringConstantExpr)x).Value);
///
/// Resolve a file set
///
/// The token expression
/// Set of files for the token
public FileSet Get(BgFileSet fileSet)
{
FileSet result = FileSet.Empty;
BgFileSetInputExpr input = (BgFileSetInputExpr)fileSet;
foreach (BgNodeOutput output in input.Outputs)
{
FileSet set = _tagNameToFileSet[output.TagName];
result = FileSet.Union(result, set);
}
return result;
}
///
/// Resolve a file set
///
/// The token expression
/// Set of files for the token
public FileSet Get(BgList fileSets)
{
FileSet result = FileSet.Empty;
foreach (BgFileSet fileSet in ((BgListConstantExpr)fileSets).Value)
{
result += Get(fileSet);
}
return result;
}
}
}