scriptcs – Living on the edge in C# without a project on the wings of Roslyn and Nuget

MP900430605[1] 

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?

image 

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.

MP900403166[1]

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.

This entry was posted in coding, rosyln. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Joe Kampschmidt

    I am just discovering scriptcs and love it. These are the kinds of projects that make me optimistic about the future of the .NET stack.

  • http://ivonna.biz/blog.aspx Artëm Smirnov

    Truth is, you don’t need a project file, but then your command for compilation becomes very long, and you’ll want to keep it in a file, voila, you have a project file of your own.

  • Lukas Bühler

    Nice, I love it! In a similar vein we just released cshell.net to quickly write scripts and run them in a REPL. But we chose a middle way of having it all run in some sort of editor to support a more integrated REPL environment. It might be nice to use CShell as a code editor (includes C# code completion) and then if you wanna run a script once it’s finished use ScriptCS separately. I’m inspired to add some NuGet integration like ScriptCS.

  • BigStone

    新生命C#文件脚本引擎v1.2正式版(用C#代替vbs/bat写脚本)
    http://www.newlifex.com/showtopic-369.aspx

    我们几年前就用脚本引擎ScriptEngine了。
    微软这个团队什么时候做的?

  • http://twitter.com/gblock Glenn Block

    Francisco, glad you found the “right” version. It started in my repo but then it moved to the scriptcs org as it became more serious.
    You should not need to create a packages folder anymore.

  • http://www.facebook.com/francisco.noriega Francisco Noriega

    Real cool stuff! It did take me awhile though to get a simple Console.WriteLine(“hello world”); running, since I wasn’t using any package yet, however you NEED to have a “packages” folder or it crashes

    EDIT:
    Nevermind, that was in your branch, I downloaded the main branch and it is quite different there :)

  • http://twitter.com/gblock Glenn Block

    Agreed, and I am hoping this project can influence that thinking. BTW, let me say again your Sublime Text plugin for ScriptCS is AWESOME and appreciated!

  • http://twitter.com/gblock Glenn Block

    I am glad!

  • http://twitter.com/gblock Glenn Block

    Update on this. It looks we’re very close to having a step through debugging story thanks to @dschenkelman. Check here to watch the story unfold: https://github.com/scriptcs/scriptcs/issues/68

  • http://twitter.com/gblock Glenn Block

    Great point, debugging today sucks! However Rosyln can create DLLs so there might be a path to compiling an attaching with VS.

  • Tudor Turcu

    About “No IDE requirement, you can just use a text editor” – all fine, also in C# you can code in Notepad – the question is: how do you debug your code without an IDE and only with a text editor?

  • http://twitter.com/SuperJMN José Manuel Nieto

    I’m going to love this! 

  • http://twitter.com/follesoe Jonas Follesø

    Cool – and should deff be a scenario Microsoft  could support in the future. This style of development is already  supported in F# – and something I often do when experimenting/trying out things (fsx script files + NuGet packages + #r for references + it supports includes).