Skip to content

Commit

Permalink
use events instead of own listener
Browse files Browse the repository at this point in the history
also re-format console-output
  • Loading branch information
NobodysNightmare committed Dec 27, 2013
1 parent f935488 commit 8dc02fc
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 118 deletions.
6 changes: 1 addition & 5 deletions CompareFS/CompareFS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DirectoryInfoExtensions.cs" />
<Compile Include="DirectoryModification.cs" />
<Compile Include="FileModification.cs" />
<Compile Include="ModificationType.cs" />
<Compile Include="ModificationEventArgs.cs" />
<Compile Include="Pair.cs" />
<Compile Include="PathComparator.cs" />
<Compile Include="PathModification.cs" />
<Compile Include="IPathModificationListener.cs" />
<Compile Include="PathModificationConsoleWriter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
24 changes: 0 additions & 24 deletions CompareFS/DirectoryModification.cs

This file was deleted.

24 changes: 0 additions & 24 deletions CompareFS/FileModification.cs

This file was deleted.

12 changes: 0 additions & 12 deletions CompareFS/IPathModificationListener.cs

This file was deleted.

28 changes: 28 additions & 0 deletions CompareFS/ModificationEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CompareFS
{
public enum ModificationType
{
None,
Added,
Removed,
Changed
}

public class ModificationEventArgs : EventArgs
{
public string Path { get; private set; }

public ModificationType Type { get; private set; }

public ModificationEventArgs(string path, ModificationType type)
{
Path = path;
Type = type;
}
}
}
15 changes: 0 additions & 15 deletions CompareFS/ModificationType.cs

This file was deleted.

27 changes: 18 additions & 9 deletions CompareFS/PathComparator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,28 @@ namespace CompareFS
{
public class PathComparator
{
private IPathModificationListener Listener;
public delegate void ModificationEventHandler(object sender, ModificationEventArgs args);

public event ModificationEventHandler Modification;

public List<string> ExcludedFileExtensions { get; private set; }

public bool CheckForModifications { get; set; }

public PathComparator(IPathModificationListener listener)
public PathComparator()
{
Listener = listener;

ExcludedFileExtensions = new List<string>();
}

protected void OnModification(ModificationEventArgs args)
{
var handler = Modification;
if (handler != null)
{
handler(this, args);
}
}

public void Compare(DirectoryInfo referenceDirectory, DirectoryInfo targetDirectory)
{
CompareSubdirectories(referenceDirectory, targetDirectory);
Expand All @@ -38,23 +47,23 @@ private void CompareSubdirectories(DirectoryInfo referenceDirectory, DirectoryIn
{
foreach (DirectoryInfo di in targetDirectory.EnumerateDirectories().Where(d => !referenceDirectory.HasSubdirectory(d)))
{
Listener.OnModification(new DirectoryModification(di, ModificationType.Added));
OnModification(new ModificationEventArgs(di.FullName, ModificationType.Added));
}
foreach (DirectoryInfo di in referenceDirectory.EnumerateDirectories().Where(d => !targetDirectory.HasSubdirectory(d)))
{
Listener.OnModification(new DirectoryModification(di, ModificationType.Removed));
OnModification(new ModificationEventArgs(di.FullName, ModificationType.Removed));
}
}

private void CompareFiles(DirectoryInfo referenceDirectory, DirectoryInfo targetDirectory)
{
foreach (FileInfo fi in EnumerateLeftFilesMissingRight(targetDirectory, referenceDirectory))
{
Listener.OnModification(new FileModification(fi, ModificationType.Added));
OnModification(new ModificationEventArgs(fi.FullName, ModificationType.Added));
}
foreach (FileInfo fi in EnumerateLeftFilesMissingRight(referenceDirectory, targetDirectory))
{
Listener.OnModification(new FileModification(fi, ModificationType.Removed));
OnModification(new ModificationEventArgs(fi.FullName, ModificationType.Removed));
}

if (CheckForModifications)
Expand All @@ -63,7 +72,7 @@ private void CompareFiles(DirectoryInfo referenceDirectory, DirectoryInfo target
{
if (!CompareFilesAsync(pair.Left, pair.Right).Result)
{
Listener.OnModification(new FileModification(pair.Left, ModificationType.Changed));
OnModification(new ModificationEventArgs(pair.Left.FullName, ModificationType.Changed));
}
}
}
Expand Down
19 changes: 0 additions & 19 deletions CompareFS/PathModification.cs

This file was deleted.

33 changes: 24 additions & 9 deletions CompareFS/PathModificationConsoleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,45 @@

namespace CompareFS
{
public class PathModificationConsoleWriter : IPathModificationListener
public class PathModificationConsoleWriter
{
public void OnModification(PathModification modification)
public PathModificationConsoleWriter(PathComparator comparator)
{
if (modification == null)
comparator.Modification += HandleModification;
}

private void HandleModification(object sender, ModificationEventArgs args)
{
if (args == null)
{
throw new ArgumentNullException("modification");
throw new ArgumentNullException("args");
}

switch (modification.Modification)
switch (args.Type)
{
case ModificationType.Added:
Console.Write("ADDED: ");
WriteColored(ConsoleColor.Green, "ADDED: ");
break;
case ModificationType.Removed:
Console.Write("REMOVED: ");
WriteColored(ConsoleColor.Red, "REMOVED: ");
break;
case ModificationType.Changed:
Console.Write("CHANGED: ");
WriteColored(ConsoleColor.Yellow, "CHANGED: ");
break;
default:
break;
}
Console.WriteLine(modification.Path);
Console.WriteLine(args.Path);
}

private static void WriteColored(ConsoleColor color, string value, params object[] args)
{
var oldColor = Console.ForegroundColor;
Console.ForegroundColor = color;

Console.Write(value, args);

Console.ForegroundColor = oldColor;
}
}
}
3 changes: 2 additions & 1 deletion ComparePathCLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ static void Main(string[] args)
return;
}

var comparator = new PathComparator(new PathModificationConsoleWriter());
var comparator = new PathComparator();
var writer = new PathModificationConsoleWriter(comparator);

for (int i = 0; i < args.Length - 2; i += 2)
{
Expand Down

0 comments on commit 8dc02fc

Please sign in to comment.