1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.CodeDom;
|
---|
24 | using System.CodeDom.Compiler;
|
---|
25 | using System.Collections.Generic;
|
---|
26 | using System.IO;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Reflection;
|
---|
29 | using System.Text;
|
---|
30 | using HeuristicLab.Common;
|
---|
31 | using HeuristicLab.Core;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using Microsoft.CSharp;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Scripting {
|
---|
36 | [StorableClass]
|
---|
37 | public abstract class Script : NamedItem, IProgrammableItem {
|
---|
38 | #region Fields & Properties
|
---|
39 |
|
---|
40 |
|
---|
41 | [Storable]
|
---|
42 | private string code;
|
---|
43 | public string Code
|
---|
44 | {
|
---|
45 | get { return code; }
|
---|
46 | set
|
---|
47 | {
|
---|
48 | if (value == code) return;
|
---|
49 | code = value;
|
---|
50 | OnCodeChanged();
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private CompilerErrorCollection compileErrors;
|
---|
55 | public CompilerErrorCollection CompileErrors
|
---|
56 | {
|
---|
57 | get { return compileErrors; }
|
---|
58 | private set
|
---|
59 | {
|
---|
60 | compileErrors = value;
|
---|
61 | OnCompileErrorsChanged();
|
---|
62 | }
|
---|
63 | }
|
---|
64 | #endregion
|
---|
65 |
|
---|
66 | #region Construction & Initialization
|
---|
67 | [StorableConstructor]
|
---|
68 | protected Script(bool deserializing) : base(deserializing) { }
|
---|
69 | protected Script(Script original, Cloner cloner)
|
---|
70 | : base(original, cloner) {
|
---|
71 | code = original.code;
|
---|
72 | if (original.compileErrors != null)
|
---|
73 | compileErrors = new CompilerErrorCollection(original.compileErrors);
|
---|
74 | }
|
---|
75 | protected Script()
|
---|
76 | : base("Script", "An empty script.") {
|
---|
77 | }
|
---|
78 | protected Script(string code)
|
---|
79 | : this() {
|
---|
80 | this.code = code;
|
---|
81 | }
|
---|
82 | #endregion
|
---|
83 |
|
---|
84 | #region Compilation
|
---|
85 | protected virtual CompilerResults DoCompile() {
|
---|
86 | var parameters = new CompilerParameters {
|
---|
87 | GenerateExecutable = false,
|
---|
88 | GenerateInMemory = true,
|
---|
89 | IncludeDebugInformation = true,
|
---|
90 | WarningLevel = 4
|
---|
91 | };
|
---|
92 |
|
---|
93 | parameters.ReferencedAssemblies.AddRange(
|
---|
94 | GetAssemblies()
|
---|
95 | .Select(a => a.Location)
|
---|
96 | .ToArray());
|
---|
97 |
|
---|
98 | var codeProvider = new CSharpCodeProvider(
|
---|
99 | new Dictionary<string, string> {
|
---|
100 | { "CompilerVersion", "v4.0"} // support C# 4.0 syntax
|
---|
101 | });
|
---|
102 |
|
---|
103 | return codeProvider.CompileAssemblyFromSource(parameters, code);
|
---|
104 | }
|
---|
105 |
|
---|
106 | public virtual Assembly Compile() {
|
---|
107 | var results = DoCompile();
|
---|
108 | CompileErrors = results.Errors;
|
---|
109 | if (results.Errors.HasErrors) {
|
---|
110 | var sb = new StringBuilder();
|
---|
111 | foreach (CompilerError error in results.Errors) {
|
---|
112 | sb.Append(error.Line).Append(':')
|
---|
113 | .Append(error.Column).Append(": ")
|
---|
114 | .AppendLine(error.ErrorText);
|
---|
115 | }
|
---|
116 | throw new CompilationException(string.Format("Compilation of \"{0}\" failed:{1}{2}",
|
---|
117 | Name, Environment.NewLine, sb.ToString()));
|
---|
118 | } else {
|
---|
119 | return results.CompiledAssembly;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | public virtual IEnumerable<Assembly> GetAssemblies() {
|
---|
124 | var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic && File.Exists(a.Location)).ToList();
|
---|
125 | assemblies.Add(typeof(Microsoft.CSharp.RuntimeBinder.Binder).Assembly); // for dlr functionality
|
---|
126 | return assemblies;
|
---|
127 | }
|
---|
128 |
|
---|
129 | protected virtual CodeCompileUnit CreateCompilationUnit() {
|
---|
130 | var unit = new CodeSnippetCompileUnit(code);
|
---|
131 | return unit;
|
---|
132 | }
|
---|
133 | #endregion
|
---|
134 |
|
---|
135 | public event EventHandler CodeChanged;
|
---|
136 | protected virtual void OnCodeChanged() {
|
---|
137 | var handler = CodeChanged;
|
---|
138 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
139 | }
|
---|
140 |
|
---|
141 | public event EventHandler CompileErrorsChanged;
|
---|
142 | protected virtual void OnCompileErrorsChanged() {
|
---|
143 | var handler = CompileErrorsChanged;
|
---|
144 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|