[PL] MTS 2009 się zakończył – obiecane prezentacje


Witam was, tak jak obiecałem na konferencji publikuję komplet materiałów z moich prezentacji na konferencji Microsoft Technology Summit:

* .NET 4.0 Inside/Out [zip]

* Modelowanie w Visual Studio 2010 [zip]

Kod klasy uruchamiającej skrypty w Ruby i IronPython poniżej.
Drobna uwaga dla tych, którzy by chcieli na jednym hoście mieć możliwość uruchamiania zarówno skrypty Ruby jak i IronPython. Jeśli osobno pobierzecie binaria jednego i drugiego, to będziecie mieli konflikt Assemblies związanych DLR.

W takiej sytuacji polecam binariów z dlr.codeplex.com gdzie zawarty jest i IronPython i Ruby w jednym buildzie.

Źródła:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

using Microsoft.Scripting.Hosting;
using Microsoft.Scripting;
using IronRuby;

namespace ScriptHost
{
    public interface IBaseScriptRunner
    {
        bool Initialize();
        string Execute(string sourceScript);
    }

    public class RubyScriptRunner : IBaseScriptRunner
    {
        //private members:
        LanguageSetup setup;
        ScriptEngine engine;
        ScriptScope scope;

        byte[] rawStream;

        public virtual bool Initialize()
        {
            try
            {
                setup = Ruby.CreateRubySetup();

                engine = Ruby.CreateEngine();
                scope = engine.CreateScope();
                scope.SetVariable("copyright", "(c) 2009 Daniel Biesiada");

                var context = IronRuby.Ruby.GetExecutionContext(engine);
                var stdout = (from gv in context.GlobalVariables
                              where gv.Key == "stdout"
                              select gv).First().Value;

                rawStream = new byte[1024];
                MemoryStream memStream =
                     new MemoryStream(rawStream);

                IronRuby.Builtins.RubyIO rio =
                    new IronRuby.Builtins.RubyIO(context,
                            memStream,
                            IronRuby.Builtins.RubyFileMode.APPEND);

                stdout.SetValue(context, null, "stdout", rio);

                return true;
            }
            catch
            {
                return false;
            }
        }

        public string Execute(string sourceScript)
        {
            var script =
                engine.CreateScriptSourceFromString(sourceScript);

            try
            {
                var result = script.Execute(scope);
                if (result != null)
                {
                    return result.ToString();
                }
                else
                {
                    string sout = "";                  
                    foreach (byte b in rawStream)
                    {
                        if (b != 0)
                            sout += (char)b;
                    }
                    return sout;
                }
            }
            catch (Exception err)
            {
                return "{Syntax Error: " + err.Message + "}";
            }
        }
    }

    public class PythonScriptRunner : IBaseScriptRunner
    {
        ScriptEngine engine;
        ScriptScope scope;

        public virtual bool Initialize()
        {
            try
            {
                var rnt =
                    IronPython.Hosting.Python.CreateRuntime();               
                engine = rnt.GetEngine("py");
                scope = engine.CreateScope(); 
                return true;
            }
            catch
            {
                return false;
            }
        }

        public string Execute(string sourceScript)
        {
            var script =
                engine.CreateScriptSourceFromString(sourceScript);
            try
            {
                var result = script.Execute();

                if (result != null)
                {
                    return result.ToString();
                }
                else
                {
                    //TODO: wrap up standard output
                    return "{null}";
                }
            }
            catch (SyntaxErrorException syntaxError)
            {
                return "Syntax Error: " + syntaxError.ToString();
            }
            //return null;
        }
    }

}

/**
#Przykładowy skrypt Ruby do sprawdzenia
#cities.rb
cities  = %w[ London
              Oslo
              Paris
              Amsterdam
              Berlin ]
visited = %w[Berlin Oslo]
puts copyright, "I still need " +
     "to visit the " +
     "following cities:",
     cities - visited 
*/


Comments (0)

Skip to main content