Skip to content

Commit

Permalink
added possibility to ignore files by extension
Browse files Browse the repository at this point in the history
  • Loading branch information
NobodysNightmare committed Dec 13, 2012
1 parent 8e2c822 commit a9f5abf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
13 changes: 11 additions & 2 deletions CompareFS/PathComparator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ public class PathComparator
{
private IPathModificationListener Listener;

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

public PathComparator(IPathModificationListener listener)
{
Listener = listener;

ExcludedFileExtensions = new List<string>();
}

public void Compare(DirectoryInfo referenceDirectory, DirectoryInfo targetDirectory)
Expand Down Expand Up @@ -40,14 +44,19 @@ private void CompareSubdirectories(DirectoryInfo referenceDirectory, DirectoryIn

private void CompareFiles(DirectoryInfo referenceDirectory, DirectoryInfo targetDirectory)
{
foreach (FileInfo fi in targetDirectory.EnumerateFiles().Where(f => !referenceDirectory.HasFile(f)))
foreach (FileInfo fi in EnumerateLeftFilesMissingRight(targetDirectory, referenceDirectory))
{
Listener.OnModification(new FileModification(fi, ModificationType.Added));
}
foreach (FileInfo fi in referenceDirectory.EnumerateFiles().Where(f => !targetDirectory.HasFile(f)))
foreach (FileInfo fi in EnumerateLeftFilesMissingRight(referenceDirectory, targetDirectory))
{
Listener.OnModification(new FileModification(fi, ModificationType.Removed));
}
}

private IEnumerable<FileInfo> EnumerateLeftFilesMissingRight(DirectoryInfo leftFiles, DirectoryInfo rightFiles)
{
return leftFiles.EnumerateFiles().Where(f => !ExcludedFileExtensions.Contains(f.Extension)).Where(f => !rightFiles.HasFile(f));
}
}
}
19 changes: 16 additions & 3 deletions ComparePathCLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,27 @@ class Program
{
static void Main(string[] args)
{
if (args.Length < 2)
if (args.Length < 2 || args.Length % 2 != 0)
{
Console.WriteLine("Usage: ComparePathCLI path1 path2");
Console.WriteLine("Usage: ComparePathCLI (-i extension)* path1 path2");
return;
}

var comparator = new PathComparator(new PathModificationConsoleWriter());
comparator.Compare(new DirectoryInfo(args[0]), new DirectoryInfo(args[1]));

for (int i = 0; i < args.Length - 2; i += 2)
{
if (args[i] == "-i")
{
comparator.ExcludedFileExtensions.Add(args[i+1]);
}
else
{
Console.WriteLine("Skipping unknown option \"{0}\"", args[i]);
}
}

comparator.Compare(new DirectoryInfo(args[args.Length - 2]), new DirectoryInfo(args[args.Length - 1]));
}
}
}

0 comments on commit a9f5abf

Please sign in to comment.