using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace SL_Switcher
{
public class ListBuilder
{
private enum ScanType
{
Copy,
Delete
}
///
/// Builds a list of file operations to sync src with dst
///
/// Source directory
/// Destination directory
/// Action list
public static List Build(string src, string dst)
{
return Build(src, null, dst);
}
///
/// Builds a list of file operations to sync src with dst.
/// This version uses a source overlay
///
/// Source directory
/// Additional source files
/// Destination directory
/// Action list
public static List Build(string src, string overlay, string dst)
{
List tmp = new List();
List ret = new List();
Console.WriteLine("Scanning for files to copy");
// List of files to add. Overlay files are skipped here
ScanRecursive(src,overlay, dst, "", tmp, ScanType.Copy);
Console.WriteLine("Scanning for files to delete");
// Files to delete. Files present in overlay are skipped
ScanRecursive(dst,overlay, src, "", tmp, ScanType.Delete);
if (overlay != null)
{
Console.WriteLine("Scanning for additional files");
// Add overlay files
ScanRecursive(overlay, null, dst, "", tmp, ScanType.Copy);
}
Console.WriteLine("Trimming list");
foreach (FileOperation op in tmp)
{
if (op.ActionNeeded())
{
// Console.WriteLine("Added " + op.ToString());
ret.Add(op);
}
}
return ret;
}
///
/// Scans a directory recursively building a list
///
/// Source directory
/// Source overlay, which contains extra source files
/// Destination directory
/// Should be ""
/// File list
/// Action type (copy or delete)
private static void ScanRecursive(string src, string overlay, string dst, string subpath, List list, ScanType type)
{
string path = Path.Combine(src, subpath);
foreach (string s in Directory.GetFiles(path))
{
string relpath = Path.Combine(subpath, Path.GetFileName(s));
//if (s.EndsWith("menu_viewer.xml"))
//{
// Console.WriteLine("xml");
//}
FileOperation op = null;
bool in_overlay = false;
if (overlay != null)
{
in_overlay = File.Exists(Path.Combine(overlay, relpath));
}
if (type == ScanType.Copy)
{
if (!in_overlay)
{
op = new FileCopy(src, dst, relpath);
}
} else {
if (!File.Exists(Path.Combine(dst, relpath)) && !in_overlay)
{
op = new FileDelete(src, relpath);
}
}
if (op != null)
{
//Console.WriteLine("Added: " + op.ToString());
list.Add(op);
}
}
foreach(string s in Directory.GetDirectories(path)) {
string tmp = Path.Combine(subpath, Path.GetFileName(s));
ScanRecursive(src,overlay, dst, tmp, list, type);
}
}
}
}