|
1 |
| -using System; |
2 |
| -using System.Collections.Generic; |
3 |
| -using System.Xml.Linq; |
4 |
| - |
5 |
| -namespace BinaryCompatChecker |
6 |
| -{ |
7 |
| - public class AppConfigFile |
8 |
| - { |
9 |
| - private List<string> errors = new List<string>(); |
10 |
| - public IEnumerable<string> Errors => errors; |
11 |
| - |
12 |
| - private List<BindingRedirect> bindingRedirects = new List<BindingRedirect>(); |
13 |
| - public IEnumerable<BindingRedirect> BindingRedirects => bindingRedirects; |
14 |
| - |
15 |
| - private string filePath; |
16 |
| - |
17 |
| - private AppConfigFile(string filePath) |
18 |
| - { |
19 |
| - this.filePath = filePath; |
20 |
| - } |
21 |
| - |
22 |
| - public class BindingRedirect |
23 |
| - { |
24 |
| - public string Name { get; set; } |
25 |
| - public string Culture { get; set; } |
26 |
| - public string PublicKeyToken { get; set; } |
27 |
| - public Version OldVersionRangeStart { get; set; } |
28 |
| - public Version OldVersionRangeEnd { get; set; } |
29 |
| - public Version NewVersion { get; set; } |
30 |
| - |
31 |
| - public override string ToString() |
32 |
| - { |
33 |
| - return $"Name={Name} Culture={Culture} PublicKeyToken={PublicKeyToken} OldVersion={OldVersionRangeStart}-{OldVersionRangeEnd} NewVersion={NewVersion}"; |
34 |
| - } |
35 |
| - } |
36 |
| - |
37 |
| - public static AppConfigFile Read(string filePath) |
38 |
| - { |
39 |
| - var appConfigFile = new AppConfigFile(filePath); |
40 |
| - try |
41 |
| - { |
42 |
| - appConfigFile.Parse(filePath); |
43 |
| - } |
44 |
| - catch (Exception ex) |
45 |
| - { |
46 |
| - appConfigFile.errors.Add(ex.Message); |
47 |
| - } |
48 |
| - |
49 |
| - return appConfigFile; |
50 |
| - } |
51 |
| - |
52 |
| - private void Parse(string appConfigFilePath) |
53 |
| - { |
54 |
| - void Error(string text) => errors.Add(text); |
55 |
| - XName Xmlns(string shortName) => XName.Get(shortName, "urn:schemas-microsoft-com:asm.v1"); |
56 |
| - |
57 |
| - var document = XDocument.Load(appConfigFilePath); |
58 |
| - var configuration = document.Root; |
59 |
| - var runtime = configuration.Element("runtime"); |
60 |
| - if (runtime == null) |
61 |
| - { |
62 |
| - Error($"Element 'runtime' not found"); |
63 |
| - return; |
64 |
| - } |
65 |
| - |
66 |
| - var assemblyBinding = runtime.Element(Xmlns("assemblyBinding")); |
67 |
| - if (assemblyBinding == null) |
68 |
| - { |
69 |
| - Error($"Element 'assemblyBinding' not found"); |
70 |
| - return; |
71 |
| - } |
72 |
| - |
73 |
| - var dependentAssemblyElements = assemblyBinding.Elements(Xmlns("dependentAssembly")); |
74 |
| - foreach (var dependentAssembly in dependentAssemblyElements) |
75 |
| - { |
76 |
| - var assemblyIdentity = dependentAssembly.Element(Xmlns("assemblyIdentity")); |
77 |
| - if (assemblyIdentity == null) |
78 |
| - { |
79 |
| - Error($"One of dependentAssembly elements doesn't have an assemblyIdentity subelement"); |
80 |
| - continue; |
81 |
| - } |
82 |
| - |
83 |
| - var name = GetAttributeValue(assemblyIdentity, "name"); |
84 |
| - if (name == null) |
85 |
| - { |
86 |
| - Error($"assemblyIdentity is missing the 'name' attribute"); |
87 |
| - continue; |
88 |
| - } |
89 |
| - |
90 |
| - var culture = GetAttributeValue(assemblyIdentity, "culture"); |
91 |
| - |
92 |
| - var publicKeyToken = GetAttributeValue(assemblyIdentity, "publicKeyToken"); |
93 |
| - if (publicKeyToken == null) |
94 |
| - { |
95 |
| - Error($"assemblyIdentity {name} is missing the 'publicKeyToken' attribute"); |
96 |
| - continue; |
97 |
| - } |
98 |
| - |
99 |
| - var bindingRedirect = dependentAssembly.Element(Xmlns("bindingRedirect")); |
100 |
| - if (bindingRedirect == null) |
101 |
| - { |
102 |
| - Error($"dependentAssembly for {name} doesn't have a bindingRedirect subelement"); |
103 |
| - continue; |
104 |
| - } |
105 |
| - |
106 |
| - var oldVersionString = GetAttributeValue(bindingRedirect, "oldVersion"); |
107 |
| - if (oldVersionString == null) |
108 |
| - { |
109 |
| - Error($"bindingRedirect for {name} is missing the 'oldVersion' attribute"); |
110 |
| - continue; |
111 |
| - } |
112 |
| - |
113 |
| - var newVersionString = GetAttributeValue(bindingRedirect, "newVersion"); |
114 |
| - if (newVersionString == null) |
115 |
| - { |
116 |
| - Error($"bindingRedirect for {name} is missing the 'newVersion' attribute"); |
117 |
| - continue; |
118 |
| - } |
119 |
| - |
120 |
| - Tuple<string, string> range = ParseVersionRange(oldVersionString); |
121 |
| - if (range == null) |
122 |
| - { |
123 |
| - Error($"oldVersion range for {name} is in incorrect format"); |
124 |
| - continue; |
125 |
| - } |
126 |
| - |
127 |
| - if (!Version.TryParse(range.Item1, out var oldVersionStart)) |
128 |
| - { |
129 |
| - Error($"Can't parse old start version: {range.Item1}"); |
130 |
| - continue; |
131 |
| - } |
132 |
| - |
133 |
| - if (!Version.TryParse(range.Item2, out var oldVersionEnd)) |
134 |
| - { |
135 |
| - Error($"Can't parse old end version: {range.Item2}"); |
136 |
| - continue; |
137 |
| - } |
138 |
| - |
139 |
| - if (!Version.TryParse(newVersionString, out var newVersion)) |
140 |
| - { |
141 |
| - Error($"Can't parse newVersion: {newVersion}"); |
142 |
| - continue; |
143 |
| - } |
144 |
| - |
145 |
| - var bindingRedirectResult = new BindingRedirect |
146 |
| - { |
147 |
| - Name = name, |
148 |
| - Culture = culture, |
149 |
| - PublicKeyToken = publicKeyToken, |
150 |
| - OldVersionRangeStart = oldVersionStart, |
151 |
| - OldVersionRangeEnd = oldVersionEnd, |
152 |
| - NewVersion = newVersion |
153 |
| - }; |
154 |
| - |
155 |
| - bindingRedirects.Add(bindingRedirectResult); |
156 |
| - } |
157 |
| - } |
158 |
| - |
159 |
| - private Tuple<string, string> ParseVersionRange(string versionRange) |
160 |
| - { |
161 |
| - int dash = versionRange.IndexOf('-'); |
162 |
| - if (dash <= 0 || dash == versionRange.Length - 1) |
163 |
| - { |
164 |
| - return null; |
165 |
| - } |
166 |
| - |
167 |
| - string first = versionRange.Substring(0, dash); |
168 |
| - string second = versionRange.Substring(dash + 1, versionRange.Length - dash - 1); |
169 |
| - return Tuple.Create(first, second); |
170 |
| - } |
171 |
| - |
172 |
| - private string GetAttributeValue(XElement element, string attributeName) |
173 |
| - { |
174 |
| - return element.Attribute(attributeName)?.Value; |
175 |
| - } |
176 |
| - } |
177 |
| -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Xml.Linq; |
| 4 | + |
| 5 | +namespace BinaryCompatChecker |
| 6 | +{ |
| 7 | + public class AppConfigFile |
| 8 | + { |
| 9 | + private List<string> errors = new List<string>(); |
| 10 | + public IEnumerable<string> Errors => errors; |
| 11 | + |
| 12 | + private List<BindingRedirect> bindingRedirects = new List<BindingRedirect>(); |
| 13 | + public IEnumerable<BindingRedirect> BindingRedirects => bindingRedirects; |
| 14 | + |
| 15 | + private string filePath; |
| 16 | + |
| 17 | + private AppConfigFile(string filePath) |
| 18 | + { |
| 19 | + this.filePath = filePath; |
| 20 | + } |
| 21 | + |
| 22 | + public class BindingRedirect |
| 23 | + { |
| 24 | + public string Name { get; set; } |
| 25 | + public string Culture { get; set; } |
| 26 | + public string PublicKeyToken { get; set; } |
| 27 | + public Version OldVersionRangeStart { get; set; } |
| 28 | + public Version OldVersionRangeEnd { get; set; } |
| 29 | + public Version NewVersion { get; set; } |
| 30 | + |
| 31 | + public override string ToString() |
| 32 | + { |
| 33 | + return $"Name={Name} Culture={Culture} PublicKeyToken={PublicKeyToken} OldVersion={OldVersionRangeStart}-{OldVersionRangeEnd} NewVersion={NewVersion}"; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public static AppConfigFile Read(string filePath) |
| 38 | + { |
| 39 | + var appConfigFile = new AppConfigFile(filePath); |
| 40 | + try |
| 41 | + { |
| 42 | + appConfigFile.Parse(filePath); |
| 43 | + } |
| 44 | + catch (Exception ex) |
| 45 | + { |
| 46 | + appConfigFile.errors.Add(ex.Message); |
| 47 | + } |
| 48 | + |
| 49 | + return appConfigFile; |
| 50 | + } |
| 51 | + |
| 52 | + private void Parse(string appConfigFilePath) |
| 53 | + { |
| 54 | + void Error(string text) => errors.Add(text); |
| 55 | + XName Xmlns(string shortName) => XName.Get(shortName, "urn:schemas-microsoft-com:asm.v1"); |
| 56 | + |
| 57 | + var document = XDocument.Load(appConfigFilePath); |
| 58 | + var configuration = document.Root; |
| 59 | + var runtime = configuration.Element("runtime"); |
| 60 | + if (runtime == null) |
| 61 | + { |
| 62 | + Error($"Element 'runtime' not found"); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + var assemblyBinding = runtime.Element(Xmlns("assemblyBinding")); |
| 67 | + if (assemblyBinding == null) |
| 68 | + { |
| 69 | + Error($"Element 'assemblyBinding' not found"); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + var dependentAssemblyElements = assemblyBinding.Elements(Xmlns("dependentAssembly")); |
| 74 | + foreach (var dependentAssembly in dependentAssemblyElements) |
| 75 | + { |
| 76 | + var assemblyIdentity = dependentAssembly.Element(Xmlns("assemblyIdentity")); |
| 77 | + if (assemblyIdentity == null) |
| 78 | + { |
| 79 | + Error($"One of dependentAssembly elements doesn't have an assemblyIdentity subelement"); |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + var name = GetAttributeValue(assemblyIdentity, "name"); |
| 84 | + if (name == null) |
| 85 | + { |
| 86 | + Error($"assemblyIdentity is missing the 'name' attribute"); |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + var culture = GetAttributeValue(assemblyIdentity, "culture"); |
| 91 | + |
| 92 | + var publicKeyToken = GetAttributeValue(assemblyIdentity, "publicKeyToken"); |
| 93 | + if (publicKeyToken == null) |
| 94 | + { |
| 95 | + Error($"assemblyIdentity {name} is missing the 'publicKeyToken' attribute"); |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + var bindingRedirect = dependentAssembly.Element(Xmlns("bindingRedirect")); |
| 100 | + if (bindingRedirect == null) |
| 101 | + { |
| 102 | + Error($"dependentAssembly for {name} doesn't have a bindingRedirect subelement"); |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + var oldVersionString = GetAttributeValue(bindingRedirect, "oldVersion"); |
| 107 | + if (oldVersionString == null) |
| 108 | + { |
| 109 | + Error($"bindingRedirect for {name} is missing the 'oldVersion' attribute"); |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + var newVersionString = GetAttributeValue(bindingRedirect, "newVersion"); |
| 114 | + if (newVersionString == null) |
| 115 | + { |
| 116 | + Error($"bindingRedirect for {name} is missing the 'newVersion' attribute"); |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + Tuple<string, string> range = ParseVersionRange(oldVersionString); |
| 121 | + if (range == null) |
| 122 | + { |
| 123 | + Error($"oldVersion range for {name} is in incorrect format"); |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + if (!Version.TryParse(range.Item1, out var oldVersionStart)) |
| 128 | + { |
| 129 | + Error($"Can't parse old start version: {range.Item1}"); |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + if (!Version.TryParse(range.Item2, out var oldVersionEnd)) |
| 134 | + { |
| 135 | + Error($"Can't parse old end version: {range.Item2}"); |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + if (!Version.TryParse(newVersionString, out var newVersion)) |
| 140 | + { |
| 141 | + Error($"Can't parse newVersion: {newVersion}"); |
| 142 | + continue; |
| 143 | + } |
| 144 | + |
| 145 | + var bindingRedirectResult = new BindingRedirect |
| 146 | + { |
| 147 | + Name = name, |
| 148 | + Culture = culture, |
| 149 | + PublicKeyToken = publicKeyToken, |
| 150 | + OldVersionRangeStart = oldVersionStart, |
| 151 | + OldVersionRangeEnd = oldVersionEnd, |
| 152 | + NewVersion = newVersion |
| 153 | + }; |
| 154 | + |
| 155 | + bindingRedirects.Add(bindingRedirectResult); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private Tuple<string, string> ParseVersionRange(string versionRange) |
| 160 | + { |
| 161 | + int dash = versionRange.IndexOf('-'); |
| 162 | + if (dash <= 0 || dash == versionRange.Length - 1) |
| 163 | + { |
| 164 | + return null; |
| 165 | + } |
| 166 | + |
| 167 | + string first = versionRange.Substring(0, dash); |
| 168 | + string second = versionRange.Substring(dash + 1, versionRange.Length - dash - 1); |
| 169 | + return Tuple.Create(first, second); |
| 170 | + } |
| 171 | + |
| 172 | + private string GetAttributeValue(XElement element, string attributeName) |
| 173 | + { |
| 174 | + return element.Attribute(attributeName)?.Value; |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments