Skip to content

Commit

Permalink
macros 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vlada-shubina committed Nov 9, 2022
1 parent 9c4bbb6 commit b1cd547
Show file tree
Hide file tree
Showing 105 changed files with 1,748 additions and 1,535 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.TemplateEngine.Core.Expressions.Cpp2
{
public class Cpp2StyleEvaluatorDefinition : SharedEvaluatorDefinition<Cpp2StyleEvaluatorDefinition, Cpp2StyleEvaluatorDefinition.Tokens>
{
private static readonly Dictionary<Encoding, ITokenTrie> TokenCache = new Dictionary<Encoding, ITokenTrie>();
private static readonly Dictionary<Encoding, ITokenTrie> TokenCache = new();

public enum Tokens
{
Expand Down Expand Up @@ -80,7 +80,7 @@ protected override ITokenTrie GetSymbols(IProcessorState processor)
{
if (!TokenCache.TryGetValue(processor.Encoding, out ITokenTrie tokens))
{
TokenTrie trie = new TokenTrie();
TokenTrie trie = new();

//Logic
trie.AddToken(processor.Encoding.GetBytes("&&"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public abstract class SharedEvaluatorDefinition<TSelf, TTokens>
where TSelf : SharedEvaluatorDefinition<TSelf, TTokens>, new()
where TTokens : struct
{
private static readonly TSelf Instance = new TSelf();
private static readonly TSelf Instance = new();
private static readonly IOperatorMap<Operators, TTokens> Map = Instance.GenerateMap();
private static readonly bool DereferenceInLiteralsSetting = Instance.DereferenceInLiterals;
private static readonly string NullToken = Instance.NullTokenValue;
Expand Down Expand Up @@ -90,10 +90,10 @@ public static bool EvaluateFromString(ILogger logger, string text, IVariableColl
/// <returns></returns>
public static bool EvaluateFromString(ILogger logger, string text, IVariableCollection variables, out string faultedMessage, HashSet<string> referencedVariablesKeys = null)
{
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(text)))
using (MemoryStream res = new MemoryStream())
using (MemoryStream ms = new(Encoding.UTF8.GetBytes(text)))
using (MemoryStream res = new())
{
EngineConfig cfg = new EngineConfig(logger, variables);
EngineConfig cfg = new(logger, variables);
IProcessorState state = new ProcessorState(ms, res, (int)ms.Length, (int)ms.Length, cfg, NoOperationProviders);
int len = (int)ms.Length;
int pos = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.TemplateEngine.Abstractions;

namespace Microsoft.TemplateEngine.Orchestrator.RunnableProjects.Abstractions
{
/// <summary>
/// interface for macros created via generated symbols.
/// An interface for macros created that can create the config from other config (deffered config).
/// </summary>
[Obsolete("Use IGeneratedSymbolConfig{T} instead.")]
public interface IDeferredMacro : IMacro
{
/// <summary>
/// Creates <see cref="IMacroConfig"/> from <paramref name="rawConfig"/>.
/// </summary>
/// <remarks>
/// Deprecated as <see cref="IMacro"/> can process only own configuration. Use generic version of interface and <see cref="IGeneratedSymbolMacro{T}.CreateConfig(IEngineEnvironmentSettings, IGeneratedSymbolConfig)"/> instead.
/// </remarks>
[Obsolete("Use IGeneratedSymbolConfig{T}.Evaluate or IGeneratedSymbolConfig{T}.CreateConfig instead")]
IMacroConfig CreateConfig(IEngineEnvironmentSettings environmentSettings, IMacroConfig rawConfig);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;

namespace Microsoft.TemplateEngine.Orchestrator.RunnableProjects.Abstractions
{
/// <summary>
/// Represents the configuration of <see cref="IGeneratedSymbolMacro"/> and <see cref="IGeneratedSymbolMacro{T}"/>.
/// </summary>
public interface IGeneratedSymbolConfig : IMacroConfig
{
/// <summary>
/// Gets data type of the variable to be created.
/// </summary>
string DataType { get; }

/// <summary>
/// Gets the collection of additional macro parameters, where key is a parameter name, and value is a JSON value serialized to string.
/// </summary>
IReadOnlyDictionary<string, string> Parameters { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Core.Contracts;

namespace Microsoft.TemplateEngine.Orchestrator.RunnableProjects.Abstractions
{
/// <summary>
/// An interface for macro created via generated symbols.
/// </summary>
public interface IGeneratedSymbolMacro : IMacro
{
/// <summary>
/// Evaluates macro defined via generated symbol (<see cref="IGeneratedSymbolConfig"/>).
/// The result of macro evaluation is modification of variable collection <paramref name="variables"/>.
/// </summary>
void Evaluate(IEngineEnvironmentSettings environmentSettings, IVariableCollection variables, IGeneratedSymbolConfig generatedSymbolConfig);
}

/// <summary>
/// An interface for macro created via generated symbols, that can create config from generated symbol config (<see cref="IGeneratedSymbolConfig"/>).
/// </summary>
/// <typeparam name="T">The type of macro config.</typeparam>
public interface IGeneratedSymbolMacro<T> : IGeneratedSymbolMacro, IMacro<T>
where T : IMacroConfig
{
/// <summary>
/// Creates macro config from <see cref="IGeneratedSymbolConfig"/>.
/// </summary>
T CreateConfig(IEngineEnvironmentSettings environmentSettings, IGeneratedSymbolConfig generatedSymbolConfig);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Core.Contracts;

namespace Microsoft.TemplateEngine.Orchestrator.RunnableProjects.Abstractions
{
/// <summary>
/// Represents a macro that can modify variable collection before template instantiation.
/// </summary>
public interface IMacro : IIdentifiedComponent
{
/// <summary>
/// Gets macro type. The type identifies the macro and should be unique.
/// </summary>
string Type { get; }

/// <summary>
/// Evaluates the macro based on <paramref name="config"/>. The result is modification of variable collection <paramref name="vars"/>.
/// </summary>
[Obsolete("Use IMacro<T>.Evaluate instead")]
void EvaluateConfig(IEngineEnvironmentSettings environmentSettings, IVariableCollection vars, IMacroConfig config);
}

/// <summary>
/// Represents a macro that can modify variable collection before template instantiation.
/// </summary>
public interface IMacro<T> : IMacro where T : IMacroConfig
{
/// <summary>
/// Evaluates the macro based on <paramref name="config"/>. The result is modification of variable collection <paramref name="variables"/>.
/// </summary>
void Evaluate(IEngineEnvironmentSettings environmentSettings, IVariableCollection variables, T config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,29 @@ public static class Components
(typeof(IOperationConfig), new ReplacementConfig()),

(typeof(IMacro), CaseChange),
(typeof(IDeferredMacro), CaseChange),
(typeof(IGeneratedSymbolMacro), CaseChange),
(typeof(IMacro), CoalesceMacro),
(typeof(IDeferredMacro), CoalesceMacro),
(typeof(IGeneratedSymbolMacro), CoalesceMacro),
(typeof(IMacro), ConstantMacro),
(typeof(IDeferredMacro), ConstantMacro),
(typeof(IGeneratedSymbolMacro), ConstantMacro),
(typeof(IMacro), new EvaluateMacro()),
(typeof(IMacro), GeneratePortNumberMacro),
(typeof(IDeferredMacro), GeneratePortNumberMacro),
(typeof(IGeneratedSymbolMacro), GeneratePortNumberMacro),
(typeof(IMacro), GuidMacro),
(typeof(IDeferredMacro), GuidMacro),
(typeof(IGeneratedSymbolMacro), GuidMacro),
(typeof(IMacro), JoinMacro),
(typeof(IDeferredMacro), JoinMacro),
(typeof(IGeneratedSymbolMacro), JoinMacro),
(typeof(IMacro), NowMacro),
(typeof(IDeferredMacro), NowMacro),
(typeof(IGeneratedSymbolMacro), NowMacro),
(typeof(IMacro), new ProcessValueFormMacro()),
(typeof(IMacro), RandomMacro),
(typeof(IDeferredMacro), RandomMacro),
(typeof(IGeneratedSymbolMacro), RandomMacro),
(typeof(IMacro), RegexMacro),
(typeof(IDeferredMacro), RegexMacro),
(typeof(IGeneratedSymbolMacro), RegexMacro),
(typeof(IMacro), RegexMatchMacro),
(typeof(IDeferredMacro), RegexMatchMacro),
(typeof(IGeneratedSymbolMacro), RegexMatchMacro),
(typeof(IMacro), SwitchMacro),
(typeof(IDeferredMacro), SwitchMacro),
(typeof(IGeneratedSymbolMacro), SwitchMacro),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class DerivedSymbol : BaseValueSymbol
{
internal const string TypeName = "derived";

internal DerivedSymbol(string name, string valueTransform, string valueSource) : base(name, null)
internal DerivedSymbol(string name, string valueTransform, string valueSource, string? replaces = null) : base(name, replaces)
{
if (string.IsNullOrWhiteSpace(valueTransform))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using Microsoft.TemplateEngine.Orchestrator.RunnableProjects.Abstractions;
using Microsoft.TemplateEngine.Utils;
using Newtonsoft.Json.Linq;

Expand All @@ -11,7 +12,7 @@ namespace Microsoft.TemplateEngine.Orchestrator.RunnableProjects.ConfigModel
/// <summary>
/// Defines the symbol of type "generated".
/// </summary>
public sealed class GeneratedSymbol : BaseReplaceSymbol
public sealed class GeneratedSymbol : BaseReplaceSymbol, IGeneratedSymbolConfig
{
internal const string TypeName = "generated";

Expand All @@ -21,21 +22,26 @@ internal GeneratedSymbol(string name, string generator) : base(name, null)
{
throw new ArgumentException($"'{nameof(generator)}' cannot be null or whitespace.", nameof(generator));
}

Generator = generator;
}

internal GeneratedSymbol(string name, string generator, IReadOnlyDictionary<string, string> parameters, string? dataType = null) : this(name, generator)
{
DataType = dataType;
Parameters = parameters;
}

internal GeneratedSymbol(string name, JObject jObject) : base(jObject, name)
{
string? generator = jObject.ToString(nameof(Generator));
if (string.IsNullOrWhiteSpace(generator))
{
throw new TemplateAuthoringException(string.Format(LocalizableStrings.SymbolModel_Error_MandatoryPropertyMissing, name, GeneratedSymbol.TypeName, nameof(Generator).ToLowerInvariant()), name);
throw new TemplateAuthoringException(string.Format(LocalizableStrings.SymbolModel_Error_MandatoryPropertyMissing, name, TypeName, nameof(Generator).ToLowerInvariant()), name);
}

Generator = generator!;
DataType = jObject.ToString(nameof(DataType));
Parameters = jObject.ToJTokenStringDictionary(StringComparer.Ordinal, nameof(Parameters));
Parameters = jObject.ToJTokenStringDictionary(StringComparer.OrdinalIgnoreCase, nameof(Parameters));
}

/// <inheritdoc/>
Expand All @@ -56,6 +62,12 @@ internal GeneratedSymbol(string name, JObject jObject) : base(jObject, name)
/// - the key is a parameter name
/// - the value is a JSON value.
/// </summary>
public IReadOnlyDictionary<string, string> Parameters { get; internal init; } = new Dictionary<string, string>();
public IReadOnlyDictionary<string, string> Parameters { get; internal init; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

string IGeneratedSymbolConfig.DataType => DataType ?? "string";

string IMacroConfig.VariableName => Name;

string IMacroConfig.Type => Generator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.TemplateEngine.Orchestrator.RunnableProjects.ConfigModel
{
public class SymbolValueFormsModel
{
private SymbolValueFormsModel(IReadOnlyList<string> globalForms)
internal SymbolValueFormsModel(IReadOnlyList<string> globalForms)
{
GlobalForms = globalForms;
}
Expand Down
Loading

0 comments on commit b1cd547

Please sign in to comment.