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 in the download for JavaScript (Js) and XML (Xs).
Background
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.
Using the Code
(CHM documentation is available from Tony's Wibbles.)
In its simplest form, it is a way to join a bunch of string
s together so that each is written on a new line:
Script script = Sb.Script(
"line 1;",
"line 2;",
"line 3;"
);
string result = script.Render();
Console.WriteLine(result);
It results in the following output:
line 1;
line 2;
line 3;
However there's a lot more to it. Scripts don't just understand string
s but accept any type of object. In particular, objects based on an IScriptItem
are specifically designed to work with scripts and can be written 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);
This 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 a 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 itself. 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 rendered 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.
History
- 25th August, 2008: Initial version 1.0.0
- 12th October, 2008: Source file updated (version 1.0.1)