ScriptCS enables developers to write C# applications using a simple text editor. Compilation is performed by Roslyn and package management by NuGet.
Glenn Block, Project Manager of the Windows Azure SDK team, started ScriptCS, a side project attempting to make C# a scripting language. While a developer can use his C# knowledge to write programs using a simple text editor, the compilation is done in Roslyn, Microsoft’s compiler-as-a-service. ScriptCS uses NuGet to discover packages it depends upon then uploads binaries. The Roslyn’s r:
syntax is used to add GAC or other DLL references.
If a file hello.csx
contains the next C# code line
Console.WriteLine("Hello World!");
then, running the command scriptcs hello.csx
results in printing the Hello World!
string at the console.
There is no need for namespaces nor class definitions for this example, and there is no project, no .obj
nor .exe
file generated. Roslyn does the compilation and ScriptCS executes the result.
Another more elaborate example is creating a Web API host:
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();
ScriptCS has a plug-in mechanism using the so called script packs as Block explains:
A script pack can offer up namespace imports, references, as well as objects which will be available to the script via the Require<T> API.
The main goal of a script pack is to make authoring scripts using frameworks easier.
As script packs can be installed via NuGet packages, they can easily be discovered and consumed.
Work is in progress to make ScriptCS work on Mono. Adding debugging capabilities to Roslyn is investigated. Sublime Text has created a plug-in for ScriptCS enabling syntax highlighting in a simple editor. Alternatively, Roslyn can be used to generate syntax highlighting in Visual Studio for .csx files.
Block lists the advantages of scripting C# based on his experience with Node.js:
- No projects, just script- One of the things I love about node.js is you don’t need a project. You can just jump in a folder start creating js files and go to town.
- No IDE requirement, you can just use a text editor.
- Packages over assemblies – In node, when you want to get something you use npm to download the packages.It’s super simple. You just have your app and your local node_modules folder and you are good to go.
- No compilation – This is a big one. With node, I just run node.exe and my app and it works. I don’t have to first create an executable to run, I just run.
All that is possible with Roslyn and NuGet. ScriptCS still deals with assemblies, but “but I don’t have to manage them individually, I just install packages.”
ScriptCS carries an Apache 2 license and it is currently not endorsed by Microsoft..