ScriptConsole is a multiplatform C# REPL (Read-Eval-Print-Loop) with a graphical interface. It can be used to quickly test C# code snippets or to run scripts without creating a full application.
It is written using .NET Core, and is available for Mac, Windows and Linux. The GUI uses the Avalonia Framework.
ScriptConsole is released under a GPLv3 license.
Along with the ScriptConsole executable, a library called ScriptConsoleLibrary is also available, which makes it possible to easily include a C# REPL into another Avalonia application.
ScriptConsole does not have any dependencies, thus you essentially need to download and run the executable.
Download ScriptConsole-win-x64.exe
.
Download ScriptConsole-linux-x64
. You may need to mark the file as executable, e.g.:
wget https://github.com/arklumpus/ScriptConsole/releases/latest/download/ScriptConsole-linux-x64
chmod +x ScriptConsole-linux-x64
Download ScriptConsole-mac-x64.dmg
. Open the file and drag the ScriptConsole program to the Applications folder. If you receive a complaint about the program being damaged when you try to open it, you may need to mark the file as executable. To do this, open a terminal and type:
chmod +x /Applications/ScriptConsole.app/Contents/MacOs/ScriptConsole
And then retry.
To include the library in your project, you will need the ScriptConsoleLibrary NuGet package.
ScriptConsole is based on the Roslyn Scripting API, thus all standard features of C# 8.0 are available; in addition to this, the following commands are available:
#load "Path\to\script.cs"
- Loads a C# script file and executes the commands therein. Please note that the backslashes (
\
) in the path to the file are not escaped. Also note that the line does not end with a semicolon.
#r "Name or Path\To\Assembly.dll"
- Loads an assembly based on the name or the path to the assembly file. Please note that the backslashes (
\
) in the path to the file are not escaped. Also note that the line does not end with a semicolon.
Ans
- Returns an object containing the result of the last evaluation. Please note that this is boxed into an object, thus it may have to be unboxed before using it. For example:
While instead:
> "test" < "test" > Ans.ToUpper(); < CompilationErrorException: 'object' does not contain a definition for 'ToLower' and no extension method 'ToLower' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
> "test" < "test" > ((string)Ans).ToUpper(); < "TEST"
Str(object obj, string filter = null, bool showEverything = false, Type context = null)
Str<T>(string filter = null, bool showEverything = false, Type context = null)
Str(Type tp = null, string @namespace = null, string filter = null, bool showEverything = false, Type context = null)
- Shows a tree displaying the members of a certain
Type
. These overloads do essentially the same thing, but allow this method to be called in different ways; for example,Str("test");
,Str<string>();
andStr(typeof(string));
will all result in the same output.- Arguments:
object obj
: only used to determine itsType
, e.g.Str("foo");
andStr("bar");
will result in the same output.string filter
: filter the output, only including members that contain this stringshowEverything
: include members that are not accessible from the current context (see below)Type context
:Type
used to determine member accessibility. For example, if a member is declared asprotected
in aType
T
, it will only be accessible ifcontext
isT
or inherits fromT
.Type tp
:Type
whose members are to be shownstring @namespace
: used to show the members of a Namespace. For example,Str(@namespace: "System")
will show the members of theSystem
namespace.
- Arguments:
Clear()
- Clears the output console. The current status is preserved (i.e. all variables declared, methods defined etc are still available). The command history is also preserved.
Reset()
- Completely re-initialises the environment. This is effectively the same as closing and restarting the application. The command history is also erased.
Once you have installed the NuGet package, you can include the control ScriptConsoleControl
in your project.
To do this from the XAML designer, you will need to add an XML namespace declaration, and then include the control, e.g. :
<Window xmlns="https://github.com/avaloniaui"
...
xmlns:sc="clr-namespace:ScriptConsoleLibrary;assembly=ScriptConsoleLibrary"
...>
<sc:ScriptConsoleControl Name="scc"></sc:ScriptConsoleControl>
</Window>
This will build the control using the default constructor (see below).
public ScriptConsoleControl()
- Default constructor when adding a
ScriptConsoleControl
through XAML; it loads automatically the following assembly references in the script context:This list is accessible asMicrosoft.CodeAnalysis, Microsoft.CodeAnalysis.CSharp, Microsoft.CodeAnalysis.CSharp.Scripting, Microsoft.CodeAnalysis.Scripting, mscorlib, netstandard, ScriptConsoleLibrary, System.AppContext, System.Collections, System.Collections.Concurrent, System.Collections.Immutable, System.Collections.NonGeneric, System.Collections.Specialized, System.ComponentModel, System.ComponentModel.Primitives, System.ComponentModel.TypeConverter, System.Console, System.Diagnostics.Debug, System.Diagnostics.Tracing, System.Drawing.Primitives, System.Globalization, System.IO, System.IO.FileSystem, System.IO.MemoryMappedFiles, System.Linq, System.Linq.Expressions, System.Memory, System.ObjectModel, System.Private.CoreLib, System.Private.Uri, System.Private.Xml, System.Private.Xml.Linq, System.Reactive, System.Reflection, System.Reflection.Emit.ILGeneration, System.Reflection.Emit.Lightweight, System.Reflection.Metadata, System.Reflection.Primitives, System.Resources.ResourceManager, System.Runtime, System.Runtime.CompilerServices.Unsafe, System.Runtime.Extensions, System.Runtime.InteropServices, System.Runtime.InteropServices.RuntimeInformation, System.Runtime.Loader, System.Security.Cryptography.Algorithms, System.Security.Cryptography.Primitives, System.Text.Encoding.Extensions, System.Threading, System.Threading.Tasks, System.Threading.Tasks.Parallel, System.Threading.Thread, System.Threading.Timer, System.Xml.ReaderWriter, System.Xml.XDocument
ScriptConsoleControl.CoreLoadedAssemblies
.
public ScriptConsoleControl(string[] preloadedAssemblies)
- Using this constructor, only the assemblies whose name (e.g.
System.Linq
) is contained inpreloadedAssemblies
will be loaded into the script context. Note that these assemblies must already be loaded in the currentAppDomain
. To load assemblies that are not in the currentAppDomain
, you need to use the#r
directive (which can be invoked from code into the script context using theRunCommand
method (see below).
public void CleanUp()
- This method releases resources held by an instance of
ScriptConsoleControl
. This should be called every time aScriptConsoleControl
finishes its useful lifetime (e.g. when the containing window is closed), otherwise the garbage collector will not collect it. If you instantiate multipleScriptConsoleControl
s, you must call this method before a new instance is created, otherwise you will not be able to call it on the previous instance and you will not be able to release memory. Note that you cannot have multipleScriptConsoleControl
s alive at the same time!
public object RunCommand(string command, bool addToOutput)
- This method invokes a command in the script context and returns the result of the command.
- Arguments:
string command
: the command to be invoked. It can also be a#load
or#r
directive (see above), which makes it possible to load into the script context an Assembly which is not loaded in the currentAppDomain
.bool addToOutput
: display the command and its output as if it had been entered by the user in the command line.
- Return value: the result of the command, boxed as an object.
- Arguments:
To be able to compile ScriptConsole from source, you will need to install the .NET Core 3.0 SDK for your operating system.
You can use Microsoft Visual Studio to compile the program. The following instructions will cover compiling ScriptConsole from the command line, instead.
First of all, you will need to download the ScriptConsole source code: ScriptConsole.tar.gz and extract it somewhere.
Open a command-line window in the folder where you have extracted the source code, and type:
Build <Target>
Where <Target>
can be one of Win-x64
, Linux-x64
or Mac-x64
depending on which platform you wish to generate executables for.
In the Release folder and in the appropriate subfolder for the target platform you selected, you will find the compiled program.
Open a terminal in the folder where you have extracted the source code, and type:
./Build.sh <Target>
Where <Target>
can be one of Win-x64
, Linux-x64
or Mac-x64
depending on which platform you wish to generate executables for.
In the Release folder and in the appropriate subfolder for the target platform you selected, you will find the compiled program.
If you receive an error about permissions being denied, try typing chmod +x BuildAll.sh
first.