This commit is contained in:
Bernie White 2024-11-12 01:20:05 +10:00 коммит произвёл GitHub
Родитель d1a3bdc5a7
Коммит b176840fe0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
6 изменённых файлов: 62 добавлений и 46 удалений

Просмотреть файл

@ -89,12 +89,10 @@ public sealed class BaselineOption : IEquatable<BaselineOption>, IBaselineOption
Group = group;
}
/// <summary>
/// Load from a dictionary.
/// </summary>
internal void Load(Dictionary<string, object> index)
/// <inheritdoc/>
public void Import(IDictionary<string, object> dictionary)
{
if (index.TryPopStringArrayMap("Baseline.Group", out var group))
if (dictionary.TryPopStringArrayMap("Baseline.Group", out var group))
Group = group;
}
}

Просмотреть файл

@ -351,48 +351,46 @@ public sealed class ExecutionOption : IEquatable<ExecutionOption>, IExecutionOpt
UnprocessedObject = unprocessedObject;
}
/// <summary>
/// Load from dictionary.
/// </summary>
internal void Load(Dictionary<string, object> index)
/// <inheritdoc/>
public void Import(IDictionary<string, object> dictionary)
{
if (index.TryPopEnum("Execution.Break", out BreakLevel @break))
if (dictionary.TryPopEnum("Execution.Break", out BreakLevel @break))
Break = @break;
if (index.TryPopEnum("Execution.HashAlgorithm", out HashAlgorithm hashAlgorithm))
if (dictionary.TryPopEnum("Execution.HashAlgorithm", out HashAlgorithm hashAlgorithm))
HashAlgorithm = hashAlgorithm;
if (index.TryPopEnum("Execution.DuplicateResourceId", out ExecutionActionPreference duplicateResourceId))
if (dictionary.TryPopEnum("Execution.DuplicateResourceId", out ExecutionActionPreference duplicateResourceId))
DuplicateResourceId = duplicateResourceId;
if (index.TryPopEnum("Execution.LanguageMode", out LanguageMode languageMode))
if (dictionary.TryPopEnum("Execution.LanguageMode", out LanguageMode languageMode))
LanguageMode = languageMode;
if (index.TryPopEnum("Execution.InitialSessionState", out SessionState initialSessionState))
if (dictionary.TryPopEnum("Execution.InitialSessionState", out SessionState initialSessionState))
InitialSessionState = initialSessionState;
if (index.TryPopEnum("Execution.RestrictScriptSource", out RestrictScriptSource restrictScriptSource))
if (dictionary.TryPopEnum("Execution.RestrictScriptSource", out RestrictScriptSource restrictScriptSource))
RestrictScriptSource = restrictScriptSource;
if (index.TryPopEnum("Execution.SuppressionGroupExpired", out ExecutionActionPreference suppressionGroupExpired))
if (dictionary.TryPopEnum("Execution.SuppressionGroupExpired", out ExecutionActionPreference suppressionGroupExpired))
SuppressionGroupExpired = suppressionGroupExpired;
if (index.TryPopEnum("Execution.RuleExcluded", out ExecutionActionPreference ruleExcluded))
if (dictionary.TryPopEnum("Execution.RuleExcluded", out ExecutionActionPreference ruleExcluded))
RuleExcluded = ruleExcluded;
if (index.TryPopEnum("Execution.RuleSuppressed", out ExecutionActionPreference ruleSuppressed))
if (dictionary.TryPopEnum("Execution.RuleSuppressed", out ExecutionActionPreference ruleSuppressed))
RuleSuppressed = ruleSuppressed;
if (index.TryPopEnum("Execution.AliasReference", out ExecutionActionPreference aliasReference))
if (dictionary.TryPopEnum("Execution.AliasReference", out ExecutionActionPreference aliasReference))
AliasReference = aliasReference;
if (index.TryPopEnum("Execution.RuleInconclusive", out ExecutionActionPreference ruleInconclusive))
if (dictionary.TryPopEnum("Execution.RuleInconclusive", out ExecutionActionPreference ruleInconclusive))
RuleInconclusive = ruleInconclusive;
if (index.TryPopEnum("Execution.InvariantCulture", out ExecutionActionPreference invariantCulture))
if (dictionary.TryPopEnum("Execution.InvariantCulture", out ExecutionActionPreference invariantCulture))
InvariantCulture = invariantCulture;
if (index.TryPopEnum("Execution.UnprocessedObject", out ExecutionActionPreference unprocessedObject))
if (dictionary.TryPopEnum("Execution.UnprocessedObject", out ExecutionActionPreference unprocessedObject))
UnprocessedObject = unprocessedObject;
}
}

Просмотреть файл

@ -8,4 +8,9 @@ namespace PSRule.Options;
/// </summary>
public interface IOption
{
/// <summary>
/// Import from a dictionary index by a string key.
/// </summary>
/// <param name="dictionary">A dictionary of key value pairs to load the option from.</param>
void Import(IDictionary<string, object> dictionary);
}

Просмотреть файл

@ -203,30 +203,28 @@ public sealed class BindingOption : IEquatable<BindingOption>, IBindingOption
UseQualifiedName = useQualifiedName;
}
/// <summary>
/// Load from a dictionary.
/// </summary>
internal void Load(Dictionary<string, object> index)
/// <inheritdoc/>
public void Import(IDictionary<string, object> dictionary)
{
if (index.TryPopValue("Binding.Field", out Hashtable map))
if (dictionary.TryPopValue("Binding.Field", out Hashtable map))
Field = new FieldMap(map);
if (index.TryPopBool("Binding.IgnoreCase", out var ignoreCase))
if (dictionary.TryPopBool("Binding.IgnoreCase", out var ignoreCase))
IgnoreCase = ignoreCase;
if (index.TryPopString("Binding.NameSeparator", out var nameSeparator))
if (dictionary.TryPopString("Binding.NameSeparator", out var nameSeparator))
NameSeparator = nameSeparator;
if (index.TryPopBool("Binding.PreferTargetInfo", out var preferTargetInfo))
if (dictionary.TryPopBool("Binding.PreferTargetInfo", out var preferTargetInfo))
PreferTargetInfo = preferTargetInfo;
if (index.TryPopStringArray("Binding.TargetName", out var targetName))
if (dictionary.TryPopStringArray("Binding.TargetName", out var targetName))
TargetName = targetName;
if (index.TryPopStringArray("Binding.TargetType", out var targetType))
if (dictionary.TryPopStringArray("Binding.TargetType", out var targetType))
TargetType = targetType;
if (index.TryPopValue("Binding.UseQualifiedName", out bool useQualifiedName))
if (dictionary.TryPopValue("Binding.UseQualifiedName", out bool useQualifiedName))
UseQualifiedName = useQualifiedName;
}
}

Просмотреть файл

@ -160,13 +160,17 @@ public sealed class PSRuleOption : IEquatable<PSRuleOption>, IBaselineV1Spec
var yaml = GetYaml();
return string.IsNullOrEmpty(_SourcePath)
? yaml
: string.Concat(
string.Format(
: string.Concat
(
string.Format
(
Thread.CurrentThread.CurrentCulture,
PSRuleResources.OptionsSourceComment,
_SourcePath),
_SourcePath
),
System.Environment.NewLine,
yaml);
yaml
);
}
/// <summary>
@ -354,10 +358,10 @@ public sealed class PSRuleOption : IEquatable<PSRuleOption>, IBaselineV1Spec
// Start loading matching values
var index = BuildIndex(hashtable);
option.Baseline.Load(index);
option.Binding.Load(index);
option.Baseline.Import(index);
option.Binding.Import(index);
option.Convention.Load(index);
option.Execution.Load(index);
option.Execution.Import(index);
option.Include.Load(index);
option.Input.Load(index);
option.Logging.Load(index);
@ -526,6 +530,7 @@ public sealed class PSRuleOption : IEquatable<PSRuleOption>, IBaselineV1Spec
.WithTypeConverter(new FieldMapYamlTypeConverter())
.WithTypeConverter(new StringArrayMapConverter())
.Build();
return s.Serialize(this);
}
}

Просмотреть файл

@ -46,12 +46,15 @@ internal sealed class EmitterCollection : IDisposable
/// Visit an object with applicable emitters.
/// </summary>
/// <param name="o">The object to visit.</param>
/// <returns>Returns <c>true</c> if the object was processed by any emitter.</returns>
public bool Visit(object? o)
{
if (o == null) return false;
if (TryGetFile(o, out var info) && info != null)
if (TryGetFile(o, out var info, out var exists) && info != null)
{
if (!exists) return false;
if (_ShouldEmitFile && _Context.ShouldQueue(info.Path))
{
// Emit the file.
@ -90,23 +93,27 @@ internal sealed class EmitterCollection : IDisposable
return o is PSObject pso ? pso.BaseObject : o;
}
private static bool TryGetFile(object o, out InternalFileInfo? info)
private static bool TryGetFile(object o, out InternalFileInfo? info, out bool exists)
{
info = null;
exists = false;
o = GetBaseObject(o);
if (o is FileInfo fileInfo && fileInfo.Exists)
if (o is FileInfo fileInfo)
{
info = new InternalFileInfo(fileInfo.FullName, fileInfo.Extension);
exists = fileInfo.Exists;
return true;
}
if (o is InputFileInfo inputFileInfo && inputFileInfo.AsFileInfo().Exists)
if (o is InputFileInfo inputFileInfo)
{
info = new InternalFileInfo(inputFileInfo.FullName, inputFileInfo.Extension);
exists = inputFileInfo.AsFileInfo().Exists;
return true;
}
if (o is InternalFileInfo internalFile && File.Exists(internalFile.Path))
if (o is InternalFileInfo internalFile)
{
info = internalFile;
exists = File.Exists(internalFile.Path);
return true;
}
return false;
@ -160,7 +167,12 @@ internal sealed class EmitterCollection : IDisposable
{
return next == null ?
(context, o, type) => e.Accepts(context, type) && e.Visit(context, o) :
(context, o, type) => e.Accepts(context, type) && e.Visit(context, o) || next(context, o, type);
(context, o, type) =>
{
var r1 = e.Accepts(context, type) && e.Visit(context, o);
var r2 = next(context, o, type);
return r1 || r2;
};
}
}