[10506] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10506] | 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;
|
---|
[10332] | 23 | using System.CodeDom;
|
---|
| 24 | using System.CodeDom.Compiler;
|
---|
| 25 | using System.Collections.Generic;
|
---|
| 26 | using System.Drawing;
|
---|
| 27 | using System.IO;
|
---|
| 28 | using System.Linq;
|
---|
| 29 | using System.Reflection;
|
---|
| 30 | using System.Text;
|
---|
| 31 | using HeuristicLab.Common;
|
---|
| 32 | using HeuristicLab.Common.Resources;
|
---|
| 33 | using HeuristicLab.Core;
|
---|
| 34 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 35 | using Microsoft.CSharp;
|
---|
| 36 |
|
---|
[10506] | 37 | namespace HeuristicLab.Scripting {
|
---|
[10332] | 38 | [StorableClass]
|
---|
[14029] | 39 | public abstract class Script : NamedItem, IProgrammableItem {
|
---|
[10332] | 40 | #region Fields & Properties
|
---|
| 41 | public static new Image StaticItemImage {
|
---|
| 42 | get { return VSImageLibrary.Script; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | [Storable]
|
---|
| 46 | private string code;
|
---|
[10857] | 47 | public string Code {
|
---|
[10332] | 48 | get { return code; }
|
---|
| 49 | set {
|
---|
| 50 | if (value == code) return;
|
---|
| 51 | code = value;
|
---|
| 52 | OnCodeChanged();
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | private CompilerErrorCollection compileErrors;
|
---|
[10857] | 57 | public CompilerErrorCollection CompileErrors {
|
---|
[10332] | 58 | get { return compileErrors; }
|
---|
| 59 | private set {
|
---|
| 60 | compileErrors = value;
|
---|
| 61 | OnCompileErrorsChanged();
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | #endregion
|
---|
| 65 |
|
---|
| 66 | #region Construction & Initialization
|
---|
| 67 | [StorableConstructor]
|
---|
[10731] | 68 | protected Script(bool deserializing) : base(deserializing) { }
|
---|
| 69 | protected Script(Script original, Cloner cloner)
|
---|
[10332] | 70 | : base(original, cloner) {
|
---|
| 71 | code = original.code;
|
---|
| 72 | if (original.compileErrors != null)
|
---|
| 73 | compileErrors = new CompilerErrorCollection(original.compileErrors);
|
---|
| 74 | }
|
---|
[14029] | 75 | protected Script()
|
---|
[10857] | 76 | : base("Script", "An empty script.") {
|
---|
[10332] | 77 | }
|
---|
[14029] | 78 | protected Script(string code)
|
---|
[10577] | 79 | : this() {
|
---|
| 80 | this.code = code;
|
---|
| 81 | }
|
---|
[10332] | 82 | #endregion
|
---|
| 83 |
|
---|
[10731] | 84 | #region Compilation
|
---|
| 85 | protected virtual CompilerResults DoCompile() {
|
---|
[10332] | 86 | var parameters = new CompilerParameters {
|
---|
| 87 | GenerateExecutable = false,
|
---|
| 88 | GenerateInMemory = true,
|
---|
[10506] | 89 | IncludeDebugInformation = true,
|
---|
| 90 | WarningLevel = 4
|
---|
[10332] | 91 | };
|
---|
[11657] | 92 |
|
---|
[10332] | 93 | parameters.ReferencedAssemblies.AddRange(
|
---|
| 94 | GetAssemblies()
|
---|
| 95 | .Select(a => a.Location)
|
---|
| 96 | .ToArray());
|
---|
[11657] | 97 |
|
---|
[14029] | 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);
|
---|
[10332] | 104 | }
|
---|
| 105 |
|
---|
[10731] | 106 | public virtual Assembly Compile() {
|
---|
[10332] | 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 | }
|
---|
[11882] | 116 | throw new CompilationException(string.Format("Compilation of \"{0}\" failed:{1}{2}",
|
---|
[10731] | 117 | Name, Environment.NewLine, sb.ToString()));
|
---|
[10332] | 118 | } else {
|
---|
[10731] | 119 | return results.CompiledAssembly;
|
---|
[10332] | 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[10731] | 123 | public virtual IEnumerable<Assembly> GetAssemblies() {
|
---|
[11657] | 124 | var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic && File.Exists(a.Location)).ToList();
|
---|
[10358] | 125 | assemblies.Add(typeof(Microsoft.CSharp.RuntimeBinder.Binder).Assembly); // for dlr functionality
|
---|
| 126 | return assemblies;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[10731] | 129 | protected virtual CodeCompileUnit CreateCompilationUnit() {
|
---|
[10401] | 130 | var unit = new CodeSnippetCompileUnit(code);
|
---|
[10332] | 131 | return unit;
|
---|
| 132 | }
|
---|
| 133 | #endregion
|
---|
| 134 |
|
---|
| 135 | public event EventHandler CodeChanged;
|
---|
[10731] | 136 | protected virtual void OnCodeChanged() {
|
---|
[10332] | 137 | var handler = CodeChanged;
|
---|
| 138 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | public event EventHandler CompileErrorsChanged;
|
---|
[10731] | 142 | protected virtual void OnCompileErrorsChanged() {
|
---|
[10332] | 143 | var handler = CompileErrorsChanged;
|
---|
| 144 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 | }
|
---|