|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | +using Exortech.NetReflector; |
| 5 | +using ThoughtWorks.CruiseControl.Remote; |
| 6 | +using System.Globalization; |
| 7 | +using Microsoft.CSharp; |
| 8 | +using System.CodeDom.Compiler; |
| 9 | +using System.Reflection; |
| 10 | + |
| 11 | +namespace ThoughtWorks.CruiseControl.Core.Label |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// <para> |
| 15 | + /// Allows CCNet create custom code-generated labels |
| 16 | + /// </para> |
| 17 | + /// <para> |
| 18 | + /// You can do this by specifying your own configuration of the default labeller in your project. |
| 19 | + /// </para> |
| 20 | + /// </summary> |
| 21 | + /// <title>Custom Labeller</title> |
| 22 | + /// <version>1.0</version> |
| 23 | + /// <example> |
| 24 | + /// <code> |
| 25 | + /// <labeller type="customlabeller"> |
| 26 | + /// <cscode>1</cscode> |
| 27 | + /// </labeller> |
| 28 | + /// </code> |
| 29 | + /// </example> |
| 30 | + [ReflectorType("customlabeller")] |
| 31 | + public class CustomLabeller |
| 32 | + : LabellerBase |
| 33 | + { |
| 34 | + /// <summary> |
| 35 | + /// Generates the specified integration result. |
| 36 | + /// </summary> |
| 37 | + /// <param name="integrationResult">The integration result.</param> |
| 38 | + /// <returns></returns> |
| 39 | + /// <remarks></remarks> |
| 40 | + public override string Generate(IIntegrationResult integrationResult) |
| 41 | + { |
| 42 | + MethodInfo method = this.CreateFunction(this.CsCode); |
| 43 | + string ret = (string)method.Invoke(null, new object[1] { integrationResult }); |
| 44 | + return ret; |
| 45 | + } |
| 46 | + |
| 47 | + [ReflectorProperty("usings", Required = false)] |
| 48 | + public string Usings { get; set; } |
| 49 | + |
| 50 | + [ReflectorProperty("referencedassemblies", Required = false)] |
| 51 | + public string ReferencedAssemblies { get; set; } |
| 52 | + |
| 53 | + [ReflectorProperty("cscode", Required = true)] |
| 54 | + public string CsCode { get; set; } |
| 55 | + |
| 56 | + private string CSCodeWrapper |
| 57 | + { |
| 58 | + get |
| 59 | + { |
| 60 | + return @" |
| 61 | +
|
| 62 | +using System; |
| 63 | +using ThoughtWorks.CruiseControl.Core; |
| 64 | +using ThoughtWorks.CruiseControl.Remote; |
| 65 | + |
| 66 | +namespace CustomLabelerGeneratorUserFunctions |
| 67 | +{ |
| 68 | + public class CustomLabelerGenerator |
| 69 | + { |
| 70 | + public static string Generate(ThoughtWorks.CruiseControl.Core.IIntegrationResult integrationResult) |
| 71 | + { |
| 72 | + string ret = ""0.0.0.0""; |
| 73 | + <customCodeForReplace> |
| 74 | + return ret; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +"; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public MethodInfo CreateFunction(string function) |
| 83 | + { |
| 84 | + System.Text.StringBuilder usings = new System.Text.StringBuilder(); |
| 85 | + if (!string.IsNullOrWhiteSpace(this.Usings)) |
| 86 | + { |
| 87 | + foreach (var each in this.Usings.Split(';')) |
| 88 | + { |
| 89 | + if (!string.IsNullOrWhiteSpace(each)) |
| 90 | + { |
| 91 | + usings.AppendFormat("using {0};", each); |
| 92 | + usings.AppendLine(); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + string finalCode = usings.ToString() + CSCodeWrapper.Replace("<customCodeForReplace>", function); |
| 98 | + |
| 99 | + CSharpCodeProvider provider = new CSharpCodeProvider(); |
| 100 | + var parameters = new CompilerParameters(); |
| 101 | + parameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location); |
| 102 | + parameters.ReferencedAssemblies.Add(typeof(ThoughtWorks.CruiseControl.Remote.IntegrationStatus).Assembly.Location); |
| 103 | + if (!string.IsNullOrWhiteSpace(this.ReferencedAssemblies)) |
| 104 | + { |
| 105 | + foreach (var each in this.ReferencedAssemblies.Split(';')) |
| 106 | + { |
| 107 | + if (!string.IsNullOrWhiteSpace(each)) |
| 108 | + { |
| 109 | + parameters.ReferencedAssemblies.Add(each); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + parameters.GenerateInMemory = true; |
| 115 | + parameters.GenerateExecutable = false; |
| 116 | + parameters.IncludeDebugInformation = false; |
| 117 | + CompilerResults results = provider.CompileAssemblyFromSource(parameters, finalCode); |
| 118 | + if (results.Errors != null && results.Errors.Count > 0) |
| 119 | + { |
| 120 | + var path = System.IO.Path.GetTempFileName(); |
| 121 | + System.IO.File.WriteAllText(path, finalCode); |
| 122 | + foreach (var each in results.Errors) |
| 123 | + { |
| 124 | + Console.WriteLine("ERROR in {0}: {1}", path, each); |
| 125 | + } |
| 126 | + |
| 127 | + throw new ApplicationException("There are compilation errors. Please see " + path); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + Type binaryFunction = results.CompiledAssembly.GetType("CustomLabelerGeneratorUserFunctions.CustomLabelerGenerator"); |
| 132 | + return binaryFunction.GetMethod("Generate"); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments