Using rScript to compile scripts

Documentation wrote using version 1.2

Using rScript to compile scripts is really simple. You will first need to instance the Object rScripting.CompileEngine, and set the Compiler property to use what compiler you are wanting to use. Once the engine knows what compiler you want to use, you can then invoke the Compile method. If you are going to use the standard C# compiler, than you won't need to worry about setting the Compiler property nor the ScriptExtension property as the engine will default to C# for you.
If you are using C#, then compiling an entire directory filled with scripts can be as simple as two lines of code!
            //Instance of the rScript Compile Engine that is used throughout the example application.
            rScripting.CompileEngine _CompileEngine = new rScripting.CompileEngine();

            //Compile our scripts.
            _CompileEngine.Compile(@"C:\Scripts");            


The following examples will demonstrate how to compile scripts from a directory, from a single file and from raw source code. They will also show how to check for errors in-case the compilation failed for any reason.

Whole Directory Compiling
            //Instance of the rScript Compile Engine that is used throughout the example application.
            rScripting.CompileEngine _CompileEngine = new rScripting.CompileEngine();
  
            //The directory that contains a collection of scripts.
            String scriptRepository = "Scripts";

            //Compile all scripts within that directory.
            Boolean isCompiled = _CompileEngine.Compile(scriptRepository);

            //Check for any compiler errors.
            if (!isCompiled)
            {
                Console.WriteLine(_CompileEngine.Errors);
            }


or...
            //Instance of the rScript Compile Engine that is used throughout the example application.
            rScripting.CompileEngine _CompileEngine = new rScripting.CompileEngine();
  
            //Compile all scripts within that directory.
            Boolean isCompiled = _CompileEngine.Compile(@"C:\Scripts");

            //Check for any compiler errors.
            if (!isCompiled)
            {
                Console.WriteLine(_CompileEngine.Errors);
            }


Single Script File Compiling
            //Instance of the rScript Compile Engine that is used throughout the example application.
            rScripting.CompileEngine _CompileEngine = new rScripting.CompileEngine();
  
            //The single script file that we will compile
            System.IO.FileInfo file = new System.IO.FileInfo(@"Scripts\Wheel.cs");

            //Compile our source script file. Script engine will perform file existance checking prior to compiling.
            Boolean isCompiled = _CompileEngine.Compile(file);

            //Check for any compiler errors.
            if (!isCompiled)
            {
                Console.WriteLine(_CompileEngine.Errors);
            }


or...
            //Instance of the rScript Compile Engine that is used throughout the example application.
            rScripting.CompileEngine _CompileEngine = new rScripting.CompileEngine();

            //Compile our source script file. Script engine will perform file existance checking prior to compiling.
            Boolean isCompiled = _CompileEngine.Compile(new System.IO.FileInfo(@"Scripts\Wheel.cs"));

            //Check for any compiler errors.
            if (!isCompiled)
            {
                Console.WriteLine(_CompileEngine.Errors);
            }


Raw Source Compiling
            //Instance of the rScript Compile Engine that is used throughout the example application.
            rScripting.CompileEngine _CompileEngine = new rScripting.CompileEngine();
  
            //Construct an array of strings with raw source code.
            String[] sourceCode = new String[] {"namespace SampleSource {\n" + 
                "public class SampleClass {" +
                "public int Size = 20; " +
                "public Double Width" +
                "{" +
                    "get" +
                    "{" +
                      "return 8.5;" +
                    "}" +
                "}" +  
                "public void GetTireBrand() {" + 
                    "System.Windows.Forms.MessageBox.Show(\"Super Tire Co.\");" +
                 "}" + 
                 "}" + 
                 "}"
            };
            
            //Compile the source code
            Boolean isCompiled = _CompileEngine.Compile(sourceCode);

            //Check for any compiler errors.
            if (!isCompiled)
            {
                Console.WriteLine(_CompileEngine.Errors);
            }


or...
{{
            //Instance of the rScript Compile Engine that is used throughout the example application.
            rScripting.CompileEngine _CompileEngine = new rScripting.CompileEngine();
  
           //Compile the source code
            Boolean isCompiled = _CompileEngine.Compile(textbox1.Text);

            //Check for any compiler errors.
            if (!isCompiled)
            {
                Console.WriteLine(_CompileEngine.Errors);
            }
}}

That's not to difficult right? If you want to write your scripts in Visual Basic, you can do so, you will just need to change two properties prior to invoking the Compile() method.

            _CompileEngine.Compiler = "VB";
            _CompileEngine.ScriptExtension = ".vb";


If you happen to have a custom compiler that implements the rScripting.ICompiler interface, then you can specify it as the CompileEngine.Compiler property. The compiler name must be the name of the Type. In the following example, we assume that the compiler Type that implements ICompiler is named SuperCompiler

            _CompileEngine.Compiler = "SuperCompiler";
            _CompileEngine.ScriptExtension = ".SuperFile";


Information regarding how to wrap your own compiler with the ICompiler interface is discussed in the Adding a Custom Compiler document.

Next, we will provide you with an example script that you can use. The script contains a Field, a Property and a method. Over the next couple of documentation pages, you will learn how to Invoke Compiled Methods and Access Compiled Fields & Properties.

Script
using System;

namespace RuntimeExample.Scripts
{
    public class Wheel
    {
        public Int32 Size = 20;

        public Double Width
        {
            get
            {
                return 8.5;
            }
        }

        public void GetTireBrand()
        {
            System.Windows.Forms.MessageBox.Show("Super Tire Co.");
        }
    }
}


The only assemblies referenced by default with the engine are System.dll, System.Core.dll and mscorlib.dll. If you need other assemblies referenced, you will need to add them. Luckily, adding references to assemblies are simple. They can be added via the AddAssemblyReference() method found in the CompileEngine. Just make sure that you add your reference's prior to compiling your scripts. If you are going to be following along by using our example script provided above, then you will need to make sure you add a reference to the System.Windows.Forms.dll file as shown below.

            //Setup our compiler engine
            rScripting.CompileEngine _CompileEngine = new rScripting.CompileEngine();

            //Add a reference to the windows forms dll as our script uses it for showing the MessageBox 
            _CompileEngine.AddAssemblyReference("System.Windows.Forms.dll");

            //Example of how to compile your raw source code
            _CompileEngine.Compile(@"C:\Scripts");


Finally, the engine only contains support for two different compilers. While by default, the compiler will assume all scripts extensions are .cs, you can change the extension if you need to, even if you're using C# as the compiler. While the VB compiler can compile VB scripts with a .cs extension, it is recommended that you change the extension to .vb. You may also change both C# and the VB script file extensions to anything custom that better suits your needs.
  • C#
    • _CompileEngine.Compiler = "C#";
    • _CompileEngine.Compiler = "CSharp";
    • _CompileEngine.ScriptExtension = ".csharp";
  • Visual Basic
    • _CompileEngine.Compiler = "VB";
    • _CompileEngine.Compiler = "VisualBasic";
    • _CompileEngine.ScriptExtension = ".vb";

Last edited Dec 12, 2010 at 3:10 PM by Scionwest, version 15

Comments

No comments yet.