-
-
Notifications
You must be signed in to change notification settings - Fork 740
/
Copy pathMethodAliasGenerator.cs
186 lines (159 loc) · 6.62 KB
/
MethodAliasGenerator.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
174
175
176
177
178
179
180
181
182
183
184
185
186
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using Cake.Core.Annotations;
namespace Cake.Core.Scripting.CodeGen
{
/// <summary>
/// Responsible for generating script method aliases.
/// </summary>
public static class MethodAliasGenerator
{
private static readonly System.Security.Cryptography.SHA256 _hasher = System.Security.Cryptography.SHA256.Create();
/// <summary>
/// Generates a script method alias from the specified method.
/// The provided method must be an extension method for <see cref="ICakeContext"/>
/// and it must be decorated with the <see cref="CakeMethodAliasAttribute"/>.
/// </summary>
/// <param name="method">The method to generate the code for.</param>
/// <returns>The generated code.</returns>
public static string Generate(MethodInfo method) => Generate(method, out _);
/// <summary>
/// Generates a script method alias from the specified method.
/// The provided method must be an extension method for <see cref="ICakeContext"/>
/// and it must be decorated with the <see cref="CakeMethodAliasAttribute"/>.
/// </summary>
/// <param name="method">The method to generate the code for.</param>
/// <param name="hash">The hash of method signature.</param>
/// <returns>The generated code.</returns>
public static string Generate(MethodInfo method, out string hash)
{
if (method == null)
{
throw new ArgumentNullException(nameof(method));
}
var builder = new StringBuilder();
var parameters = method.GetParameters()[1..];
// Method is obsolete?
var obsolete = method.GetCustomAttribute<ObsoleteAttribute>();
var isObsolete = obsolete != null;
// Generate method signature.
builder.AppendLine("[System.Diagnostics.DebuggerStepThrough]");
builder.Append("public ");
builder.Append(GetReturnType(method));
builder.Append(' ');
builder.Append(method.Name);
if (method.IsGenericMethod)
{
// Add generic arguments to proxy signature.
BuildGenericArguments(method, builder);
}
builder.Append('(');
builder.Append(string.Concat(GetProxyParameters(parameters, true)));
builder.Append(')');
// if the method is generic, emit any constraints that might exist.
if (method.IsGenericMethod)
{
GenericParameterConstraintEmitter.BuildGenericConstraints(method, builder);
}
hash = Convert.ToHexString(
_hasher.ComputeHash(Encoding.UTF8.GetBytes(builder.ToString())));
builder.AppendLine();
if (isObsolete && !obsolete.IsError)
{
builder.AppendLine("#pragma warning disable 0618");
}
builder.Append(" => ");
if (isObsolete)
{
var message = GetObsoleteMessage(method, obsolete);
if (obsolete.IsError)
{
// Throw an exception.
var exception = string.Format(
CultureInfo.InvariantCulture,
"throw new Cake.Core.CakeException(\"{0}\");", message);
builder.AppendLine(exception);
// End method.
builder.AppendLine();
return builder.ToString();
}
}
// Call extension method.
builder.Append(method.GetFullName());
if (method.IsGenericMethod)
{
// Add generic arguments to method call.
BuildGenericArguments(method, builder);
}
builder.Append('(');
builder.Append(string.Concat(GetProxyParameters(parameters, false)));
builder.AppendLine(");");
if (isObsolete)
{
builder.AppendLine("#pragma warning restore 0618");
}
// End method.
builder.AppendLine();
return builder.ToString();
}
private static string GetReturnType(MethodInfo method)
{
if (method.ReturnType == typeof(void))
{
return "void";
}
var isDynamic = method.ReturnTypeCustomAttributes.GetCustomAttributes(typeof(DynamicAttribute), true).Any();
var isNullable = method.ReturnTypeCustomAttributes.GetCustomAttributes(true).Any(attr => attr.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute");
return string.Concat(
isDynamic ? "dynamic" : method.ReturnType.GetFullName(),
isNullable ? "?" : string.Empty);
}
private static IEnumerable<string> GetProxyParameters(IEnumerable<ParameterInfo> parameters, bool includeType)
{
var first = includeType;
if (!includeType)
{
yield return "Context";
}
foreach (var parameter in parameters)
{
if (first)
{
first = false;
}
else
{
yield return ", ";
}
yield return ParameterEmitter.Emit(parameter, includeType);
}
}
private static void BuildGenericArguments(MethodInfo method, StringBuilder builder)
{
builder.Append('<');
var genericArguments = new List<string>();
foreach (var argument in method.GetGenericArguments())
{
genericArguments.Add(argument.Name);
}
builder.Append(string.Join(", ", genericArguments));
builder.Append('>');
}
private static string GetObsoleteMessage(MethodInfo method, ObsoleteAttribute attribute)
{
const string format = "The alias {0} has been made obsolete. {1}";
var message = string.Format(
CultureInfo.InvariantCulture,
format, method.Name, attribute.Message);
return message.Trim();
}
}
}