pp/4/Program.cs 2.3 KB (2309b); Sat, 04 Jun 2011 21:28
´╗┐using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace Cselian.Utils.Precompare
{
	class Program
	{
		private static DirectoryInfo Dir;

		static void Main(string[] args)
		{
			CheckDir();

			if (args == null || args.Length == 0) args = new string[] { "sample-old.vsmdi", "sample.vsmdi", "debug" };
			var file = args[0];
			var other = args[1];
			var extn = Path.GetExtension(file);
			if (extn == ".vsmdi")
			{
				if (args.Length == 3 && args[2] == "debug") Start(file, other);
				file = SortXml(file, "TestLinks", "name");
				other = SortXml(other, "TestLinks", "name");
			}
			Start(file, other);
		}

		private static void CheckDir()
		{
			Dir = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "tmp"));
			if (Dir.Exists == false) Dir.Create();
		}

		private static void Start(string file, string other)
		{
			var diff = System.Configuration.ConfigurationManager.AppSettings["ExternalDiffTool"];
			var args = string.Format("{0} {1}", file, other);
			var logFile = Path.Combine(Dir.FullName, "Log.txt");
			File.AppendAllText(logFile, System.DateTime.Now.ToString("dd MMM yy hh:mm:ss ") + args + System.Environment.NewLine);
			System.Diagnostics.Process.Start(diff, args);
		}

		private static string SortXml(string file, string nodeName, string sortAttributeName)
		{
			var xml = XDocument.Load(file);
			var sorted = new XDocument(xml);
			var orig = xml.Root.FirstNode as XContainer;
			var copy = sorted.Root.FirstNode as XContainer;

			while (orig != null)
			{
				var copyLinks = copy.Nodes().OfType<XElement>().FirstOrDefault(x => x.Name.LocalName == nodeName);
				if (copyLinks != null)
				{
					copyLinks.RemoveNodes();
					var links = orig.Nodes().OfType<XElement>().FirstOrDefault(x => x.Name.LocalName == nodeName);

					var linksToAdd = links.Nodes().OfType<XElement>().OrderBy(x => x.Attributes().First(y => y.Name.LocalName == sortAttributeName).Value).ToArray();

					foreach (var item in linksToAdd) copyLinks.Add(item);
				}
				orig = orig.NextNode as XContainer;
				copy = copy.NextNode as XContainer;
			}

			var output = Path.Combine(Dir.FullName, Path.GetFileName(file));

			sorted.Save(output);
			return output;
		}
	}
}