This repository was archived by the owner on Feb 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathCustomLabeller.cs
135 lines (121 loc) · 4.56 KB
/
CustomLabeller.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
using System;
using System.Linq;
using System.Text.RegularExpressions;
using Exortech.NetReflector;
using ThoughtWorks.CruiseControl.Remote;
using System.Globalization;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
namespace ThoughtWorks.CruiseControl.Core.Label
{
/// <summary>
/// <para>
/// Allows CCNet create custom code-generated labels
/// </para>
/// <para>
/// You can do this by specifying your own configuration of the default labeller in your project.
/// </para>
/// </summary>
/// <title>Custom Labeller</title>
/// <version>1.0</version>
/// <example>
/// <code>
/// <labeller type="customlabeller">
/// <cscode>1</cscode>
/// </labeller>
/// </code>
/// </example>
[ReflectorType("customlabeller")]
public class CustomLabeller
: LabellerBase
{
/// <summary>
/// Generates the specified integration result.
/// </summary>
/// <param name="integrationResult">The integration result.</param>
/// <returns></returns>
/// <remarks></remarks>
public override string Generate(IIntegrationResult integrationResult)
{
MethodInfo method = this.CreateFunction(this.CsCode);
string ret = (string)method.Invoke(null, new object[1] { integrationResult });
return ret;
}
[ReflectorProperty("usings", Required = false)]
public string Usings { get; set; }
[ReflectorProperty("referencedassemblies", Required = false)]
public string ReferencedAssemblies { get; set; }
[ReflectorProperty("cscode", Required = true)]
public string CsCode { get; set; }
private string CSCodeWrapper
{
get
{
return @"
using System;
using ThoughtWorks.CruiseControl.Core;
using ThoughtWorks.CruiseControl.Remote;
namespace CustomLabelerGeneratorUserFunctions
{
public class CustomLabelerGenerator
{
public static string Generate(ThoughtWorks.CruiseControl.Core.IIntegrationResult integrationResult)
{
string ret = ""0.0.0.0"";
<customCodeForReplace>
return ret;
}
}
}
";
}
}
public MethodInfo CreateFunction(string function)
{
System.Text.StringBuilder usings = new System.Text.StringBuilder();
if (!string.IsNullOrWhiteSpace(this.Usings))
{
foreach (var each in this.Usings.Split(';'))
{
if (!string.IsNullOrWhiteSpace(each))
{
usings.AppendFormat("using {0};", each);
usings.AppendLine();
}
}
}
string finalCode = usings.ToString() + CSCodeWrapper.Replace("<customCodeForReplace>", function);
CSharpCodeProvider provider = new CSharpCodeProvider();
var parameters = new CompilerParameters();
parameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
parameters.ReferencedAssemblies.Add(typeof(ThoughtWorks.CruiseControl.Remote.IntegrationStatus).Assembly.Location);
if (!string.IsNullOrWhiteSpace(this.ReferencedAssemblies))
{
foreach (var each in this.ReferencedAssemblies.Split(';'))
{
if (!string.IsNullOrWhiteSpace(each))
{
parameters.ReferencedAssemblies.Add(each);
}
}
}
parameters.GenerateInMemory = true;
parameters.GenerateExecutable = false;
parameters.IncludeDebugInformation = false;
CompilerResults results = provider.CompileAssemblyFromSource(parameters, finalCode);
if (results.Errors != null && results.Errors.Count > 0)
{
var path = System.IO.Path.GetTempFileName();
System.IO.File.WriteAllText(path, finalCode);
foreach (var each in results.Errors)
{
Console.WriteLine("ERROR in {0}: {1}", path, each);
}
throw new ApplicationException("There are compilation errors. Please see " + path);
}
Type binaryFunction = results.CompiledAssembly.GetType("CustomLabelerGeneratorUserFunctions.CustomLabelerGenerator");
return binaryFunction.GetMethod("Generate");
}
}
}