Giter Site home page Giter Site logo

csharprepllib's Introduction

CSharpReplLib

Library to easily add a C# REPL feature

How to start

You can clone this library or download the CSharpReplLib NuGet package.

Create an instance of ScriptHandler.

ScriptHandler takes an optional execution context ( Func<Func<Task>,Task> ) as parameter, which allow to execute all REPL script asynchronously in an application context. For instance, when using WPF you can call :

var handler = new ScriptHandler(func => DispatcherAsync.Invoke(func).Task);

When using a Reactive Scheduler :

// Method extension to put in a static class ***
public static Task ScheduleAsyncWait(this IScheduler scheduler, Func<Task> funcAsync)
{
    return Observable.Return<Unit>(default)
        .ObserveOn(scheduler)
        .SelectMany(async _ => { await funcAsync(); return _; })
        .FirstAsync()
        .ToTask();
}
// ***

var handler = new ScriptHandler(func => myScheduler.ScheduleAsyncWait(func));

( if someone has a more elegant solution with IScheduler feel free to share)

You can also specify to the ScriptHandler which assemblies it can references and which global variables to use using the class extensions :

Adding assemblies

// .AddReferences(params Assembly[] references)

handler
    .AddReferences
    (
        Assembly.GetExecutingAssembly(), // current project assembly
        typeof(Newtonsoft.Json.JsonConverter).Assembly, // Newtonsoft.Json
        typeof(int).Assembly // mscorlib
    )
// .AddReferences(Assembly reference, bool includeReferencedAssemblies = false)

handler
    .AddReferences
    (
        Assembly.GetExecutingAssembly(), // current project assembly
        includeReferencedAssemblies : true // this will include all dependent assemblies
    )

Adding usings

You can also add usings later by simply executing "using System.Linq;"

// .AddUsings(params string[] usings)

handler
    .AddUsings
    (
        "System",
        "System.Text",
        "System.IO",
        "MyCurrentNameSpace"
    )

Adding globals

These globals will be directly accessible through your scripts

// .AddGlobal(params (string name, object value)[] globals)

Foo foo = new Foo();
Bar bar = new Bar();

handler
    .AddGlobals
    (
        (nameof(foo), foo),
        (nameof(bar), bar)
    )

Handling Execution

ScriptHandler has 2 events :

  • EventHandler<ScriptResult> ScriptResultReceived
  • EventHandler<ScriptRequest> ScriptExecuted

Example with ScriptResultReceived :

handler.ScriptResultReceived += handler_ScriptResultReceived;

private void ScriptHandler_ScriptResultReceived(object sender, ScriptHandler.ScriptResult e)
{
    StringBuilder str = new StringBuilder();

    if (e.IsError)
        str.Append("[ERROR] ");
    
    if (e.IsCancelled)
        str.Append("[CANCELLED] ");

    str.Append(e.Result);

    Console.WriteLine(e.ToString());
}

Executing code

To execute code, simply call the asynchronous method ExecuteCode.

// public async Task<bool> ExecuteCode(string code, CancellationToken token = default, IScriptWriter sender = null)

var success = await handler.ExecuteCode("1 + 1");

The executed code can be catch with ScriptExecuted and its result received with ScriptResultReceived.

At the first call, ExecuteCode will call the public method InitScript. This first call can be very long (a few seconds). It's advised to manually call InitScript earlier to avoid the waiting time. InitScript can be called in another thread.

Once InitScript has been called, you cannot modify the ScriptHandler (by calling AddReferences, AddUsings or AddGlobals), this will throw a NotSupportedException.

You can still however add usings by calling await handler.ExecuteCode("using System.Linq;").

Using Visual studio code

This library allows you to generate a temporary project and open Visual Studio Code to edit your script. Saving the script will call ExecuteCode.

Requirement

The CSharpReplLib.VSCode NuGet package.

You obviously need Visual Studio code installed on your machine with code accessible in your PATH.

Usage

Simply call :

VSCodeWriter vsCode;
ScriptHandler handler;

void OpenVsCode()
{
    vsCode = new VSCodeWriter();
    vsCode.Open(handler);
}

void Dispose()
{
    vsCode?.Dispose();
}

This has the big advantage to have access to all the Intellisense, colorization and the tons of features included in Visual Studio Code. You can also access your global values with intellisense.

Don't forget to call Dispose to delete the temporary files.

I might add other tools in the future.

csharprepllib's People

Contributors

yexcoffier avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

xyncgas

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.