Accessing compilation date and git revision during compilation in HaXe

It is often useful if a program knows the time when it was build and/or the git repository revision id it was build from. Unfortunately, one often forgets to update this information before launching the build. The following code can be used to do this automatically.

(P.S.: The code works even when compiling towards “non-Sys” platforms like JavaScript.)

class BuildInfo {
    /**
     * The time when this library was build.
     */
    public static var buildTime(default, null):Int = getBuildTime();

    /**
     * The git reversion that was used for the build.
     */
    public static var gitRev(default, null):String = getGITRevision();

    /**
     * Macros initalizing the build information.
     */
    public static macro function getBuildTime() {
        var buildTime = Math.floor(Date.now().getTime() / 1000);
        return macro $v{buildTime};
    }
    public static macro function getGITRevision() {
        var gitrev = new sys.io.Process("git", [ "rev-parse", "--verify", "HEAD" ]).stdout.readAll().toString();
        return macro $v{gitrev};
    }

    public static function main() {
        trace("This library was build the " + BuildInfo.buildTime);
        trace("by using revision " + BuildInfo.gitRev);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *