Skip to content

Commit

Permalink
Review comments updates.
Browse files Browse the repository at this point in the history
Remaining Args are now being passed into the commands and passed through to OperationsExecutor and XyzOperations classes.
  • Loading branch information
lajones committed Mar 26, 2020
1 parent 6db1c0e commit 2823e3b
Show file tree
Hide file tree
Showing 27 changed files with 121 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public virtual IServiceProvider Create([NotNull] string[] args)
_reporter.WriteVerbose(DesignStrings.FindingServiceProvider);

return CreateFromHosting(args)
?? CreateArgsServiceProvider(args);
?? CreateEmptyServiceProvider();
}

private IServiceProvider CreateFromHosting(string[] args)
Expand Down Expand Up @@ -97,13 +97,11 @@ private IServiceProvider CreateFromHosting(string[] args)
}
}

private IServiceProvider CreateArgsServiceProvider(string[] args)
private IServiceProvider CreateEmptyServiceProvider()
{
_reporter.WriteVerbose(DesignStrings.NoServiceProvider);

return new ServiceCollection()
.AddScoped(typeof(string[]), sp => args)
.BuildServiceProvider();
return new ServiceCollection().BuildServiceProvider();
}
}
}
2 changes: 2 additions & 0 deletions src/EFCore.Design/Design/Internal/DatabaseOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class DatabaseOperations
private readonly string _rootNamespace;
private readonly string _language;
private readonly DesignTimeServicesBuilder _servicesBuilder;
private readonly string[] _args;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -51,6 +52,7 @@ public DatabaseOperations(
_projectDir = projectDir;
_rootNamespace = rootNamespace;
_language = language;
_args = args;

_servicesBuilder = new DesignTimeServicesBuilder(assembly, startupAssembly, reporter, args);
}
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Design/Design/Internal/MigrationsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class MigrationsOperations
private readonly string _language;
private readonly DesignTimeServicesBuilder _servicesBuilder;
private readonly DbContextOperations _contextOperations;
private readonly string[] _args;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -60,6 +61,7 @@ public MigrationsOperations(
_projectDir = projectDir;
_rootNamespace = rootNamespace;
_language = language;
_args = args;
_contextOperations = new DbContextOperations(
reporter,
assembly,
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Design/Design/OperationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class OperationExecutor : MarshalByRefObject
/// <para><c>projectDir</c>--The target project's root directory.</para>
/// <para><c>rootNamespace</c>--The target project's root namespace.</para>
/// <para><c>language</c>--The programming language to be used to generate classes.</para>
/// <para><c>appArgs</c>--Extra arguments passed into the operation.</para>
/// <para><c>remainingArguments</c>--Extra arguments passed into the operation.</para>
/// </summary>
/// <param name="reportHandler"> The <see cref="IOperationReportHandler" />. </param>
/// <param name="args"> The executor arguments. </param>
Expand All @@ -63,7 +63,7 @@ public OperationExecutor([NotNull] IOperationReportHandler reportHandler, [NotNu
_projectDir = (string)args["projectDir"];
_rootNamespace = (string)args["rootNamespace"];
_language = (string)args["language"];
_designArgs = (string[])args["appArgs"];
_designArgs = (string[])args["remainingArguments"];

var toolsVersion = (string)args["toolsVersion"];
var runtimeVersion = ProductInfo.GetVersion();
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-ef/RootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override void Configure(CommandLineApplication command)
_command = command;
}

protected override int Execute()
protected override int Execute(string[] _)
{
var commands = _args.TakeWhile(a => a[0] != '-').ToList();
if (_help.HasValue()
Expand Down
8 changes: 5 additions & 3 deletions src/ef/AppDomainOperationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public AppDomainOperationExecutor(
string projectDir,
string dataDirectory,
string rootNamespace,
string language)
: base(assembly, startupAssembly, projectDir, rootNamespace, language)
string language,
string[] remainingArguments)
: base(assembly, startupAssembly, projectDir, rootNamespace, language, remainingArguments)
{
var info = new AppDomainSetup { ApplicationBase = AppBasePath };

Expand Down Expand Up @@ -66,7 +67,8 @@ public AppDomainOperationExecutor(
{ "projectDir", ProjectDirectory },
{ "rootNamespace", RootNamespace },
{ "language", Language },
{ "toolsVersion", ProductInfo.GetVersion() }
{ "toolsVersion", ProductInfo.GetVersion() },
{ "remainingArguments", RemainingArguments }
}
},
null,
Expand Down
14 changes: 7 additions & 7 deletions src/ef/CommandLineUtils/CommandLineApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ private enum ParseOptionResult
// remaining arguments, including the first unexpected argument, will be stored in RemainingArguments property.
private readonly bool _throwOnUnexpectedArg;

public CommandLineApplication(bool throwOnUnexpectedArg = true)
public CommandLineApplication(bool throwOnUnexpectedArg = false)
{
_throwOnUnexpectedArg = throwOnUnexpectedArg;
Options = new List<CommandOption>();
Arguments = new List<CommandArgument>();
Commands = new List<CommandLineApplication>();
RemainingArguments = new List<string>();
Invoke = () => 0;
Invoke = (args) => 0;
}

public CommandLineApplication Parent { get; set; }
Expand All @@ -48,7 +48,7 @@ public CommandLineApplication(bool throwOnUnexpectedArg = true)
public List<CommandArgument> Arguments { get; }
public List<string> RemainingArguments { get; }
public bool IsShowingInformation { get; protected set; } // Is showing help or version?
public Func<int> Invoke { get; set; }
public Func<string[], int> Invoke { get; set; }
public Func<string> LongVersionGetter { get; set; }
public Func<string> ShortVersionGetter { get; set; }
public List<CommandLineApplication> Commands { get; }
Expand All @@ -62,7 +62,7 @@ public CommandLineApplication Command(string name, bool throwOnUnexpectedArg = t

public CommandLineApplication Command(
string name, Action<CommandLineApplication> configuration,
bool throwOnUnexpectedArg = true)
bool throwOnUnexpectedArg = false)
{
var command = new CommandLineApplication(throwOnUnexpectedArg) { Name = name, Parent = this };
Commands.Add(command);
Expand Down Expand Up @@ -98,9 +98,9 @@ public CommandArgument Argument(string name, string description, Action<CommandA
return argument;
}

public void OnExecute(Func<int> invoke) => Invoke = invoke;
public void OnExecute(Func<string[], int> invoke) => Invoke = invoke;

public void OnExecute(Func<Task<int>> invoke) => Invoke = () => invoke().Result;
public void OnExecute(Func<string[], Task<int>> invoke) => Invoke = (args) => invoke(args).Result;

public int Execute(params string[] args)
{
Expand Down Expand Up @@ -158,7 +158,7 @@ public int Execute(params string[] args)
}
}

return command.Invoke();
return command.Invoke(command.RemainingArguments.ToArray());
}

private ParseOptionResult ParseOption(
Expand Down
11 changes: 4 additions & 7 deletions src/ef/Commands/CommandBase.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using JetBrains.Annotations;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.EntityFrameworkCore.Tools.Properties;

namespace Microsoft.EntityFrameworkCore.Tools.Commands
{
internal abstract class CommandBase
{
protected string[] RemainingArguments { get; private set; }

public virtual void Configure(CommandLineApplication command)
{
var verbose = command.Option("-v|--verbose", Resources.VerboseDescription);
var noColor = command.Option("--no-color", Resources.NoColorDescription);
var prefixOutput = command.Option("--prefix-output", Resources.PrefixDescription);

command.HandleResponseFiles = true;
RemainingArguments = command.RemainingArguments.ToArray();

command.OnExecute(
() =>
(args) =>
{
Reporter.IsVerbose = verbose.HasValue();
Reporter.NoColor = noColor.HasValue();
Reporter.PrefixOutput = prefixOutput.HasValue();
Validate();
return Execute();
return Execute(args);
});
}

protected virtual void Validate()
{
}

protected virtual int Execute()
protected virtual int Execute([NotNull] string[] args)
=> 0;
}
}
10 changes: 5 additions & 5 deletions src/ef/Commands/DatabaseDropCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace Microsoft.EntityFrameworkCore.Tools.Commands
// ReSharper disable once ArrangeTypeModifiers
internal partial class DatabaseDropCommand
{
protected override int Execute()
protected override int Execute(string[] args)
{
var executor = CreateExecutor();
var executor = CreateExecutor(args);

void LogDropCommand(Func<object, object, string> resource)
{
var result = executor.GetContextInfo(Context.Value(), RemainingArguments);
var result = executor.GetContextInfo(Context.Value());
var databaseName = result["DatabaseName"] as string;
var dataSource = result["DataSource"] as string;
Reporter.WriteInformation(resource(databaseName, dataSource));
Expand All @@ -38,9 +38,9 @@ void LogDropCommand(Func<object, object, string> resource)
}
}

executor.DropDatabase(Context.Value(), RemainingArguments);
executor.DropDatabase(Context.Value());

return base.Execute();
return base.Execute(args);
}
}
}
6 changes: 3 additions & 3 deletions src/ef/Commands/DatabaseUpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace Microsoft.EntityFrameworkCore.Tools.Commands
// ReSharper disable once ArrangeTypeModifiers
internal partial class DatabaseUpdateCommand
{
protected override int Execute()
protected override int Execute(string[] args)
{
CreateExecutor().UpdateDatabase(_migration.Value, _connection.Value(), Context.Value(), RemainingArguments);
CreateExecutor(args).UpdateDatabase(_migration.Value, _connection.Value(), Context.Value());

return base.Execute();
return base.Execute(args);
}
}
}
6 changes: 3 additions & 3 deletions src/ef/Commands/DbContextInfoCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace Microsoft.EntityFrameworkCore.Tools.Commands
// ReSharper disable once ArrangeTypeModifiers
internal partial class DbContextInfoCommand
{
protected override int Execute()
protected override int Execute(string[] args)
{
var result = CreateExecutor().GetContextInfo(Context.Value(), RemainingArguments);
var result = CreateExecutor(args).GetContextInfo(Context.Value());

if (_json.HasValue())
{
Expand All @@ -22,7 +22,7 @@ protected override int Execute()
ReportResult(result);
}

return base.Execute();
return base.Execute(args);
}

private static void ReportJsonResult(IDictionary result)
Expand Down
6 changes: 3 additions & 3 deletions src/ef/Commands/DbContextListCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace Microsoft.EntityFrameworkCore.Tools.Commands
// ReSharper disable once ArrangeTypeModifiers
internal partial class DbContextListCommand
{
protected override int Execute()
protected override int Execute(string[] args)
{
var types = CreateExecutor().GetContextTypes().ToList();
var types = CreateExecutor(args).GetContextTypes().ToList();

if (_json.HasValue())
{
Expand All @@ -24,7 +24,7 @@ protected override int Execute()
ReportResults(types);
}

return base.Execute();
return base.Execute(args);
}

private static void ReportJsonResults(IReadOnlyList<IDictionary> contextTypes)
Expand Down
9 changes: 4 additions & 5 deletions src/ef/Commands/DbContextScaffoldCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ protected override void Validate()
}
}

protected override int Execute()
protected override int Execute(string[] args)
{
var result = CreateExecutor().ScaffoldContext(
var result = CreateExecutor(args).ScaffoldContext(
_provider.Value,
_connection.Value,
_outputDir.Value(),
Expand All @@ -37,14 +37,13 @@ protected override int Execute()
_tables.Values,
_dataAnnotations.HasValue(),
_force.HasValue(),
_useDatabaseNames.HasValue(),
RemainingArguments);
_useDatabaseNames.HasValue());
if (_json.HasValue())
{
ReportJsonResults(result);
}

return base.Execute();
return base.Execute(args);
}

private static void ReportJsonResults(IDictionary result)
Expand Down
7 changes: 3 additions & 4 deletions src/ef/Commands/DbContextScriptCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ namespace Microsoft.EntityFrameworkCore.Tools.Commands
// ReSharper disable once ArrangeTypeModifiers
internal partial class DbContextScriptCommand
{
protected override int Execute()
protected override int Execute(string[] args)
{
var sql = CreateExecutor().ScriptDbContext(
Context.Value(), RemainingArguments);
var sql = CreateExecutor(args).ScriptDbContext(Context.Value());

if (!_output.HasValue())
{
Expand All @@ -37,7 +36,7 @@ protected override int Execute()
File.WriteAllText(output, sql, Encoding.UTF8);
}

return base.Execute();
return base.Execute(args);
}
}
}
4 changes: 2 additions & 2 deletions src/ef/Commands/HelpCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public override void Configure(CommandLineApplication command)
base.Configure(command);
}

protected override int Execute()
protected override int Execute(string[] args)
{
_command.ShowHelp();

return base.Execute();
return base.Execute(args);
}
}
}
7 changes: 4 additions & 3 deletions src/ef/Commands/MigrationsAddCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ protected override void Validate()
}
}

protected override int Execute()
protected override int Execute(string[] args)
{
var files = CreateExecutor().AddMigration(_name.Value, _outputDir.Value(), Context.Value(), RemainingArguments);
var files = CreateExecutor(args)
.AddMigration(_name.Value, _outputDir.Value(), Context.Value());

if (_json.HasValue())
{
Expand All @@ -32,7 +33,7 @@ protected override int Execute()
Reporter.WriteInformation(Resources.MigrationsAddCompleted);
}

return base.Execute();
return base.Execute(args);
}

private static void ReportJson(IDictionary files)
Expand Down
Loading

0 comments on commit 2823e3b

Please sign in to comment.