.netscriptcs

Getting Parameters in ScriptCS from a .Net Application


I have a little beginner problem with ScriptCS and can't find an answer to this. We want to use ScriptCS to dynamically extend a hosted application. For this we need to pass parameters into the Script and getting values back from the Script. I have currently an example console applicatin in which I'm testing the script execution:

Program.cs

using ScriptCs.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScriptCs.Contracts;
using ScriptCs;
using ScriptCs.Engine.Mono;
using ScriptCs.Engine.Roslyn;
using System.Diagnostics;
namespace ScriptCsPoc
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            var console = (IConsole)new ScriptConsole();
            var logProvider = new ColoredConsoleLogProvider(LogLevel.Info, console);

            var builder = new ScriptServicesBuilder(console, logProvider);

            SetEngine(builder);
            var services = builder.Build();

            var executor = (ScriptExecutor)services.Executor;
            executor.Initialize(Enumerable.Empty<string>(), Enumerable.Empty<IScriptPack>(), "Guten Tag aus der Konsole");
            ExecuteLooseScript(executor);
            ExecuteFile(executor);
            ExecuteFileFromCommandLine();
        }

        public static void ExecuteLooseScript(ScriptExecutor executor)
        {
            var script = @"Console.WriteLine(""Hello from scriptcs"")";
            executor.ExecuteScript(script);
        }

        public static void ExecuteFile(ScriptExecutor executor)
        {

            ScriptResult result = executor.Execute("HelloWorld.csx", "parameter");
            Console.WriteLine("Done Script");
        }


        public static void ExecuteFileFromCommandLine()
        {

            Process cmd = new Process();
            cmd.StartInfo.FileName = "cmd.exe";
            cmd.StartInfo.RedirectStandardInput = true;
            cmd.StartInfo.RedirectStandardOutput = true;
            cmd.StartInfo.CreateNoWindow = true;
            cmd.StartInfo.UseShellExecute = false;
            cmd.Start();

            cmd.StandardInput.WriteLine("scriptcs HelloWorld.csx -- blubber");
            cmd.StandardInput.Flush();
            cmd.StandardInput.Close();
            cmd.WaitForExit();
            string standardOut = cmd.StandardOutput.ReadToEnd();
            Console.WriteLine("Done Script");
        }
        static void SetEngine(ScriptServicesBuilder builder)
        {
            var useMono = Type.GetType("Mono.Runtime") != null;
            if (useMono)
            {
                builder.ScriptEngine<MonoScriptEngine>();
            }
            else {
                builder.ScriptEngine<CSharpScriptEngine>();
            }
        }
    }
}

HelloWorld.csx

#load child/greeter.csx
var greeter = new Greeter();
if (Env.ScriptArgs.Count > 0)
    greeter.Greet(Env.ScriptArgs[0]);
else
    greeter.Greet("Inside Script");

greeter.csx

public class Greeter {
    public void Greet(string message) {
        Console.WriteLine(message);
    }
}

I'm able to call the script within a call of the Console (ExecuteFileFromCommandLine()), but if I'm using an instance of the ScriptExecutor-Class, the passed arguments aren't available in the Env.ScriptArgs. Does anybody know, how exactly can I get the Parameters from the ScriptExecutor-Class?

With kind regards


Solution

  • Okay, the solution to this goes over an additional project:

    https://github.com/swn1/scriptcsambient

    With the Scriptcsambient it is possible to easy give parameters into a ScriptCS-Script and get values back from it.

    With kind regards