Rebol has a powerfull range of tools for generating code. So why not integrate it with IDEs like Visual Studio ? Yes, I know it’s no more a secret that Visual Studio Professional and above supports its own Templating Engine (T4) but not Visual Studio Express Edition. Also Rebol can create DSL much more easily than any other existing tools on the market today that’s why Rebol can complement T4.
In this part, we will see how to create a simple Winform template (the same created by Visual Studio Designer). In next part, we’ll see how to make Visual Studio add the generated files to its Project Explorer automatically.
First create a new project, then a new directory (call it RebolCodeGenerator for example) and an empty file with .r extension (Rebol’s extension):

To create our Rebol template for Visual Studio, create a new form with Visual Studio Designer, copy Form1.cs and Form1.Designer.cs codes into NewForm.r then replace namespace and form name by
<%Namespace%>
<%Form-Name%>
Surround each template with braces and assign them to template and template2 variables like this:
Rebol[]
template: {
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace <%Namespace%>
{
public partial class <%Form-Name%> : Form
{
public <%Form-Name%>()
{
InitializeComponent();
}
}
}
}
template2: {
namespace <%Namespace%>
{
partial class <%Form-Name%>
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "<%Form-Name%>";
}
#endregion
}
}
}
Double-clik on NewForm.r to execute Rebol. For unknow reason, it won’t work unless you insert a blank line like shown on the picture below:

You can now add this code which requests the variables content at the beginning of the script:
Namespace: ask "Namespace: "
Form-Name: ask "Form Name: "
And this one at the end of the script to generate the files:
;we should go up one directory level ("../") to be in project directory
file1: to-rebol-file rejoin ["../" Form-Name ".cs"]
file2: to-rebol-file rejoin ["../" Form-Name ".Designer.cs"]
write file1 build-markup template
write file2 build-markup template2
So that the whole script should look like this:
Rebol[]
Namespace: ask "Namespace: "
Form-Name: ask "Form Name: "
template: {
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace <%Namespace%>
{
public partial class <%Form-Name%> : Form
{
public <%Form-Name%>()
{
InitializeComponent();
}
}
}
}
template2: {
namespace <%Namespace%>
{
partial class <%Form-Name%>
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "<%Form-Name%>";
}
#endregion
}
}
}
file1: to-rebol-file rejoin ["../" Form-Name ".cs"]
file2: to-rebol-file rejoin ["../" Form-Name ".Designer.cs"]
write file1 build-markup template
write file2 build-markup template2
As usual we use the build-markup function (which internally use the parse function) to render the final source code. If you now execute the script by double-clicking on it, it will generate the two Cshap files within your Project directory. You can then add it manually to your Project Explorer (next time, we’ll see how to do so automatically).
















Tags: Build-Markup, C#, Code Generation, Templating, Visual Studio