Scott Smith

Personal site and blog

Getting Started With ScriptCS

So what is ScriptCS?

ScriptCS allows you to use C# as a scripting language. It harnesses the power of Roslyn and NuGet to enable you to write .NET applications with your favorite editor.

ScriptCS allows you to do something like this:

1
2
3
4
5
6
7
8
copy con Sample.csx
var x = 2;
var y = 3;
var z = x * y;
Console.WriteLine(z);

scriptcs Sample.csx
6

ScriptCS also offers its own REPL providing an interactive experience. You can install nuget packages, type some code, and have it execute instantly! As of writing this post, this functionality is in the dev branch and not yet available on the master branch. If you want learn more, head over to this blog post.

Installation

There are two ways to install ScriptCS:

  • Get the source from GitHub, build it, and add scriptcs.exe to your path
  • Use Chocolatey to download and install it automatically

For simplicity, we will be using the Chocolatey Nuget method of installation. Copy and paste the following command into your console. This will execute a PowerShell script that will download Chocolatey, place it on your local drive, and configure your path. Make sure to reload your session so your new path is loaded.

1
@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin

Next execute the following command in your console. This will download ScriptCS, place it on your local drive, and configure your path. Make sure to reload your session again so your new path is loaded.

1
cinst scriptcs

Note: If you are uncomfortable running these commands found on some random blog, you can either go to the Chocolatey site for installation instructions or get the ScriptCS source and build it yourself. Don’t worry, I won’t be offended.

Exploring one of the samples

We will be exploring the RavenDB sample.

First, we need to get the samples from GitHub. The easiest way is to clone the repo:

1
git clone https://github.com/scriptcs/scriptcs-samples.git

Change into the directory scriptcs-samples/ravendb. If you look in this directory you will notice there is a packages.config file. This file defines the packages dependencies for this sample. Since we do not have the packages yet (no packages directory) we need to get them. This is done by executing the command scriptcs -install.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ scriptcs -install
Installing packages...
Installed: RavenDB.Embedded 2.0.2261.0
Installation completed successfully.
Copying assemblies to bin folder...
Copied: Raven.Client.Embedded.dll.
Copied: Esent.Interop.dll.
Copied: GeoAPI.dll.
Copied: ICSharpCode.NRefactory.CSharp.dll.
Copied: ICSharpCode.NRefactory.dll.
Copied: Jint.Raven.dll.
Copied: Lucene.Net.Contrib.FastVectorHighlighter.dll.
Copied: Lucene.Net.Contrib.Spatial.NTS.dll.
Copied: Lucene.Net.dll.
Copied: Mono.Cecil.dll.
Copied: NetTopologySuite.dll.
Copied: PowerCollections.dll.
Copied: Raven.Abstractions.dll.
Copied: Raven.Database.dll.
Copied: Spatial4n.Core.NTS.dll.
Copied: Raven.Abstractions.dll.
Copied: Raven.Client.Lightweight.dll.
Restore completed successfully.

This will instruct NuGet to download all the packages defined in the packages.config. Now that we have all the necessary packages, we can run the sample with scriptcs start.csx.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ scriptcs start.csx
Found assembly reference: Esent.Interop.dll
Found assembly reference: GeoAPI.dll
Found assembly reference: ICSharpCode.NRefactory.CSharp.dll
Found assembly reference: ICSharpCode.NRefactory.dll
Found assembly reference: Jint.Raven.dll
Found assembly reference: Lucene.Net.Contrib.FastVectorHighlighter.dll
Found assembly reference: Lucene.Net.Contrib.Spatial.NTS.dll
Found assembly reference: Lucene.Net.dll
Found assembly reference: Mono.Cecil.dll
Found assembly reference: NetTopologySuite.dll
Found assembly reference: PowerCollections.dll
Found assembly reference: Raven.Abstractions.dll
Found assembly reference: Raven.Client.Embedded.dll
Found assembly reference: Raven.Client.Lightweight.dll
Found assembly reference: Raven.Database.dll
Found assembly reference: Spatial4n.Core.NTS.dll
Starting RavenDB Server, Please Be Patient...
RavenDB Started, Listening On http://localhost:8080

Just like that we have RavenDB started and listing on port 8080.

Write our own

Let’s get our environment setup to create our own example. Create a directory and name it whatever you want. We will call ours Sample. Change into the newly created directory and create a file called Start.csx. Open this file in whatever editor you want to and enter the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.IO;
using System.Web.Http;
using System.Web.Http.SelfHost;

var address = "http://localhost:8080";
var conf = new HttpSelfHostConfiguration(new Uri(address));
conf.Routes.MapHttpRoute(name: "DefaultApi",
   routeTemplate: "api/{controller}/{id}",
   defaults: new { id = RouteParameter.Optional }
);

var server = new HttpSelfHostServer(conf);
server.OpenAsync().Wait();
Console.WriteLine("Listening...");
Console.ReadKey();

You may notice we have references to System.Web.Http and System.Web.Http.SelfHost but those assemblies are not currently available. Try it out and see for yourself by running the command scriptcs Start.csx. The error you are receiving is because those assemblies are not available. To install them we need to execute the command scriptcs -install Microsoft.AspNet.WebApi.SelfHost.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
scriptcs -install Microsoft.AspNet.WebApi.SelfHost
Installing packages...
Installed: Microsoft.AspNet.WebApi.SelfHost
Installation completed successfully.
Copying assemblies to bin folder...
Copied: System.Net.Http.Formatting.dll.
Copied: System.Web.Http.dll.
Copied: System.Web.Http.SelfHost.dll.
Copied: System.Net.Http.dll.
Copied: System.Net.Http.WebRequest.dll.
Copied: Newtonsoft.Json.dll.
Restore completed successfully.
Initiated saving packages into packages.config...
Added Microsoft.AspNet.WebApi.Client, Version 4.0.20710.0, .NET 4.0
Added Microsoft.AspNet.WebApi.Core, Version 4.0.20710.0, .NET 4.0
Added Microsoft.AspNet.WebApi.SelfHost, Version 4.0.20918.0, .NET 4.0
Added Microsoft.Net.Http, Version 2.0.20710.0, .NET 4.5
Added Newtonsoft.Json, Version 4.5.11, .NET 4.0
Packages.config successfully created!

Now that we have the required assemblies available, run the command scriptcs Start.csx.

1
2
3
4
5
6
7
8
scriptcs start.csx
Found assembly reference: Newtonsoft.Json.dll
Found assembly reference: System.Net.Http.dll
Found assembly reference: System.Net.Http.Formatting.dll
Found assembly reference: System.Net.Http.WebRequest.dll
Found assembly reference: System.Web.Http.dll
Found assembly reference: System.Web.Http.SelfHost.dll
Listening...

If you open up your browser and navigate to http://localhost:8080 you should see that our ScriptCS program is running and listening for connections on port 8080.

Sublime Text 2

There is a Sublime Text 2 package available to turn on syntax highlighting for CSX files. You can either install it manually via git or via the Package Control Plugin.

Package Control Plugin

The easiest way is to install the package is using the Package Control Plugin.

Package Control Plugin

Git

To install manually using git, go to your packages directory and run the command:

1
git clone https://github.com/scriptcs/scriptcs-sublime.git

If you want to update the scriptcs-sublime package, simply run this command from inside the scriptcs-sublime directory:

1
git pull origin master

Along with syntax highlighting, the package will also provide a build system. To build, use Cmd+B on Mac or Ctrl+B on Windows. If you encounter any compilation errors, you can hit F4 to navigate to the error.

Summary

ScriptCS offers some great functionality for those wanting to experience C# as a scripting language. With the power of Roslyn and NuGet, ScriptCS is poised to become a very powerful tool.

Resources

Project site
GitHub repository
Official examples
Sublime Text 2 package

Comments