-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
173 lines (161 loc) · 5.75 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using Microsoft.DocAsCode.Glob;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace MarkdownVariation
{
internal static class Program
{
private static void Main(string[] args)
{
if (args.Length != 2)
{
PrintUsage();
}
FindVariation(Path.GetFullPath(args[0]), args[1]);
}
private static void PrintUsage()
{
Console.WriteLine("Usage:");
Console.WriteLine($" {nameof(MarkdownVariation)} <cwd> <glob>");
}
private static void FindVariation(string folder, string glob)
{
var files = FileGlob.GetFiles(folder, new[] { glob }, new string[0]);
var oldProxy = GetMarkProxy(CreateDomain("old", "old.config"));
var newDomain = CreateDomain("new", "new/new.config");
var newProxy = GetMarkProxy(newDomain);
foreach (var file in files)
{
var markdown = File.ReadAllText(file);
var oldTreeJson = oldProxy.Parse(markdown);
var newTreeJson = newProxy.Parse(markdown);
Compare(file, oldTreeJson, newTreeJson);
}
}
private static void PrintAssemblies()
{
foreach (var ass in AppDomain.CurrentDomain.GetAssemblies())
{
Console.WriteLine(ass.GetName().FullName);
}
}
private static void Compare(string file, string oldTreeJson, string newTreeJson)
{
if (oldTreeJson == newTreeJson)
{
Console.WriteLine($"File {file} has no change.");
}
var oldObject = Parse(oldTreeJson);
var newObject = Parse(newTreeJson);
try
{
Compare(oldObject, newObject);
}
catch (NotSameValueException ex)
{
Console.WriteLine($"File {file} has different: {ex.Message}:");
Console.WriteLine($"\tOld: {oldObject}");
Console.WriteLine($"\tNew: {newObject}");
}
catch (NotSameException ex)
{
Console.WriteLine($"File {file} has different: {ex.Message}:");
Console.WriteLine($"\tOld: {ex.OldToken}");
Console.WriteLine($"\tNew: {ex.NewToken}");
}
}
private static JObject Parse(string json)
{
try
{
var fixedJson = Regex.Replace(json, @"(?<!\\)\\(?!(r|n|t|\|""|'|\\))", @"\\");
return JObject.Parse(fixedJson);
}
catch (Exception)
{
return new JObject
{
["error"] = "bad json."
};
}
}
private static void Compare(JToken oldToken, JToken newToken)
{
if (oldToken is JArray && newToken is JArray)
{
Compare((JArray)oldToken, (JArray)newToken);
}
else if (oldToken is JObject && newToken is JObject)
{
Compare((JObject)oldToken, (JObject)newToken);
}
else if (oldToken.Type == JTokenType.String && newToken.Type == JTokenType.String)
{
Compare(oldToken.ToString(), newToken.ToString());
}
else
{
throw new NotSameValueException();
}
}
private static void Compare(JArray oldArray, JArray newArray)
{
for (int i = 0; i < oldArray.Count; i++)
{
if (newArray.Count <= i)
{
throw new NotSameException("More tokens in old version.", oldArray[i], null);
}
if (!JToken.EqualityComparer.Equals(oldArray[i], newArray[i]))
{
try
{
Compare(oldArray[i], newArray[i]);
}
catch (NotSameValueException)
{
throw new NotSameException("Token is different.", oldArray[i], newArray[i]);
}
}
}
if (newArray.Count > oldArray.Count)
{
throw new NotSameException("More tokens in new version.", null, newArray[oldArray.Count]);
}
}
private static void Compare(JObject oldObject, JObject newObject)
{
var oldKeySet = ((IDictionary<string, JToken>)oldObject).Keys;
var newKeySet = ((IDictionary<string, JToken>)newObject).Keys;
foreach (var key in oldKeySet.Intersect(newKeySet))
{
if (!JToken.EqualityComparer.Equals(oldObject[key], newObject[key]))
{
Compare(oldObject[key], newObject[key]);
}
}
}
private static void Compare(string oldText, string newText)
{
if (oldText.TrimEnd('\n') != newText.TrimEnd('\n'))
{
throw new NotSameValueException();
}
}
private static AppDomain CreateDomain(string folder, string configFile)
{
var si = AppDomain.CurrentDomain.SetupInformation;
si.PrivateBinPath = folder;
si.ConfigurationFile = configFile;
return AppDomain.CreateDomain(folder, null, si);
}
private static MarkProxy GetMarkProxy(AppDomain domain)
{
return (MarkProxy)domain.CreateInstanceAndUnwrap(nameof(MarkdownVariation), typeof(MarkProxy).FullName);
}
}
}