Cleanup
This commit is contained in:
24
src/Core/FileTime.Core/Command/CommandExecutor.cs
Normal file
24
src/Core/FileTime.Core/Command/CommandExecutor.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
namespace FileTime.Core.Command
|
||||
{
|
||||
public class CommandExecutor
|
||||
{
|
||||
private readonly List<ICommandHandler> _commandHandlers;
|
||||
|
||||
public CommandExecutor(IEnumerable<ICommandHandler> commandHandlers)
|
||||
{
|
||||
_commandHandlers = commandHandlers.ToList();
|
||||
}
|
||||
|
||||
public void ExecuteCommand(ICommand command)
|
||||
{
|
||||
if (command is IExecutableCommand executableCommand)
|
||||
{
|
||||
executableCommand.Execute();
|
||||
}
|
||||
else
|
||||
{
|
||||
_commandHandlers.Find(c => c.CanHandle(command))?.Execute(command);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
62
src/Core/FileTime.Core/Command/CopyCommand.cs
Normal file
62
src/Core/FileTime.Core/Command/CopyCommand.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Timeline;
|
||||
|
||||
namespace FileTime.Core.Command
|
||||
{
|
||||
public class CopyCommand : ITransportationCommand
|
||||
{
|
||||
public IList<IAbsolutePath> Sources { get; } = new List<IAbsolutePath>();
|
||||
|
||||
public IContainer? Target { get; set; }
|
||||
|
||||
public TransportMode TransportMode { get; set; } = TransportMode.Merge;
|
||||
|
||||
public PointInTime SimulateCommand(PointInTime delta)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Execute(Action<IAbsolutePath, IAbsolutePath> copy)
|
||||
{
|
||||
DoCopy(Sources, Target, TransportMode, copy);
|
||||
}
|
||||
|
||||
private void DoCopy(IEnumerable<IAbsolutePath> sources, IContainer target, TransportMode transportMode, Action<IAbsolutePath, IAbsolutePath> copy)
|
||||
{
|
||||
foreach (var source in sources)
|
||||
{
|
||||
var item = source.ContentProvider.GetByPath(source.Path);
|
||||
|
||||
if (item is IContainer container)
|
||||
{
|
||||
var targetContainer = target.Containers.FirstOrDefault(d => d.Name == container.Name) ?? (target.CreateContainer(container.Name)!);
|
||||
|
||||
var childDirectories = container.Containers.Select(d => new AbsolutePath(item.Provider, d.FullName!));
|
||||
var childFiles = container.Elements.Select(f => new AbsolutePath(item.Provider, f.FullName!));
|
||||
|
||||
DoCopy(childDirectories.Concat(childFiles), targetContainer, transportMode, copy);
|
||||
}
|
||||
else if (item is IElement element)
|
||||
{
|
||||
var targetName = element.Name;
|
||||
|
||||
if (transportMode == TransportMode.Merge)
|
||||
{
|
||||
for (var i = 0; target.IsExists(targetName); i++)
|
||||
{
|
||||
targetName = element.Name + (i == 0 ? "_" : $"_{i}");
|
||||
}
|
||||
}
|
||||
else if (transportMode == TransportMode.Skip && target.IsExists(targetName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var targetPath = target.FullName + Constants.SeparatorChar + targetName;
|
||||
|
||||
copy(new AbsolutePath(source.ContentProvider, element.FullName!), new AbsolutePath(target.Provider, targetPath));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
src/Core/FileTime.Core/Command/CreateContainerCommand.cs
Normal file
12
src/Core/FileTime.Core/Command/CreateContainerCommand.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using FileTime.Core.Timeline;
|
||||
|
||||
namespace FileTime.Core.Command
|
||||
{
|
||||
public class CreateContainerCommand : ICommand
|
||||
{
|
||||
public PointInTime SimulateCommand(PointInTime delta)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
12
src/Core/FileTime.Core/Command/CreateElementCommand.cs
Normal file
12
src/Core/FileTime.Core/Command/CreateElementCommand.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using FileTime.Core.Timeline;
|
||||
|
||||
namespace FileTime.Core.Command
|
||||
{
|
||||
public class CreateElementCommand : ICommand
|
||||
{
|
||||
public PointInTime SimulateCommand(PointInTime delta)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
41
src/Core/FileTime.Core/Command/DeleteCommand.cs
Normal file
41
src/Core/FileTime.Core/Command/DeleteCommand.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Timeline;
|
||||
|
||||
namespace FileTime.Core.Command
|
||||
{
|
||||
public class DeleteCommand : IExecutableCommand
|
||||
{
|
||||
public IList<IAbsolutePath> ItemsToDelete { get; } = new List<IAbsolutePath>();
|
||||
|
||||
public PointInTime SimulateCommand(PointInTime delta)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
foreach (var item in ItemsToDelete)
|
||||
{
|
||||
DoDelete(item.ContentProvider.GetByPath(item.Path)!);
|
||||
}
|
||||
}
|
||||
|
||||
private void DoDelete(IItem item)
|
||||
{
|
||||
if (item is IContainer container)
|
||||
{
|
||||
foreach (var child in container.Items)
|
||||
{
|
||||
DoDelete(child);
|
||||
child.Delete();
|
||||
}
|
||||
|
||||
item.Delete();
|
||||
}
|
||||
else if(item is IElement element)
|
||||
{
|
||||
element.Delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
9
src/Core/FileTime.Core/Command/ICommand.cs
Normal file
9
src/Core/FileTime.Core/Command/ICommand.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using FileTime.Core.Timeline;
|
||||
|
||||
namespace FileTime.Core.Command
|
||||
{
|
||||
public interface ICommand
|
||||
{
|
||||
PointInTime SimulateCommand(PointInTime moment);
|
||||
}
|
||||
}
|
||||
8
src/Core/FileTime.Core/Command/ICommandHandler.cs
Normal file
8
src/Core/FileTime.Core/Command/ICommandHandler.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace FileTime.Core.Command
|
||||
{
|
||||
public interface ICommandHandler
|
||||
{
|
||||
bool CanHandle(object command);
|
||||
void Execute(object command);
|
||||
}
|
||||
}
|
||||
7
src/Core/FileTime.Core/Command/IExecutableCommand.cs
Normal file
7
src/Core/FileTime.Core/Command/IExecutableCommand.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace FileTime.Core.Command
|
||||
{
|
||||
public interface IExecutableCommand : ICommand
|
||||
{
|
||||
void Execute();
|
||||
}
|
||||
}
|
||||
11
src/Core/FileTime.Core/Command/ITransportationCommand.cs
Normal file
11
src/Core/FileTime.Core/Command/ITransportationCommand.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using FileTime.Core.Models;
|
||||
|
||||
namespace FileTime.Core.Command
|
||||
{
|
||||
public interface ITransportationCommand : ICommand
|
||||
{
|
||||
IList<IAbsolutePath> Sources { get; }
|
||||
IContainer Target { get; set;}
|
||||
TransportMode TransportMode { get; set; }
|
||||
}
|
||||
}
|
||||
18
src/Core/FileTime.Core/Command/MoveCommand.cs
Normal file
18
src/Core/FileTime.Core/Command/MoveCommand.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Timeline;
|
||||
|
||||
namespace FileTime.Core.Command
|
||||
{
|
||||
public class MoveCommand : ITransportationCommand
|
||||
{
|
||||
public IList<IAbsolutePath> Sources { get; } = new List<IAbsolutePath>();
|
||||
|
||||
public IContainer? Target { get; set; }
|
||||
public TransportMode TransportMode { get; set; } = TransportMode.Merge;
|
||||
|
||||
public PointInTime SimulateCommand(PointInTime delta)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
9
src/Core/FileTime.Core/Command/TransportMode.cs
Normal file
9
src/Core/FileTime.Core/Command/TransportMode.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace FileTime.Core.Command
|
||||
{
|
||||
public enum TransportMode
|
||||
{
|
||||
Merge,
|
||||
Overwrite,
|
||||
Skip
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user