A few questions about CodeSmith
I have recently started studying CodeSmith and I have a few questions. I would like to make a template with 4 blocks. Each block will be selected by the user.
- How can I set the text block (function) to selecting user?
- How can I move blocks in separate files?
For example there is a template
using System;
publ开发者_开发百科ic class Hello3
{
public static void Main(string[] args)
{
Blocl 1
Blocl 2
Blocl 3
Blocl 4
}
}
Each of these blocks should be selected by the user. Each block is stored in a separate file. Each block is a function. The output of one block enters to the input of another block.
P.S. Sorry for my bad english.
You could use a string property to set the name of a template or generated value. Then you would just render this string content during generation time.
Another option would be to create an enum that names different Code Blocks. Then inside of your template you could render a sub template or return some static text.
You could also create a custom dropdown list that allows you to choose a CodeSmith template. All of this logic would need to happen in a UITypeEditor and you would need to return a CodeTemplate using the API. This is a lot tougher than the first or second option.
I don't know how to do it using CodeSmith, but you would be better off using a "building-blocks" approach. AtomWeaver offers a way to build a code generator by smaller parts, called "Atoms". These Atoms are Templates that you can combine together.
In you case, I would build an Atom Template called cs_class:
The Template's Exec Code would be:
code([[
using System;
public class Hello3
{
public static void Main(string[] args)
{
{{code_blocks}}
}
}
]])
Notice the {{code_blocks}} marker. Another Template will put some code there.
Then, create the cs_code_block Atom Template. Use this code:
For the Admin Section:
under("cs_class")
For the Exec Section:
cursor("code_blocks")
code([[
<put user code here>
]])
The under() command helps you build an interactive modeling environment in AtomWeaver. Now, your user can create a model with one cs_class Atom, and then he can add as many cs_code_block Atoms as he wish. Executing the model will generate the desired code.
AtomWeaver lets you evolve your models and code generators. It's easy to start with a few lines and then grow it to obtain complete generators.
I know this is a very, very simplistic example of what you can build with AtomWeaver, but it's just to give you a quick idea of what you can accomplish.
精彩评论