BT

ScriptCS: Turning C# into a Scripting Language

by Abel Avram on Apr 25, 2013 |

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..

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Interesting... by Mark N

I just love how people state advantages without truly weighing the Pros and Cons and not realizing that some "advantages" are dis-advantages.

Re: Interesting... by Silver S

Could you please elaborate? I think its obvious it depends on the context - I think you have some other context in mind that this solution maybe does not (want to) target? Thank you!

Re: Interesting... by Mark N

Sorry, i was talking about the Node.js comments. LOL.

It seems this tool over comes some of the issues of a typical scripting language and seems like a good option for scripting - meaning you are not writing an application.

FYI, we have been able to do this same sort of thing for years with, uh, another major platform. ;)

Some of the information here is incorrect by Glenn Block

Hi Abel

Thanks for doing a write up on scriptcs. I need to clarify a few things, this is NOT a Microsoft project and I am not leading a team. This is a side project that I started but which is now being run by the community with my two other coordinators Justin Rusbatch [@jrusbtach] (author of Compilify.net) and Filip Wojceizyn (@filp_woj).

Please correct the article and email me if you need more: gblock@microsoft.com.

Thanks!

More clarification by Glenn Block

There's no team at MS on their spare time. It's just me :-)

Re: Interesting... by Glenn Block

Mark

This is not trying to compete with node. I have been living and breathing node for the past two years and love it. This is about bringing a nodejs "style" of development for C# developers. There have been several advancements in .NET including nuget and Roslyn that scriptcs brings together to offer a reasonable scripting experience. For a c# developer it allows them to leverage the skills they know to write scripts. It also allows them to grow their script into a full fledged C# project.

Re: Interesting... by Mark N

Glenn, I wasn't saying (or thinking) that it was competing with node. I totally understand why you'd do this for C#. Its nice to be able to have this option when I do .NET development.

My only point is that developers tend to only see the pros and not the cons or don't realize their pros might be cons. As usual, I probably should have not said anything. So, back to the main point of this post which is your cool project.

Re: Interesting... by Glenn Block

Hey Mark

You are absolutely right that there are pros and cons. I probably misinterpreted your comment so apologies there.

Thanks
Glenn

muddy the water by Binoj Antony

PowerShell is a first class scripting language and runs on .NET, its better to invest time and learn PowerShell (Learning curve is not so steep anyways) for scripting. Although I love c#, this type of shortcuts just muddy the water.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

9 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT