Skip to content

Commit

Permalink
Merge pull request #3 from EngincanV/EngincanV/database-drop
Browse files Browse the repository at this point in the history
Add ef core database drop support.
  • Loading branch information
enisn authored Dec 4, 2023
2 parents f22b394 + e2faf8e commit f33e774
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
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();

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

0 comments on commit f33e774

Please sign in to comment.