add file and folder exclusions to copy task

This commit is contained in:
IsWaffle 2023-09-16 16:10:40 -04:00
parent 227e68f8fd
commit 4508735c4f
2 changed files with 29 additions and 10 deletions

View File

@ -1,4 +1,5 @@
using System.Linq; using System.Collections.Generic;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Serilog; using Serilog;
@ -8,12 +9,18 @@ namespace SPTInstaller.Helpers;
public static class FileHelper public static class FileHelper
{ {
private static Result IterateDirectories(DirectoryInfo sourceDir, DirectoryInfo targetDir) private static Result IterateDirectories(DirectoryInfo sourceDir, DirectoryInfo targetDir, string[] exclusions)
{ {
try try
{ {
foreach (var dir in sourceDir.GetDirectories("*", SearchOption.AllDirectories)) foreach (var dir in sourceDir.GetDirectories("*", SearchOption.AllDirectories))
{ {
if (exclusions.Contains(dir.FullName.Replace(sourceDir.FullName, "")))
{
Log.Information($"Excluding Dir: {dir.FullName}");
continue;
}
Directory.CreateDirectory(dir.FullName.Replace(sourceDir.FullName, targetDir.FullName)); Directory.CreateDirectory(dir.FullName.Replace(sourceDir.FullName, targetDir.FullName));
} }
return Result.FromSuccess(); return Result.FromSuccess();
@ -25,7 +32,7 @@ public static class FileHelper
} }
} }
private static Result IterateFiles(DirectoryInfo sourceDir, DirectoryInfo targetDir, Action<string, int> updateCallback = null) private static Result IterateFiles(DirectoryInfo sourceDir, DirectoryInfo targetDir, string[] exclusions, Action<string, int> updateCallback = null)
{ {
try try
{ {
@ -36,6 +43,16 @@ public static class FileHelper
{ {
updateCallback?.Invoke(file.Name, (int)Math.Floor(((double)processedFiles / totalFiles) * 100)); updateCallback?.Invoke(file.Name, (int)Math.Floor(((double)processedFiles / totalFiles) * 100));
foreach (var exclusion in exclusions)
{
var currentFileRelativePath = file.FullName.Replace(sourceDir.FullName, "");
if (currentFileRelativePath.StartsWith(exclusion) || currentFileRelativePath == exclusion)
{
continue;
}
}
File.Copy(file.FullName, file.FullName.Replace(sourceDir.FullName, targetDir.FullName), true); File.Copy(file.FullName, file.FullName.Replace(sourceDir.FullName, targetDir.FullName), true);
processedFiles++; processedFiles++;
} }
@ -62,18 +79,18 @@ public static class FileHelper
return path; return path;
} }
public static Result CopyDirectoryWithProgress(DirectoryInfo sourceDir, DirectoryInfo targetDir, IProgress<double> progress = null) => public static Result CopyDirectoryWithProgress(DirectoryInfo sourceDir, DirectoryInfo targetDir, IProgress<double> progress = null, string[] exclusions = null) =>
CopyDirectoryWithProgress(sourceDir, targetDir, (msg, prog) => progress?.Report(prog)); CopyDirectoryWithProgress(sourceDir, targetDir, (msg, prog) => progress?.Report(prog), exclusions);
public static Result CopyDirectoryWithProgress(DirectoryInfo sourceDir, DirectoryInfo targetDir, Action<string, int> updateCallback = null) public static Result CopyDirectoryWithProgress(DirectoryInfo sourceDir, DirectoryInfo targetDir, Action<string, int> updateCallback = null, string[] exclusions = null)
{ {
try try
{ {
var iterateDirectoriesResult = IterateDirectories(sourceDir, targetDir); var iterateDirectoriesResult = IterateDirectories(sourceDir, targetDir, exclusions ??= new string[0]);
if(!iterateDirectoriesResult.Succeeded) return iterateDirectoriesResult; if(!iterateDirectoriesResult.Succeeded) return iterateDirectoriesResult;
var iterateFilesResult = IterateFiles(sourceDir, targetDir, updateCallback); var iterateFilesResult = IterateFiles(sourceDir, targetDir, exclusions ??= new string[0], updateCallback);
if (!iterateFilesResult.Succeeded) return iterateDirectoriesResult; if (!iterateFilesResult.Succeeded) return iterateDirectoriesResult;

View File

@ -21,6 +21,8 @@ public class CopyClientTask : InstallerTaskBase
var originalGameDirInfo = new DirectoryInfo(_data.OriginalGamePath); var originalGameDirInfo = new DirectoryInfo(_data.OriginalGamePath);
var targetInstallDirInfo = new DirectoryInfo(_data.TargetInstallPath); var targetInstallDirInfo = new DirectoryInfo(_data.TargetInstallPath);
return FileHelper.CopyDirectoryWithProgress(originalGameDirInfo, targetInstallDirInfo, (message, progress) => { SetStatus(null, message, progress, null, true); }); var exclusions = new[] { "\\Logs" };
return FileHelper.CopyDirectoryWithProgress(originalGameDirInfo, targetInstallDirInfo, (message, progress) => { SetStatus(null, message, progress, null, true); }, exclusions);
} }
} }