Code highlighting

Tuesday, November 24, 2009

SysCompilerTarget - compiling X++ code without showing the compilation window

I have been posed with a question about how to avoid showing the compilation window during compilation of some X++ code (When doing compilation from code, naturally).

So I decided to post a possible solution for this question, re-using one of my previous posts about ClassBuild class.

Basically, AOTcompile method on TreeNode class supports an optional flag, controlling the compilation output:
An integer that indicates whether output should be sent to the message box. If the value 1 is passed, no output is sent to the message box. The default value is 0. This parameter is optional.


So all we need to do now is change the compilation output target to Message Window. Looking at the SysCompilerSetup form code, it's rather easy to come up with the following code:


static void SysCompilerTargetExample(Args _args)
{
ClassBuild classBuild;
DictClass dictClass;
SysCompilerTarget targetOrig;
;
// Setting Compilation Target = Message Window
targetOrig = SysUserInfo::compilerTarget();
SysUserInfo::compilerTarget(SysCompilerTarget::MessageWindow);
SysCompilerOutput::setCompilerTarget(SysCompilerTarget::MessageWindow);
SysCompilerOutput::updateParm();

// Building the class with 1 method
classBuild = new ClassBuild("TRN_ClassBuild", false);
classBuild.addMethod("test", 'void test()\n{\n}');
classBuild.addSourceToMethod("test", @"
;
info('We created a Class and can call its methods');");

// The actual compilation. Note the _flag argument == 1
classBuild.classNode().AOTcompile(1);

// Call the class method to show that we are done and the code is compiled
dictClass = new DictClass(className2Id(classBuild.name()));
dictClass.callObject('test', dictClass.makeObject());

// Restoring Compilation Target to its original value
SysUserInfo::compilerTarget(targetOrig);
SysCompilerOutput::setCompilerTarget(targetOrig);
SysCompilerOutput::updateParm();
}


Don't forget to restore the original settings after the compilation is complete, as shown above.

6 comments:

  1. great!

    Thanks!
    Mariano Vicario

    ReplyDelete
  2. Great post! :)

    ReplyDelete
  3. Is it possible to prevent the message window from appearing at all? I don't know why every object I compile gets printed there. It's not like I don't know what object I just compile.

    ReplyDelete
  4. No, I don't think you can change whether the message window appears or not.
    But I guess I can agree with your comment, that it makes little sense to print each object compiled there.

    ReplyDelete
  5. i ran the code and this worked perfectly, however how can I get the compilation window back? is there a way to delete this code affect?

    ReplyDelete
  6. What do you mean? It should be automatically restored to the original format in the docked form after you run the above code.
    Have you modified it from the above somehow?

    This code does not do any special change, just the one described above controlling where the compilation output is sent. That's it.

    ReplyDelete

Please don't forget to leave your contact details if you expect a reply from me. Thank you