-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathReflectionOperationExecutor.cs
More file actions
179 lines (157 loc) · 6.31 KB
/
ReflectionOperationExecutor.cs
File metadata and controls
179 lines (157 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Tools.Properties;
#if NET
using System.Runtime.Loader;
#endif
namespace Microsoft.EntityFrameworkCore.Tools;
internal class ReflectionOperationExecutor : OperationExecutorBase
{
private readonly object _executor;
private readonly Assembly _commandsAssembly;
private const string ReportHandlerTypeName = "Microsoft.EntityFrameworkCore.Design.OperationReportHandler";
private const string ResultHandlerTypeName = "Microsoft.EntityFrameworkCore.Design.OperationResultHandler";
private readonly Type _resultHandlerType;
private string? _efcoreVersion;
#if NET
private AssemblyLoadContext? _assemblyLoadContext;
#endif
public ReflectionOperationExecutor(
string assembly,
string? startupAssembly,
string? designAssembly,
string? project,
string? projectDir,
string? dataDirectory,
string? rootNamespace,
string? language,
bool nullable,
string[] remainingArguments,
IOperationReportHandler reportHandler)
: base(
assembly, startupAssembly, designAssembly, project, projectDir, rootNamespace, language, nullable, remainingArguments,
reportHandler)
{
var reporter = new OperationReporter(reportHandler);
var configurationFile = (startupAssembly ?? assembly) + ".config";
if (File.Exists(configurationFile))
{
reporter.WriteVerbose(Resources.UsingConfigurationFile(configurationFile));
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configurationFile);
}
if (dataDirectory != null)
{
reporter.WriteVerbose(Resources.UsingDataDir(dataDirectory));
AppDomain.CurrentDomain.SetData("DataDirectory", dataDirectory);
}
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;
#if NET
_commandsAssembly = DesignAssemblyPath != null
? AssemblyLoadContext.LoadFromAssemblyPath(DesignAssemblyPath)
: AssemblyLoadContext.LoadFromAssemblyName(new AssemblyName(DesignAssemblyName));
#else
_commandsAssembly = DesignAssemblyPath != null
? Assembly.LoadFrom(DesignAssemblyPath)
: Assembly.Load(DesignAssemblyName);
#endif
var reportHandlerType = _commandsAssembly.GetType(ReportHandlerTypeName, throwOnError: true, ignoreCase: false)!;
var designReportHandler = Activator.CreateInstance(
reportHandlerType,
(Action<string>)reportHandler.OnError,
(Action<string>)reportHandler.OnWarning,
(Action<string>)reportHandler.OnInformation,
(Action<string>)reportHandler.OnVerbose)!;
_executor = Activator.CreateInstance(
_commandsAssembly.GetType(ExecutorTypeName, throwOnError: true, ignoreCase: false)!,
designReportHandler,
new Dictionary<string, object?>
{
{ "targetName", AssemblyFileName },
{ "startupTargetName", StartupAssemblyFileName },
{ "project", Project },
{ "projectDir", ProjectDirectory },
{ "rootNamespace", RootNamespace },
{ "language", Language },
{ "nullable", Nullable },
{ "toolsVersion", ProductInfo.GetVersion() },
{ "remainingArguments", RemainingArguments }
})!;
_resultHandlerType = _commandsAssembly.GetType(ResultHandlerTypeName, throwOnError: true, ignoreCase: false)!;
}
#if NET
protected AssemblyLoadContext AssemblyLoadContext
{
get
{
if (_assemblyLoadContext != null)
{
return _assemblyLoadContext;
}
AssemblyLoadContext.Default.Resolving += (context, name) =>
{
var assemblyPath = Path.Combine(AppBasePath, name.Name + ".dll");
return File.Exists(assemblyPath) ? context.LoadFromAssemblyPath(assemblyPath) : null;
};
_assemblyLoadContext = AssemblyLoadContext.Default;
return AssemblyLoadContext.Default;
}
}
#endif
public override string? EFCoreVersion
{
get
{
if (_efcoreVersion != null)
{
return _efcoreVersion;
}
Assembly? assembly = null;
#if NET
assembly = DesignAssemblyPath != null
? AssemblyLoadContext.LoadFromAssemblyPath(DesignAssemblyPath)
: AssemblyLoadContext.LoadFromAssemblyName(new AssemblyName(DesignAssemblyName));
#else
assembly = DesignAssemblyPath != null
? Assembly.LoadFrom(DesignAssemblyPath)
: Assembly.Load(DesignAssemblyName);
#endif
_efcoreVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
?.InformationalVersion;
return _efcoreVersion;
}
}
protected override object CreateResultHandler()
=> Activator.CreateInstance(_resultHandlerType)!;
protected override void Execute(string operationName, object resultHandler, IDictionary arguments)
=> Activator.CreateInstance(
_commandsAssembly.GetType(ExecutorTypeName + "+" + operationName, throwOnError: true, ignoreCase: true)!,
_executor,
resultHandler,
arguments);
private Assembly? ResolveAssembly(object? sender, ResolveEventArgs args)
{
var assemblyName = new AssemblyName(args.Name);
foreach (var extension in new[] { ".dll", ".exe" })
{
var path = Path.Combine(AppBasePath, assemblyName.Name + extension);
if (File.Exists(path))
{
try
{
return Assembly.LoadFrom(path);
}
catch
{
}
}
}
return null;
}
public override void Dispose()
=> AppDomain.CurrentDomain.AssemblyResolve -= ResolveAssembly;
}