Tony's Wibbles

GeekyClockWork Script Builder .Net

Introduction

ClockWork Script Builder is a .Net library (2.0) to make the building of scripts more readable and structured.

An extendible architecture lets developers add script building blocks (Script Items) for languages such as the ones provided for javascript (Js) and xml (Xs).

Motivation

Recently I started playing with ExtJs and I ended up needing to build large chunks of javascript from .Net code. I've never found a nice way to construct scripts in C# so this time I decided to write a library to help.

Demo

I've extended the project to include a demo that generates ExtJs JavaScript for the Northwind Database.  You can see the demo at An ASP.Net based ExtJs Demo using the ClockWork ScriptBuilder.

How to use it

In its simplest form it is a way to join a bunch of strings together so that each is writen on a new line...

 
Script script = Sb.Script( 
	"line 1;", 
	"line 2;", 
	"line 3;" 
); 
 
string result = script.Render(); 
 
Console.WriteLine(result); 

Results in the following output...

 
line 1; 
line 2; 
line 3; 

However theres a lot more to it. Scripts don't just understand strings but accept any type of object. In particular, objects based on an IScriptItem are specifically designed to work with scripts and can be writen by anyone to extend scripting abilities.

This example demonstrates the use of some core layout items.

 
Script script = Sb.Script(	 
	Sb.Line("1","2","3","4","5",";"), 
	Sb.Line("1","2","3","4","5",";"), 
	Sb.Indent( 
		Sb.Line("indented ","1","2","3","4","5",";"), 
		Sb.Line("indented ","1","2","3","4","5",";"), 
		Sb.Script( 
			Sb.Line("Some objects"), 
			Sb.Line("Number = ", 47), 
			Sb.Line("True = ", true), 
			Sb.Line("Now is = ", DateTime.Now) 
		)	 
	) 
); 
 
string result = script.Render(); 
 
Console.WriteLine(result); 

Results in the following output...

 
12345; 
12345; 
	indented 12345; 
	indented 12345; 
	Some objects 
	Number = 47 
	True = True 
	Now is = 23/08/2008 11:23:00 AM 

You will have noticed all the Script items are created using "Sb.". To make scripts easier to write and read I have created helper classes for each set of script items. They provide static methods that will create instances of script items for you.

Script Languages

I have currently developed the following sets of script items to help in writing scripts

Script Set Description Helper Class
Core Core Script Items to enable constructing formatted scripts ClockWork.ScriptBuilder.Sb
JavaScript Items to help construct standard Javascript elements such as Objects, Arrays, Lists, Functions, Calls, Blocks, Statements ClockWork.ScriptBuilder.JavaScript.Js
XML Items to construct basic xml nodes of Element, Attribute, Text and CData. Also supports direct writing to an XmlElement or XmlDocument ClockWork.ScriptBuilder.XmlScript.Xs
ExtJs Class and Component items designed to make it easier to write subclasses for ExtJs classes ClockWork.ScriptBuilder.JavaScript.ExtJs.ExtJs

 

Points of Interest

DOM & Rendering

All objects added to a script are stored as-is (like a DOM). The string representation of the script is only created when its Render method is called. Among other things, this lets you build the script in steps letting you structure your builder code.

If an IScriptItem is found while rendering then it will use its own Render method to render iteself. Other objects are rendered using ToString with an optional format provider.

Rendering is performed using a ScriptWriter which is passed into the Render methods. This object provides the ability to render to several destinations and also handles indentation.

ScriptLayout

Most items have the option to select a layout mode (Inline, InlineBlock, Block). The effects if the item will render across multiple lines.

ScriptIf

This item will do an if-then-else check at render time and then render the object that won.

A collection based ScriptItem (ScriptSet) will result in false if its collection is empty. Therefore you can decide if something is renderered based on the content of another item (all at render time).

Objects can implement the IScriptIfCondition interface if they wish to dynamically provide results for the if condition.

 

Licence

This libraries licence is based on the BSD licence. Generally speaking, anyone can use it but you can't sue me if it kills you.

Download

The library and documenation follow. It includes binaries, source and documentation.

DownloadDownloadClockWork-ScriptBuilder-1-0-1 DownloadDownloadClockWork-ScriptBuilder-Doc-1-0-1

Comments

Add Comment

Share ande Enjoy with Social Bookmarks...

Support Me

If you wish to support my work on this website including my software developement, then you can donate a few beers to keep me sane!

I can be easily influenced on what I will work on next ;-)

Thanks, Tony

Bookmark and Share Go
30 Nov Published " Southern Africa Truck Tour - Nomad Safari"
29 Nov Updated "Google Maps Location Finder"
Some improvements
16 Nov Updated "Google Maps Location Finder"
Added a downloadable version
15 Nov Published "On the road with Chris and Tony - Part I"
A tour of Mexico with a few stories
10 Nov Updated "ClockWork Firefox to KeePass Converter"
2.06 released
PicLens Logo Copyright ©2008 Anthony James McCreath. All Rights Reserved. Contact Me. Powered by GRUNT 2.1 Alpha