public static void RubyRepl()
        {
            AcadOutputStream io_stream = new AcadOutputStream();
            var engine = Ruby.CreateEngine();
            engine.Runtime.IO.SetOutput(io_stream, System.Text.Encoding.ASCII);

            var scope = engine.CreateScope();
            var exception_services = engine.GetService<ExceptionOperations>();

            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            PromptStringOptions opts = new PromptStringOptions(" ");
            opts.AllowSpaces = true;
            PromptResult res;
            string line = "";

            while (true)
            {
                try
                {
                    ed.WriteMessage("\nrb> ");
                    res = ed.GetString(opts);
                    if (res.Status == PromptStatus.OK)
                    {
                        line = res.StringResult;
                        line += "\n";
                    }
                    else break;
                    if (line == "\n") break;
                    if (line == "#exit\n") break;
                    ScriptSource source = read_code(line, engine, scope);
                    source.Execute(scope);
                }
                catch (System.Exception e)
                {
                    ed.WriteMessage("\nError: {0}", e.Message);
                }
            }
        }
Exemple #2
0
        private static bool runRspec(string file)
        {
            // If the file exists, let's load and execute it
            bool ret = System.IO.File.Exists(file);

            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            if (ret)
            {
                try
                {
                    AcadOutputStream io_stream = new AcadOutputStream();
                    var engine = Ruby.CreateEngine();

                    //redirect the stdout
                    engine.Runtime.IO.SetOutput(io_stream, System.Text.Encoding.ASCII);
                    engine.Runtime.IO.SetErrorOutput(io_stream, System.Text.Encoding.ASCII);

                    List<string> search_paths = CommandsAndFunctions.read_search_paths();

                    engine.SetSearchPaths(search_paths);

                    //read the AutoCAD requires
                    List<string> acad_requires = CommandsAndFunctions.read_autocad_requires();
                    foreach (string acad_require in acad_requires)
                    {
                        engine.RequireRubyFile(acad_require);
                    }

                    string code = @"require 'rubygems';
                                    $0='c:/ironruby-0.9.0/lib/IronRuby/gems/1.8/bin/spec'
                                    ARGV << '" + file + @"' << '-c' << '-f' << 'nested' ;
                                    gem 'rspec', '>=0';
                                    load 'spec'; ";
                    var scope = engine.CreateScope();
                    ed.WriteMessage("\n...Beginning Tests\n");

                    var exception_services = engine.GetService<ExceptionOperations>();

                    ScriptSource interactive_code = engine.CreateScriptSourceFromString(code, SourceCodeKind.Statements);
                    ScriptCodeParseResult props = interactive_code.GetCodeProperties();
                    if (props == ScriptCodeParseResult.Complete)
                        interactive_code.Execute(scope);
                }
                //catch the SyntaxError so we can show the file name and line number
                catch (Microsoft.Scripting.SyntaxErrorException se)
                {
                    ed.WriteMessage("\nProblem executing script: {0}:{1}\n::{2}", se.Message, se.Line, se.SourcePath);
                }
                catch (System.Exception ex)
                {
                    if (ex.Message == "exit")
                    {
                        //don't do anyting because, for some reason, rspec raise an Exit exception
                        // when it finishes cleanly
                        ed.WriteMessage("\n");

                    }
                    else
                    {
                        ed.WriteMessage("\nThere was a problem executing script: {0}", ex.Message);
                    }
                }
            }
            return ret;
        }
        private static bool AddRubyCommands(string file)
        {
            // If the file exists, let's load and execute it
            bool ret = System.IO.File.Exists(file);
            if (ret)
            {
                try
                {
                    AcadOutputStream io_stream = new AcadOutputStream();

                   //redirect the stdout so we can output info during loading
                    ScriptEngine engine = Ruby.CreateEngine();
                    engine.Runtime.IO.SetOutput(io_stream, System.Text.Encoding.ASCII);
                    engine.Runtime.IO.SetErrorOutput(io_stream, System.Text.Encoding.ASCII);

                    ScriptScope scope = engine.CreateScope();

                    //read the searh paths
                    engine.SetSearchPaths(read_search_paths());

                    //read the AutoCAD requires
                    List<string> acad_requires = read_autocad_requires();
                    foreach (string acad_require in acad_requires)
                    {
                        engine.RequireRubyFile(acad_require);
                    }

                    //read the entire file - syntax errors will be caught here and
                    //SyntaxErrorException raised
                     engine.ExecuteFile(file,scope);

                    //execute the Ruby code to register the desired commands with AutoCAD
                    var code = @"Ai =  Autodesk::AutoCAD::Internal; Aiu = Autodesk::AutoCAD::Internal::Utils;
                                  added = []; @exclude ||= [];
                                  new_commands = private_methods(false) - %w(initialize method_missing) - @exclude;
                                  new_commands.reject!{|y|  method(y).arity != 0}
                                  new_commands.each{ |x| Aiu.RemoveCommand('rbcmds', x);
                                        cc= Ai::CommandCallback.new(method(x));
                                        Aiu.AddCommand('rbcmds', x, x,  Ar::CommandFlags.Modal, cc);
                                        added << x}
                                  puts added.inspect";
                    var new_result = engine.Execute(code, scope);
                 }
                //catch the SyntaxError so we can show the file name and line number
                catch (Microsoft.Scripting.SyntaxErrorException se)
                {
                    ed.WriteMessage("\nProblem executing script: {0}, on line {1} of {2}", se.Message, se.Line, se.SourcePath);
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage("\nProblem executing script: {0}", ex.Message);
                }
            }
            return ret;
        }