Base features

This commit is contained in:
2022-01-06 18:36:25 +01:00
parent 033b280175
commit 7b3584543e
60 changed files with 2576 additions and 0 deletions

View 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);
}
}
}
}

View 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));
}
}
}
}
}

View 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();
}
}
}

View 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();
}
}
}

View 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();
}
}
}
}

View File

@@ -0,0 +1,9 @@
using FileTime.Core.Timeline;
namespace FileTime.Core.Command
{
public interface ICommand
{
PointInTime SimulateCommand(PointInTime moment);
}
}

View File

@@ -0,0 +1,8 @@
namespace FileTime.Core.Command
{
public interface ICommandHandler
{
bool CanHandle(object command);
void Execute(object command);
}
}

View File

@@ -0,0 +1,7 @@
namespace FileTime.Core.Command
{
public interface IExecutableCommand : ICommand
{
void Execute();
}
}

View 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; }
}
}

View 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();
}
}
}

View File

@@ -0,0 +1,9 @@
namespace FileTime.Core.Command
{
public enum TransportMode
{
Merge,
Overwrite,
Skip
}
}