2 // The main differences with mono-api-diff are:
3 // * this tool directly produce HTML similar to gdiff.sh used for Xamarin.iOS
4 // * this tool reports changes in an "evolutionary" way, not in a breaking way,
5 // i.e. it does not assume the source assembly is right (but simply older)
6 // * the diff .xml output was not easy to convert back into the HTML format
7 // that gdiff.sh produced
10 // Sebastien Pouliot <sebastien@xamarin.com>
12 // Copyright 2013-2014 Xamarin Inc. http://www.xamarin.com
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections.Generic;
37 using System.Text.RegularExpressions;
41 namespace Xamarin.ApiDiff {
43 public static class State {
44 static TextWriter output;
46 public static TextWriter Output {
52 set { output = value; }
55 public static string Assembly { get; set; }
56 public static string Namespace { get; set; }
57 public static string Type { get; set; }
58 public static string BaseType { get; set; }
60 public static int Indent { get; set; }
62 static List<Regex> ignoreAdded = new List<Regex> ();
63 public static List<Regex> IgnoreAdded {
64 get { return ignoreAdded; }
67 static List<Regex> ignoreNew = new List<Regex> ();
68 public static List<Regex> IgnoreNew {
69 get { return ignoreNew; }
72 static List<Regex> ignoreRemoved = new List<Regex> ();
73 public static List<Regex> IgnoreRemoved {
74 get { return ignoreRemoved; }
77 public static bool IgnoreParameterNameChanges { get; set; }
78 public static bool IgnoreVirtualChanges { get; set; }
79 public static bool IgnoreAddedPropertySetters { get; set; }
81 public static bool Lax;
86 public static int Main (string[] args)
90 List<string> extra = null;
92 var options = new OptionSet {
93 { "h|help", "Show this help", v => showHelp = true },
94 { "d|diff=", "HTML diff file out output (omit for stdout)", v => diff = v },
95 { "i|ignore=", "Ignore new, added, and removed members whose description matches a given C# regular expression (see below).",
97 var r = new Regex (v);
98 State.IgnoreAdded.Add (r);
99 State.IgnoreRemoved.Add (r);
100 State.IgnoreNew.Add (r);
103 { "a|ignore-added=", "Ignore added members whose description matches a given C# regular expression (see below).",
104 v => State.IgnoreAdded.Add (new Regex (v))
106 { "r|ignore-removed=", "Ignore removed members whose description matches a given C# regular expression (see below).",
107 v => State.IgnoreRemoved.Add (new Regex (v))
109 { "n|ignore-new=", "Ignore new namespaces and types whose description matches a given C# regular expression (see below).",
110 v => State.IgnoreNew.Add (new Regex (v))
112 { "ignore-changes-parameter-names", "Ignore changes to parameter names for identically prototyped methods.",
113 v => State.IgnoreParameterNameChanges = v != null
115 { "ignore-changes-property-setters", "Ignore adding setters to properties.",
116 v => State.IgnoreAddedPropertySetters = v != null
118 { "ignore-changes-virtual", "Ignore changing non-`virtual` to `virtual` or adding `override`.",
119 v => State.IgnoreVirtualChanges = v != null
121 { "x|lax", "Ignore duplicate XML entries", v => State.Lax = true }
125 extra = options.Parse (args);
126 } catch (OptionException e) {
127 Console.WriteLine ("Option error: {0}", e.Message);
131 if (showHelp || extra == null || extra.Count < 2 || extra.Count > 3) {
132 Console.WriteLine (@"Usage: mono-api-html [options] <reference.xml> <assembly.xml> [diff.html]");
133 Console.WriteLine ();
134 Console.WriteLine ("Available options:");
135 options.WriteOptionDescriptions (Console.Out);
136 Console.WriteLine ();
137 Console.WriteLine ("Ignoring Members:");
138 Console.WriteLine ();
139 Console.WriteLine (" Members that were added can be filtered out of the diff by using the");
140 Console.WriteLine (" -i, --ignore-added option. The option takes a C# regular expression");
141 Console.WriteLine (" to match against member descriptions. For example, to ignore the");
142 Console.WriteLine (" introduction of the interfaces 'INSCopying' and 'INSCoding' on types");
143 Console.WriteLine (" pass the following to mono-api-html:");
144 Console.WriteLine ();
145 Console.WriteLine (" mono-api-html ... -i 'INSCopying$' -i 'INSCoding$'");
146 Console.WriteLine ();
147 Console.WriteLine (" The regular expressions will match any member description ending with");
148 Console.WriteLine (" 'INSCopying' or 'INSCoding'.");
149 Console.WriteLine ();
153 var input = extra [0];
154 var output = extra [1];
155 if (extra.Count == 3 && diff == null)
159 var ac = new AssemblyComparer (input, output);
161 string diffHtml = String.Empty;
162 using (var writer = new StringWriter ()) {
163 State.Output = writer;
165 diffHtml = State.Output.ToString ();
167 if (diffHtml.Length > 0) {
168 using (var file = new StreamWriter (diff)) {
169 if (ac.SourceAssembly == ac.TargetAssembly) {
170 file.WriteLine ("<h1>{0}.dll</h1>", ac.SourceAssembly);
172 file.WriteLine ("<h1>{0}.dll vs {1}.dll</h1>", ac.SourceAssembly, ac.TargetAssembly);
174 file.Write (diffHtml);
178 State.Output = Console.Out;
182 catch (Exception e) {
183 Console.WriteLine (e);