We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

ScriptCS: Exploring the C# Scripting Experience with scriptcs

00:00

Formal Metadata

Title
ScriptCS: Exploring the C# Scripting Experience with scriptcs
Title of Series
Number of Parts
150
Author
License
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this
Identifiers
Publisher
Release Date
Language

Content Metadata

Subject Area
Genre
Abstract
While Visual Studio, and other IDEs, are powerful tools, they can sometimes hinder productivity more than they promote it. You don’t always need, or want, to create a whole new project and solution. scriptcs is a new and exciting open source project that takes the overhead out of writing C# applications. Write and execute C# script files, written with a relaxed syntax that eliminates the need for namespaces and top-level classes, with only a simple text editor and your command prompt. Use NuGet to manage your dependencies and to install Script Packs, which further reduce the amount of code you need to write to setup your favorite frameworks. Inthis session I'll show you how to get started with scriptcs.
Projective planeInformation technology consultingCodeCoordinate systemCore dumpIntegrated development environmentScripting languageWeb browserMetropolitan area networkSoftware developerCompilerPhysical systemBitXMLUML
Cartesian coordinate systemUtility softwareVideo game consolePhysical systemProjective planeGoodness of fitDivisorSoftware testing
Visualization (computer graphics)Gaussian eliminationBookmark (World Wide Web)BlogWritingScripting languageProjective planeText editorC sharpTwitterCodeLine (geometry)Right angleSoftware bugNichtlineares GleichungssystemCompilerComputer fileBlock (periodic table)Service (economics)Presentation of a groupOpen sourceRevision control
James Waddell Alexander IIBlock (periodic table)Projective planeRight angleRevision controlScripting languageElectronic mailing list40 (number)Computer animation
Revision controlLine (geometry)Scripting languageRegular graphComputer fileHome pageCodeFunction (mathematics)Social classExtension (kinesiology)Cartesian coordinate systemExpressionContent (media)Visualization (computer graphics)Application service providerInstallation artFunctional (mathematics)WebsiteAuthorizationPower (physics)Letterpress printingMetropolitan area networkLoop (music)Area
Electronic data interchangeDirection (geometry)Scripting languageDifferent (Kate Ryan album)Structural loadWritingComputer animation
Computer fileScripting languageFunctional (mathematics)CodeMereologySocial classModule (mathematics)Right angleStatement (computer science)Level (video gaming)NamespaceEndliche ModelltheorieProjective planeComputer animation
Scripting languageSocial classDirectory serviceComputer fileStructural loadTrailStatement (computer science)Object (grammar)Functional (mathematics)Computer animation
Assembly languageDirection (geometry)Scripting languageDifferent (Kate Ryan album)Directory serviceStructural loadVisualization (computer graphics)WindowProjective planeGame controllerSocial classPhysical systemAdditionComputer fileAudiovisualisierungBlogSource codeComputer animation
Scripting languageRepetitionText editorVirtual machineDirectory serviceScripting languageMultiplication signComputer fileBoilerplate (text)CodeIntegrated development environmentSoftware frameworkOverhead (computing)Utility softwarePresentation of a groupCASE <Informatik>Uniform resource locatorWritingC sharpForm (programming)Visualization (computer graphics)Video game consoleText editorFilm editingComputer animation
Software frameworkScripting languageSampling (statistics)WeightFrame problemDemo (music)WebsiteRepository (publishing)Data storage deviceRight angleComputer fileJSON
Inclusion mapServer (computing)Scripting languageComputer animation
Dedekind cutFermat's Last TheoremParsingDecision tree learningLucas sequenceBit rateScripting languageServer (computing)Group actionCodeStructural loadEinbettung <Mathematik>Line (geometry)Computer animation
Video game consoleScripting languageInstallation artConfiguration spaceHoaxComputer fileComputer animation
InformationNetwork topologyInterior (topology)BlogError messageInstallation artAbstractionDemo (music)Directory serviceSoftware developerLine (geometry)CodeScripting languageServer (computing)Bit rate1 (number)Source codeComputer animation
Scripting languageAssembly languageStatement (computer science)Function (mathematics)Hacker (term)Boilerplate (text)Scripting languageIntegrated development environmentInterface (computing)Visualization (computer graphics)Object (grammar)Web 2.0Assembly languageType theoryFunctional (mathematics)AuthorizationAdditionAudiovisualisierungMachine visionRight angleSystem callStatement (computer science)Sampling (statistics)Computer animation
Scripting languageInterface (computing)Visualization (computer graphics)Source code
SineInterface (computing)Context awarenessFunctional (mathematics)Scripting languageNamespaceAssembly languageFamilyComputer animation
Pointer (computer programming)Term (mathematics)Default (computer science)Scripting languageGame controllerInterface (computing)CodeService (economics)Electronic mailing listContext awarenessBoilerplate (text)Web 2.0Projective planeCASE <Informatik>Server (computing)Message passingRootImplementationAssembly languageDynamical systemSource code
Address spaceConfiguration spaceService (economics)Server (computing)Video game consoleGame controllerPhysical systemDefault (computer science)WritingScripting languageGame controllerCodeWeb 2.0Server (computing)Text editorLine (geometry)NumberTerm (mathematics)Default (computer science)Computer file2 (number)Projective planeRoutingComputer clusterWebcastSet (mathematics)Frame problemLocal ringJSON
Scripting languageCommon Language InfrastructureRepetitionVisual systemCore dumpStandard deviationVisualization (computer graphics)Web 2.0Human migrationService (economics)QuicksortRight angleWindowAuthorizationComputer fileRegular graphIntegrated development environmentSoftware frameworkStructural loadScripting languageCartesian coordinate systemCodeCuboidDirection (geometry)Statement (computer science)Utility softwareBus (computing)PrototypeLevel (video gaming)Physical systemTotal S.A.Core dumpBuildingMultiplication signComputer clusterAudiovisualisierungProjective planePower (physics)Computer animation
Set (mathematics)Fermat's Last TheoremAreaChi-squared distributionFingerprintKeyboard shortcutStreaming mediaVisual systemStructural loadAssembly languageDirection (geometry)Home pageLine (geometry)Visualization (computer graphics)Domain nameWindowInteractive televisionScripting languageBinary fileIntegrated development environmentRight angleTerm (mathematics)Social classOpen setPlug-in (computing)Mobile appInstance (computer science)Internet service providerOpen sourceCore dumpLink (knot theory)Software repositoryCodePhysical systemWebsiteSource code
Gamma functionScripting languageSet (mathematics)Installation artUnit testingSoftware frameworkExecution unitStructural loadLibrary (computing)Web 2.0Directory serviceSource codeComputer animation
XMLUML
Transcript: English(auto-generated)
All right, thanks for coming. This is pretty impressive, considering John Skates talking in another room. So thanks. My name is Justin Russbatch. I created compileify.net, which is an online C-sharp IDE where you can actually execute your C-sharp code
in your browser, basically. I'm also with Philip Wojciechian and Glenn Block, a coordinator for the ScriptCS project. Yep. My name is Christian Helland. I work as a consultant here in Norway, in Stavanger.
And I've been a core contributor to the ScriptCS project. So if you're anything like me and many other developers, you probably have a folder somewhere in your system that looks a little bit like that, just from years and years of building utility applications or just starting a new console application
to test a refactor or try a new concept. Up until now, there really hasn't been a good way around this. You always really needed to just create a new console application or something, this monolithic solution, just so that you could do something very, very simple.
So we just recently changed that by releasing a project that we call ScriptCS. And what ScriptCS is is a lightweight scripting experience for C Sharp. We enable you to write C Sharp, a really relaxed syntax of C Sharp using your favorite text editor
and plain text files, and then execute them from your command line. So we completely eliminate Visual Studio from the equation, if you so choose. And we try to make it as easy as possible for you to do what you need to do in as few lines of code as possible. The project was started by Glenn Block.
I'm not sure, probably heard his name before. He works for Microsoft. I believe he's worked in the WCF team before, and he's currently involved with the Azure Mobile Services. He was exploring Node.js a lot in his work at Microsoft, and he had to come back to C Sharp for a few things,
and he got really annoyed with the whole experience and how heavy it was to work in C Sharp. So he came up with this project, and it started off as a blog post, and a few tweets later, it was a full-blown thing. So it's based on the Roslyn compiler, which offers a scripting experience in itself,
but we do a few things, and we wanna show you through this presentation. We do a few things to enhance that experience for you and make it even easier to write C Sharp in script. We are an open-source project. We're on GitHub. We've been very active. This project was started four months ago, actually.
We just released version 0.5, which is actually bugs, so don't install it, and we're doing all right. This is a list of the total contributors so far. We actually have 525 watchers and 29 unique contributors, over 100 forks, and about 142 pull requests,
as of probably a week ago. So things are really moving along, and it's really awesome to see the contributions that are currently coming from the community. It's really easy to install ScriptCS. We're actually available on Chocolaty. Have any of you heard of Chocolaty, anybody?
Yeah? Oh, nice, wow, that's impressive. So for those of you who haven't, Chocolaty is a NuGet-based tool that allows you to install actual applications through NuGet, instead of simply DLLs and content for your ASP.NET sites.
It installs them, well, the author of the Chocolaty package decides where to install them, but you basically install it with one line of PowerShell, and then after that, you can just do a C-inst command, stands for C install, to install new applications,
or a C update to update them, just like you would with NuGet in Visual Studio. So we distribute ourselves through that, and actually demo, or yeah, you're gonna demo that now. Yeah, I'll be Justin's code monkey for today, so if you would like to switch. I'd like to show you just how easy it is to install it.
Yeah, so I got my command line. I already got Chocolaty installed. If you're wondering how you can install Chocolaty, just go to their homepage. So I just go C-inst script cs,
and then it will automatically go use the NuGet APIs and go and fetch the latest package and install it, add it to the path, and then you're ready to go. So there, it's installed with all the dependencies, and then I can jump right into using script cs.
There's two ways of using script cs. You have the REPL, which stands for read, evil, print, loop, and you get the script files.
So first off, I'm going to show off the REPL functionality. So the REPL lets you execute things C-sharp on the command line by just typing whatever C-sharp syntax. So 2 plus 2, I can define classes.
So here I'm defining a class, and now this class is defined. So I can instantiate it,
and then I can output the person just in. So basically you can write C-sharp on the fly, and for every expression you evaluate,
it will be saved in the loop. So, yep, it will be saved in the session. Shall I show the scripts? Yeah, yeah. All right, so that's one aspect of it, and then you also have script files. So script files is just a regular text file
with a CSX extension. So I'll just create a new CSX file, and then I can write C-sharp in here.
So there is some things that make scripts special is that you have different directives. So you can, for example, we have a greeting, no, a load directive. So if you have multiple scripts, then you can load different scripts,
like you can modularize your scripts. So if you're doing anything more complicated than a simple few statements, you can basically split your code up into classes and move each class into its own file if you so choose.
You can define functions and statements, actually. In the top level, this is actually a very interesting part of the Roslyn project, is that the scripting API that it provides, like I said, gives you a relaxed syntax. So you're not restricted and forced to declaring a namespace,
then declaring a class, and then being able to declare a method. You can write whatever you want right at the top level, statements, functions, whatever. But for more complicated things, when you feel like you need to split it out, you can simply define your classes in one file and then load them into the next,
as he's going to demonstrate now. Alright, so here I defined a simple class called Greeter in one file, and then I have another file which loads the Greeter script, and then new up the Greeter object, and just call the method on it.
So I can execute that, just writing script cs, start.csx. From the right directory. From the right directory.
So there you go. You can modulize your scripts and load different scripts into another script. You also have the R directive, which lets you load your custom assemblies or gacked assemblies.
So that's useful in situations where perhaps you have, say, controllers or classes that you already have compiled through some Visual Studio project that you've already created in Visual Studio, and you just want to do something very simple with them,
so you can spin up a script cs script and actually interact with those assemblies. So, in addition to, like you said, assemblies that are found in the GAC. Notice how I'm using Sublime Text. We've actually got a great contribution from Jonas Volluser.
He wrote a package for Sublime Text to let you get syntax highlighting for CSX files, and also there's this build system which lets you press Ctrl-B and run scripts right from within Sublime. So if I do install package,
and then script cs, it will download the package and install it. And if I close and open the window again, you see now I have full syntax highlighting, and I can also press Ctrl-B. No, I can't.
Let's try to open this again. Fair enough, fair enough. At least one failure. There is build support. I tested it prior to the presentation,
and it worked on my machine. So yeah, that is a great contribution, and like we said, the beauty here is really that you can use any text editor. You don't need to use Sublime. You're not tied to Visual Studio because we've eliminated all the fat. We cut all the fat from your code.
With the utility console applications, every time you create one of those, you're creating 15 files in, I believe, seven different directories. So you usually only create them to edit your CS file. You don't go edit any of the other files that Visual Studio just created for you. You just want to write your code. So we've cut all that out
and allow you now to just do it how you want and as simply as you want. So we provide a very lightweight experience. As you've seen, you can install scripts yesterday from Chocolaty. You can execute C Sharp directly from the command line
for really simple use cases when you just want to, I don't know, see what 2 plus 2 is. And we allow you to write C Sharp in script form as well in any text editor that you want. We took it a step further. We realized that a lot of the boilerplate that we were trying to cut
was mostly related to actually your frameworks. So not all the frameworks are really, really meld well with the scripting environment. They're meant for full C Sharp. So we were able to eliminate a lot of stuff, a lot of overhead by eliminating Visual Studio, but we also wanted to make it easy for you
to eliminate overhead with your favorite frameworks. So we integrated NuGet. And what this allows you to do is pull down those frameworks and actually reference them from the script. So you can do more simple things than adding 2 plus 2 and defining greeter.
There are several samples on our website, scriptcs.net. We have a samples repository. You can find a whole bunch of frameworks demoed there, but I believe you will now show you an example of installing frameworks for your scripts.
All right. So I have a folder here with a simple CSX file, a script. And I'm thinking I'm going to show you just running RavenDB from a script. So this is the whole script to just define a new document store, initialize it, and then start the browser
and see the embedded HTTP server for RavenDB. This is basically the whole script. But to do this, you need the RavenDB embedded package. So if I just navigate into the RavenDB folder,
and then I can just do scriptcs install, RavenDB embedded. So this will use the NuGet APIs and go to NuGet and get the RavenDB package and add it into your folder.
So after doing this, I should be able to run the script. So there. It's installed. And now I can do a scriptcs start CSX. Let's hope it starts.
So now it's starting up the RavenDB server. And there, hopefully, it still will load. There you have RavenDB running. And it's all in 12 lines of code. So it's a pretty slimmed-down C-sharp experience.
So as he demonstrated, you can actually install individual NuGet packages straight from your console with the dash install and then the package name. But we also support the packages config file,
which NuGet adds to your solution if you've enabled package restore. In fact, we generate that for you. So when he ran scriptcs install, we created the packages config and placed it right there right next to your script. What that lets you do, actually, is take those two files, your script and the packages config file,
and transfer them to anybody that you'd like. And when they get the file, what they'll do is simply run scriptcs-install in that directory without specifying a package, and we'll scan that packages.config file and restore them all for you. So it makes it really easy to distribute your scriptcs scripts to other people.
So if I delete my packages folder here, when I'm in the directory, I can just call scriptcs-install without specifying any packages. And we'll go through the packages config and install the packages.
Eventually. Eventually. So that's really great. One thing you haven't seen in this demo so far is Visual Studio at all. And we're already hosting a full RavenDB server. And it took, what, 12 lines of code you said?
Yep. So we like that. Can you switch it over? All right. So as I mentioned in the beginning, scriptcs was inspired by Node.js, Glenn's experiences with Node.js.
And if you think about it, scriptcs is kind of like Node.js and NuGet acts as NPM for C Sharp developers. So that's really what we're taking advantage of there. We've also made it really easy to extend your scripts, or to extend scriptcs and really bootstrap the environment so that you can reduce that extra boilerplate that's related to your framework.
And we do this through what we call script hacks. And script hacks allow you, or assemblies that you right now build in Visual Studio, and they allow you to add using statements, reference additional assemblies,
and expose additional functionality for your scripts in a very concise and modular way. If you're familiar with the require.js, kind of how you use require.js, you call require, and it returns an object that you can then interact with.
We expose the same method, require, to your scripts. And when you call require, you give it the type of the script pack that you expect to get back. And the author of the script pack implements an interface that requires him to return an object. And whatever methods he places on that object,
whatever he exposes on that object, you can access through the object that's returned to your script after the require call. I believe we have a few examples. Yep, I can show the Web API sample. All right, so to create a script pack,
you still have to use Visual Studio. We're aiming for a solution where you can use scripts to create script packs. But as for now, we have to use Visual Studio to create a script pack. And it's really easy to do it. You have to add a NuGet.
Are you kidding me? This is why you shouldn't use Visual Studio. Let's try to restart Visual Studio. Okay, basically, you go off to NuGet, and you get a NuGet package called scriptcs.contracts that contains a couple of interfaces.
If we could just get Visual Studio up and running. Maybe you should just use Sublime. Okay, so when you install a NuGet package, you have to implement an interface called iscriptpack. It has three methods on it.
You have the getContext. The context is what's returned to the script. And that's where you expose your functionality. You have the initialize method, which passes in a session. And in this session, you can import namespaces. You can add references to GACT assemblies or other assemblies.
And finally, you have the terminate method, which is meant for doing cleanup. But the real magic happens in the context itself. And this is the implementation. You just have to implement iscriptpack context. This has no methods. It's just a marker interface.
And here, this is where you expose the methods to your script. So these methods can be called from the script. And it does a lot of boilerplate code for you. This example isn't necessarily a very complex example.
But it does some things like register a service for resolving controllers. I don't know how many of you know Web API very well. But by default, it doesn't pick up controllers from dynamic assemblies. And it does the root mapping for you.
And then it hosts the server as a self-host. So maybe we'll show the code. Sure. Can you bring that back up? The Web API? Yep. Maybe. OK. There it is. So as he said, each of those members, the createServer methods in this case,
are exposed to your script to call. So this is an example of a CSX project, I use that term very loosely, that hosts Web API using the self-host method. And that's basically, for those of you who aren't familiar with Web API, a way to host Web API without running IIS or interacting with IIS.
So you can see there are four files here. You have the packages config, which we've already covered. Start.csx, which is actually doing the work to spin up this server. Controllers, which is where you would place your controllers. We won't go into that because that's very standard code. And finally, script controller resolver,
which is the resolver that he had just mentioned that script pack is setting up for you. Now this is an example without the script pack. So this is what your script would look like, or your scripts would look like, if you were not using a script pack. When you add the script pack, they're distributed via NuGet,
and you would just install it like any other NuGet packages we've demonstrated. And the Web API script pack has a dependency on Web API, so that pulls it down for you as well, further reducing the number of steps you need to get started. But when you add the script pack, you go from this to this,
which is a huge savings. Eleven lines of code to spin up an entire server. And you now have the default routes configured. So you'd be able to go to localhost 8080 slash API and get to the first controller, the main controller. And you did this all through your console, and your text editor. And we've reduced a file,
because we've moved the script resolver out of the script and into this assembly that you can share across your projects. We have a bunch of script packs already available right now. A lot of frameworks are kind of picking up on this. We have one for Nancy, WPF. You can launch WPF applications from a script.
Web API, as we've shown, service stack and unit. So it's really great to see that authors are picking up on this. And feel free to contribute more. So what's next with ScriptCS? There are a few goals that we have. We'd like to do a pluggable CLI commands.
And what we're looking at there is... Actually, do you want to explain that one? The CLI commands. The throw you under the bus? Yeah. Not really. Oh, OK. We're not going to explain that one right now. Pluggable REPL commands.
Roslyn offers a few REPL commands that allow you to reset your session, and we'd like to expand on that. We'd like to allow you to define your own so that you could quickly bootstrap your REPL environment with just a simple one statement to set up whatever you want to set up. Export to Visual Studio solution is something that we started with
in the beginning of ScriptCS, but it didn't really get traction. The idea there is that you can start your project, start prototyping really simply in a simple text document, and then run a utility of some sort or a command that would allow you to create a full-blown Visual Studio solution so you can just start from simple and go with it.
You don't have to start over and copy all your code into the new file. That also helps because some of the code, like the load directives and the R directive, aren't compatible with standard C Sharp, so we'd like to make that migration to a full solution easier. Manu support's a huge one.
This has a lot of advantages, namely replacing PowerShell. It would offer cross-platform support, and it would just be really awesome if your scripts could just be copied to a non-Windows system and run as they do on a regular Windows box.
Scriptpacks as scripts, as you mentioned earlier, is also a cool idea. Like I said right now, you do need to dive into Visual Studio to get started with Scriptpacks so that you can define them and extend your scripts, but we'd like to make that possible without having to launch Visual Studio for the reasons that we have already demonstrated on the stage.
There's a few things that stand in our way, but we're definitely working towards it. And finally, an improved scriptcs.core API. I don't think we've mentioned this, but there's a scriptcs.core package out there on Nougat right now that if you add to your application, would allow you to host the scriptcs environment
directly in your application, so you don't need to use a command prompt. You could embed this in any application that you're building. We want to support that. We just don't have a working story for it yet, so it's very rough right now, and we'd really like to make it easier to do that.
So at this time, does anybody have any questions about Scriptcs? Yes? Cool. Oh, yeah? Just Chocolaty. All you need is the Chocolaty utility, and can you pull up the Chocolaty website? Just the home page.
So as you can see, this is the PowerShell command I was talking about. It's really just one line of code, and Chocolaty is on your system, and then all you need to do is scriptcs, and bam, it's in your path. You're ready to go. Yeah. Yeah, you can.
You could do it in LinkPad. LinkPad, I don't believe, supports the load directives and the R directives? No, LinkPad doesn't have any of these scripting, like you can load in other scripts or other, yeah, and there's also the Nougat aspect of everything,
pulling in packages and, yeah.
Well, we search for a bin folder, so when you start the REPL, yeah, when you start the REPL, it will load everything that's in the bin folder, so if you have custom assemblies with classes or whatever,
those will be automatically loaded into the app domain and will be usable from the REPL. Yeah. There we go. No, not yet. Not yet. It's open source, you can contribute it. Yeah, that is something that's often requested.
Yeah. Something like the, oh, to embed this in Visual Studio?
Yeah. Roslyn, if you choose to install Roslyn, which is currently in CTP, I think it's CTP3 right now, when you install that, it will actually add a C Sharp interactive window,
which is a REPL environment inside Visual Studio. You could create a Visual Studio plugin that uses ScriptCS.core to provide ScriptCS solution, but right now there isn't much differentiating the two
in terms of the interactive window in our REPL. So, anything else? We also have lots of examples. We have a separate repo, so if you go to our GitHub repo,
you'll find a bunch of examples for using different frameworks. We have Nancy running NUnit tests, Active Directory querying, the Web API hosts, WPF, SignalR library load.
So, there's a lot of examples if you want to take a look at that. Alright, that's all we have for you. So, thank you for attending, and I'm going to let you out early enough to catch the end of somebody else's talk.