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 file purging to tray #515

Merged
merged 13 commits into from
Jul 18, 2024
5 changes: 5 additions & 0 deletions docs/mdsource/tray.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ Clicking "file1" or "file2" will delete file1 or file2 respectively. The drop do
Discard will clear all currently tracked items.


### Purge verified files

Prompts for a directory, and then recursively deletes all `*.verified.*` in that directory.


### Options

<img src="..\src\DiffEngineTray.Tests\OptionsFormTests.Default.verified.png">
Expand Down
5 changes: 5 additions & 0 deletions docs/tray.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ Clicking "file1" or "file2" will delete file1 or file2 respectively. The drop do
Discard will clear all currently tracked items.


### Purge verified files

Prompts for a directory, and then recursively deletes all `*.verified.*` in that directory.


### Options

<img src="..\src\DiffEngineTray.Tests\OptionsFormTests.Default.verified.png">
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/DiffEngineTray.Tests/MenuBuilderTest.Empty.verified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/DiffEngineTray.Tests/MenuBuilderTest.Full.verified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/DiffEngineTray.Tests/MenuBuilderTest.FullGrouped.verified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/DiffEngineTray.Tests/MenuBuilderTest.Grouped.verified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/DiffEngineTray.Tests/MenuBuilderTest.Many.verified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/DiffEngineTray.Tests/MenuBuilderTest.OnlyDelete.verified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/DiffEngineTray.Tests/MenuBuilderTest.OnlyMove.verified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 91 additions & 0 deletions src/DiffEngineTray/FilePurger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
static class FilePurger
{
public static void Launch()
{
var thread = new Thread(Inner);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}

static void Inner()
{
using var dialog = new FolderBrowserDialog();
var directoryResult = dialog.ShowDialog();

var path = dialog.SelectedPath;
if (directoryResult != DialogResult.OK ||
string.IsNullOrWhiteSpace(path))
{
return;
}

var files = Directory.GetFiles(path, "*.verified.*", SearchOption.AllDirectories);

if (files.Length == 0)
{
MessageBox.Show($"No *.verified.* files found in {path}");
return;
}

if (Confirm(files))
{
DeleteFiles(files);
}
}

static bool Confirm(string[] files)
{
var result = AskQuestion(
$"""
Files found: {files.Length}.
Delete files?
""",
"Confirm",
MessageBoxButtons.OKCancel);
return result == DialogResult.OK;
}

static void DeleteFiles(string[] files)
{
for (var index = 0; index < files.Length; index++)
{
var file = files[index];
try
{
if (File.Exists(file))
{
File.Delete(file);
}
}
catch (Exception exception)
{
var failedResult = AskQuestion(
$"""
Could not delete file: {file}
Exception: {exception.Message}
""",
"Delete failed",
MessageBoxButtons.AbortRetryIgnore);

if (failedResult == DialogResult.Abort)
{
return;
}

if (failedResult == DialogResult.Retry)
{
index--;
}
}
}
}

static DialogResult AskQuestion(string text, string caption, MessageBoxButtons buttons) =>
MessageBox.Show(
text,
caption,
buttons,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
}
41 changes: 22 additions & 19 deletions src/DiffEngineTray/MenuBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,25 @@ public static ContextMenuStrip Build(Action exit, Action launchOptions, Tracker
items.Add(new MenuButton("Exit", exit, Images.Exit));
items.Add(new MenuButton("Options", launchOptions, Images.Options));
items.Add(new MenuButton("Open logs", Logging.OpenDirectory, Images.Folder));
items.Add(new MenuButton("Purge verified files", FilePurger.Launch, Images.Folder));
items.Add(new MenuButton("Raise issue", IssueLauncher.Launch, Images.Link));
return menu;
}

static IEnumerable<ToolStripItem> NonDefaultMenus(ToolStripItemCollection items)
{
foreach (ToolStripItem item in items)
{
if (item.Text != "Exit" &&
item.Text != "Options" &&
item.Text != "Open logs" &&
item.Text != "Raise issue")
{
yield return item;
}
}
}
static IEnumerable<ToolStripItem> NonDefaultMenus(ToolStripItemCollection items) =>
items
.Cast<ToolStripItem>()
.Where(_ => _.Text != "Exit" &&
_.Text != "Options" &&
_.Text != "Open logs" &&
_.Text != "Raise issue" &&
_.Text != "Purge verified files")
.ToList();

static void RemovePreviousItems(ToolStripItemCollection items)
{
// Use ToList to avoid deferred execution of NonDefaultMenus
foreach (var item in NonDefaultMenus(items).ToList())
foreach (var item in NonDefaultMenus(items))
{
items.Remove(item);
}
Expand All @@ -53,7 +50,7 @@ static void RemovePreviousItems(ToolStripItemCollection items)
static void DisposePreviousItems(ToolStripItemCollection items)
{
// Use ToList to avoid deferred execution of NonDefaultMenus
foreach (var item in NonDefaultMenus(items).ToList())
foreach (var item in NonDefaultMenus(items))
{
item.Dispose();
}
Expand All @@ -66,11 +63,13 @@ static IEnumerable<ToolStripItem> BuildTrackingMenuItems(Tracker tracker)
yield break;
}

var deletes = tracker.Deletes
var deletes = tracker
.Deletes
.OrderBy(_ => _.File)
.ToList();

var moves = tracker.Moves
var moves = tracker
.Moves
.OrderBy(_ => _.Temp)
.ToList();

Expand Down Expand Up @@ -104,8 +103,12 @@ static IEnumerable<ToolStripItem> BuildGroupedMenuItems(
foreach (var toolStripItem in BuildMovesAndDeletes(
group,
tracker,
deletes.Where(_ => _.Group == group).ToList(),
moves.Where(_ => _.Group == group).ToList()))
deletes
.Where(_ => _.Group == group)
.ToList(),
moves
.Where(_ => _.Group == group)
.ToList()))
{
yield return toolStripItem;
addedCount++;
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649</NoWarn>
<Version>15.4.6</Version>
<Version>15.5.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Testing, Snapshot, Diff, Compare</PackageTags>
<Description>Launches diff tools based on file extensions. Designed to be consumed by snapshot testing libraries.</Description>
Expand Down
Loading