The CodeGen API
As an alternative to using the command-line interface a developer could integrate CodeGen more tightly into their development environment by writing custom tools or utilities to generate code, using the core CodeGen environment.
To make this possible CodeGen provides a .NET API that developers can code directly against in order to generate code. 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 = new CodeGenTaskSet()
taskset.LoggingLevel = LoggingLevel.Verbose
;;Create a task and define what it shold do
data task = 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 = 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 © 2018 Synergex International, Inc.