Getting Parameters in ScriptCS from a .Net Application

118 views
Skip to first unread message

Jens Milbredt

unread,
Aug 3, 2016, 6:46:10 AM8/3/16
to scriptcs
Hello together,

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
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. How exactly can I get the Parameters from the ScriptExecutor-Class?

With kind regards,
Jens Milbredt

Glenn Block

unread,
Aug 3, 2016, 3:50:24 PM8/3/16
to Jens Milbredt, scriptcs
Hi Jens

ScriptArgs cannot pass objects, that is for passing simple command line args. There's a few different ways to pass in state, but the most common way if you are hosting is to use a custom ScriptHost which can expose ambient properties into the script.

I recently helped one of our folks in the community do this, and he created a sample showing how it can be done / how state can be injected.


Also if you have questions, we have a slack room: https://scriptcs-slack.herokuapp.com/

If you join the #hosting channel, that is where these types of discussions happen.
Reply all
Reply to author
Forward
0 new messages