-
Notifications
You must be signed in to change notification settings - Fork 370
/
RegexMacro.cs
41 lines (35 loc) · 2.04 KB
/
RegexMacro.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Orchestrator.RunnableProjects.Abstractions;
namespace Microsoft.TemplateEngine.Orchestrator.RunnableProjects.Macros
{
internal class RegexMacro : BaseGeneratedSymbolMacro<RegexMacroConfig>
{
public override Guid Id { get; } = new Guid("8A4D4937-E23F-426D-8398-3BDBD1873ADB");
public override string Type => "regex";
public override void Evaluate(IEngineEnvironmentSettings environmentSettings, IVariableCollection variableCollection, RegexMacroConfig config)
{
if (!variableCollection.TryGetValue(config.Source, out object working))
{
environmentSettings.Host.Logger.LogDebug("[{macro}]: Source variable '{sourceVar}' was not found, skipping processing for macro '{var}'.", nameof(RegexMacro), config.Source, config.VariableName);
return;
}
if (working == null)
{
environmentSettings.Host.Logger.LogDebug("[{macro}]: The value of source variable '{sourceVar}' is null, skipping processing for macro '{var}'.", nameof(RegexMacro), config.Source, config.VariableName);
return;
}
string value = working.ToString();
foreach ((string Regex, string Replacement) stepInfo in config.Steps)
{
value = Regex.Replace(value, stepInfo.Regex, stepInfo.Replacement);
}
variableCollection[config.VariableName] = value;
environmentSettings.Host.Logger.LogDebug("[{macro}]: Assigned variable '{var}' to '{value}'.", nameof(RegexMacro), config.VariableName, value);
}
public override RegexMacroConfig CreateConfig(IEngineEnvironmentSettings environmentSettings, IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
}
}