Delete empty folders after install. Ends up being a bit tricky because folders might exist that weren't part of the original list. So we have to find all the folders, and then find the ones that aren't part of the folders to be created by the installer, and we delete those folders.

This commit is contained in:
Timothy Baldridge 2020-01-04 22:07:00 -07:00
parent 5e03ceda2e
commit 528b1daded
2 changed files with 31 additions and 0 deletions

View File

@ -377,6 +377,30 @@ namespace Wabbajack.Lib
.Where(d => d != null)
.Do(d => indexed.Remove(d.To));
Utils.Log("Cleaning empty folders");
var expectedFolders = indexed.Keys.SelectMany(path =>
{
// Get all the folders and all the folder parents
// so for foo\bar\baz\qux.txt this emits ["foo", "foo\\bar", "foo\\bar\\baz"]
var split = path.Split('\\');
return Enumerable.Range(1, split.Length - 1).Select(t => string.Join("\\", split.Take(t)));
}).Distinct()
.Select(p => Path.Combine(OutputFolder, p))
.ToHashSet();
try
{
Directory.EnumerateDirectories(OutputFolder, DirectoryEnumerationOptions.Recursive)
.Where(p => !expectedFolders.Contains(p))
.OrderByDescending(p => p.Length)
.Do(p => Directory.Delete(p));
}
catch (Exception)
{
// ignored because it's not worth throwing a fit over
Utils.Log("Error when trying to clean empty folders. This doesn't really matter.");
}
UpdateTracker.NextStep("Updating Modlist");
Utils.Log($"Optimized {ModList.Directives.Count} directives to {indexed.Count} required");
var requiredArchives = indexed.Values.OfType<FromArchive>()

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@ -87,6 +88,11 @@ namespace Wabbajack.Test
var extra_path = utils.PathOfInstalledFile(mod, @"something_i_made.foo");
File.WriteAllText(extra_path, "bleh");
var extra_folder = Path.Combine(Path.GetDirectoryName(utils.PathOfInstalledFile(mod, @"something_i_made.foo")), "folder_i_made");
Directory.CreateDirectory(extra_folder);
Assert.IsTrue(Directory.Exists(extra_folder));
var unchanged_modified = File.GetLastWriteTime(unchanged_path);
var modified_modified = File.GetLastWriteTime(modified_path);
@ -105,6 +111,7 @@ namespace Wabbajack.Test
Assert.AreEqual(unchanged_modified, File.GetLastWriteTime(unchanged_path));
Assert.AreNotEqual(modified_modified, File.GetLastWriteTime(modified_path));
Assert.IsFalse(File.Exists(extra_path));
Assert.IsFalse(Directory.Exists(extra_folder));
}