Well, after many days of hard labor, much progress has been made on the Bono.Generation namespace. This namespace supports classes that can generate code in any output language that supports ICodeDomProvider. The end-goal is to create a fully-featured templating engine that works much like CodeSmith's. The namespace is based largely on Refly written by Jonathan de Halleux in 2004 -- with enhancements. Take the following code example:
1 ' Creates a new Namespace .. End Namespace block.
2 Dim ns As NamespaceDeclaration = New NamespaceDeclaration("Bono.Console.TemplateTest")
3
4 ' Creates a class declaration in that namespace
5 Dim cs As ClassDeclaration = ns.AddClass("TestGeneratedClass")
6
7 ' Sets the class to inherit from Bono.Templates.CodeTemplate
8 cs.Parent = New TypeTypeDeclaration(GetType(Bono.Templates.CodeTemplate))
9 cs.OutputType = ClassOutputType.ClassAndInterface
10
11 ' Add a field to this class. Private _name as String
12 Dim fname As FieldDeclaration = cs.AddField(GetType(System.String), "_name")
13
14 ' Add a single constructor that takes an integer parameter.
15 Dim constructor As ConstructorDeclaration = cs.AddConstructor()
16
17 ' Add the parameter to the constructor
18 Dim pname As ParameterDeclaration = constructor.Signature.AddParam(GetType(System.String), "name", True)
19
20 ' Add the assignmet (Me._name = name)
21 constructor.Body.AddAssign(Expr.This.Field(fname), Expr.Arg(pname))
22
23 ' Add a comment for the constructor
24 constructor.Doc.Summary.AddText("This class must be initialized with a Name property set. It's written this way for testing purposes.")
25
26 ' Add a Public Property Name to this class.
27 Dim nameProperty As PropertyDeclaration = cs.AddProperty(fname, True, True, True)
28
29 ' Set the scope of this property to Public
30 nameProperty.Attributes = CodeDom.MemberAttributes.Public
31
32 ' Time to compile!
33 Dim generator As New CodeGenerator
34
35 ' Set the provider to the Visual Basic code provider.
36 generator.Provider = CodeGenerator.VbProvider
37
38 ' Generate the class.
39 generator.GenerateCode("C:\Projects\Test", ns)
This code will generate a sample class with a constructor, and a property. Similarily, the Bono.Generation namespace houses classes for creating methods with signatures, and generating code with any standardized expression or statement you can imagine. The wrapper's syntax is way simpler than the CodeDom native syntax. We will be using this library to finish the code template and generation wizards in Bono 2.0. Here is the output of the above code snippet.
1 Imports System
2
3 Namespace TemplateTest
4
5 '''<summary />
6 '''<remarks />
7 Public Class TestGeneratedClass
8 Inherits Bono.Templates.CodeTemplate
9 Implements ITestGeneratedClass
10
11 '''<summary />
12 '''<remarks />
13 Private _name As String
14
15 '''<summary>This class must be initialized with a Name property set. It's written this way for testing purposes.</summary>
16 '''<remarks />
17 Public Sub New(ByVal name As String)
18 MyBase.New
19 Me._name = name
20 End Sub
21
22 '''<summary />
23 '''<remarks />
24 Public Overridable Property Name() As String Implements ITestGeneratedClass.Name
25 Get
26 Return Me._name
27 End Get
28 Set(ByVal value As String)
29 If (value Is Nothing) Then
30 Throw New System.ArgumentNullException
31 End If
32 Me._name = value
33 End Set
34 End Property
35 End Class
36
37 '''<summary />
38 '''<remarks />
39 Public Interface ITestGeneratedClass
40
41 '''<summary />
42 '''<remarks />
43 '''<exception cref="System.ArgumentNullException">set property, value is a null reference</exception>
44 Property Name() As String
45 End Interface
46 End Namespace
I look foward to hearing all your feedback on this.
Brandon Kelly
President,
estatic.org, Inc.