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.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.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/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/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 607f25f..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,9 +42,7 @@ public IReadOnlyCollection Execute() public PowerShellVariable ExecuteAndSetTo(string variableName) { var powerShellVariable = new PowerShellVariable(variableName); - PowerShellQuery powerShellQuery = BuildQuery(); - return new PowerShellVariableInitializer(_accessor, powerShellVariable, _powerShellObjectMapper).With(powerShellQuery); } @@ -68,8 +66,8 @@ public PowerShellAccessorCmdletExecutionContext(IPowerShellAccessor accessor, IP _cmdlet = cmdlet; _accessor = accessor; - _morphisms = new List>(); _powerShellObjectMapper = PowerShellObjectMapper.Instance; + _morphisms = new List>(); } public PowerShellAccessorCmdletExecutionContext Continue(Func morphism) diff --git a/Sources/Kysect.PowerShellRunner/Executions/PowerShellVariableInitializer.cs b/Sources/Kysect.PowerShellRunner/Executions/PowerShellVariableInitializer.cs index 85aefe7..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,9 +12,9 @@ public class PowerShellVariableInitializer { private readonly IPowerShellAccessor _accessor; private readonly PowerShellVariable _variable; - private readonly PowerShellObjectMapper _powerShellObjectMapper; + private readonly IPowerShellObjectMapper _powerShellObjectMapper; - public PowerShellVariableInitializer(IPowerShellAccessor accessor, PowerShellVariable variable, PowerShellObjectMapper powerShellObjectMapper) + public PowerShellVariableInitializer(IPowerShellAccessor accessor, PowerShellVariable variable, IPowerShellObjectMapper powerShellObjectMapper) { _accessor = accessor.ThrowIfNull(); _variable = variable.ThrowIfNull(); 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;