-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
RootCommand.cs
344 lines (290 loc) · 11.2 KB
/
RootCommand.cs
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Reflection;
using System.Runtime.Versioning;
using System.Text.Json;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.EntityFrameworkCore.Tools.Commands;
using Microsoft.EntityFrameworkCore.Tools.Properties;
using EFCommand = Microsoft.EntityFrameworkCore.Tools.Commands.RootCommand;
namespace Microsoft.EntityFrameworkCore.Tools;
internal class RootCommand : CommandBase
{
private CommandLineApplication? _command;
private CommandOption? _project;
private CommandOption? _startupProject;
private CommandOption? _framework;
private CommandOption? _configuration;
private CommandOption? _runtime;
private CommandOption? _msbuildprojectextensionspath;
private CommandOption? _noBuild;
private CommandOption? _help;
private IList<string>? _args;
private IList<string>? _applicationArgs;
public override void Configure(CommandLineApplication command)
{
command.FullName = Resources.DotnetEfFullName;
command.AllowArgumentSeparator = true;
var options = new ProjectOptions();
options.Configure(command);
_project = options.Project;
_startupProject = options.StartupProject;
_framework = options.Framework;
_configuration = options.Configuration;
_runtime = options.Runtime;
_msbuildprojectextensionspath = options.MSBuildProjectExtensionsPath;
_noBuild = options.NoBuild;
command.VersionOption("--version", GetVersion);
_help = command.Option("-h|--help", description: null);
_args = command.RemainingArguments;
_applicationArgs = command.ApplicationArguments;
base.Configure(command);
_command = command;
}
protected override int Execute(string[] _)
{
var commands = _args!.TakeWhile(a => a[0] != '-').ToList();
if (_help!.HasValue()
|| ShouldHelp(commands))
{
return ShowHelp(_help.HasValue(), commands);
}
var (projectFile, startupProjectFile) = ResolveProjects(
_project!.Value(),
_startupProject!.Value());
Reporter.WriteVerbose(Resources.UsingProject(projectFile));
Reporter.WriteVerbose(Resources.UsingStartupProject(startupProjectFile));
var project = Project.FromFile(projectFile, _msbuildprojectextensionspath!.Value());
var startupProject = Project.FromFile(
startupProjectFile,
_msbuildprojectextensionspath.Value(),
_framework!.Value(),
_configuration!.Value(),
_runtime!.Value());
if (!_noBuild!.HasValue())
{
Reporter.WriteInformation(Resources.BuildStarted);
startupProject.Build();
Reporter.WriteInformation(Resources.BuildSucceeded);
}
string executable;
var args = new List<string>();
var toolsPath = Path.Combine(
Path.GetDirectoryName(typeof(Program).Assembly.Location)!,
"tools");
var targetDir = Path.GetFullPath(Path.Combine(startupProject.ProjectDir!, startupProject.OutputPath!));
var targetPath = Path.Combine(targetDir, project.TargetFileName!);
var startupTargetPath = Path.Combine(targetDir, startupProject.TargetFileName!);
var depsFile = Path.Combine(
targetDir,
startupProject.AssemblyName + ".deps.json");
var runtimeConfig = Path.Combine(
targetDir,
startupProject.AssemblyName + ".runtimeconfig.json");
var projectAssetsFile = startupProject.ProjectAssetsFile;
var targetFramework = new FrameworkName(startupProject.TargetFrameworkMoniker!);
if (targetFramework.Identifier == ".NETFramework")
{
executable = Path.Combine(
toolsPath,
"net461",
startupProject.PlatformTarget == "x86"
? "win-x86"
: "any",
"ef.exe");
}
else if (targetFramework.Identifier == ".NETCoreApp")
{
if (targetFramework.Version < new Version(2, 0))
{
throw new CommandException(
Resources.NETCoreApp1StartupProject(startupProject.ProjectName, targetFramework.Version));
}
var targetPlatformIdentifier = startupProject.TargetPlatformIdentifier!;
if (targetPlatformIdentifier.Length != 0
&& !string.Equals(targetPlatformIdentifier, "Windows", StringComparison.OrdinalIgnoreCase))
{
executable = Path.Combine(
toolsPath,
"net461",
startupProject.PlatformTarget switch
{
"x86" => "win-x86",
"ARM64" => "win-arm64",
_ => "any"
},
"ef.exe");
}
executable = "dotnet";
args.Add("exec");
args.Add("--depsfile");
args.Add(depsFile);
if (!string.IsNullOrEmpty(projectAssetsFile))
{
using var file = File.OpenRead(projectAssetsFile);
using var reader = JsonDocument.Parse(file);
var projectAssets = reader.RootElement;
var packageFolders = projectAssets.GetProperty("packageFolders").EnumerateObject().Select(p => p.Name);
foreach (var packageFolder in packageFolders)
{
args.Add("--additionalprobingpath");
args.Add(packageFolder.TrimEnd(Path.DirectorySeparatorChar));
}
}
if (File.Exists(runtimeConfig))
{
args.Add("--runtimeconfig");
args.Add(runtimeConfig);
}
else if (startupProject.RuntimeFrameworkVersion!.Length != 0)
{
args.Add("--fx-version");
args.Add(startupProject.RuntimeFrameworkVersion);
}
args.Add(Path.Combine(toolsPath, "netcoreapp2.0", "any", "ef.dll"));
}
else if (targetFramework.Identifier == ".NETStandard")
{
throw new CommandException(Resources.NETStandardStartupProject(startupProject.ProjectName));
}
else
{
throw new CommandException(
Resources.UnsupportedFramework(startupProject.ProjectName, targetFramework.Identifier));
}
args.AddRange(_args!);
args.Add("--assembly");
args.Add(targetPath);
args.Add("--project");
args.Add(projectFile);
args.Add("--startup-assembly");
args.Add(startupTargetPath);
args.Add("--startup-project");
args.Add(startupProjectFile);
args.Add("--project-dir");
args.Add(project.ProjectDir!);
args.Add("--root-namespace");
args.Add(project.RootNamespace!);
args.Add("--language");
args.Add(project.Language!);
args.Add("--framework");
args.Add(startupProject.TargetFramework!);
if (_configuration.HasValue())
{
args.Add("--configuration");
args.Add(_configuration.Value()!);
}
if (string.Equals(project.Nullable, "enable", StringComparison.OrdinalIgnoreCase)
|| string.Equals(project.Nullable, "annotations", StringComparison.OrdinalIgnoreCase))
{
args.Add("--nullable");
}
args.Add("--working-dir");
args.Add(Directory.GetCurrentDirectory());
if (Reporter.IsVerbose)
{
args.Add("--verbose");
}
if (Reporter.NoColor)
{
args.Add("--no-color");
}
if (Reporter.PrefixOutput)
{
args.Add("--prefix-output");
}
if (_applicationArgs!.Any())
{
args.Add("--");
args.AddRange(_applicationArgs!);
}
return Exe.Run(executable, args, startupProject.ProjectDir);
}
private static (string, string) ResolveProjects(
string? projectPath,
string? startupProjectPath)
{
var projects = ResolveProjects(projectPath);
var startupProjects = ResolveProjects(startupProjectPath);
if (projects.Count > 1)
{
throw new CommandException(
projectPath != null
? Resources.MultipleProjectsInDirectory(projectPath)
: Resources.MultipleProjects);
}
if (startupProjects.Count > 1)
{
throw new CommandException(
startupProjectPath != null
? Resources.MultipleProjectsInDirectory(startupProjectPath)
: Resources.MultipleStartupProjects);
}
if (projectPath != null
&& projects.Count == 0)
{
throw new CommandException(Resources.NoProjectInDirectory(projectPath));
}
if (startupProjectPath != null
&& startupProjects.Count == 0)
{
throw new CommandException(Resources.NoProjectInDirectory(startupProjectPath));
}
if (projectPath == null
&& startupProjectPath == null)
{
return projects.Count == 0
? throw new CommandException(Resources.NoProject)
: (projects[0], startupProjects[0]);
}
if (projects.Count == 0)
{
return (startupProjects[0], startupProjects[0]);
}
if (startupProjects.Count == 0)
{
return (projects[0], projects[0]);
}
return (projects[0], startupProjects[0]);
}
private static List<string> ResolveProjects(string? path)
{
if (path == null)
{
path = Directory.GetCurrentDirectory();
}
else
{
path = Path.GetFullPath(path);
if (!Directory.Exists(path)) // It's not a directory
{
return [path];
}
}
var projectFiles = Directory.EnumerateFiles(path, "*.*proj", SearchOption.TopDirectoryOnly)
.Where(f => !string.Equals(Path.GetExtension(f), ".xproj", StringComparison.OrdinalIgnoreCase))
.Take(2).ToList();
return projectFiles;
}
private static string GetVersion()
=> typeof(RootCommand).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!
.InformationalVersion;
private static bool ShouldHelp(IReadOnlyList<string> commands)
=> commands.Count == 0
|| (commands.Count == 1
&& (commands[0] == "database"
|| commands[0] == "dbcontext"
|| commands[0] == "migrations"));
private int ShowHelp(bool help, IEnumerable<string> commands)
{
var app = new CommandLineApplication { Name = _command!.Name };
new EFCommand().Configure(app);
app.FullName = _command.FullName;
var args = new List<string>(commands);
if (help)
{
args.Add("--help");
}
return app.Execute(args.ToArray());
}
}