[10506] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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;
|
---|
[17110] | 31 | using System.Text.RegularExpressions;
|
---|
| 32 | using HEAL.Attic;
|
---|
[10332] | 33 | using HeuristicLab.Common;
|
---|
| 34 | using HeuristicLab.Common.Resources;
|
---|
| 35 | using HeuristicLab.Core;
|
---|
| 36 | using Microsoft.CSharp;
|
---|
| 37 |
|
---|
[10506] | 38 | namespace HeuristicLab.Scripting {
|
---|
[16565] | 39 | [StorableType("0FA4F218-E1F5-4C09-9C2F-12B32D4EC373")]
|
---|
[13218] | 40 | public abstract class Script : NamedItem, IProgrammableItem {
|
---|
[10332] | 41 | #region Fields & Properties
|
---|
| 42 | public static new Image StaticItemImage {
|
---|
| 43 | get { return VSImageLibrary.Script; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | [Storable]
|
---|
| 47 | private string code;
|
---|
[10857] | 48 | public string Code {
|
---|
[10332] | 49 | get { return code; }
|
---|
| 50 | set {
|
---|
| 51 | if (value == code) return;
|
---|
| 52 | code = value;
|
---|
| 53 | OnCodeChanged();
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | private CompilerErrorCollection compileErrors;
|
---|
[10857] | 58 | public CompilerErrorCollection CompileErrors {
|
---|
[10332] | 59 | get { return compileErrors; }
|
---|
| 60 | private set {
|
---|
| 61 | compileErrors = value;
|
---|
| 62 | OnCompileErrorsChanged();
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | #endregion
|
---|
| 66 |
|
---|
| 67 | #region Construction & Initialization
|
---|
| 68 | [StorableConstructor]
|
---|
[16565] | 69 | protected Script(StorableConstructorFlag _) : base(_) { }
|
---|
[10731] | 70 | protected Script(Script original, Cloner cloner)
|
---|
[10332] | 71 | : base(original, cloner) {
|
---|
| 72 | code = original.code;
|
---|
| 73 | if (original.compileErrors != null)
|
---|
| 74 | compileErrors = new CompilerErrorCollection(original.compileErrors);
|
---|
| 75 | }
|
---|
[13218] | 76 | protected Script()
|
---|
[10857] | 77 | : base("Script", "An empty script.") {
|
---|
[10332] | 78 | }
|
---|
[13218] | 79 | protected Script(string code)
|
---|
[10577] | 80 | : this() {
|
---|
| 81 | this.code = code;
|
---|
| 82 | }
|
---|
[10332] | 83 | #endregion
|
---|
| 84 |
|
---|
[10731] | 85 | #region Compilation
|
---|
| 86 | protected virtual CompilerResults DoCompile() {
|
---|
[10332] | 87 | var parameters = new CompilerParameters {
|
---|
| 88 | GenerateExecutable = false,
|
---|
| 89 | GenerateInMemory = true,
|
---|
[10506] | 90 | IncludeDebugInformation = true,
|
---|
| 91 | WarningLevel = 4
|
---|
[10332] | 92 | };
|
---|
[11657] | 93 |
|
---|
[10332] | 94 | parameters.ReferencedAssemblies.AddRange(
|
---|
| 95 | GetAssemblies()
|
---|
| 96 | .Select(a => a.Location)
|
---|
| 97 | .ToArray());
|
---|
[11657] | 98 |
|
---|
[13218] | 99 | var codeProvider = new CSharpCodeProvider(
|
---|
| 100 | new Dictionary<string, string> {
|
---|
| 101 | { "CompilerVersion", "v4.0"} // support C# 4.0 syntax
|
---|
| 102 | });
|
---|
| 103 |
|
---|
| 104 | return codeProvider.CompileAssemblyFromSource(parameters, code);
|
---|
[10332] | 105 | }
|
---|
| 106 |
|
---|
[10731] | 107 | public virtual Assembly Compile() {
|
---|
[10332] | 108 | var results = DoCompile();
|
---|
| 109 | CompileErrors = results.Errors;
|
---|
| 110 | if (results.Errors.HasErrors) {
|
---|
| 111 | var sb = new StringBuilder();
|
---|
| 112 | foreach (CompilerError error in results.Errors) {
|
---|
| 113 | sb.Append(error.Line).Append(':')
|
---|
| 114 | .Append(error.Column).Append(": ")
|
---|
| 115 | .AppendLine(error.ErrorText);
|
---|
| 116 | }
|
---|
[11882] | 117 | throw new CompilationException(string.Format("Compilation of \"{0}\" failed:{1}{2}",
|
---|
[10731] | 118 | Name, Environment.NewLine, sb.ToString()));
|
---|
[10332] | 119 | } else {
|
---|
[10731] | 120 | return results.CompiledAssembly;
|
---|
[10332] | 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[10731] | 124 | public virtual IEnumerable<Assembly> GetAssemblies() {
|
---|
[17110] | 125 | var assemblies = AppDomain.CurrentDomain.GetAssemblies()
|
---|
| 126 | .Where(a => !a.IsDynamic && File.Exists(a.Location))
|
---|
| 127 | .GroupBy(x => Regex.Replace(Path.GetFileName(x.Location), @"-[\d.]+\.dll$", ""))
|
---|
| 128 | .Select(x => x.OrderByDescending(y => y.GetName().Version).First())
|
---|
| 129 | .ToList();
|
---|
[10358] | 130 | assemblies.Add(typeof(Microsoft.CSharp.RuntimeBinder.Binder).Assembly); // for dlr functionality
|
---|
| 131 | return assemblies;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[10731] | 134 | protected virtual CodeCompileUnit CreateCompilationUnit() {
|
---|
[10401] | 135 | var unit = new CodeSnippetCompileUnit(code);
|
---|
[10332] | 136 | return unit;
|
---|
| 137 | }
|
---|
| 138 | #endregion
|
---|
| 139 |
|
---|
| 140 | public event EventHandler CodeChanged;
|
---|
[10731] | 141 | protected virtual void OnCodeChanged() {
|
---|
[10332] | 142 | var handler = CodeChanged;
|
---|
| 143 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | public event EventHandler CompileErrorsChanged;
|
---|
[10731] | 147 | protected virtual void OnCompileErrorsChanged() {
|
---|
[10332] | 148 | var handler = CompileErrorsChanged;
|
---|
| 149 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | }
|
---|