Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Scripting/3.3/Script.cs @ 16565

Last change on this file since 16565 was 16565, checked in by gkronber, 5 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 4.6 KB
RevLine 
[10506]1#region License Information
2/* HeuristicLab
[16565]3 * Copyright (C) 2002-2019 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
22using System;
[10332]23using System.CodeDom;
24using System.CodeDom.Compiler;
25using System.Collections.Generic;
26using System.Drawing;
27using System.IO;
28using System.Linq;
29using System.Reflection;
30using System.Text;
31using HeuristicLab.Common;
32using HeuristicLab.Common.Resources;
33using HeuristicLab.Core;
[16565]34using HEAL.Attic;
[10332]35using Microsoft.CSharp;
36
[10506]37namespace HeuristicLab.Scripting {
[16565]38  [StorableType("0FA4F218-E1F5-4C09-9C2F-12B32D4EC373")]
[13218]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]
[16565]68    protected Script(StorableConstructorFlag _) : base(_) { }
[10731]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    }
[13218]75    protected Script()
[10857]76      : base("Script", "An empty script.") {
[10332]77    }
[13218]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
[13218]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}
Note: See TracBrowser for help on using the repository browser.