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
4 changes: 4 additions & 0 deletions src/dotnet-ef/ProjectOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace Microsoft.EntityFrameworkCore.Tools;
internal class ProjectOptions
{
public CommandOption? Project { get; private set; }
public CommandOption? File { get; private set; }
public CommandOption? StartupProject { get; private set; }
public CommandOption? StartupFile { get; private set; }
public CommandOption? Framework { get; private set; }
public CommandOption? Configuration { get; private set; }
public CommandOption? Runtime { get; private set; }
Expand All @@ -20,7 +22,9 @@ internal class ProjectOptions
public void Configure(CommandLineApplication command)
{
Project = command.Option("-p|--project <PROJECT>", Resources.ProjectDescription);
File = command.Option("--file <FILE>", Resources.FileDescription);
StartupProject = command.Option("-s|--startup-project <PROJECT>", Resources.StartupProjectDescription);
StartupFile = command.Option("--startup-file <FILE>", Resources.StartupFileDescription);
Framework = command.Option("--framework <FRAMEWORK>", Resources.FrameworkDescription);
Configuration = command.Option("--configuration <CONFIGURATION>", Resources.ConfigurationDescription);
Runtime = command.Option("--runtime <RUNTIME_IDENTIFIER>", Resources.RuntimeDescription);
Expand Down
24 changes: 22 additions & 2 deletions src/dotnet-ef/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions src/dotnet-ef/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@
<data name="DotnetEfFullName" xml:space="preserve">
<value>Entity Framework Core .NET Command-line Tools</value>
</data>
<data name="FileDescription" xml:space="preserve">
<value>The file-based app to use. An alias for --project.</value>
</data>
<data name="DotNetEfConfigInvalidJson" xml:space="preserve">
<value>Unable to read '{configFile}'. Fix the JSON and try again. {details}</value>
</data>
Expand Down Expand Up @@ -291,6 +294,9 @@
<data name="MultipleStartupProjects" xml:space="preserve">
<value>More than one project was found in the current working directory. Use the --startup-project option.</value>
</data>
<data name="MutuallyExclusiveOptions" xml:space="preserve">
<value>The --{option1} and --{option2} options cannot be used together.</value>
</data>
<data name="MultipleTargetFrameworks" xml:space="preserve">
<value>The project targets multiple frameworks. Use the --framework option to specify which target framework to use.</value>
</data>
Expand Down Expand Up @@ -355,7 +361,7 @@
<value>Prefix output with level.</value>
</data>
<data name="ProjectDescription" xml:space="preserve">
<value>The project or file-based app to use. Defaults to the current working directory.</value>
<value>The project to use. Defaults to the current working directory.</value>
</data>
<data name="ProjectExtensionsDescription" xml:space="preserve">
<value>Obsolete</value>
Expand All @@ -376,7 +382,10 @@
<value>Also bundle the .NET runtime so it doesn't need to be installed on the machine.</value>
</data>
<data name="StartupProjectDescription" xml:space="preserve">
<value>The startup project or file-based app to use. Defaults to the current working directory.</value>
<value>The startup project to use. Defaults to the current working directory.</value>
</data>
<data name="StartupFileDescription" xml:space="preserve">
<value>The startup file-based app to use. An alias for --startup-project.</value>
</data>
<data name="SuffixDescription" xml:space="preserve">
<value>The suffix to attach to the name of all the generated files</value>
Expand Down
22 changes: 20 additions & 2 deletions src/dotnet-ef/RootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ internal class RootCommand : CommandBase
{
private CommandLineApplication? _command;
private CommandOption? _project;
private CommandOption? _file;
private CommandOption? _startupProject;
private CommandOption? _startupFile;
private CommandOption? _framework;
private CommandOption? _configuration;
private CommandOption? _runtime;
Expand All @@ -33,7 +35,9 @@ public override void Configure(CommandLineApplication command)
options.Configure(command);

_project = options.Project;
_file = options.File;
_startupProject = options.StartupProject;
_startupFile = options.StartupFile;
_framework = options.Framework;
_configuration = options.Configuration;
_runtime = options.Runtime;
Expand All @@ -60,8 +64,8 @@ protected override int Execute(string[] _)
}

var config = DotNetEfConfigLoader.Load(Directory.GetCurrentDirectory());
var projectPath = _project!.Value() ?? config?.Project;
var startupProjectPath = _startupProject!.Value() ?? config?.StartupProject;
var projectPath = ResolveOption(_project!, _file!, config?.Project);
var startupProjectPath = ResolveOption(_startupProject!, _startupFile!, config?.StartupProject);
var framework = _framework!.Value() ?? config?.Framework;
var configuration = _configuration!.Value() ?? config?.Configuration;
var runtime = _runtime!.Value() ?? config?.Runtime;
Expand Down Expand Up @@ -304,6 +308,20 @@ private static (string, string) ResolveProjects(
return (projects[0], startupProjects[0]);
}

internal static string? ResolveOption(
CommandOption primary,
CommandOption alias,
string? configValue)
{
if (primary.HasValue() && alias.HasValue())
{
throw new CommandException(
Resources.MutuallyExclusiveOptions(primary.LongName!, alias.LongName!));
}

return alias.Value() ?? primary.Value() ?? configValue;
}
Comment thread
jjonescz marked this conversation as resolved.

private static List<string> ResolveProjects(string? path)
{
if (path == null)
Expand Down
65 changes: 65 additions & 0 deletions test/dotnet-ef.Tests/ProjectTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,74 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.Cli.CommandLine;

namespace Microsoft.EntityFrameworkCore.Tools;

public sealed class ProjectTest(ITestOutputHelper output)
{
private const string TargetFramework = "net10.0";

[Fact]
public void Alias_option_is_used()
{
var primary = CreateOption("--project");
var alias = CreateOption("--file");
alias.TryParse("MyApp.cs");

Assert.Equal("MyApp.cs", RootCommand.ResolveOption(primary, alias, configValue: null));
}

[Fact]
public void Primary_option_is_used_when_alias_is_not_specified()
{
var primary = CreateOption("--project");
var alias = CreateOption("--file");
primary.TryParse("MyApp.csproj");

Assert.Equal("MyApp.csproj", RootCommand.ResolveOption(primary, alias, configValue: null));
}

[Fact]
public void Config_value_is_used_when_no_options_specified()
{
var primary = CreateOption("--project");
var alias = CreateOption("--file");

Assert.Equal("FromConfig", RootCommand.ResolveOption(primary, alias, configValue: "FromConfig"));
}

[Fact]
public void Alias_option_takes_precedence_over_config()
{
var primary = CreateOption("--project");
var alias = CreateOption("--file");
alias.TryParse("MyApp.cs");

Assert.Equal("MyApp.cs", RootCommand.ResolveOption(primary, alias, configValue: "FromConfig"));
}

[Fact]
public void Primary_and_alias_options_together_throws()
{
var primary = CreateOption("--project");
var alias = CreateOption("--file");
primary.TryParse("MyApp.csproj");
alias.TryParse("MyApp.cs");

Assert.Throws<CommandException>(
() => RootCommand.ResolveOption(primary, alias, configValue: null));
}

[Fact]
public void Returns_null_when_nothing_specified()
{
var primary = CreateOption("--project");
var alias = CreateOption("--file");

Assert.Null(RootCommand.ResolveOption(primary, alias, configValue: null));
}

[Fact]
public void Csproj_metadata_can_be_extracted()
{
Expand Down Expand Up @@ -92,4 +154,7 @@ public override void WriteLine(string? value)
}
}
}

private static CommandOption CreateOption(string name)
=> new($"{name} <VALUE>", CommandOptionType.SingleValue);
}
Loading