[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]
|
---|
[10731] | 39 | public class Script : NamedItem {
|
---|
| 40 | protected virtual string CodeTemplate {
|
---|
| 41 | get { return string.Empty; }
|
---|
| 42 | }
|
---|
[10332] | 43 |
|
---|
| 44 | #region Fields & Properties
|
---|
| 45 | public static new Image StaticItemImage {
|
---|
| 46 | get { return VSImageLibrary.Script; }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | [Storable]
|
---|
| 50 | private string code;
|
---|
[10857] | 51 | public string Code {
|
---|
[10332] | 52 | get { return code; }
|
---|
| 53 | set {
|
---|
| 54 | if (value == code) return;
|
---|
| 55 | code = value;
|
---|
| 56 | OnCodeChanged();
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | private CompilerErrorCollection compileErrors;
|
---|
[10857] | 61 | public CompilerErrorCollection CompileErrors {
|
---|
[10332] | 62 | get { return compileErrors; }
|
---|
| 63 | private set {
|
---|
| 64 | compileErrors = value;
|
---|
| 65 | OnCompileErrorsChanged();
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | #endregion
|
---|
| 69 |
|
---|
| 70 | #region Construction & Initialization
|
---|
| 71 | [StorableConstructor]
|
---|
[10731] | 72 | protected Script(bool deserializing) : base(deserializing) { }
|
---|
| 73 | protected Script(Script original, Cloner cloner)
|
---|
[10332] | 74 | : base(original, cloner) {
|
---|
| 75 | code = original.code;
|
---|
| 76 | if (original.compileErrors != null)
|
---|
| 77 | compileErrors = new CompilerErrorCollection(original.compileErrors);
|
---|
| 78 | }
|
---|
[10857] | 79 | public Script()
|
---|
| 80 | : base("Script", "An empty script.") {
|
---|
[11657] | 81 | code = CodeTemplate;
|
---|
[10332] | 82 | }
|
---|
[10577] | 83 | public Script(string code)
|
---|
| 84 | : this() {
|
---|
| 85 | this.code = code;
|
---|
| 86 | }
|
---|
[10332] | 87 |
|
---|
| 88 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[10401] | 89 | return new Script(this, cloner);
|
---|
[10332] | 90 | }
|
---|
| 91 | #endregion
|
---|
| 92 |
|
---|
[10731] | 93 | #region Compilation
|
---|
| 94 | protected virtual CSharpCodeProvider CodeProvider {
|
---|
| 95 | get {
|
---|
| 96 | return new CSharpCodeProvider(
|
---|
| 97 | new Dictionary<string, string> {
|
---|
| 98 | {"CompilerVersion", "v4.0"}, // support C# 4.0 syntax
|
---|
| 99 | });
|
---|
| 100 | }
|
---|
[10332] | 101 | }
|
---|
| 102 |
|
---|
[10731] | 103 | protected virtual CompilerResults DoCompile() {
|
---|
[10332] | 104 | var parameters = new CompilerParameters {
|
---|
| 105 | GenerateExecutable = false,
|
---|
| 106 | GenerateInMemory = true,
|
---|
[10506] | 107 | IncludeDebugInformation = true,
|
---|
| 108 | WarningLevel = 4
|
---|
[10332] | 109 | };
|
---|
[11657] | 110 |
|
---|
[10332] | 111 | parameters.ReferencedAssemblies.AddRange(
|
---|
| 112 | GetAssemblies()
|
---|
| 113 | .Select(a => a.Location)
|
---|
| 114 | .ToArray());
|
---|
[11657] | 115 |
|
---|
| 116 | return CodeProvider.CompileAssemblyFromSource(parameters, code);
|
---|
[10332] | 117 | }
|
---|
| 118 |
|
---|
[10731] | 119 | public virtual Assembly Compile() {
|
---|
[10332] | 120 | var results = DoCompile();
|
---|
| 121 | CompileErrors = results.Errors;
|
---|
| 122 | if (results.Errors.HasErrors) {
|
---|
| 123 | var sb = new StringBuilder();
|
---|
| 124 | foreach (CompilerError error in results.Errors) {
|
---|
| 125 | sb.Append(error.Line).Append(':')
|
---|
| 126 | .Append(error.Column).Append(": ")
|
---|
| 127 | .AppendLine(error.ErrorText);
|
---|
| 128 | }
|
---|
[11882] | 129 | throw new CompilationException(string.Format("Compilation of \"{0}\" failed:{1}{2}",
|
---|
[10731] | 130 | Name, Environment.NewLine, sb.ToString()));
|
---|
[10332] | 131 | } else {
|
---|
[10731] | 132 | return results.CompiledAssembly;
|
---|
[10332] | 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[10731] | 136 | public virtual IEnumerable<Assembly> GetAssemblies() {
|
---|
[11657] | 137 | var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic && File.Exists(a.Location)).ToList();
|
---|
[10358] | 138 | assemblies.Add(typeof(Microsoft.CSharp.RuntimeBinder.Binder).Assembly); // for dlr functionality
|
---|
| 139 | return assemblies;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[10731] | 142 | protected virtual CodeCompileUnit CreateCompilationUnit() {
|
---|
[10401] | 143 | var unit = new CodeSnippetCompileUnit(code);
|
---|
[10332] | 144 | return unit;
|
---|
| 145 | }
|
---|
| 146 | #endregion
|
---|
| 147 |
|
---|
| 148 | public event EventHandler CodeChanged;
|
---|
[10731] | 149 | protected virtual void OnCodeChanged() {
|
---|
[10332] | 150 | var handler = CodeChanged;
|
---|
| 151 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | public event EventHandler CompileErrorsChanged;
|
---|
[10731] | 155 | protected virtual void OnCompileErrorsChanged() {
|
---|
[10332] | 156 | var handler = CompileErrorsChanged;
|
---|
| 157 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | }
|
---|