Using the CodeGen API
As an alternative to using the CodeGen command-line interface, you might prefer to integrate CodeGen more tightly into your development environment by writing custom tools or utilities to generate code.
To make this possible CodeGen now provides a .NET API that you can code directly against in order to drive the functionality of CodeGen.
The main classes in the CodeGen API are:
•CodeGen.Engine.CodeGenTaskSet
This is a very simple example of using the CodeGen API. This code segment is essentially the same as using the command line:
codegen -s CUSTOMER -t DataClass -r -v
Here's the sample code:
;;Create a new task setdata taskset, @CodeGenTaskSet, new CodeGenTaskSet()
taskset.LoggingLevel = LoggingLevel.Verbose
;;Create a task and define what it shold do
data task, @CodeGenTask, new CodeGenTask()
task.Structures.Add("CUSTOMER")
task.Templates.Add("DataClass")
task.ReplaceFiles = true
;;Add the task to the task set
taskset.Tasks.Add(task)
;;Create a code generator and tell it about the task set
data generator, @CodeGenerator, new CodeGenerator(taskset)
;;Generate the code
generator.GenerateCode()
;;Did it work?
if (taskset.Complete) then
;;Good to go
else
;;Something failed!
You won't see anything happening when you execute this code because the CodeGen API doesn't implement any UI. But if you wanted to see the messages that are generated as the task set it processed you could register an event handler method against either taskset.Messages.CollectionChanged or task.Messages.CollectionChanged and monitor / report on the messages as they are generated. For example if you were in a console application you could log messages to standard out like this:
lambda messageFromTaskSet(sender, e)
begin
if (e.Action==NotifyCollectionChangedAction.Add)
begin
data message, String
foreach message in e.NewItems
if (message!=^null)
Console.WriteLine(message)
end
end
;;Listen for messages from the taskset as it processes
taskset.Messages.CollectionChanged += messageFromTaskSet
Copyright © 2012 Synergex International, Inc.