Disclaimer: I am not the first person to create a tool for C# scripting. There are a long list of projects from too many people to name, and I respect all of them A big part of why I did this was also to mess with Roslyn.

Learnings from node

For the past 18 months I’ve been living mostly in text editors and script getting fully absorbed in node.js. Recently I started to think a lot about my C# development experience and if there are things I could bring back from my learning’s with node / JavaScript

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.

On first glance it might seem like this is just oil and water with C#.

Thanks to Roslyn and Nuget however I no longer think that’s the case.

What is Roslyn?

Roslyn is a compiler as a service. It gives you an API that lets you compile and analyze code on the fly. Additionally it gives you hooks to dynamically refactor and generate code. Beyond that it also includes really powerful scripting support allowing you to author applications and components as loose C# scripts. You don’t need a class if you don’t want one and you don’t need to compile. And you don’t need to use Visual Studio, you can host Roslyn in your own applications and components and it’s even got nuget packages.. You can read more on Roslyn at MSDN or in a nice post from Filip on Roslyn and Web API

Roslyn is a breath of fresh air in terms of it’s simplicity. If you have worked with the existing horrendous mechanisms for doing this like the Codedom you are going to love Roslyn. You can literally be up and running with Roslyn having it compile scripts for you in a matter of minutes.

How does Roslyn help?

I can cross off three bullets

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.

Now yes it does compile, but that’s all taken care of for me, I don’t have to think about it.

Packages vs Assemblies

For the last bullet I looked to nuget. The nuget command line provides an easy way to install packages right from the command line into the same folder as a set of scripts. However, the challenge is how to make those scripts see those packages? Today Nuget works great with a csproj and within Visual Studio or using MS Build. But what if you don’t have Visual Studio or a csproj? Well when you install packages using the cmd line you get folders for each package that at some level actually do contain binaries. So, if you can find those binaries and get them loaded up into Rosyln, you are golden. Yes this doesn’t get rid of assemblies, but I don’t have to manage them individually, I just install packages.

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.

Roslyn + Nuget = scriptcs

Having crossed off all my bullets, I then decided to spike a little with Roslyn and out came Scriptcs: https://github.com/glennblock/scriptcs

scriptcs is a little command line tool which lets you have the following work flow.

You author your “app” as a .csx file or csharp script in your favorite editor. There’s no project.

You add dependencies to your app by simply installing nuget packages from the command line. By convention the dlls within those packages will be loaded up. No need for references as the idea is if it’s there you want it.

You add GAC references or other dll references (not nuget packages) by using the “r:” Roslyn syntax, i.e.”r: System.Net”.

You run your app using “scriptcs” passing the csx file.

For example here is a snippet of setting up a web api host.

Back to reality, though the future looks bright.

At this point I would say this is just an experiment. I am not claiming you can build an enterprise app with this or anything like that. I am using this more as an exploration to understand what a c# script experience from outside VS could look like.

From here there’s a lot more places to go. One would be allowing you to have includes of other csx files as you probably don’t want your whole app in a single file! Beyond that Roslyn offers a ton of rewriting, AST analysis or even auto-refactoring capabilities that I am sure can be leveraged in scriptcs but are not.

Seeing this definitely makes me excited about where things can go in the future. I’d love to hear your thoughts.