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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
//
// The main differences with mono-api-diff are:
// * this tool directly produce HTML similar to gdiff.sh used for Xamarin.iOS
// * this tool reports changes in an "evolutionary" way, not in a breaking way,
// i.e. it does not assume the source assembly is right (but simply older)
// * the diff .xml output was not easy to convert back into the HTML format
// that gdiff.sh produced
//
// Authors
// Sebastien Pouliot <sebastien@xamarin.com>
//
// Copyright 2013-2014 Xamarin Inc. http://www.xamarin.com
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Mono.Options;
namespace Xamarin.ApiDiff {
public static class State {
static TextWriter output;
public static TextWriter Output {
get {
if (output == null)
output = Console.Out;
return output;
}
set { output = value; }
}
public static string Assembly { get; set; }
public static string Namespace { get; set; }
public static string Type { get; set; }
public static string BaseType { get; set; }
public static int Indent { get; set; }
static List<Regex> ignoreAdded = new List<Regex> ();
public static List<Regex> IgnoreAdded {
get { return ignoreAdded; }
}
static List<Regex> ignoreNew = new List<Regex> ();
public static List<Regex> IgnoreNew {
get { return ignoreNew; }
}
static List<Regex> ignoreRemoved = new List<Regex> ();
public static List<Regex> IgnoreRemoved {
get { return ignoreRemoved; }
}
public static bool IgnoreParameterNameChanges { get; set; }
public static bool IgnoreVirtualChanges { get; set; }
public static bool IgnoreAddedPropertySetters { get; set; }
public static bool Lax;
public static bool Colorize = true;
}
class Program {
public static int Main (string[] args)
{
var showHelp = false;
string diff = null;
List<string> extra = null;
var options = new OptionSet {
{ "h|help", "Show this help", v => showHelp = true },
{ "d|diff=", "HTML diff file out output (omit for stdout)", v => diff = v },
{ "i|ignore=", "Ignore new, added, and removed members whose description matches a given C# regular expression (see below).",
v => {
var r = new Regex (v);
State.IgnoreAdded.Add (r);
State.IgnoreRemoved.Add (r);
State.IgnoreNew.Add (r);
}
},
{ "a|ignore-added=", "Ignore added members whose description matches a given C# regular expression (see below).",
v => State.IgnoreAdded.Add (new Regex (v))
},
{ "r|ignore-removed=", "Ignore removed members whose description matches a given C# regular expression (see below).",
v => State.IgnoreRemoved.Add (new Regex (v))
},
{ "n|ignore-new=", "Ignore new namespaces and types whose description matches a given C# regular expression (see below).",
v => State.IgnoreNew.Add (new Regex (v))
},
{ "ignore-changes-parameter-names", "Ignore changes to parameter names for identically prototyped methods.",
v => State.IgnoreParameterNameChanges = v != null
},
{ "ignore-changes-property-setters", "Ignore adding setters to properties.",
v => State.IgnoreAddedPropertySetters = v != null
},
{ "ignore-changes-virtual", "Ignore changing non-`virtual` to `virtual` or adding `override`.",
v => State.IgnoreVirtualChanges = v != null
},
{ "c|colorize:", "Colorize HTML output", v => State.Colorize = string.IsNullOrEmpty (v) ? true : bool.Parse (v) },
{ "x|lax", "Ignore duplicate XML entries", v => State.Lax = true }
};
try {
extra = options.Parse (args);
} catch (OptionException e) {
Console.WriteLine ("Option error: {0}", e.Message);
showHelp = true;
}
if (showHelp || extra == null || extra.Count < 2 || extra.Count > 3) {
Console.WriteLine (@"Usage: mono-api-html [options] <reference.xml> <assembly.xml> [diff.html]");
Console.WriteLine ();
Console.WriteLine ("Available options:");
options.WriteOptionDescriptions (Console.Out);
Console.WriteLine ();
Console.WriteLine ("Ignoring Members:");
Console.WriteLine ();
Console.WriteLine (" Members that were added can be filtered out of the diff by using the");
Console.WriteLine (" -i, --ignore-added option. The option takes a C# regular expression");
Console.WriteLine (" to match against member descriptions. For example, to ignore the");
Console.WriteLine (" introduction of the interfaces 'INSCopying' and 'INSCoding' on types");
Console.WriteLine (" pass the following to mono-api-html:");
Console.WriteLine ();
Console.WriteLine (" mono-api-html ... -i 'INSCopying$' -i 'INSCoding$'");
Console.WriteLine ();
Console.WriteLine (" The regular expressions will match any member description ending with");
Console.WriteLine (" 'INSCopying' or 'INSCoding'.");
Console.WriteLine ();
return 1;
}
var input = extra [0];
var output = extra [1];
if (extra.Count == 3 && diff == null)
diff = extra [2];
try {
var ac = new AssemblyComparer (input, output);
if (diff != null) {
string diffHtml = String.Empty;
using (var writer = new StringWriter ()) {
State.Output = writer;
ac.Compare ();
diffHtml = State.Output.ToString ();
}
if (diffHtml.Length > 0) {
using (var file = new StreamWriter (diff)) {
file.WriteLine ("<div>");
if (State.Colorize) {
file.WriteLine ("<style scoped>");
file.WriteLine ("\t.obsolete { color: gray; }");
file.WriteLine ("\t.added { color: green; }");
file.WriteLine ("\t.removed-inline { text-decoration: line-through; }");
file.WriteLine ("\t.removed-breaking-inline { color: red;}");
file.WriteLine ("\t.added-breaking-inline { text-decoration: underline; }");
file.WriteLine ("\t.nonbreaking { color: black; }");
file.WriteLine ("\t.breaking { color: red; }");
file.WriteLine ("</style>");
}
file.WriteLine (
@"<script type=""text/javascript"">
// Only some elements have 'data-is-[non-]breaking' attributes. Here we
// iterate over all descendents elements, and set 'data-is-[non-]breaking'
// depending on whether there are any descendents with that attribute.
function propagateDataAttribute (element)
{
if (element.hasAttribute ('data-is-propagated'))
return;
var i;
var any_breaking = element.hasAttribute ('data-is-breaking');
var any_non_breaking = element.hasAttribute ('data-is-non-breaking');
for (i = 0; i < element.children.length; i++) {
var el = element.children [i];
propagateDataAttribute (el);
any_breaking |= el.hasAttribute ('data-is-breaking');
any_non_breaking |= el.hasAttribute ('data-is-non-breaking');
}
if (any_breaking)
element.setAttribute ('data-is-breaking', null);
else if (any_non_breaking)
element.setAttribute ('data-is-non-breaking', null);
element.setAttribute ('data-is-propagated', null);
}
function hideNonBreakingChanges ()
{
var topNodes = document.querySelectorAll ('[data-is-topmost]');
var n;
var i;
for (n = 0; n < topNodes.length; n++) {
propagateDataAttribute (topNodes [n]);
var elements = topNodes [n].querySelectorAll ('[data-is-non-breaking]');
for (i = 0; i < elements.length; i++) {
var el = elements [i];
if (!el.hasAttribute ('data-original-display'))
el.setAttribute ('data-original-display', el.style.display);
el.style.display = 'none';
}
}
var links = document.getElementsByClassName ('hide-nonbreaking');
for (i = 0; i < links.length; i++)
links [i].style.display = 'none';
links = document.getElementsByClassName ('restore-nonbreaking');
for (i = 0; i < links.length; i++)
links [i].style.display = '';
}
function showNonBreakingChanges ()
{
var elements = document.querySelectorAll ('[data-original-display]');
var i;
for (i = 0; i < elements.length; i++) {
var el = elements [i];
el.style.display = el.getAttribute ('data-original-display');
}
var links = document.getElementsByClassName ('hide-nonbreaking');
for (i = 0; i < links.length; i++)
links [i].style.display = '';
links = document.getElementsByClassName ('restore-nonbreaking');
for (i = 0; i < links.length; i++)
links [i].style.display = 'none';
}
</script>");
if (ac.SourceAssembly == ac.TargetAssembly) {
file.WriteLine ("<h1>{0}.dll</h1>", ac.SourceAssembly);
} else {
file.WriteLine ("<h1>{0}.dll vs {1}.dll</h1>", ac.SourceAssembly, ac.TargetAssembly);
}
file.WriteLine ("<a href='javascript: hideNonBreakingChanges (); ' class='hide-nonbreaking'>Hide non-breaking changes</a>");
file.WriteLine ("<a href='javascript: showNonBreakingChanges (); ' class='restore-nonbreaking' style='display: none;'>Show non-breaking changes</a>");
file.WriteLine ("<br/>");
file.WriteLine ("<div data-is-topmost>");
file.Write (diffHtml);
file.WriteLine ("</div> <!-- end topmost div -->");
file.WriteLine ("</div>");
}
}
} else {
State.Output = Console.Out;
ac.Compare ();
}
}
catch (Exception e) {
Console.WriteLine (e);
return 1;
}
return 0;
}
}
}
|