Skip to content

Commit

Permalink
Finished up plugin loading and removed application started event
Browse files Browse the repository at this point in the history
  • Loading branch information
tarikguney committed Dec 19, 2018
1 parent 5bf4311 commit 695fd86
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
Copier.sln.DotSettings.user
/Client/bin/*
CopierPluginBase/obj/*
CopierPluginBase/bin*
4 changes: 4 additions & 0 deletions Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@
<PackageReference Include="CommandLineParser" Version="2.3.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CopierPluginBase\CopierPluginBase.csproj" />
</ItemGroup>

</Project>
21 changes: 16 additions & 5 deletions Client/FileCopier.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System;
using System.IO;
using CopierPluginBase;

namespace Copier.Client
{
class FileCopier : IFileCopier
class FileCopier : IFileCopier, IPreCopyEventBroadcaster, IPostCopyEventBroadcaster
{
public event Action<string> PreCopyEvent = delegate { };
public event Action<string> PostCopy = delegate { };

private readonly ILogger _logger;

public FileCopier(ILogger logger)
{
_logger = logger;
}

public Action<string> PreCopy = delegate { };
public Action<string> PostCopy = delegate { };

public void CopyFile(CommandOptions options, string fileName)
{
var absoluteSourceFilePath = Path.Combine(options.SourceDirectoryPath, fileName);
Expand All @@ -26,9 +27,19 @@ public void CopyFile(CommandOptions options, string fileName)
return;
}

PreCopy(absoluteSourceFilePath);
PreCopyEvent(absoluteSourceFilePath);
File.Copy(absoluteSourceFilePath, absoluteTargetFilePath, options.OverwriteTargetFile);
PostCopy(absoluteSourceFilePath);
}
}

public interface IPostCopyEventBroadcaster
{
event Action<string> PostCopy;
}

public interface IPreCopyEventBroadcaster
{
event Action<string> PreCopyEvent;
}
}
7 changes: 7 additions & 0 deletions Client/IPluginLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Copier.Client
{
public interface IPluginLoader
{
void Subscribe(IPreCopyEventBroadcaster pre, IPostCopyEventBroadcaster post);
}
}
51 changes: 32 additions & 19 deletions Client/PluginLoader.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,49 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using CopierPluginBase;

namespace Copier.Client
{
public class PluginLoader
public class PluginLoader : IPluginLoader
{
public void Load()
private readonly List<Type> _preCopyListeners = new List<Type>();
private readonly List<Type> _postCopyListeners = new List<Type>();

public PluginLoader()
{
var pluginDirectory = Path.Combine(Directory.GetCurrentDirectory(), "plugins");
var files = Directory.GetFiles(pluginDirectory, "*.dll");
foreach (var file in files)
var assemblyFiles = Directory.GetFiles(pluginDirectory, "*.dll");

foreach (var assemblyFile in assemblyFiles)
{
var pluginAssembly = Assembly.LoadFile(file);
var existingTypes = pluginAssembly.GetTypes();
var pluginAssembly = Assembly.LoadFile(assemblyFile);

var preCopyType = existingTypes
.FirstOrDefault(a => a.Name == "PreCopyEventListener")?.GetType();
if (preCopyType != null)
{
var preCopyObject = Activator.CreateInstance(preCopyType);
}
var postCopyListenerTypes = pluginAssembly
.GetTypes().Where(a => a.IsClass && a.IsSubclassOf(typeof(IPostCopyEventListener)));
_postCopyListeners.AddRange(postCopyListenerTypes);

var postCopyType = existingTypes
.FirstOrDefault(a => a.Name == "PostCopyEventListener")?.GetType();

if (postCopyType != null)
{
var preCopyObject = Activator.CreateInstance(preCopyType);
}
var preCopyListenerTypes = pluginAssembly
.GetTypes().Where(a => a.IsClass && a.IsSubclassOf(typeof(IPreCopyEventListener)));
_preCopyListeners.AddRange(preCopyListenerTypes);
}
}

public void Subscribe(IPreCopyEventBroadcaster pre, IPostCopyEventBroadcaster post)
{
_preCopyListeners.ForEach(a =>
{
var listenerObject = (IPreCopyEventListener) Activator.CreateInstance(a);
pre.PreCopyEvent += listenerObject.OnPreCopy;
});

_postCopyListeners.ForEach(a =>
{
var listenerObject = (IPostCopyEventListener) Activator.CreateInstance(a);
post.PostCopy += listenerObject.OnPostCopy;
});
}
}
}
9 changes: 5 additions & 4 deletions Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ namespace Copier.Client
{
class Program
{
public static event Action ApplicationStarted = delegate { };

static void Main(string[] args)
{
Parser.Default.ParseArguments<CommandOptions>(args)
Expand All @@ -20,17 +18,20 @@ static void Main(string[] args)

private static void StartWatching(CommandOptions options)
{
ApplicationStarted();

Console.WriteLine("Watching has started...");

options.SourceDirectoryPath = string.IsNullOrWhiteSpace(options.SourceDirectoryPath)
? Directory.GetCurrentDirectory()
: options.SourceDirectoryPath;

PluginLoader loader = new PluginLoader();

ILogger logger = new ConsoleLogger();
IFileCopier copier = new FileCopier(logger);
IFileWatcher fileWatcher = new FileWatcher(copier, logger);

loader.Subscribe((IPreCopyEventBroadcaster) copier, (IPostCopyEventBroadcaster) copier);

fileWatcher.Watch(options);
}
}
Expand Down
7 changes: 0 additions & 7 deletions CopierPluginBase/IApplicationStartedEventListener.cs

This file was deleted.

2 changes: 1 addition & 1 deletion CopierPluginBase/IPreCopyEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace CopierPluginBase
{
public interface IPreCopyEventListener
{
void OnPostCopy(string filePath);
void OnPreCopy(string filePath);
}
}

0 comments on commit 695fd86

Please sign in to comment.