hxml tricks every haxe user should know

Haxe projects usually are compiled with a hxml file, which basically contains compiler arguments. So most of the tricks I am going to show are actually compiler arguments tricks, which may also applicable to nmml file or IDE that does not use hxml (though they should).

1. Run project right after compilation

Being able to do so is critical to rapid development. The simple trick to use -cmd, which run the a command after successful compilation. For different target, you have to use different command:

Neko

-neko Test.n  
-main Test  
-cmd neko Test.n

Or there is actually a shortcut that performs exactly the same as above:

-x Test

C++

-cpp bin
-main Test
-cmd ./bin/Test

Notice the resulting file will be Test-debug if you compile with -debug.

JavaScript

-js Test.js  
-main Test  
-cmd phantomjs Test.js

Here we use phantomjs to run the JS file, trace() will output to the terminal. In case it is something graphical, you have to launch a browser with a HTML file that loads the script.

PHP

-php bin  
-main Test  
-cmd php bin/index.php

I guess you knew you can execute a php script this way?

Flash

-swf Test.swf  
-main Test  
-cmd path/to/FlashDebugger Test.swf

Or if you’re on Mac, you can actually use -cmd open Test.swf, it will do the same as you double clicked on a swf file (or any other file!).

Java

-java bin  
-main Test  
-cmd java -jar bin/Test.jar

Notice the resulting file will be Test-Debug.jar if you compile with -debug.

C#

-cs bin  
-main Test  
-cmd mono bin/bin/Test.exe

Mac/Linux has to use Mono to execute it. Windows should able to run it with -cmd bin\bin\Test.exe.
Notice the resulting file will be Test-Debug.exe if you compile with -debug.

2. Commenting a hxml file

Use a hash (i.e. #) to comment out the rest of the line. Despite of being useful to put documentation, it can let us switch between different compilation configurations:

-js Main.js  
-main Main  
### comment out one of the following logging levels  
#-D my-log-error  
#-D my-log-warning  
-D my-log-info

And in the source file:

function attack(target:Monster, power:Int):Void {
    if (target == null) {
        #if (my_log_error || my_log_warning || my_log_info)
        trace("target is null");
        #end
        return;
    }
    if (power <= 0) {
        #if (my_log_warning || my_log_info)
        trace("attack power should be positive");
        #end
        return;
    }
    mp -= power;
    target.hp -= power;
    #if my_log_info
    trace("attacked " + target + " by " + power);
    #end
}

3. Append extra compiler argument when using the command line

Another useful trick to switch between compilation configurations, is to simple supply more arguments after the hxml file. For example temporarily switch to debug mode:

haxe project.hxml -debug

4. Multiple compilations at once

The majority knows the existence of --next, used for separating different builds:

all.hxml

-js script/MainPage.js  
-main MainPage  
-lib jQueryExtern  
--next  
-js script/ContactPage.js  
-main ContactPage  
-lib jQueryExtern  
--next  
-js script/AlbumPage.js  
-main AlbumPage  
-lib jQueryExtern

There is an —each option, which reduce the repeating params. It is used in the haxe compiler unit test hxml. Rewriting the above, we will get:

all.hxml

-lib jQueryExtern  
--each  
-js script/MainPage.js  
-main MainPage  
--next  
-js script/ContactPage.js  
-main ContactPage  
--next  
-js script/AlbumPage.js  
-main AlbumPage

But really, separating each of them in different hxml is easier to maintain and has better compatibility with code completion (because many editors don’t read hxml with --next very well). So we may use the following instead:

MainPage.hxml

-js
script/MainPage.js  
-main MainPage  
-lib jQueryExtern

ContactPage.hxml

-js
script/ContactPage.js  
-main ContactPage  
-lib jQueryExtern

AlbumPage.hxml

-js
script/AlbumPage.js  
-main AlbumPage  
-lib jQueryExtern

all.hxml

MainPage.hxml  
--next  
ContactPage.hxml  
--next  
AlbumPage.hxml

Switch the hxml for code completion when working in different pages, and use the all.hxml to compile at once.

comments powered by Disqus