| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | using System; |
| 4 | using System.IO; |
| 5 | using System.Collections; |
| 6 | |
| 7 | namespace WixToolset.Dtf.Tools.DDiff |
| 8 | { |
| 9 | public class DirectoryDiffEngine : IDiffEngine |
| 10 | { |
| 11 | public DirectoryDiffEngine() |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | public virtual float GetDiffQuality(string diffInput1, string diffInput2, string[] options, IDiffEngineFactory diffFactory) |
| 16 | { |
| 17 | if(diffInput1 != null && Directory.Exists(diffInput1) && |
| 18 | diffInput2 != null && Directory.Exists(diffInput2)) |
| 19 | { |
| 20 | return .70f; |
| 21 | } |
| 22 | else |
| 23 | { |
| 24 | return 0; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | public bool GetDiff(string diffInput1, string diffInput2, string[] options, TextWriter diffOutput, string linePrefix, IDiffEngineFactory diffFactory) |
| 29 | { |
| 30 | bool difference = false; |
| 31 | IComparer caseInsComp = CaseInsensitiveComparer.Default; |
| 32 | |
| 33 | string[] files1 = Directory.GetFiles(diffInput1); |
| 34 | string[] files2 = Directory.GetFiles(diffInput2); |
| 35 | for(int i1 = 0; i1 < files1.Length; i1++) |
| 36 | { |
| 37 | files1[i1] = Path.GetFileName(files1[i1]); |
| 38 | } |
| 39 | for(int i2 = 0; i2 < files2.Length; i2++) |
| 40 | { |
| 41 | files2[i2] = Path.GetFileName(files2[i2]); |
| 42 | } |
| 43 | Array.Sort(files1, caseInsComp); |
| 44 | Array.Sort(files2, caseInsComp); |
| 45 | |
| 46 | for(int i1 = 0, i2 = 0; i1 < files1.Length || i2 < files2.Length; ) |
| 47 | { |
| 48 | int comp; |
| 49 | if(i1 == files1.Length) |
| 50 | { |
| 51 | comp = 1; |
| 52 | } |
| 53 | else if(i2 == files2.Length) |
| 54 | { |
| 55 | comp = -1; |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | comp = caseInsComp.Compare(files1[i1], files2[i2]); |
| 60 | } |
| 61 | if(comp < 0) |
| 62 | { |
| 63 | diffOutput.WriteLine("{0}< {1}", linePrefix, files1[i1]); |
| 64 | i1++; |
| 65 | difference = true; |
| 66 | } |
| 67 | else if(comp > 0) |
| 68 | { |
| 69 | diffOutput.WriteLine("{0}> {1}", linePrefix, files2[i2]); |
| 70 | i2++; |
| 71 | difference = true; |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | string file1 = Path.Combine(diffInput1, files1[i1]); |
| 76 | string file2 = Path.Combine(diffInput2, files2[i2]); |
| 77 | IDiffEngine diffEngine = diffFactory.GetDiffEngine(file1, file2, options); |
| 78 | StringWriter sw = new StringWriter(); |
| 79 | if(diffEngine.GetDiff(file1, file2, options, sw, linePrefix + " ", diffFactory)) |
| 80 | { |
| 81 | diffOutput.WriteLine("{0}{1}", linePrefix, files1[i1]); |
| 82 | diffOutput.Write(sw.ToString()); |
| 83 | difference = true; |
| 84 | } |
| 85 | i1++; |
| 86 | i2++; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | string[] dirs1 = Directory.GetDirectories(diffInput1); |
| 91 | string[] dirs2 = Directory.GetDirectories(diffInput2); |
| 92 | for(int i1 = 0; i1 < dirs1.Length; i1++) |
| 93 | { |
| 94 | dirs1[i1] = Path.GetFileName(dirs1[i1]); |
| 95 | } |
| 96 | for(int i2 = 0; i2 < dirs2.Length; i2++) |
| 97 | { |
| 98 | dirs2[i2] = Path.GetFileName(dirs2[i2]); |
| 99 | } |
| 100 | Array.Sort(dirs1, caseInsComp); |
| 101 | Array.Sort(dirs2, caseInsComp); |
| 102 | |
| 103 | for(int i1 = 0, i2 = 0; i1 < dirs1.Length || i2 < dirs2.Length; ) |
| 104 | { |
| 105 | int comp; |
| 106 | if(i1 == dirs1.Length) |
| 107 | { |
| 108 | comp = 1; |
| 109 | } |
| 110 | else if(i2 == dirs2.Length) |
| 111 | { |
| 112 | comp = -1; |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | comp = caseInsComp.Compare(dirs1[i1], dirs2[i2]); |
| 117 | } |
| 118 | if(comp < 0) |
| 119 | { |
| 120 | diffOutput.WriteLine("{0}< {1}", linePrefix, dirs1[i1]); |
| 121 | i1++; |
| 122 | difference = true; |
| 123 | } |
| 124 | else if(comp > 0) |
| 125 | { |
| 126 | diffOutput.WriteLine("{0}> {1}", linePrefix, dirs2[i2]); |
| 127 | i2++; |
| 128 | difference = true; |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | string dir1 = Path.Combine(diffInput1, dirs1[i1]); |
| 133 | string dir2 = Path.Combine(diffInput2, dirs2[i2]); |
| 134 | IDiffEngine diffEngine = diffFactory.GetDiffEngine(dir1, dir2, options); |
| 135 | StringWriter sw = new StringWriter(); |
| 136 | if(diffEngine.GetDiff(dir1, dir2, options, sw, linePrefix + " ", diffFactory)) |
| 137 | { |
| 138 | diffOutput.WriteLine("{0}{1}\\", linePrefix, dirs1[i1]); |
| 139 | diffOutput.Write(sw.ToString()); |
| 140 | difference = true; |
| 141 | } |
| 142 | i1++; |
| 143 | i2++; |
| 144 | } |
| 145 | } |
| 146 | return difference; |
| 147 | } |
| 148 | |
| 149 | public virtual IDiffEngine Clone() |
| 150 | { |
| 151 | return new DirectoryDiffEngine(); |
| 152 | } |
| 153 | } |
| 154 | } |