Skip to content

Commit

Permalink
improved logging in GeneratePortNumberMacro
Browse files Browse the repository at this point in the history
  • Loading branch information
vlada-shubina committed Nov 9, 2022
1 parent 2c6607a commit 915f4f4
Show file tree
Hide file tree
Showing 30 changed files with 272 additions and 19 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ Details: {1}</value>
<value>Unrecognized evaluator: '{0}'.</value>
<comment>{0} - invalid evaluator value</comment>
</data>
<data name="GeneratePortNumberConfig_Warning_InvalidHighBound" xml:space="preserve">
<value>The high port bound '{0}' is greater than the maximum allowed, '{1}' will be used instead.</value>
</data>
<data name="GeneratePortNumberConfig_Warning_InvalidLowBound" xml:space="preserve">
<value>The low port bound '{0}' is less than the minimum allowed, '{1}' will be used instead.</value>
</data>
<data name="GeneratePortNumberConfig_Warning_InvalidLowHighBound" xml:space="preserve">
<value>The low port bound '{0}' is greater the high port bound '{1}', the default range [{2}-{3}] will be used instead.</value>
</data>
<data name="JoinMacroConfig_Exception_ValuePropertyIsEmpty" xml:space="preserve">
<value>Generated symbol '{0}': array '{1}' should contain JSON objects with property non-empty '{2}' when '{3}' is '{4}'.</value>
<comment>{0} - name of generated symbol, {1} - the invalid property name; {2}, {3}, {4} - the property names and values.</comment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public IMacroConfig CreateConfig(IEngineEnvironmentSettings environmentSettings,
{
throw new InvalidCastException($"Couldn't cast the {nameof(rawConfig)} as {nameof(IGeneratedSymbolConfig)}.");
}
return CreateConfig(deferredConfig);
return CreateConfig(environmentSettings, deferredConfig);
}

public void Evaluate(IEngineEnvironmentSettings environmentSettings, IVariableCollection vars, IGeneratedSymbolConfig deferredConfig)
{
Evaluate(environmentSettings, vars, CreateConfig(deferredConfig));
Evaluate(environmentSettings, vars, CreateConfig(environmentSettings, deferredConfig));
}

protected abstract T CreateConfig(IGeneratedSymbolConfig deferredConfig);
protected abstract T CreateConfig(IEngineEnvironmentSettings environmentSettings, IGeneratedSymbolConfig deferredConfig);
}

/// <summary>
Expand All @@ -49,6 +49,6 @@ internal abstract class BaseNondeterministicMacro<T> : BaseGeneratedSymbolMacro<
/// <typeparam name="T">The macro config.</typeparam>
internal abstract class BaseNondeterministicGenSymMacro<T> : BaseNondeterministicMacro<T>, IDeterministicModeMacro<IGeneratedSymbolConfig> where T : BaseMacroConfig, IMacroConfig
{
public void EvaluateDeterministically(IEngineEnvironmentSettings environmentSettings, IVariableCollection variables, IGeneratedSymbolConfig config) => EvaluateDeterministically(environmentSettings, variables, CreateConfig(config));
public void EvaluateDeterministically(IEngineEnvironmentSettings environmentSettings, IVariableCollection variables, IGeneratedSymbolConfig config) => EvaluateDeterministically(environmentSettings, variables, CreateConfig(environmentSettings, config));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public override void Evaluate(IEngineEnvironmentSettings environmentSettings, IV
environmentSettings.Host.Logger.LogDebug("[{macro}]: Assigned variable '{var}' to '{value}'.", nameof(CaseChangeMacro), config.VariableName, value);
}

protected override CaseChangeMacroConfig CreateConfig(IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
protected override CaseChangeMacroConfig CreateConfig(IEngineEnvironmentSettings environmentSettings, IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public override void Evaluate(IEngineEnvironmentSettings environmentSettings, IV
environmentSettings.Host.Logger.LogDebug("[{macro}]: Variable '{var}' was not assigned, neither source nor fallback variable was found.", nameof(CoalesceMacro), config.VariableName);
}

protected override CoalesceMacroConfig CreateConfig(IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
protected override CoalesceMacroConfig CreateConfig(IEngineEnvironmentSettings environmentSettings, IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public override void Evaluate(IEngineEnvironmentSettings environmentSettings, IV
environmentSettings.Host.Logger.LogDebug("[{macro}]: Variable '{var}' was assigned to value '{value}'.", nameof(ConstantMacro), config.VariableName, config.Value);
}

protected override ConstantMacroConfig CreateConfig(IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
protected override ConstantMacroConfig CreateConfig(IEngineEnvironmentSettings environmentSettings, IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// 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 System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using Microsoft.Extensions.Logging;
using Microsoft.TemplateEngine.Orchestrator.RunnableProjects.Abstractions;

namespace Microsoft.TemplateEngine.Orchestrator.RunnableProjects.Macros
Expand Down Expand Up @@ -45,29 +47,47 @@ internal class GeneratePortNumberConfig : BaseMacroConfig<GeneratePortNumberMacr
internal GeneratePortNumberConfig(GeneratePortNumberMacro macro, string variableName, string? dataType, int fallback, int low, int high)
: base(macro, variableName, dataType)
{
if (low < LowPortDefault)
{
throw new ArgumentException($"{nameof(low)} should be greater than {LowPortDefault}.", nameof(low));
}

if (high > HighPortDefault)
{
throw new ArgumentException($"{nameof(high)} should be less than {HighPortDefault}.", nameof(high));
}

if (low > high)
{
throw new ArgumentException($"{nameof(low)} should be greater than {nameof(high)}.", nameof(low));
}

Fallback = fallback;
Low = low;
High = high;
Port = AllocatePort(low, high, fallback);
}

internal GeneratePortNumberConfig(GeneratePortNumberMacro macro, IGeneratedSymbolConfig generatedSymbolConfig)
internal GeneratePortNumberConfig(ILogger logger, GeneratePortNumberMacro macro, IGeneratedSymbolConfig generatedSymbolConfig)
: base(macro, generatedSymbolConfig.VariableName, generatedSymbolConfig.DataType)
{
int low = GetOptionalParameterValue(generatedSymbolConfig, "low", ConvertJTokenToInt, LowPortDefault);
int high = GetOptionalParameterValue(generatedSymbolConfig, "high", ConvertJTokenToInt, HighPortDefault);
if (low < LowPortDefault)
{
logger.LogWarning(LocalizableStrings.GeneratePortNumberConfig_Warning_InvalidLowBound, low, LowPortDefault);
low = LowPortDefault;
}

if (high > HighPortDefault)
{
logger.LogWarning(LocalizableStrings.GeneratePortNumberConfig_Warning_InvalidHighBound, high, HighPortDefault);
high = HighPortDefault;
}

if (low > high)
{
logger.LogWarning(LocalizableStrings.GeneratePortNumberConfig_Warning_InvalidLowHighBound, low, high, LowPortDefault, HighPortDefault);
low = LowPortDefault;
high = HighPortDefault;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public override void EvaluateDeterministically(IEngineEnvironmentSettings enviro
environmentSettings.Host.Logger.LogDebug("[{macro}]: Variable '{var}' was assigned to value '{value}' in deterministic mode.", nameof(GeneratePortNumberMacro), config.VariableName, config.Low);
}

protected override GeneratePortNumberConfig CreateConfig(IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
protected override GeneratePortNumberConfig CreateConfig(IEngineEnvironmentSettings environmentSettings, IGeneratedSymbolConfig deferredConfig)
=> new(environmentSettings.Host.Logger, this, deferredConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override void EvaluateDeterministically(IEngineEnvironmentSettings enviro
EvaluateInternal(DeterministicModeValue, environmentSettings, variables, config);
}

protected override GuidMacroConfig CreateConfig(IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
protected override GuidMacroConfig CreateConfig(IEngineEnvironmentSettings environmentSettings, IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);

private void EvaluateInternal(Guid g, IEngineEnvironmentSettings environmentSettings, IVariableCollection vars, GuidMacroConfig config)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ public override void Evaluate(IEngineEnvironmentSettings environmentSettings, IV
environmentSettings.Host.Logger.LogDebug("[{macro}]: Variable '{var}' was assigned to value '{value}'.", nameof(JoinMacro), config.VariableName, result);
}

protected override JoinMacroConfig CreateConfig(IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
protected override JoinMacroConfig CreateConfig(IEngineEnvironmentSettings environmentSettings, IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public override void EvaluateDeterministically(IEngineEnvironmentSettings enviro
environmentSettings.Host.Logger.LogDebug("[{macro}]: Variable '{var}' was assigned to value '{value}' in deterministic mode.", nameof(NowMacro), config.VariableName, value);
}

protected override NowMacroConfig CreateConfig(IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
protected override NowMacroConfig CreateConfig(IEngineEnvironmentSettings environmentSettings, IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public override void EvaluateDeterministically(IEngineEnvironmentSettings enviro
environmentSettings.Host.Logger.LogDebug("[{macro}]: Variable '{var}' was assigned to value '{value}' in deterministic mode.", nameof(RandomMacro), config.VariableName, config.Low);
}

protected override RandomMacroConfig CreateConfig(IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
protected override RandomMacroConfig CreateConfig(IEngineEnvironmentSettings environmentSettings, IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public override void Evaluate(IEngineEnvironmentSettings environmentSettings, IV
environmentSettings.Host.Logger.LogDebug("[{macro}]: Assigned variable '{var}' to '{value}'.", nameof(RegexMacro), config.VariableName, value);
}

protected override RegexMacroConfig CreateConfig(IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
protected override RegexMacroConfig CreateConfig(IEngineEnvironmentSettings environmentSettings, IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public override void Evaluate(IEngineEnvironmentSettings environmentSettings, IV
environmentSettings.Host.Logger.LogDebug("[{macro}]: Assigned variable '{var}' to '{value}'.", nameof(RegexMatchMacro), config.VariableName, result);
}

protected override RegexMatchMacroConfig CreateConfig(IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
protected override RegexMatchMacroConfig CreateConfig(IEngineEnvironmentSettings environmentSettings, IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public override void Evaluate(IEngineEnvironmentSettings environmentSettings, IV
environmentSettings.Host.Logger.LogDebug("[{macro}]: Assigned variable '{var}' to '{value}'.", nameof(SwitchMacro), config.VariableName, result);
}

protected override SwitchMacroConfig CreateConfig(IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
protected override SwitchMacroConfig CreateConfig(IEngineEnvironmentSettings environmentSettings, IGeneratedSymbolConfig deferredConfig) => new(this, deferredConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ Podrobnosti: {1}</target>
<target state="new">Unrecognized evaluator: '{0}'.</target>
<note>{0} - invalid evaluator value</note>
</trans-unit>
<trans-unit id="GeneratePortNumberConfig_Warning_InvalidHighBound">
<source>The high port bound '{0}' is greater than the maximum allowed, '{1}' will be used instead.</source>
<target state="new">The high port bound '{0}' is greater than the maximum allowed, '{1}' will be used instead.</target>
<note />
</trans-unit>
<trans-unit id="GeneratePortNumberConfig_Warning_InvalidLowBound">
<source>The low port bound '{0}' is less than the minimum allowed, '{1}' will be used instead.</source>
<target state="new">The low port bound '{0}' is less than the minimum allowed, '{1}' will be used instead.</target>
<note />
</trans-unit>
<trans-unit id="GeneratePortNumberConfig_Warning_InvalidLowHighBound">
<source>The low port bound '{0}' is greater the high port bound '{1}', the default range [{2}-{3}] will be used instead.</source>
<target state="new">The low port bound '{0}' is greater the high port bound '{1}', the default range [{2}-{3}] will be used instead.</target>
<note />
</trans-unit>
<trans-unit id="JoinMacroConfig_Exception_ValuePropertyIsEmpty">
<source>Generated symbol '{0}': array '{1}' should contain JSON objects with property non-empty '{2}' when '{3}' is '{4}'.</source>
<target state="new">Generated symbol '{0}': array '{1}' should contain JSON objects with property non-empty '{2}' when '{3}' is '{4}'.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ Details: {1}</target>
<target state="new">Unrecognized evaluator: '{0}'.</target>
<note>{0} - invalid evaluator value</note>
</trans-unit>
<trans-unit id="GeneratePortNumberConfig_Warning_InvalidHighBound">
<source>The high port bound '{0}' is greater than the maximum allowed, '{1}' will be used instead.</source>
<target state="new">The high port bound '{0}' is greater than the maximum allowed, '{1}' will be used instead.</target>
<note />
</trans-unit>
<trans-unit id="GeneratePortNumberConfig_Warning_InvalidLowBound">
<source>The low port bound '{0}' is less than the minimum allowed, '{1}' will be used instead.</source>
<target state="new">The low port bound '{0}' is less than the minimum allowed, '{1}' will be used instead.</target>
<note />
</trans-unit>
<trans-unit id="GeneratePortNumberConfig_Warning_InvalidLowHighBound">
<source>The low port bound '{0}' is greater the high port bound '{1}', the default range [{2}-{3}] will be used instead.</source>
<target state="new">The low port bound '{0}' is greater the high port bound '{1}', the default range [{2}-{3}] will be used instead.</target>
<note />
</trans-unit>
<trans-unit id="JoinMacroConfig_Exception_ValuePropertyIsEmpty">
<source>Generated symbol '{0}': array '{1}' should contain JSON objects with property non-empty '{2}' when '{3}' is '{4}'.</source>
<target state="new">Generated symbol '{0}': array '{1}' should contain JSON objects with property non-empty '{2}' when '{3}' is '{4}'.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ Detalles: {1}</target>
<target state="new">Unrecognized evaluator: '{0}'.</target>
<note>{0} - invalid evaluator value</note>
</trans-unit>
<trans-unit id="GeneratePortNumberConfig_Warning_InvalidHighBound">
<source>The high port bound '{0}' is greater than the maximum allowed, '{1}' will be used instead.</source>
<target state="new">The high port bound '{0}' is greater than the maximum allowed, '{1}' will be used instead.</target>
<note />
</trans-unit>
<trans-unit id="GeneratePortNumberConfig_Warning_InvalidLowBound">
<source>The low port bound '{0}' is less than the minimum allowed, '{1}' will be used instead.</source>
<target state="new">The low port bound '{0}' is less than the minimum allowed, '{1}' will be used instead.</target>
<note />
</trans-unit>
<trans-unit id="GeneratePortNumberConfig_Warning_InvalidLowHighBound">
<source>The low port bound '{0}' is greater the high port bound '{1}', the default range [{2}-{3}] will be used instead.</source>
<target state="new">The low port bound '{0}' is greater the high port bound '{1}', the default range [{2}-{3}] will be used instead.</target>
<note />
</trans-unit>
<trans-unit id="JoinMacroConfig_Exception_ValuePropertyIsEmpty">
<source>Generated symbol '{0}': array '{1}' should contain JSON objects with property non-empty '{2}' when '{3}' is '{4}'.</source>
<target state="new">Generated symbol '{0}': array '{1}' should contain JSON objects with property non-empty '{2}' when '{3}' is '{4}'.</target>
Expand Down
Loading

0 comments on commit 915f4f4

Please sign in to comment.