Example #1
1
 public RoslynScriptEngine(IScriptHostFactory scriptHostFactory, ILog logger)
 {
     _scriptEngine = new ScriptEngine();
     _scriptEngine.AddReference(typeof(ScriptExecutor).Assembly);
     _scriptHostFactory = scriptHostFactory;
     _logger = logger;
 }
        public void Execute(string script, IEnumerable<string> paths, IEnumerable<IScriptcsRecipe> recipes)
        {
            var engine = new ScriptEngine();
            engine.AddReference("System");
            engine.AddReference("System.Core");
            var bin = _fileSystem.CurrentDirectory + @"\bin";
            engine.BaseDirectory = bin;

            if (!_fileSystem.DirectoryExists(bin))
                _fileSystem.CreateDirectory(bin);

            foreach (var file in paths)
            {
                var destFile = bin + @"\" + Path.GetFileName(file);
                var sourceFileLastWriteTime = _fileSystem.GetLastWriteTime(file);
                var destFileLastWriteTime = _fileSystem.GetLastWriteTime(destFile);
                if (sourceFileLastWriteTime != destFileLastWriteTime)
                    _fileSystem.Copy(file, destFile,true);

                engine.AddReference(destFile);
            }

            var session = engine.CreateSession();
            var csx = _fileSystem.ReadFile(_fileSystem.CurrentDirectory + @"\" + script);
            session.Execute(csx);
        }
Example #3
0
        protected OpohoExecutorBase()
        {
            if (!Provider.IsValid)
            {
                Log.Error("Invalid CompositionProvider. Unable to initialise OpohoExecutor");
                return;
            }
            Config = Provider.ConfigurationProvider as IConsoleConfigurationProvider;
            if (Config == null)
            {
                Log.Error("Invalid ConfigurationProvider. Unable to initialise OpohoExecutor");
                return;
            }
            if (!Config.IsValid)
            {
                Log.Error("Invalid configuration. Unable to initialise OpohoExecutor");
                return;
            }
            CsharpEngine = new ScriptEngine { BaseDirectory = Config.LibDirectory };
            CsharpEngine.AddReference(typeof(System.Linq.Enumerable).Assembly);
            CsharpEngine.AddReference(typeof(IOpohoReportCommand).Assembly);
            CsharpEngine.AddReference(typeof(ReplContextBase).Assembly);

            if (Config.IsValid)
            {
                AddReferences();
                AddImports();
            }
        }
Example #4
0
 public void Compiler_SystemType()
 {
     var session = new ScriptEngine().CreateSession();
     session.AddReference(typeof (QueryState<EventBase>).Assembly);
     session.AddReference(typeof(Func<int, bool>).Assembly);
     var actual = session.Execute<Func<int, bool>>("i => i == 1");
     actual.Should().NotBeNull("null compilation");
     actual.Invoke(2).Should().BeFalse("Should return false on 2");
     actual.Invoke(1).Should().BeTrue("Should return true on 1");
 }
Example #5
0
 public void Compiler_CustomType()
 {
     var session = new ScriptEngine().CreateSession();
     Type generic = typeof (Func<,>);
     session.AddReference(generic.Assembly);
     session.AddReference(typeof(EventBase).Assembly);
     var actual = session.Execute<Func<EventBase, bool>>("@event => @event.Type == 1");
     actual.Should().NotBeNull("null compilation");
     actual.Invoke(new Event<int>(1, 1)).Should().BeTrue();
     actual.Invoke(new Event<int>(2, 1)).Should().BeFalse();
 }
		public ExecutifyEngine(ILog logger, IFileSystem fileSystem)
		{
			_scriptEngine = new ScriptEngine();
			_scriptEngine.AddReference(typeof(ScriptExecutor).Assembly);
			_logger = logger;
			_fileSystem = fileSystem;
		}
        public ChartScriptEngine()
        {
            engine = new ScriptEngine();

            new[]
                {
                    typeof(System.ComponentModel.Component).Assembly,
                    typeof (int).Assembly,
                    typeof (HttpContext).Assembly,
                    typeof (Color).Assembly,
                    typeof (Chart).Assembly,
                    typeof (IEnumerable<>).Assembly,
                    typeof (IQueryable).Assembly
                }.ToList().ForEach(asm => engine.AddReference(asm));

            new[]
                {
                   "System",
                   "System.Web",
                   "System.Drawing",
                   "System.Linq",
                   "System.Collections",
                   "System.Collections.Generic",
                   "System.Web.UI.DataVisualization.Charting"
                }.ToList().ForEach(ns => engine.ImportNamespace(ns));
        }
 public ScriptingHost(dynamic context)
 {
     // Create the script engine
     _engine = new ScriptEngine();
     // Let us use engine’s Addreference for
     // adding some common assemblies
     new[]
         {
         typeof (Type).Assembly,
         typeof (ICollection).Assembly,
         typeof (ListDictionary).Assembly,
         typeof (Console).Assembly,
         typeof (ScriptingHost).Assembly,
         typeof (IEnumerable<>).Assembly,
         typeof (IQueryable).Assembly,
         typeof (DataSet).Assembly,
         GetType().Assembly
         }.ToList().ForEach(asm => _engine.
                     AddReference(asm));
     //Import common namespaces
     new[]
         {
         "System", "System.Linq",
         "System.Collections",
         "System.Collections.Generic"
         }.ToList().ForEach(ns => _engine.
                     ImportNamespace(ns));
     _session = _engine.CreateSession(context);
 }
Example #9
0
        private void CreateEngine()
        {
            //Create the script engine
            //Script engine constructor parameters go changed
            _engine = new ScriptEngine();
            //Let us use engine's Addreference for adding the required
            //assemblies
            new[]
                {
                    typeof (Type).Assembly,
                    typeof (ICollection).Assembly,
                    typeof (ListDictionary).Assembly,
                    typeof (Console).Assembly,
                    typeof (ScriptingHost).Assembly,
                    typeof (IEnumerable<>).Assembly,
                    typeof (IQueryable).Assembly,
                    //typeof (DbSet).Assembly,
                    GetType().Assembly
                }.ToList().ForEach(asm => _engine.AddReference(asm));

            new[]
                {
                    "System", "System.Linq",
                    "System.Collections",
                    "System.Data.Entity",
                    "System.Collections.Generic"
                }.ToList().ForEach(ns => _engine.ImportNamespace(ns));
        }
Example #10
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Usage: node.cs <filepath>");
                return;
            }
            try
            {
                using (StreamReader sr = new StreamReader(args[0]))
                {
                    string code = sr.ReadToEnd();
                    var engine = new ScriptEngine();
                    engine.AddReference("System");

                    var session = engine.CreateSession();
                    session.Execute("using System;");

                    session.Execute(code);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Example #11
0
        private static ScriptEngine ConfigureScriptEngine(ScriptInfo scriptInfo, Assembly modelAssembly)
        {
            var scriptEngine = new ScriptEngine();

            scriptEngine.AddReference(modelAssembly);
            scriptInfo.Assemblies.ToList().ForEach(scriptEngine.AddReference);
            scriptInfo.Namespaces.ToList().ForEach(scriptEngine.ImportNamespace);

            return scriptEngine;
        }
Example #12
0
        public SecureRoslynWrapper()
        {
            new PermissionSet(PermissionState.Unrestricted).Assert();
            Engine = new ScriptEngine();
            foreach (var assembly in references)
                Engine.AddReference(assembly);
            foreach (var name in namespaces)
                Engine.ImportNamespace(name);

            Session = Engine.CreateSession();

            CodeAccessPermission.RevertAssert();
        }
Example #13
0
        static void Main(string[] args)
        {
            var scriptEngine = new ScriptEngine();

            scriptEngine.AddReference("System");

            WritePromptLine();

            var code = new Code();
            var session = scriptEngine.CreateSession();
            session.Execute("");
            while (!code.IsExit)
            {
                try
                {
                    AddCodeLine(code);

                    if (code.IsExit) break;

                    var result = session.Execute(code.Text);

                    Console.WriteLine(result);

                    code.ClearNonLocalDeclarationLines();
                }
                catch (CompilationErrorException compilationErrorException)
                {
                    Console.WriteLine(compilationErrorException.Message);
                    code.Clear();
                }
                finally
                {
                    WritePromptLine();
                }
            }

            Console.WriteLine("bye");
        }
Example #14
0
        static void Main(string[] args)
        {
            {
                var engine = new ScriptEngine();

                var session = engine.CreateSession();

                session.Execute(@"var a = 42;");
                session.Execute(@"System.Console.WriteLine(a);");
            }

            {
                // Interacting with host application
                var hostObject = new HostObject();
                var engine = new ScriptEngine();

                //Let us use engine's Addreference for adding the required assemblies
                new[]
                {
                        typeof (Console).Assembly,
                        typeof (HostObject).Assembly,
                        typeof (IEnumerable<>).Assembly,
                        typeof (IQueryable).Assembly
                }.ToList().ForEach(asm => engine.AddReference(asm));

                new[]
                {
                   "System", "System.Linq",
                   "System.Collections",
                   "System.Collections.Generic"
                }.ToList().ForEach(ns => engine.ImportNamespace(ns));

                var session = engine.CreateSession(hostObject);

                session.Execute(@"Value = 156;");
                session.Execute(@"System.Console.WriteLine(Value);");
            }
        }
Example #15
0
 private Session ConfigureSession(QueryState<EventBase> query)
 {
     var session = new ScriptEngine().CreateSession(query);
     session.AddReference(GetType().Assembly);
     session.AddReference(typeof (QueryState<EventBase>).Assembly);
     return session;
 }
Example #16
0
        public void ShareStateBetweenHostAndScript()
        {
            // The host object.
            var state = new State();

            // Note that free identifiers in the script code bind to public members of the host object.
            var script1 = @"
            using System;
            using System.Linq;

            int i = 1; int j = 2;
            var array = new[] {i, j};
            var query = array.Where(a => a > 0).Select(a => a + 1);

            SharedValue += query.First() + query.Last();
            SharedValue"; // Script simply returns updated value of the shared state.

            // Create a new ScriptEngine.
            var engine = new ScriptEngine();
            engine.AddReference("System.Core");

            // Reference to current assembly is required so that script can find the host object.
            engine.AddReference(typeof(State).Assembly);

            // Create a Session that holds the host object that is used to share state between host and script.
            var session = engine.CreateSession(state);

            // Execute above script once under the session so that variables i and j are declared.
            session.Execute(script1);

            // Note that free identifiers in the script code bind to public members of the host object.
            var script2 = @"
            SharedValue += i + j;
            SharedValue"; // Script simply returns updated value of the shared state.
            var script3 = "i = 3; j = 4;";

            // Execute other scripts under the above session.
            // Note that script execution remembers variables declared previously in the session and
            // also sees changes in the shared session state.
            state.SharedValue = 3;
            int result = session.Execute<int>(script2);
            Assert.AreEqual(6, result);
            Assert.AreEqual(6, state.SharedValue);

            state.SharedValue = 4;
            session.Execute(script3);
            result = session.Execute<int>(script2);
            Assert.AreEqual(11, result);
            Assert.AreEqual(11, state.SharedValue);
        }
Example #17
0
        private static Session CreateSession(Debugger currentDebugger,bool getOldSession)
        {
            //CSScript = script;
            //var s = new CommonScriptEngine();

            if (getOldSession && Session != null)
                return Session;


            var asm = System.Reflection.Assembly.GetExecutingAssembly();

            var csharpEngine = new ScriptEngine(null, null);
            

            
            //csharpEngine.AddReference("System.Diagnostics");
            csharpEngine.ImportNamespace("System");
            csharpEngine.ImportNamespace("System.Collections.Generic");
            //csharpEngine.ImportNamespace("System.Linq");
            csharpEngine.ImportNamespace("System.Text");
            csharpEngine.ImportNamespace("System.IO");
            //csharpEngine.ImportNamespace("System.Diagnostics");
            
            csharpEngine.SetReferenceSearchPaths(asm.Location);
            csharpEngine.AddReference(typeof(System.Diagnostics.Debug).Assembly);

            csharpEngine.AddReference(typeof(System.Dynamic.DynamicObject).Assembly);

            csharpEngine.AddReference(asm);
            csharpEngine.ImportNamespace("ExtCS.Debugger");


            Session = csharpEngine.CreateSession(currentDebugger);

            return Session;
        }