Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Scripting/3.3/Script.cs @ 13806

Last change on this file since 13806 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

File size: 4.5 KB
RevLine 
[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
22using System;
[10332]23using System.CodeDom;
24using System.CodeDom.Compiler;
25using System.Collections.Generic;
26using System.IO;
27using System.Linq;
28using System.Reflection;
29using System.Text;
30using HeuristicLab.Common;
31using HeuristicLab.Core;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using Microsoft.CSharp;
34
[10506]35namespace HeuristicLab.Scripting {
[10332]36  [StorableClass]
[13218]37  public abstract class Script : NamedItem, IProgrammableItem {
[10332]38    #region Fields & Properties
39
[13656]40
[10332]41    [Storable]
42    private string code;
[13656]43    public string Code
44    {
[10332]45      get { return code; }
[13656]46      set
47      {
[10332]48        if (value == code) return;
49        code = value;
50        OnCodeChanged();
51      }
52    }
53
54    private CompilerErrorCollection compileErrors;
[13656]55    public CompilerErrorCollection CompileErrors
56    {
[10332]57      get { return compileErrors; }
[13656]58      private set
59      {
[10332]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    }
[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.