git » sdk » compiled » tree

[compiled] / snikket / Util.hx

package snikket;

function setupTrace() {
#if js
	haxe.Log.trace = (v, ?infos) -> {
		if (js.Syntax.typeof(untyped console) != "undefined" && (untyped console).debug != null) {
			final params = infos.customParams ?? [];
			infos.customParams = [];
			final str: Dynamic = haxe.Log.formatOutput(v, infos);
			(untyped console).debug.apply(null, [str].concat(params));
		} else if (js.Syntax.typeof(untyped console) != "undefined" && (untyped console).log != null) {
			final str = haxe.Log.formatOutput(v, infos);
			(untyped console).log(str);
		}
	}
#end
}

// Std.downcast doesn't play well with null safety
function downcast<T, S>(value: T, c: Class<S>): Null<S> {
	return cast Std.downcast(cast value, cast c);
}

function xmlEscape(s: String) {
	// NOTE: using STringTools.htmlEscape breaks things if this is one half of a surrogate pair in an adjacent cdata
	return StringTools.replace(StringTools.replace(StringTools.replace(s, "&", "&amp;"), "<", "&lt;"), ">", "&gt;");
}

macro function getGitVersion():haxe.macro.Expr.ExprOf<String> {
	#if !display
	var process = new sys.io.Process('git', ['describe', '--always']);
	if (process.exitCode() != 0) {
		var message = process.stderr.readAll().toString();
		var pos = haxe.macro.Context.currentPos();
		haxe.macro.Context.error("Cannot execute `git describe`. " + message, pos);
	}

	// read the output of the process
	var commitHash:String = process.stdout.readLine();

	// Generates a string expression
	return macro $v{commitHash};
	#else
	// `#if display` is used for code completion. In this case returning an
	// empty string is good enough; We don't want to call git on every hint.
	var commitHash:String = "";
	return macro $v{commitHash};
	#end
}