Welcome to part 1 of the ScriptCS series
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 |
|
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
|
|
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
|
|
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
|
|
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 |
|
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 |
|
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 |
|
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 |
|
Now that we have the required assemblies available, run the command scriptcs Start.csx
.
1 2 3 4 5 6 7 8 |
|
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.
Git
To install manually using git, go to your packages directory and run the command:
1
|
|
If you want to update the scriptcs-sublime package, simply run this command from inside the scriptcs-sublime directory:
1
|
|
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