mirror of
https://github.com/actions/runner.git
synced 2025-12-12 05:37:01 +00:00
GitHub Actions Runner
This commit is contained in:
231
src/Sdk/DTObjectTemplating/ObjectTemplating/TemplateContext.cs
Normal file
231
src/Sdk/DTObjectTemplating/ObjectTemplating/TemplateContext.cs
Normal file
@@ -0,0 +1,231 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using GitHub.DistributedTask.Expressions2;
|
||||
using GitHub.DistributedTask.ObjectTemplating.Schema;
|
||||
using GitHub.DistributedTask.ObjectTemplating.Tokens;
|
||||
|
||||
namespace GitHub.DistributedTask.ObjectTemplating
|
||||
{
|
||||
/// <summary>
|
||||
/// Context object that is flowed through while loading and evaluating object templates
|
||||
/// </summary>
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public sealed class TemplateContext
|
||||
{
|
||||
internal CancellationToken CancellationToken { get; set; }
|
||||
|
||||
internal TemplateValidationErrors Errors
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_errors == null)
|
||||
{
|
||||
m_errors = new TemplateValidationErrors();
|
||||
}
|
||||
|
||||
return m_errors;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
m_errors = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Available functions within expression contexts
|
||||
/// </summary>
|
||||
internal IList<IFunctionInfo> ExpressionFunctions
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_expressionFunctions == null)
|
||||
{
|
||||
m_expressionFunctions = new List<IFunctionInfo>();
|
||||
}
|
||||
|
||||
return m_expressionFunctions;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Available values within expression contexts
|
||||
/// </summary>
|
||||
internal IDictionary<String, Object> ExpressionValues
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_expressionValues == null)
|
||||
{
|
||||
m_expressionValues = new Dictionary<String, Object>(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
return m_expressionValues;
|
||||
}
|
||||
}
|
||||
|
||||
internal TemplateMemory Memory { get; set; }
|
||||
|
||||
internal TemplateSchema Schema { get; set; }
|
||||
|
||||
internal IDictionary<String, Object> State
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_state == null)
|
||||
{
|
||||
m_state = new Dictionary<String, Object>(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
return m_state;
|
||||
}
|
||||
}
|
||||
|
||||
internal ITraceWriter TraceWriter { get; set; }
|
||||
|
||||
private IDictionary<String, Int32> FileIds
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_fileIds == null)
|
||||
{
|
||||
m_fileIds = new Dictionary<String, Int32>(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
return m_fileIds;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_fileIds = value;
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> FileNames
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_fileNames == null)
|
||||
{
|
||||
m_fileNames = new List<String>();
|
||||
}
|
||||
|
||||
return m_fileNames;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_fileNames = value;
|
||||
}
|
||||
}
|
||||
|
||||
internal void Error(TemplateValidationError error)
|
||||
{
|
||||
Errors.Add(error);
|
||||
TraceWriter.Error(error.Message);
|
||||
}
|
||||
|
||||
internal void Error(
|
||||
TemplateToken value,
|
||||
Exception ex)
|
||||
{
|
||||
Error(value?.FileId, value?.Line, value?.Column, ex);
|
||||
}
|
||||
|
||||
internal void Error(
|
||||
Int32? fileId,
|
||||
Int32? line,
|
||||
Int32? column,
|
||||
Exception ex)
|
||||
{
|
||||
var prefix = GetErrorPrefix(fileId, line, column);
|
||||
Errors.Add(prefix, ex);
|
||||
TraceWriter.Error(prefix, ex);
|
||||
}
|
||||
|
||||
internal void Error(
|
||||
TemplateToken value,
|
||||
String message)
|
||||
{
|
||||
Error(value?.FileId, value?.Line, value?.Column, message);
|
||||
}
|
||||
|
||||
internal void Error(
|
||||
Int32? fileId,
|
||||
Int32? line,
|
||||
Int32? column,
|
||||
String message)
|
||||
{
|
||||
var prefix = GetErrorPrefix(fileId, line, column);
|
||||
if (!String.IsNullOrEmpty(prefix))
|
||||
{
|
||||
message = $"{prefix} {message}";
|
||||
}
|
||||
|
||||
Errors.Add(message);
|
||||
TraceWriter.Error(message);
|
||||
}
|
||||
|
||||
internal INamedValueInfo[] GetExpressionNamedValues()
|
||||
{
|
||||
if (m_expressionValues?.Count > 0)
|
||||
{
|
||||
return m_expressionValues.Keys.Select(x => new NamedValueInfo<ContextValueNode>(x)).ToArray();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
internal Int32 GetFileId(String file)
|
||||
{
|
||||
if (!FileIds.TryGetValue(file, out Int32 id))
|
||||
{
|
||||
id = FileIds.Count + 1;
|
||||
FileIds.Add(file, id);
|
||||
FileNames.Add(file);
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
internal String GetFileName(Int32 fileId)
|
||||
{
|
||||
return FileNames[fileId - 1];
|
||||
}
|
||||
|
||||
private String GetErrorPrefix(
|
||||
Int32? fileId,
|
||||
Int32? line,
|
||||
Int32? column)
|
||||
{
|
||||
if (fileId != null)
|
||||
{
|
||||
var fileName = GetFileName(fileId.Value);
|
||||
if (line != null && column != null)
|
||||
{
|
||||
return $"{fileName} {TemplateStrings.LineColumn(line, column)}:";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"{fileName}:";
|
||||
}
|
||||
}
|
||||
else if (line != null && column != null)
|
||||
{
|
||||
return $"{TemplateStrings.LineColumn(line, column)}:";
|
||||
}
|
||||
else
|
||||
{
|
||||
return String.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
private TemplateValidationErrors m_errors;
|
||||
private IList<IFunctionInfo> m_expressionFunctions;
|
||||
private IDictionary<String, Object> m_expressionValues;
|
||||
private IDictionary<String, Int32> m_fileIds;
|
||||
private List<String> m_fileNames;
|
||||
private IDictionary<String, Object> m_state;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user