Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/EFCore.Tools/tools/EntityFrameworkCore.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1433,28 +1433,49 @@ function EF($project, $startupProject, $params, $applicationArgs, [switch] $skip

$dotnetParams = 'exec', '--depsfile', $depsFile

# `migrations bundle` launches a child ef.dll to discover the DbContext, and needs the same host
# context to start it. Only that command understands these options; the others reject unknown ones.
$isBundle = $params.Count -ge 2 -and $params[0] -eq 'migrations' -and $params[1] -eq 'bundle'
$bundleHostParams = @()
if ($isBundle -and [IO.File]::Exists($depsFile))
{
$bundleHostParams += '--deps-file', $depsFile
}

if ($projectAssetsFile)
{
# NB: Don't use Get-Content. It doesn't handle UTF-8 without a signature
# NB: Don't use ReadAllLines. ConvertFrom-Json won't work on PowerShell 3.0
$projectAssets = [IO.File]::ReadAllText($projectAssetsFile) | ConvertFrom-Json
$projectAssets.packageFolders.psobject.Properties.Name | %{
$dotnetParams += '--additionalprobingpath', $_.TrimEnd('\')
if ($isBundle)
{
$bundleHostParams += '--additional-probing-path', $_.TrimEnd('\')
}
}
}

if ([IO.File]::Exists($runtimeConfig))
{
$dotnetParams += '--runtimeconfig', $runtimeConfig
if ($isBundle)
{
$bundleHostParams += '--runtime-config', $runtimeConfig
}
}
elseif ($runtimeFrameworkVersion)
{
$dotnetParams += '--fx-version', $runtimeFrameworkVersion
if ($isBundle)
{
$bundleHostParams += '--runtime-framework-version', $runtimeFrameworkVersion
}
}

$dotnetParams += $efPath

$params = $dotnetParams + $params
$params = $dotnetParams + $params + $bundleHostParams
}
elseif ($targetFramework -eq '.NETStandard')
{
Expand Down
58 changes: 44 additions & 14 deletions src/dotnet-ef/Exe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,16 @@ public static int Run(
string? workingDirectory = null,
Action<string?>? handleOutput = null,
Action<string?>? handleError = null,
Action<string>? processCommandLine = null)
Action<string>? processCommandLine = null,
Encoding? outputEncoding = null)
{
var arguments = ToArguments(args);

processCommandLine ??= Reporter.WriteVerbose;
processCommandLine(executable + " " + arguments);

var startInfo = new ProcessStartInfo
{
FileName = executable,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = handleOutput != null,
RedirectStandardError = handleError != null
};
if (workingDirectory != null)
{
startInfo.WorkingDirectory = workingDirectory;
}
var startInfo = CreateStartInfo(
executable, arguments, workingDirectory, handleOutput != null, handleError != null, outputEncoding);

var process = new Process { StartInfo = startInfo };

Expand Down Expand Up @@ -63,6 +54,45 @@ public static int Run(
return process.ExitCode;
}

// 'outputEncoding' is how the redirected streams are decoded; null keeps the platform default.
internal static ProcessStartInfo CreateStartInfo(
string executable,
string arguments,
string? workingDirectory,
bool redirectOutput,
bool redirectError,
Encoding? outputEncoding)
{
var startInfo = new ProcessStartInfo
{
FileName = executable,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = redirectOutput,
RedirectStandardError = redirectError
};

if (workingDirectory != null)
{
startInfo.WorkingDirectory = workingDirectory;
}

if (outputEncoding != null)
{
if (redirectOutput)
{
startInfo.StandardOutputEncoding = outputEncoding;
}

if (redirectError)
{
startInfo.StandardErrorEncoding = outputEncoding;
}
}

return startInfo;
}

public static string ToArguments(IReadOnlyList<string> args)
{
var builder = new StringBuilder();
Expand All @@ -80,7 +110,7 @@ public static string ToArguments(IReadOnlyList<string> args)
continue;
}

if (args[i].IndexOf(' ') == -1)
if (args[i].IndexOf(' ') == -1 && args[i].IndexOf('"') == -1)
{
builder.Append(args[i]);

Expand Down
61 changes: 58 additions & 3 deletions src/dotnet-ef/RootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ protected override int Execute(string[] _)
Resources.PlatformSpecificProject(startupProject.ProjectName, startupProject.TargetFramework));
}

var additionalProbingPaths = new List<string>();

var targetFramework = new FrameworkName(startupProject.TargetFrameworkMoniker!);
if (targetFramework.Identifier == ".NETFramework")
{
Expand All @@ -171,21 +173,24 @@ protected override int Execute(string[] _)
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);
var packageFolders = projectAssets.GetProperty("packageFolders")
.EnumerateObject().Select(p => Path.TrimEndingDirectorySeparator(p.Name)).ToList();

foreach (var packageFolder in packageFolders)
{
args.Add("--additionalprobingpath");
args.Add(packageFolder.TrimEnd(Path.DirectorySeparatorChar));
args.Add(packageFolder);
}

additionalProbingPaths = packageFolders;
}

if (File.Exists(runtimeConfig))
{
args.Add("--runtimeconfig");
args.Add(runtimeConfig);
}
else if (startupProject.RuntimeFrameworkVersion!.Length != 0)
else if (!string.IsNullOrEmpty(startupProject.RuntimeFrameworkVersion))
{
args.Add("--fx-version");
args.Add(startupProject.RuntimeFrameworkVersion);
Expand Down Expand Up @@ -258,6 +263,13 @@ protected override int Execute(string[] _)
args.Add("--prefix-output");
}

// Only `migrations bundle` understands these (it launches a child ef.dll for DbContext discovery);
// every other ef.dll command rejects unrecognized options.
if (IsMigrationsBundle(commands))
{
args.AddRange(GetBundleHostArgs(depsFile, runtimeConfig, startupProject.RuntimeFrameworkVersion, additionalProbingPaths));
}

if (_applicationArgs!.Any())
{
args.Add("--");
Expand All @@ -267,6 +279,49 @@ protected override int Execute(string[] _)
return Exe.Run(executable, args, startupProject.ProjectDir);
}

internal static bool IsMigrationsBundle(IReadOnlyList<string> commands)
=> commands.Count >= 2
&& string.Equals(commands[0], "migrations", StringComparison.OrdinalIgnoreCase)
&& string.Equals(commands[1], "bundle", StringComparison.OrdinalIgnoreCase);

// The startup project's host context, forwarded so `migrations bundle` can launch its out-of-process
// discovery child the same way this tool launched ef.dll. See https://github.com/dotnet/efcore/issues/25555.
internal static IReadOnlyList<string> GetBundleHostArgs(
string depsFile,
string runtimeConfig,
string? runtimeFrameworkVersion,
IReadOnlyList<string> additionalProbingPaths)
{
var args = new List<string>();

if (File.Exists(depsFile))
{
args.Add("--deps-file");
args.Add(depsFile);
}

if (File.Exists(runtimeConfig))
{
args.Add("--runtime-config");
args.Add(runtimeConfig);
}
else if (!string.IsNullOrEmpty(runtimeFrameworkVersion))
{
// Mirrors the host --fx-version so the child can pin the same shared framework when there is
// no runtimeconfig.json.
args.Add("--runtime-framework-version");
args.Add(runtimeFrameworkVersion);
}

foreach (var additionalProbingPath in additionalProbingPaths)
{
args.Add("--additional-probing-path");
args.Add(additionalProbingPath);
}

return args;
}

private static (string, string) ResolveProjects(
string? projectPath,
string? startupProjectPath)
Expand Down
6 changes: 6 additions & 0 deletions src/ef/Commands/MigrationsBundleCommand.Configure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public override void Configure(CommandLineApplication command)
_selfContained = command.Option("--self-contained", Resources.SelfContainedDescription);
_runtime = command.Option("-r|--target-runtime <RUNTIME_IDENTIFIER>", Resources.MigrationsBundleRuntimeDescription);

ConfigureHostOptions(command);

base.Configure(command);
}

// Implemented in MigrationsBundleCommand.cs, which is only compiled into ef.dll: dotnet-ef also
// compiles this file but must not expose (or swallow) the host options it forwards to ef.dll.
partial void ConfigureHostOptions(CommandLineApplication command);
}
Loading
Loading