Skip to content

Commit

Permalink
Separate subcommands into separate classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate McMaster committed Jul 13, 2016
1 parent edb4b63 commit ecdf1a7
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 75 deletions.
68 changes: 1 addition & 67 deletions src/Tools.Console/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,55 +68,7 @@ public static CommandLineOptions Parse(params string[] args)
"The dispatcher version", inherited: true);
dispatcherVersion.ShowInHelpText = false;

app.Command("database", command =>
{
command.Description = "Commands to manage your database";
command.HelpOption();
command.Command("update", c => DatabaseUpdateCommand.ParseOptions(c, options));
command.Command("drop", c => DatabaseDropCommand.ParseOptions(c, options));
command.OnExecute(() =>
{
WriteLogo();
app.ShowHelp("database");
});
});

app.Command("dbcontext", command =>
{
command.Description = "Commands to manage your DbContext types";
command.HelpOption();
command.Command("list", c => DbContextListCommand.ParseOptions(c, options));
command.Command("scaffold", c => DbContextScaffoldCommand.ParseOptions(c, options));
command.OnExecute(() =>
{
WriteLogo();
command.ShowHelp();
});
});

app.Command("migrations", command =>
{
command.Description = "Commands to manage your migrations";
command.HelpOption();
command.Command("add", c => MigrationsAddCommand.ParseOptions(c, options));
command.Command("list", c => MigrationsListCommand.ParseOptions(c, options));
command.Command("remove", c => MigrationsRemoveCommand.ParseOptions(c, options));
command.Command("script", c => MigrationsScriptCommand.ParseOptions(c, options));
command.OnExecute(() =>
{
WriteLogo();
command.ShowHelp();
});
});

app.OnExecute(() =>
{
WriteLogo();
app.ShowHelp();
});
EfCommand.Configure(app, options);

var result = app.Execute(args);

Expand All @@ -143,23 +95,5 @@ public static CommandLineOptions Parse(params string[] args)

return options;
}

private static void WriteLogo()
{
const string Bold = "\x1b[1m";
const string Normal = "\x1b[22m";
const string Magenta = "\x1b[35m";
const string White = "\x1b[37m";
const string Default = "\x1b[39m";

Console.WriteLine();
Console.WriteLine(@" _/\__ ".Insert(21, Bold + White));
Console.WriteLine(@" ---==/ \\ ".Insert(20, Bold + White));
Console.WriteLine(@" ___ ___ |. \|\ ".Insert(26, Bold).Insert(21, Normal).Insert(20, Bold + White).Insert(9, Normal + Magenta));
Console.WriteLine(@" | __|| __| | ) \\\ ".Insert(20, Bold + White).Insert(8, Normal + Magenta));
Console.WriteLine(@" | _| | _| \_/ | //|\\ ".Insert(20, Bold + White).Insert(8, Normal + Magenta));
Console.WriteLine(@" |___||_| / \\\/\\".Insert(33, Normal + Default).Insert(23, Bold + White).Insert(8, Normal + Magenta));
Console.WriteLine();
}
}
}
25 changes: 25 additions & 0 deletions src/Tools.Console/Commands/DatabaseCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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 Microsoft.Extensions.CommandLineUtils;

namespace Microsoft.EntityFrameworkCore.Tools
{
public class DatabaseCommand
{
public static void Configure(CommandLineApplication command, CommandLineOptions options)
{
command.Description = "Commands to manage your database";
command.HelpOption();

command.Command("update", c => DatabaseUpdateCommand.Configure(c, options));
command.Command("drop", c => DatabaseDropCommand.Configure(c, options));

command.OnExecute(() =>
{
EfCommand.WriteLogo();
command.ShowHelp();
});
}
}
}
2 changes: 1 addition & 1 deletion src/Tools.Console/Commands/DatabaseDropCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.EntityFrameworkCore.Tools
{
public class DatabaseDropCommand : ICommand
{
public static void ParseOptions([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions commonOptions)
public static void Configure([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions commonOptions)
{
command.Description = "Drop the database for specific environment";
command.HelpOption();
Expand Down
2 changes: 1 addition & 1 deletion src/Tools.Console/Commands/DatabaseUpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.EntityFrameworkCore.Tools
{
public class DatabaseUpdateCommand : ICommand
{
public static void ParseOptions([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions commonOptions)
public static void Configure([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions commonOptions)
{
command.Description = "Updates the database to a specified migration";
command.HelpOption();
Expand Down
24 changes: 24 additions & 0 deletions src/Tools.Console/Commands/DbContextCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 Microsoft.Extensions.CommandLineUtils;

namespace Microsoft.EntityFrameworkCore.Tools
{
public class DbContextCommand
{
public static void Configure(CommandLineApplication command, CommandLineOptions options)
{
command.Description = "Commands to manage your DbContext types";
command.HelpOption();

command.Command("list", c => DbContextListCommand.Configure(c, options));
command.Command("scaffold", c => DbContextScaffoldCommand.Configure(c, options));
command.OnExecute(() =>
{
EfCommand.WriteLogo();
command.ShowHelp();
});
}
}
}
2 changes: 1 addition & 1 deletion src/Tools.Console/Commands/DbContextListCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.EntityFrameworkCore.Tools
{
public class DbContextListCommand : ICommand
{
public static void ParseOptions([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions options)
public static void Configure([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions options)
{
command.Description = "List your DbContext types";
command.HelpOption();
Expand Down
2 changes: 1 addition & 1 deletion src/Tools.Console/Commands/DbContextScaffoldCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.EntityFrameworkCore.Tools
{
public class DbContextScaffoldCommand : ICommand
{
public static void ParseOptions([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions options)
public static void Configure([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions options)
{
command.Description = "Scaffolds a DbContext and entity type classes for a specified database";
command.HelpOption();
Expand Down
44 changes: 44 additions & 0 deletions src/Tools.Console/Commands/EfCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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;
using Microsoft.Extensions.CommandLineUtils;

namespace Microsoft.EntityFrameworkCore.Tools
{
public static class EfCommand
{
public static void Configure(CommandLineApplication app, CommandLineOptions options)
{
app.Command("database", c => DatabaseCommand.Configure(c, options));

app.Command("dbcontext", c => DbContextCommand.Configure(c, options));

app.Command("migrations", c => MigrationsCommand.Configure(c, options));

app.OnExecute(() =>
{
WriteLogo();
app.ShowHelp();
});
}

public static void WriteLogo()
{
const string Bold = "\x1b[1m";
const string Normal = "\x1b[22m";
const string Magenta = "\x1b[35m";
const string White = "\x1b[37m";
const string Default = "\x1b[39m";

Console.WriteLine();
Console.WriteLine(@" _/\__ ".Insert(21, Bold + White));
Console.WriteLine(@" ---==/ \\ ".Insert(20, Bold + White));
Console.WriteLine(@" ___ ___ |. \|\ ".Insert(26, Bold).Insert(21, Normal).Insert(20, Bold + White).Insert(9, Normal + Magenta));
Console.WriteLine(@" | __|| __| | ) \\\ ".Insert(20, Bold + White).Insert(8, Normal + Magenta));
Console.WriteLine(@" | _| | _| \_/ | //|\\ ".Insert(20, Bold + White).Insert(8, Normal + Magenta));
Console.WriteLine(@" |___||_| / \\\/\\".Insert(33, Normal + Default).Insert(23, Bold + White).Insert(8, Normal + Magenta));
Console.WriteLine();
}
}
}
2 changes: 1 addition & 1 deletion src/Tools.Console/Commands/MigrationsAddCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.EntityFrameworkCore.Tools
{
public class MigrationsAddCommand : ICommand
{
public static void ParseOptions([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions options)
public static void Configure([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions options)
{
command.Description = "Add a new migration";
command.HelpOption();
Expand Down
26 changes: 26 additions & 0 deletions src/Tools.Console/Commands/MigrationsCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 Microsoft.Extensions.CommandLineUtils;

namespace Microsoft.EntityFrameworkCore.Tools
{
public class MigrationsCommand
{
public static void Configure(CommandLineApplication command, CommandLineOptions options)
{
command.Description = "Commands to manage your migrations";
command.HelpOption();

command.Command("add", c => MigrationsAddCommand.Configure(c, options));
command.Command("list", c => MigrationsListCommand.Configure(c, options));
command.Command("remove", c => MigrationsRemoveCommand.Configure(c, options));
command.Command("script", c => MigrationsScriptCommand.Configure(c, options));
command.OnExecute(() =>
{
EfCommand.WriteLogo();
command.ShowHelp();
});
}
}
}
2 changes: 1 addition & 1 deletion src/Tools.Console/Commands/MigrationsListCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.EntityFrameworkCore.Tools
{
public class MigrationsListCommand : ICommand
{
public static void ParseOptions([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions options)
public static void Configure([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions options)
{
command.Description = "List the migrations";
command.HelpOption();
Expand Down
2 changes: 1 addition & 1 deletion src/Tools.Console/Commands/MigrationsRemoveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.EntityFrameworkCore.Tools
{
public class MigrationsRemoveCommand : ICommand
{
public static void ParseOptions([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions options)
public static void Configure([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions options)
{
command.Description = "Remove the last migration";
command.HelpOption();
Expand Down
2 changes: 1 addition & 1 deletion src/Tools.Console/Commands/MigrationsScriptCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.EntityFrameworkCore.Tools
{
public class MigrationsScriptCommand : ICommand
{
public static void ParseOptions([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions options)
public static void Configure([NotNull] CommandLineApplication command, [NotNull] CommandLineOptions options)
{
command.Description = "Generate a SQL script from migrations";
command.HelpOption();
Expand Down
4 changes: 4 additions & 0 deletions src/Tools.Console/Tools.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
<Compile Include="..\Shared\CodeAnnotations.cs">
<Link>CodeAnnotations.cs</Link>
</Compile>
<Compile Include="Commands\EfCommand.cs" />
<Compile Include="Commands\MigrationsCommand.cs" />
<Compile Include="Commands\DbContextCommand.cs" />
<Compile Include="Commands\DatabaseCommand.cs" />
<Compile Include="Commands\ICommand.cs" />
<Compile Include="Commands\MigrationsScriptCommand.cs" />
<Compile Include="Extensions\CommandLineApplicationExtensions.cs" />
Expand Down

0 comments on commit ecdf1a7

Please sign in to comment.