scriptcs

Scripting made easy using C#

Lunch break session February 24th 2014 at Trifork, Aarhus. 

by Martin Öbrink-Hansen / @martinobrink

so what is it?

  • OSS project enabling easy C# scripting experience
  • Write + execute scripts / REPL
  • Low ceremony, lightweight
  • Use your favorite text editor - VS *not* required
  • No explicit script compilation - just run them
  • No project/solution files - more code, less meta
  • NuGet integration
  • node.js-like extensibility model (script packs)

history

Started *almost* a year ago:


scriptcs on github

Location

https://github.com/scriptcs/scriptcs

Project Coordinators

  • Glenn Block (@gblock)
  • Justin Rusbatch (@jrusbatch)
  • Filip Wojcieszyn (@filip_woj)

Core committers

  • Damian Schenkelman (@dschenkelman)
  • Kristian Hellang (@khellang)

Vanity metrics:

900+ commits

35 contributors

775 stars

160 forks

Getting started

Chocolatey install: 

(apt-get/homebrew for windows)


                    @powershell -NoProfile -ExecutionPolicy Unrestricted -Command "iex ((New-Object Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin
                    

Scriptcs install (using chocolatey):


                    cinst scriptcs
                    

Demo: repl + Helloworld.csx


script code semantics

  • no top-level class/Main method
  • no script namespaces
  • global functions allowed
  • MODULES, ASSEMBLIES LOADED AUTOMATICALLY/BY CONVENTION
  • #load directive to load script from script
  • #r directive to reference an assembly from script
  • SCRIPT PACKS ACCESSIBLE THROUGH REQUIRE<T>

Scriptcs.request script pack (1/2)

using ...
namespace ScriptCs.Request
{
    public class Request : IScriptPackContext
    {
        public HttpResponseMessage PostJson<T>(string url, T objectToPost) where T : class
        {
            var httpClient = new HttpClient();
            var bodyContent = new StringContent(JsonConvert.SerializeObject(objectToPost), Encoding.UTF8, "application/json");
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            return httpClient.PostAsync(url, bodyContent).Result;
        }
    }
}
    

SCRIPTCS.REQUEST SCRIPT PACK (2/2)

namespace ScriptCs.Request
{
    public class ScriptPack : IScriptPack
    {
        IScriptPackContext IScriptPack.GetContext()
        {
            //context to be used in scripts
            return new Request();
        }

        void IScriptPack.Initialize(IScriptPackSession session)
        {
            session.AddReference("System.Net.Http");
            session.ImportNamespace("System.Net.Http");
            session.ImportNamespace("Newtonsoft.Json");
        }

        void IScriptPack.Terminate()
        {
            //Cleanup any resources here
        }
    }
}

demo: send push notification


demo: syst login server


the future of scriptcs


  • scripted script packs (more info here)
  • deeper integration with Visual studio
  • ...?

THE END

  • scriptcs: http://scriptcs.net
  • scriptcs (GitHub): https://github.com/scriptcs/scriptcs
  • Writing Script Packs: http://blog.martindoms.com/2013/05/14/building-scriptcs-script-pack/
  • People to follow on Twitter: @scriptcsnet, @gblock, @filip_woj

scriptcs

By Martin Öbrink-Hansen