Skip to content

Commit d24ba6e

Browse files
Merge pull request #64 from max-ieremenko/powershell/dependency-loader
load internal dependencies into private context
2 parents 412c777 + 4fb97fb commit d24ba6e

26 files changed

+372
-158
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using SqlDatabase.Adapter;
2+
using SqlDatabase.CommandLine;
3+
using SqlDatabase.Configuration;
4+
5+
namespace SqlDatabase.PowerShell.Internal;
6+
7+
public static class CmdLetExecutor
8+
{
9+
// only for tests
10+
internal static ISqlDatabaseProgram? Program { get; set; }
11+
12+
public static void RunCreate(IDictionary<string, object?> param, string currentDirectory, Action<string> writeInfo, Action<string> writeError)
13+
{
14+
var commandLine = new CreateCommandLine
15+
{
16+
Database = (string)param[nameof(CreateCommandLine.Database)]!,
17+
Configuration = (string?)param[nameof(CreateCommandLine.Configuration)],
18+
Log = (string?)param[nameof(CreateCommandLine.Log)],
19+
WhatIf = (bool)param[nameof(CreateCommandLine.WhatIf)]!
20+
};
21+
22+
CommandLineTools.AppendFrom(commandLine.From, false, (string[]?)param[nameof(CreateCommandLine.From)]);
23+
CommandLineTools.AppendVariables(commandLine.Variables, (string[]?)param[nameof(CreateCommandLine.Variables)]);
24+
25+
Run(commandLine, currentDirectory, writeInfo, writeError);
26+
}
27+
28+
public static void RunExecute(IDictionary<string, object?> param, string currentDirectory, Action<string> writeInfo, Action<string> writeError)
29+
{
30+
var commandLine = new ExecuteCommandLine
31+
{
32+
Database = (string)param[nameof(ExecuteCommandLine.Database)]!,
33+
Transaction = (TransactionMode)((int)param[nameof(ExecuteCommandLine.Transaction)]!),
34+
Configuration = (string?)param[nameof(ExecuteCommandLine.Configuration)],
35+
Log = (string?)param[nameof(ExecuteCommandLine.Log)],
36+
WhatIf = (bool)param[nameof(ExecuteCommandLine.WhatIf)]!
37+
};
38+
39+
CommandLineTools.AppendFrom(commandLine.From, false, (string[]?)param[nameof(ExecuteCommandLine.From)]);
40+
CommandLineTools.AppendFrom(commandLine.From, true, (string[]?)param["FromSql"]);
41+
CommandLineTools.AppendVariables(commandLine.Variables, (string[]?)param[nameof(ExecuteCommandLine.Variables)]);
42+
43+
Run(commandLine, currentDirectory, writeInfo, writeError);
44+
}
45+
46+
public static void RunExport(IDictionary<string, object?> param, string currentDirectory, Action<string> writeInfo, Action<string> writeError)
47+
{
48+
var commandLine = new ExportCommandLine
49+
{
50+
Database = (string)param[nameof(ExportCommandLine.Database)]!,
51+
Configuration = (string?)param[nameof(ExportCommandLine.Configuration)],
52+
DestinationFileName = (string?)param[nameof(ExportCommandLine.DestinationFileName)],
53+
DestinationTableName = (string?)param[nameof(ExportCommandLine.DestinationTableName)],
54+
Log = (string?)param[nameof(ExecuteCommandLine.Log)]
55+
};
56+
57+
CommandLineTools.AppendFrom(commandLine.From, false, (string[]?)param[nameof(ExportCommandLine.From)]);
58+
CommandLineTools.AppendFrom(commandLine.From, true, (string[]?)param["FromSql"]);
59+
CommandLineTools.AppendVariables(commandLine.Variables, (string[]?)param[nameof(ExportCommandLine.Variables)]);
60+
61+
Run(commandLine, currentDirectory, writeInfo, writeError);
62+
}
63+
64+
public static void RunUpdate(IDictionary<string, object?> param, string currentDirectory, Action<string> writeInfo, Action<string> writeError)
65+
{
66+
var commandLine = new UpgradeCommandLine
67+
{
68+
Database = (string)param[nameof(UpgradeCommandLine.Database)]!,
69+
Transaction = (TransactionMode)((int)param[nameof(UpgradeCommandLine.Transaction)]!),
70+
Configuration = (string?)param[nameof(UpgradeCommandLine.Configuration)],
71+
Log = (string?)param[nameof(UpgradeCommandLine.Log)],
72+
WhatIf = (bool)param[nameof(UpgradeCommandLine.WhatIf)]!,
73+
FolderAsModuleName = (bool)param[nameof(UpgradeCommandLine.FolderAsModuleName)]!
74+
};
75+
76+
CommandLineTools.AppendFrom(commandLine.From, false, (string[]?)param[nameof(UpgradeCommandLine.From)]);
77+
CommandLineTools.AppendVariables(commandLine.Variables, (string[]?)param[nameof(UpgradeCommandLine.Variables)]);
78+
79+
Run(commandLine, currentDirectory, writeInfo, writeError);
80+
}
81+
82+
public static string GetDefaultConfigurationFile() => ConfigurationManager.GetDefaultConfigurationFile();
83+
84+
private static void Run(ICommandLine commandLine, string currentDirectory, Action<string> writeInfo, Action<string> writeError)
85+
{
86+
var program = Program ?? new SqlDatabaseProgram(new CmdLetLogger(writeInfo, writeError), currentDirectory);
87+
program.ExecuteCommand(commandLine);
88+
}
89+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using SqlDatabase.Log;
2+
3+
namespace SqlDatabase.PowerShell.Internal;
4+
5+
internal sealed class CmdLetLogger : LoggerBase
6+
{
7+
private readonly Action<string> _writeInfo;
8+
private readonly Action<string> _writeError;
9+
10+
public CmdLetLogger(Action<string> writeInfo, Action<string> writeError)
11+
{
12+
_writeInfo = writeInfo;
13+
_writeError = writeError;
14+
}
15+
16+
protected override void WriteError(string message) => _writeError(message);
17+
18+
protected override void WriteInfo(string message) => _writeInfo(message);
19+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("SqlDatabase.PowerShell.Test, PublicKey=002400000480000094000000060200000024000052534131000400000100010055AB0DC1F8A24FB41E7358B65A606EC92141F1ABAFBFF062635AB5FAEB22308CFFBC8B54F3436694F14F6FD6C145D4F16C13A3E739FFCA837902BB78E2D51B890D964CC7384C2CC6B844AE37323F501F29E3EDC2DFADA82C99F5FBB5197ED757D795C2E5408DCB3FBAF9DDDF39E60B137ED0A23603A361EA811E6ADB605DFECC")]
4+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<OutputPath>..\..\bin\SqlDatabase.PowerShell</OutputPath>
6+
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\SqlDatabase\SqlDatabase.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

Sources/SqlDatabase.PowerShell/Internal/SqlDatabaseProgram.cs renamed to Sources/SqlDatabase.PowerShell.Internal/SqlDatabaseProgram.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,5 @@ public SqlDatabaseProgram(ILogger logger, string currentDirectory)
1414
_currentDirectory = currentDirectory;
1515
}
1616

17-
public void ExecuteCommand(ICommandLine command)
18-
{
19-
Program.RunPowershell(_logger, command, _currentDirectory);
20-
}
17+
public void ExecuteCommand(ICommandLine command) => Program.RunPowershell(_logger, command, _currentDirectory);
2118
}

Sources/SqlDatabase.PowerShell.Test/Internal/DependencyResolverFactoryTest.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ namespace SqlDatabase.PowerShell.Internal;
88
public class DependencyResolverFactoryTest
99
{
1010
[Test]
11-
[TestCase("Desktop", typeof(PowerShellDesktopDependencyResolver))]
12-
[TestCase(null, typeof(PowerShellDesktopDependencyResolver))]
13-
[TestCase("Core", typeof(PowerShellCoreDependencyResolver))]
14-
public void CreateProgram(string? psEdition, Type expected)
11+
[TestCase("Desktop")]
12+
[TestCase(null)]
13+
public void CreateDesktop(string? psEdition)
1514
{
1615
var psVersionTable = new Hashtable();
1716
if (psEdition != null)
@@ -21,6 +20,19 @@ public void CreateProgram(string? psEdition, Type expected)
2120

2221
var actual = DependencyResolverFactory.Create(new PSVersionTable(psVersionTable));
2322

24-
actual.ShouldBeOfType(expected);
23+
actual.ShouldBeOfType<PowerShellDesktopDependencyResolver>();
24+
}
25+
26+
[Test]
27+
public void CreateCore()
28+
{
29+
var psVersionTable = new Hashtable
30+
{
31+
{ "PSEdition", "Core" }
32+
};
33+
34+
var ex = Should.Throw<FileNotFoundException>(() => DependencyResolverFactory.Create(new PSVersionTable(psVersionTable)));
35+
36+
ex.Message.ShouldContain("System.Runtime.Loader");
2537
}
2638
}

Sources/SqlDatabase.PowerShell.Test/TestApi/SqlDatabaseCmdLetTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ public void BeforeEachTest()
4040
.Callback<ICommandLine>(_commandLines.Add);
4141

4242
_commandLines.Clear();
43-
PowerShellCommand.Program = program.Object;
43+
CmdLetExecutor.Program = program.Object;
4444
}
4545

4646
[TearDown]
4747
public void AfterEachTest()
4848
{
49-
PowerShellCommand.Program = null;
49+
CmdLetExecutor.Program = null;
5050

5151
foreach (var row in _powerShell.Streams.Information)
5252
{

Sources/SqlDatabase.PowerShell/CreateCmdLet.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,15 @@ public sealed class CreateCmdLet : PSCmdlet
3232

3333
protected override void ProcessRecord()
3434
{
35-
var commandLine = new CreateCommandLine
35+
var param = new Dictionary<string, object?>
3636
{
37-
Database = Database,
38-
Configuration = Configuration,
39-
Log = Log,
40-
WhatIf = WhatIf
37+
{ nameof(CreateCommandLine.Database), Database },
38+
{ nameof(CreateCommandLine.Configuration), Configuration },
39+
{ nameof(CreateCommandLine.Log), Log },
40+
{ nameof(CreateCommandLine.WhatIf), (bool)WhatIf },
41+
{ nameof(CreateCommandLine.From), From },
42+
{ nameof(CreateCommandLine.Variables), Var }
4143
};
42-
43-
CommandLineTools.AppendFrom(commandLine.From, false, From);
44-
CommandLineTools.AppendVariables(commandLine.Variables, Var);
45-
46-
PowerShellCommand.Execute(this, commandLine);
44+
PowerShellCommand.Execute(this, nameof(CmdLetExecutor.RunCreate), param);
4745
}
4846
}

0 commit comments

Comments
 (0)