-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
поддержка указания произвольной папки с бэкапами в приложении командн…
…ой строки
- Loading branch information
Showing
12 changed files
with
193 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,47 @@ | ||
namespace ShibuyaTools.Core; | ||
|
||
public sealed class FileSource(string sourcePath) | ||
public sealed class FileSource(string sourcePath, string backupPath) | ||
{ | ||
private readonly string backupPath = sourcePath + ".bak"; | ||
|
||
private string ReadPath => File.Exists(backupPath) ? backupPath : sourcePath; | ||
public string FileName => Path.GetFileName(sourcePath); | ||
public string FileNameWithoutExtension => Path.GetFileNameWithoutExtension(sourcePath); | ||
public FileDestination Destination => new(sourcePath); | ||
public DateTime LastWriteTimeUtc => File.GetLastWriteTimeUtc(ReadPath); | ||
|
||
private string ReadPath => File.Exists(backupPath) ? backupPath : sourcePath; | ||
|
||
public FileStream OpenRead() => File.OpenRead(ReadPath); | ||
public bool CanUnroll() => File.Exists(backupPath); | ||
public FileTarget CreateTarget() => new(sourcePath, createBackupIfNotExists: true); | ||
|
||
public void Unroll() | ||
public FileTarget CreateTarget(ProgressCallback<long> callback) | ||
{ | ||
if (File.Exists(backupPath)) | ||
{ | ||
File.Move(backupPath, sourcePath, overwrite: true); | ||
} | ||
} | ||
var createBackupIfNotExists = !Path.Exists(backupPath); | ||
|
||
public static IEnumerable<FileSource> EnumerateFiles(string directory, string searchPattern) | ||
{ | ||
foreach (var path in Directory.EnumerateFiles(directory, searchPattern)) | ||
if (createBackupIfNotExists) | ||
{ | ||
yield return new FileSource(path); | ||
// Backup source if backup directory is different | ||
var sourceDir = Path.GetDirectoryName(sourcePath); | ||
var backupDir = Path.GetDirectoryName(backupPath); | ||
|
||
if (sourceDir != backupDir) | ||
{ | ||
using var target = new FileTarget(backupPath); | ||
using var source = File.OpenRead(sourcePath); | ||
source.CopyTo(target.Stream, callback); | ||
target.CopyFileInfo(sourcePath); | ||
target.Commit(); | ||
|
||
createBackupIfNotExists = false; // already backed up | ||
} | ||
} | ||
|
||
return new(sourcePath, createBackupIfNotExists); | ||
} | ||
|
||
public static IEnumerable<FileSource> EnumerateFiles(string directory, params string[] searchPatterns) | ||
public void Unroll() | ||
{ | ||
foreach (var searchPattern in searchPatterns) | ||
if (File.Exists(backupPath)) | ||
{ | ||
foreach (var source in EnumerateFiles(directory, searchPattern)) | ||
{ | ||
yield return source; | ||
} | ||
File.Move(backupPath, sourcePath, overwrite: true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace ShibuyaTools.Core; | ||
|
||
public delegate void ProgressCallback<T>(ProgressPayload<T> payload); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace ShibuyaTools.Core; | ||
|
||
public readonly record struct ProgressPayload<T>(T Total, T Position); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.