Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ef core database drop support. #3

Merged
merged 2 commits into from
Dec 4, 2023
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
95 changes: 95 additions & 0 deletions src/AbpDevTools/Commands/DatabaseDropCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System.Diagnostics;
using AbpDevTools.Notifications;
using CliFx.Infrastructure;
using Spectre.Console;

namespace AbpDevTools.Commands;

[Command("database-drop", Description = "Drops all databases in the working directory")]
public class DatabaseDropCommand : ICommand
{
[CommandParameter(0, IsRequired = false, Description = "Working directory to search for EntityFramework projects. Default: . (Current Directory)")]
public string WorkingDirectory { get; set; }

[CommandOption("force", 'f')]
public bool Force { get; set; }

protected readonly INotificationManager notificationManager;

public DatabaseDropCommand(INotificationManager notificationManager)
{
this.notificationManager = notificationManager;
}

public async ValueTask ExecuteAsync(IConsole console)
{
if (string.IsNullOrEmpty(WorkingDirectory))
{
WorkingDirectory = Directory.GetCurrentDirectory();
}

var efCoreProjects = await GetEfCoreProjectsAsync();

var cancellationToken = console.RegisterCancellationHandler();
enisn marked this conversation as resolved.
Show resolved Hide resolved

var projectCount = efCoreProjects.Length;
if (projectCount == 0)
{
await console.Output.WriteLineAsync("Could not find any EntityFrameworkCore project in the working directory...");
return;
}

AnsiConsole.MarkupLine($"[green]{projectCount}[/] EntityFrameworkCore project(s) found in the directory. Trying to find and drop databases...");

var forcePostfix = Force ? " --force" : string.Empty;

for (var i = 0; i < projectCount; i++)
{
var efCoreProject = efCoreProjects[i];

AnsiConsole.MarkupLine($"[blue]## Project{(i + 1)} - {efCoreProject.Name.Replace(".csproj", string.Empty)}[/]");

var startInfo = new ProcessStartInfo("dotnet", $"ef database drop{forcePostfix}")
{
WorkingDirectory = efCoreProject.DirectoryName!,
RedirectStandardOutput = true,
CreateNoWindow = false
};

var process = Process.Start(startInfo)!;

process.OutputDataReceived += async (sender, args) =>
{
if (args?.Data != null)
{
await console.Output.WriteLineAsync("* " + args.Data);
}
};

process.BeginOutputReadLine();

await process.WaitForExitAsync(cancellationToken);
}

if (!cancellationToken.IsCancellationRequested)
{
await notificationManager.SendAsync("Dropped database(s)", $"Dropped all databases in {WorkingDirectory}");
}
}

private async Task<FileInfo[]> GetEfCoreProjectsAsync()
{
return await AnsiConsole.Status()
.StartAsync("Searching EntityFrameworkCore projects...", ctx =>
{
ctx.Spinner(Spinner.Known.SimpleDotsScrolling);

var efCoreProjects = Directory.EnumerateFiles(WorkingDirectory, "*.csproj", SearchOption.AllDirectories)
.Where(x => x.EndsWith("EntityFrameworkCore.csproj"))
.Select(x => new FileInfo(x))
.ToArray();

return Task.FromResult(efCoreProjects);
});
}
}
3 changes: 2 additions & 1 deletion src/AbpDevTools/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public static CliApplicationBuilder BuildServices(this CliApplicationBuilder bui
typeof(AbpBundleCommand),
typeof(TestCommand),
typeof(UpdateCheckCommand),
typeof(CleanCommand)
typeof(CleanCommand),
typeof(DatabaseDropCommand)
};

foreach (var commandType in commands)
Expand Down
Loading