Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ApiCheck.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Reflection;
6 using System.Text;
7 
8 using Mono.Cecil;
9 
10 namespace SiliconStudio.Paradox.PublicApiCheck
11 {
12  /// <summary>
13  /// Helper class to check public API consistency between assemblies.
14  /// </summary>
15  public static class ApiCheck
16  {
17 
18  /// <summary>
19  /// Gets all the public API as string items from an assembly.
20  /// </summary>
21  /// <param name="assemblyRef">The assembly to get the items from.</param>
22  /// <returns>An enumeration of string with a fullname API.</returns>
23  public static IEnumerable<string> GetPublicApiItems(string assemblyRef)
24  {
25  var assembly = AssemblyDefinition.ReadAssembly(assemblyRef);
26  return assembly.MainModule.Types.SelectMany(GetPublicApiItems);
27  }
28 
29  /// <summary>
30  /// Gets all the public API as string items from a type.
31  /// </summary>
32  /// <param name="type">The type.</param>
33  /// <returns></returns>
34  public static IEnumerable<string> GetPublicApiItems(TypeDefinition type)
35  {
36  if (!type.IsPublic)
37  yield break;
38 
39  yield return type.FullName;
40 
41  foreach (var field in type.Fields.Where(field => (field.IsPublic || field.IsAssembly)))
42  {
43  yield return field.FullName;
44  }
45 
46  foreach (var property in type.Properties)
47  {
48  if (property.GetMethod != null && (property.GetMethod.IsPublic || property.GetMethod.IsAssembly))
49  yield return property.GetMethod.FullName;
50 
51  if (property.SetMethod != null && (property.SetMethod.IsPublic || property.SetMethod.IsAssembly))
52  yield return property.SetMethod.FullName;
53  }
54 
55  foreach (var method in type.Methods)
56  {
57  if (method.IsPublic || method.IsAssembly)
58  {
59  if (method.IsSetter || method.IsGetter)
60  continue;
61 
62  yield return method.FullName;
63  }
64  }
65  }
66 
67  /// <summary>
68  /// Performs a diff between the public API of two assemblies.
69  /// </summary>
70  /// <param name="from">The from assembly (reference).</param>
71  /// <param name="to">The to assembly (against).</param>
72  /// <returns>A list of public API defined in the [from] assembly but not present in the [to] assembly.</returns>
73  public static List<string> DiffAssembly(string from, string to)
74  {
75  var fromTypes = GetPublicApiItems(from);
76  var toTypes = GetPublicApiItems(to);
77 
78  return fromTypes.Except(toTypes).ToList();
79  }
80 
81  /// <summary>
82  /// Performs a diff between the public API of two assemblies.
83  /// </summary>
84  /// <param name="from">The from assembly (reference).</param>
85  /// <param name="to">The to assembly (against).</param>
86  /// <returns>null if API is the same, elase a string with a list of public API defined in the [from] assembly but not present in the [to] assembly.</returns>
87  public static string DiffAssemblyToString(string from, string to)
88  {
89  var diff = DiffAssembly(from, to);
90  if (diff.Count > 0)
91  {
92  var output = new StringBuilder();
93  output.AppendFormat("{0} public missing in {1}", diff.Count, Path.GetFileName(to));
94  output.AppendLine();
95  foreach (var diffItem in diff)
96  {
97  output.AppendLine(diffItem);
98  }
99  return output.ToString();
100  }
101  return null;
102  }
103 
104  static void Main(string[] args)
105  {
106  if (args.Length != 2)
107  {
108  Console.WriteLine("{0} assemblyRef assemblyAgainst", Path.GetFileName(Assembly.GetEntryAssembly().Location));
109  Environment.Exit(-1);
110  }
111 
112  var diff = DiffAssemblyToString(args[0], args[1]);
113  if (diff != null)
114  {
115  Console.WriteLine(diff);
116  Environment.Exit(-1);
117  }
118  }
119  }
120 }
static string DiffAssemblyToString(string from, string to)
Performs a diff between the public API of two assemblies.
Definition: ApiCheck.cs:87
static List< string > DiffAssembly(string from, string to)
Performs a diff between the public API of two assemblies.
Definition: ApiCheck.cs:73
static IEnumerable< string > GetPublicApiItems(string assemblyRef)
Gets all the public API as string items from an assembly.
Definition: ApiCheck.cs:23
static IEnumerable< string > GetPublicApiItems(TypeDefinition type)
Gets all the public API as string items from a type.
Definition: ApiCheck.cs:34
Helper class to check public API consistency between assemblies.
Definition: ApiCheck.cs:15