diff --git a/Sources/Kysect.PowerShellRunner.Abstractions/Cmdlets/IPowerShellCmdletExecutor.cs b/Sources/Kysect.PowerShellRunner.Abstractions/Cmdlets/IPowerShellCmdletExecutor.cs new file mode 100644 index 0000000..1575b91 --- /dev/null +++ b/Sources/Kysect.PowerShellRunner.Abstractions/Cmdlets/IPowerShellCmdletExecutor.cs @@ -0,0 +1,13 @@ +using Kysect.PowerShellRunner.Abstractions.Objects; +using Kysect.PowerShellRunner.Abstractions.Variables; + +namespace Kysect.PowerShellRunner.Abstractions.Cmdlets; + +public interface IPowerShellCmdletExecutor +{ + IReadOnlyCollection Execute(IPowerShellCmdlet cmdlet); + PowerShellVariable InitializeVariable(string variableName, IPowerShellCmdlet cmdlet); + + IReadOnlyCollection Execute(IPowerShellCmdlet cmdlet) where T : notnull; + PowerShellVariable InitializeVariable(string variableName, IPowerShellCmdlet cmdlet) where T : notnull; +} \ No newline at end of file diff --git a/Sources/Kysect.PowerShellRunner.Abstractions/Cmdlets/IPowerShellObjectMapper.cs b/Sources/Kysect.PowerShellRunner.Abstractions/Cmdlets/IPowerShellObjectMapper.cs new file mode 100644 index 0000000..cf08499 --- /dev/null +++ b/Sources/Kysect.PowerShellRunner.Abstractions/Cmdlets/IPowerShellObjectMapper.cs @@ -0,0 +1,8 @@ +using Kysect.PowerShellRunner.Abstractions.Objects; + +namespace Kysect.PowerShellRunner.Abstractions.Cmdlets; + +public interface IPowerShellObjectMapper +{ + T Map(IPowerShellObject powerShellObject) where T : notnull; +} \ No newline at end of file diff --git a/Sources/Kysect.PowerShellRunner.Abstractions/Queries/PowerShellQuery.cs b/Sources/Kysect.PowerShellRunner.Abstractions/Queries/PowerShellQuery.cs index b54e44c..7e28bad 100644 --- a/Sources/Kysect.PowerShellRunner.Abstractions/Queries/PowerShellQuery.cs +++ b/Sources/Kysect.PowerShellRunner.Abstractions/Queries/PowerShellQuery.cs @@ -1,5 +1,6 @@ using Kysect.CommonLib.BaseTypes.Extensions; using Kysect.PowerShellRunner.Abstractions.Variables; +using System.Text; namespace Kysect.PowerShellRunner.Abstractions.Queries; @@ -60,4 +61,18 @@ public PowerShellQuery WithRedirection(string path) { return this with { RedirectionPath = path }; } + + public readonly string Format() + { + var sb = new StringBuilder(); + + if (ResultVariable is not null) + sb.Append($"{ResultVariable.AsReference()} = "); + + sb.Append(Query); + if (!string.IsNullOrEmpty(RedirectionPath)) + sb.Append($" *> \"{RedirectionPath}\""); + + return sb.ToString(); + } } \ No newline at end of file diff --git a/Sources/Kysect.PowerShellRunner.Abstractions/Queries/PowerShellQueryFormatter.cs b/Sources/Kysect.PowerShellRunner.Abstractions/Queries/PowerShellQueryFormatter.cs deleted file mode 100644 index 06b7de3..0000000 --- a/Sources/Kysect.PowerShellRunner.Abstractions/Queries/PowerShellQueryFormatter.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Text; - -namespace Kysect.PowerShellRunner.Abstractions.Queries; - -public class PowerShellQueryFormatter -{ - public string Format(PowerShellQuery query) - { - var sb = new StringBuilder(); - - if (query.ResultVariable is not null) - sb.Append($"{query.ResultVariable.AsReference()} = "); - - sb.Append(query.Query); - if (!string.IsNullOrEmpty(query.RedirectionPath)) - sb.Append($" *> \"{query.RedirectionPath}\""); - - return sb.ToString(); - } -} \ No newline at end of file diff --git a/Sources/Kysect.PowerShellRunner.Configuration/PowerShellServiceCollectionExtensions.cs b/Sources/Kysect.PowerShellRunner.Configuration/PowerShellServiceCollectionExtensions.cs index c1d1440..f716a7b 100644 --- a/Sources/Kysect.PowerShellRunner.Configuration/PowerShellServiceCollectionExtensions.cs +++ b/Sources/Kysect.PowerShellRunner.Configuration/PowerShellServiceCollectionExtensions.cs @@ -1,7 +1,9 @@ using Kysect.CommonLib.BaseTypes.Extensions; using Kysect.CommonLib.DependencyInjection.Logging; using Kysect.PowerShellRunner.Abstractions.Accessors; +using Kysect.PowerShellRunner.Abstractions.Cmdlets; using Kysect.PowerShellRunner.Accessors; +using Kysect.PowerShellRunner.Cmdlets; using Microsoft.Extensions.DependencyInjection; namespace Kysect.PowerShellRunner.Configuration; @@ -29,6 +31,11 @@ public static IServiceCollection AddPowerShellAccessor(this IServiceCollection s return serviceCollection.AddSingleton(GetPowerShellAccessor); } + public static IServiceCollection AddPowerShellCmdletExecutor(this IServiceCollection serviceCollection) + { + return serviceCollection.AddSingleton(); + } + private static IPowerShellAccessor GetPowerShellAccessor(IServiceProvider serviceProvider) { IPowerShellLogger powerShellLogger = serviceProvider.GetRequiredService(); diff --git a/Sources/Kysect.PowerShellRunner.Tests/PowerShellAccessorCmdletExecutionContextTests.cs b/Sources/Kysect.PowerShellRunner.Tests/PowerShellAccessorCmdletExecutionContextTests.cs deleted file mode 100644 index a09d4c0..0000000 --- a/Sources/Kysect.PowerShellRunner.Tests/PowerShellAccessorCmdletExecutionContextTests.cs +++ /dev/null @@ -1,27 +0,0 @@ -using FluentAssertions; -using Kysect.PowerShellRunner.Abstractions.Objects; -using Kysect.PowerShellRunner.CustomCmdlets; -using Kysect.PowerShellRunner.Executions; -using Kysect.PowerShellRunner.Tests.Mocks; -using NUnit.Framework; - -namespace Kysect.PowerShellRunner.Tests; - -public class PowerShellAccessorCmdletExecutionContextTests -{ - [Test] - public void ExecuteAndSetTo_NonGenericImplementation_FinishWithoutError() - { - string expectedResult = @"C:\\Folder"; - - using var testPowerShellAccessor = new FakePowerShellAccessor(); - var executionContext = new PowerShellAccessorCmdletExecutionContext(testPowerShellAccessor, new GetLocationCmdlet()); - testPowerShellAccessor.SetSuccessResult(new GetLocationCmdletWrapperResult(expectedResult)); - - var result = executionContext.ExecuteAndSetTo("$result"); - - IPowerShellObject resultObject = result.Values.Single(); - IPowerShellObjectMember? pathProperty = resultObject.GetProperties().FirstOrDefault(p => p.Name == nameof(GetLocationCmdletWrapperResult.Path)); - pathProperty.Should().NotBeNull(); - } -} \ No newline at end of file diff --git a/Sources/Kysect.PowerShellRunner.Tests/PowerShellCmdletExecutorTests.cs b/Sources/Kysect.PowerShellRunner.Tests/PowerShellCmdletExecutorTests.cs new file mode 100644 index 0000000..6d6f9bf --- /dev/null +++ b/Sources/Kysect.PowerShellRunner.Tests/PowerShellCmdletExecutorTests.cs @@ -0,0 +1,27 @@ +using FluentAssertions; +using Kysect.PowerShellRunner.Abstractions.Variables; +using Kysect.PowerShellRunner.Cmdlets; +using Kysect.PowerShellRunner.CustomCmdlets; +using Kysect.PowerShellRunner.Mapping; +using Kysect.PowerShellRunner.Tests.Mocks; +using NUnit.Framework; + +namespace Kysect.PowerShellRunner.Tests; + +public class PowerShellCmdletExecutorTests +{ + [Test] + public void ExecuteAndSetTo_NonGenericImplementation_FinishWithoutError() + { + string expectedResult = @"C:\\Folder"; + + using var testPowerShellAccessor = new FakePowerShellAccessor(); + var powerShellCmdletExecutor = new PowerShellCmdletExecutor(testPowerShellAccessor, PowerShellObjectMapper.Instance); + testPowerShellAccessor.SetSuccessResult(new GetLocationCmdletWrapperResult(expectedResult)); + + PowerShellVariable result = powerShellCmdletExecutor.InitializeVariable("$result", new GetLocationCmdlet()); + + GetLocationCmdletWrapperResult resultObject = result.Values.Single(); + resultObject.Path.Should().NotBeNull(); + } +} \ No newline at end of file diff --git a/Sources/Kysect.PowerShellRunner.Tests/PowerShellQueryFormatterTests.cs b/Sources/Kysect.PowerShellRunner.Tests/PowerShellQueryFormatterTests.cs index 2db84e4..2328053 100644 --- a/Sources/Kysect.PowerShellRunner.Tests/PowerShellQueryFormatterTests.cs +++ b/Sources/Kysect.PowerShellRunner.Tests/PowerShellQueryFormatterTests.cs @@ -7,12 +7,6 @@ namespace Kysect.PowerShellRunner.Tests; public class PowerShellQueryFormatterTests { - private readonly PowerShellQueryFormatter _sut; - - public PowerShellQueryFormatterTests() - { - _sut = new PowerShellQueryFormatter(); - } [Test] public void Format_QueryWithCmdletName_ReturnCorrectQuery() @@ -20,7 +14,7 @@ public void Format_QueryWithCmdletName_ReturnCorrectQuery() string expectedValue = "Get-Value"; var query = new PowerShellQuery(expectedValue); - string result = _sut.Format(query); + string result = query.Format(); Assert.That(result, Is.EqualTo(expectedValue)); } @@ -31,7 +25,7 @@ public void Format_WithRedirection_ReturnCorrectQuery() var expectedValue = "GetValue *> \"Log/file.log\""; var query = new PowerShellQuery("GetValue").WithRedirection("Log/file.log"); - string result = _sut.Format(query); + string result = query.Format(); Assert.That(result, Is.EqualTo(expectedValue)); } @@ -44,7 +38,7 @@ public void Format_ResultVariableSet_ReturnCorrectQuery() var variable = new PowerShellVariable("$variable_name"); query = query with { ResultVariable = variable }; - string result = _sut.Format(query); + string result = query.Format(); Assert.That(result, Is.EqualTo(expectedValue)); } diff --git a/Sources/Kysect.PowerShellRunner.sln b/Sources/Kysect.PowerShellRunner.sln index 31c89c6..493f36b 100644 --- a/Sources/Kysect.PowerShellRunner.sln +++ b/Sources/Kysect.PowerShellRunner.sln @@ -15,6 +15,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1D69C86F-577E-4246-B9E7-B7D2C3F6E0A3}" ProjectSection(SolutionItems) = preProject Directory.Build.props = Directory.Build.props + Directory.Packages.props = Directory.Packages.props EndProjectSection EndProject Global diff --git a/Sources/Kysect.PowerShellRunner/Accessors/PowerShellAccessor.cs b/Sources/Kysect.PowerShellRunner/Accessors/PowerShellAccessor.cs index 5860dfd..81529f3 100644 --- a/Sources/Kysect.PowerShellRunner/Accessors/PowerShellAccessor.cs +++ b/Sources/Kysect.PowerShellRunner/Accessors/PowerShellAccessor.cs @@ -7,33 +7,23 @@ namespace Kysect.PowerShellRunner.Accessors; -public class PowerShellAccessor : IPowerShellAccessor +public class PowerShellAccessor(PowerShell powerShellInstance) : IPowerShellAccessor { - private readonly PowerShell _powerShellInstance; - private readonly PowerShellQueryFormatter _executionStringFormatter; - - public PowerShellAccessor(PowerShell powerShellInstance) - { - _powerShellInstance = powerShellInstance; - - _executionStringFormatter = new PowerShellQueryFormatter(); - } - public IPowerShellExecutionResult Execute(PowerShellQuery query) { - string fullCommand = _executionStringFormatter.Format(query); + string fullCommand = query.Format(); - lock (_powerShellInstance) + lock (powerShellInstance) { - _powerShellInstance.AddScript(fullCommand); - Collection result = _powerShellInstance.Invoke(); + powerShellInstance.AddScript(fullCommand); + Collection result = powerShellInstance.Invoke(); - IPowerShellExecutionResult methodResult = _powerShellInstance.HadErrors + IPowerShellExecutionResult methodResult = powerShellInstance.HadErrors ? CreateFailedResult(result) : CreateSuccessResult(result); - _powerShellInstance.Streams.ClearStreams(); - _powerShellInstance.Commands.Clear(); + powerShellInstance.Streams.ClearStreams(); + powerShellInstance.Commands.Clear(); return methodResult; } } @@ -50,7 +40,7 @@ private PowerShellSuccessExecutionResult CreateSuccessResult(Collection result) { - var errors = _powerShellInstance.Streams.Error.ToList(); + var errors = powerShellInstance.Streams.Error.ToList(); var errorMessages = errors.Select(e => e.ToString()).ToList(); var failedPowerShellExecutionResult = new PowerShellFailedExecutionResult(errorMessages, result @@ -63,7 +53,7 @@ private PowerShellFailedExecutionResult CreateFailedResult(Collection protected virtual void Dispose(bool disposing) { - _powerShellInstance.Dispose(); + powerShellInstance.Dispose(); } public void Dispose() diff --git a/Sources/Kysect.PowerShellRunner/Accessors/PowerShellAccessorFactory.cs b/Sources/Kysect.PowerShellRunner/Accessors/PowerShellAccessorFactory.cs index 5921712..7301d6e 100644 --- a/Sources/Kysect.PowerShellRunner/Accessors/PowerShellAccessorFactory.cs +++ b/Sources/Kysect.PowerShellRunner/Accessors/PowerShellAccessorFactory.cs @@ -6,6 +6,8 @@ public class PowerShellAccessorFactory : IPowerShellAccessorFactory { public IPowerShellAccessor Create() { +#pragma warning disable CA2000 // Dispose objects before losing scope return new PowerShellAccessor(System.Management.Automation.PowerShell.Create()); +#pragma warning restore CA2000 // Dispose objects before losing scope } } \ No newline at end of file diff --git a/Sources/Kysect.PowerShellRunner/Accessors/PowerShellAccessorLoggingDecorator.cs b/Sources/Kysect.PowerShellRunner/Accessors/PowerShellAccessorLoggingDecorator.cs index 72a5594..2bd2ebb 100644 --- a/Sources/Kysect.PowerShellRunner/Accessors/PowerShellAccessorLoggingDecorator.cs +++ b/Sources/Kysect.PowerShellRunner/Accessors/PowerShellAccessorLoggingDecorator.cs @@ -9,57 +9,46 @@ namespace Kysect.PowerShellRunner.Accessors; -public class PowerShellAccessorLoggingDecorator : IPowerShellAccessor +public class PowerShellAccessorLoggingDecorator(IPowerShellAccessor innerImplementation, ILogger logger) + : IPowerShellAccessor { - private readonly IPowerShellAccessor _innerImplementation; - private readonly ILogger _logger; - private readonly PowerShellQueryFormatter _executionStringFormatter; - - public PowerShellAccessorLoggingDecorator(IPowerShellAccessor innerImplementation, ILogger logger) - { - _innerImplementation = innerImplementation; - _logger = logger; - - _executionStringFormatter = new PowerShellQueryFormatter(); - } - public IPowerShellExecutionResult Execute(PowerShellQuery query) { if (query.ContainsSensitiveInfo) { - _logger.LogDebug("Execute PowerShell command with sensitive data. Query will not be logged."); + logger.LogDebug("Execute PowerShell command with sensitive data. Query will not be logged."); } else { - string executableQuery = _executionStringFormatter.Format(query); - _logger.LogDebug($"Execute PowerShell command: {executableQuery}"); + string executableQuery = query.Format(); + logger.LogDebug($"Execute PowerShell command: {executableQuery}"); } - IPowerShellExecutionResult result = _innerImplementation.Execute(query); + IPowerShellExecutionResult result = innerImplementation.Execute(query); switch (result) { case PowerShellFailedExecutionResult failedPowerShellExecutionResult: - _logger.LogError("PS command executed failed."); + logger.LogError("PS command executed failed."); if (failedPowerShellExecutionResult.Errors.Any()) { - _logger.LogError("Errors:"); + logger.LogError("Errors:"); foreach (string error in failedPowerShellExecutionResult.Errors) - _logger.LogTabError(1, error); + logger.LogTabError(1, error); } if (failedPowerShellExecutionResult.OtherMessages.Any()) { - _logger.LogError("Other messages:"); + logger.LogError("Other messages:"); foreach (string otherMessage in failedPowerShellExecutionResult.OtherMessages) - _logger.LogTabError(1, otherMessage); + logger.LogTabError(1, otherMessage); } break; case PowerShellSuccessExecutionResult successPowerShellExecutionResult: - _logger.LogDebug("PS command executed successfully."); + logger.LogDebug("PS command executed successfully."); foreach (IPowerShellObject powerShellObject in successPowerShellExecutionResult.Output) - _logger.LogPowerShellObject(powerShellObject); + logger.LogPowerShellObject(powerShellObject); break; default: @@ -71,7 +60,7 @@ public IPowerShellExecutionResult Execute(PowerShellQuery query) protected virtual void Dispose(bool disposing) { - _innerImplementation.Dispose(); + innerImplementation.Dispose(); } public void Dispose() diff --git a/Sources/Kysect.PowerShellRunner/Cmdlets/PowerShellCmdletExecutor.cs b/Sources/Kysect.PowerShellRunner/Cmdlets/PowerShellCmdletExecutor.cs new file mode 100644 index 0000000..5d057d7 --- /dev/null +++ b/Sources/Kysect.PowerShellRunner/Cmdlets/PowerShellCmdletExecutor.cs @@ -0,0 +1,43 @@ +using Kysect.PowerShellRunner.Abstractions.Accessors; +using Kysect.PowerShellRunner.Abstractions.Cmdlets; +using Kysect.PowerShellRunner.Abstractions.Objects; +using Kysect.PowerShellRunner.Abstractions.Queries; +using Kysect.PowerShellRunner.Abstractions.Variables; +using Kysect.PowerShellRunner.Executions; +using Kysect.PowerShellRunner.QueryBuilding; + +namespace Kysect.PowerShellRunner.Cmdlets; + +public class PowerShellCmdletExecutor(IPowerShellAccessor accessor, IPowerShellObjectMapper powerShellObjectMapper) + : IPowerShellCmdletExecutor +{ + public IReadOnlyCollection Execute(IPowerShellCmdlet cmdlet) + { + PowerShellQuery powerShellQuery = cmdlet.BuildFromCmdlet(); + + return accessor.ExecuteAndGet(powerShellQuery).ToList(); + } + + public PowerShellVariable InitializeVariable(string variableName, IPowerShellCmdlet cmdlet) + { + var powerShellVariable = new PowerShellVariable(variableName); + PowerShellQuery powerShellQuery = cmdlet.BuildFromCmdlet(); + return new PowerShellVariableInitializer(accessor, powerShellVariable, powerShellObjectMapper).With(powerShellQuery); + } + + public IReadOnlyCollection Execute(IPowerShellCmdlet cmdlet) where T : notnull + { + PowerShellQuery powerShellQuery = cmdlet.BuildFromCmdlet(); + + return accessor.ExecuteAndGet(powerShellQuery) + .Select(powerShellObjectMapper.Map) + .ToList(); + } + + public PowerShellVariable InitializeVariable(string variableName, IPowerShellCmdlet cmdlet) where T : notnull + { + var powerShellVariable = new PowerShellVariable(variableName); + PowerShellQuery powerShellQuery = cmdlet.BuildFromCmdlet(); + return new PowerShellVariableInitializer(accessor, powerShellVariable, powerShellObjectMapper).With(powerShellQuery); + } +} \ No newline at end of file diff --git a/Sources/Kysect.PowerShellRunner/CustomCmdlets/ConvertToSecureStringCmdlet.cs b/Sources/Kysect.PowerShellRunner/CustomCmdlets/ConvertToSecureStringCmdlet.cs index 6d3fe2a..aab27f9 100644 --- a/Sources/Kysect.PowerShellRunner/CustomCmdlets/ConvertToSecureStringCmdlet.cs +++ b/Sources/Kysect.PowerShellRunner/CustomCmdlets/ConvertToSecureStringCmdlet.cs @@ -9,9 +9,7 @@ public class ConvertToSecureStringCmdlet : IPowerShellCmdlet { public string CmdletName => "ConvertTo-SecureString"; -#pragma warning disable CA1720 // Identifier contains type name public IPowerShellCmdletParameter String { get; } = null!; -#pragma warning restore CA1720 // Identifier contains type name public IPowerShellCmdletParameter AsPlainText { get; } = null!; public IPowerShellCmdletParameter Force { get; } = null!; } diff --git a/Sources/Kysect.PowerShellRunner/Executions/PowerShellAccessorCmdletExecutionContext.cs b/Sources/Kysect.PowerShellRunner/Executions/PowerShellAccessorCmdletExecutionContext.cs index eccd4c2..730a39a 100644 --- a/Sources/Kysect.PowerShellRunner/Executions/PowerShellAccessorCmdletExecutionContext.cs +++ b/Sources/Kysect.PowerShellRunner/Executions/PowerShellAccessorCmdletExecutionContext.cs @@ -20,8 +20,8 @@ public PowerShellAccessorCmdletExecutionContext(IPowerShellAccessor accessor, IP _cmdlet = cmdlet; _accessor = accessor; - _morphisms = new List>(); _powerShellObjectMapper = PowerShellObjectMapper.Instance; + _morphisms = new List>(); } public PowerShellAccessorCmdletExecutionContext Continue(Func morphism) @@ -42,10 +42,8 @@ public IReadOnlyCollection Execute() public PowerShellVariable ExecuteAndSetTo(string variableName) { var powerShellVariable = new PowerShellVariable(variableName); - PowerShellQuery powerShellQuery = BuildQuery(); - - return new PowerShellVariableInitializer(_accessor, powerShellVariable).With(powerShellQuery); + return new PowerShellVariableInitializer(_accessor, powerShellVariable, _powerShellObjectMapper).With(powerShellQuery); } private PowerShellQuery BuildQuery() @@ -61,12 +59,14 @@ public class PowerShellAccessorCmdletExecutionContext private readonly IPowerShellAccessor _accessor; private readonly IPowerShellCmdlet _cmdlet; private readonly List> _morphisms; + private readonly PowerShellObjectMapper _powerShellObjectMapper; public PowerShellAccessorCmdletExecutionContext(IPowerShellAccessor accessor, IPowerShellCmdlet cmdlet) { _cmdlet = cmdlet; _accessor = accessor; + _powerShellObjectMapper = PowerShellObjectMapper.Instance; _morphisms = new List>(); } @@ -79,16 +79,14 @@ public PowerShellAccessorCmdletExecutionContext Continue(Func Execute() { PowerShellQuery powerShellQuery = BuildQuery(); - - return _accessor.ExecuteAndGet(powerShellQuery) - .ToList(); + return _accessor.ExecuteAndGet(powerShellQuery); } public PowerShellVariable ExecuteAndSetTo(string variableName) { var powerShellVariable = new PowerShellVariable(variableName); PowerShellQuery powerShellQuery = BuildQuery(); - return new PowerShellVariableInitializer(_accessor, powerShellVariable).With(powerShellQuery); + return new PowerShellVariableInitializer(_accessor, powerShellVariable, _powerShellObjectMapper).With(powerShellQuery); } private PowerShellQuery BuildQuery() diff --git a/Sources/Kysect.PowerShellRunner/Executions/PowerShellVariableInitializer.cs b/Sources/Kysect.PowerShellRunner/Executions/PowerShellVariableInitializer.cs index e2b6f99..6296c93 100644 --- a/Sources/Kysect.PowerShellRunner/Executions/PowerShellVariableInitializer.cs +++ b/Sources/Kysect.PowerShellRunner/Executions/PowerShellVariableInitializer.cs @@ -4,7 +4,6 @@ using Kysect.PowerShellRunner.Abstractions.Objects; using Kysect.PowerShellRunner.Abstractions.Queries; using Kysect.PowerShellRunner.Abstractions.Variables; -using Kysect.PowerShellRunner.Mapping; using Kysect.PowerShellRunner.QueryBuilding; namespace Kysect.PowerShellRunner.Executions; @@ -13,11 +12,13 @@ public class PowerShellVariableInitializer { private readonly IPowerShellAccessor _accessor; private readonly PowerShellVariable _variable; + private readonly IPowerShellObjectMapper _powerShellObjectMapper; - public PowerShellVariableInitializer(IPowerShellAccessor accessor, PowerShellVariable variable) + public PowerShellVariableInitializer(IPowerShellAccessor accessor, PowerShellVariable variable, IPowerShellObjectMapper powerShellObjectMapper) { _accessor = accessor.ThrowIfNull(); _variable = variable.ThrowIfNull(); + _powerShellObjectMapper = powerShellObjectMapper.ThrowIfNull(); } public PowerShellVariable With(PowerShellQuery query) @@ -58,7 +59,7 @@ private PowerShellVariable Map(PowerShellVariable powerShellVariable) wher { var mappedValues = powerShellVariable .Values - .Select(PowerShellObjectMapper.Instance.Map) + .Select(_powerShellObjectMapper.Map) .ToList(); return new PowerShellVariable(powerShellVariable.Name, mappedValues); diff --git a/Sources/Kysect.PowerShellRunner/Mapping/PowerShellObjectMapper.cs b/Sources/Kysect.PowerShellRunner/Mapping/PowerShellObjectMapper.cs index 7fd6629..777a4c8 100644 --- a/Sources/Kysect.PowerShellRunner/Mapping/PowerShellObjectMapper.cs +++ b/Sources/Kysect.PowerShellRunner/Mapping/PowerShellObjectMapper.cs @@ -1,6 +1,7 @@ using Kysect.CommonLib.BaseTypes.Extensions; using Kysect.CommonLib.Reflection; using Kysect.CommonLib.Reflection.TypeCache; +using Kysect.PowerShellRunner.Abstractions.Cmdlets; using Kysect.PowerShellRunner.Abstractions.Objects; using Kysect.PowerShellRunner.Tools; using System.Reflection; @@ -8,7 +9,7 @@ namespace Kysect.PowerShellRunner.Mapping; -public class PowerShellObjectMapper +public class PowerShellObjectMapper : IPowerShellObjectMapper { private readonly PowerShellObjectMappingConfiguration _configuration; private readonly JsonSerializerOptions _serializerOptions;