dotnet/roslyn

Add Script property to InteractiveScriptGlobals and CommandLineScriptGlobals that returns a ScriptContext object

Open

#5,979 opened on Oct 14, 2015

View on GitHub
 (17 comments) (0 reactions) (0 assignees)C# (20,414 stars) (4,257 forks)batch import
Area-InteractiveInteractive-ScriptingLogichelp wanted

Description

public class CommandLineScriptGlobals 
{
   public ScriptContext Script { get; }

   public CommandLineScriptGlobals(..., ScriptContext context)
}

public class InteractiveScriptGlobals 
{
   public ScriptContext Script { get; }

   public InteractiveScriptGlobals(..., ScriptContext context)
}

public class ScriptContext 
{
   public string GetPath([CallerFilePath]string path = null) { get; }
   public string GetDirectory([CallerFilePath]string path = null) { get; }

   public ScriptContext(string path)
}

The Script property would thus be available to scripts:

foreach (var arg in Script.Args) 
   Console.Writeline(File.ReadAllText(Path.Combine(Script.GetDirectory(), arg)));
...

In future we can add more features to this context:

class ScriptContext 
{
    Task<object> EvalAsync(string code, object globals);
}

F# has a similar concept: "fsi" object:

image

Contributor guide