git » sdk » commit b98ab7c

Include git version in output

author Stephen Paul Weber
2024-11-06 20:40:12 UTC
committer Stephen Paul Weber
2024-11-06 20:40:12 UTC
parent d08e853dfc198ef09a17b1e2bf4df8e9c8d66047

Include git version in output

browserjs.hxml +1 -0
nodejs.hxml +1 -0
npm/index.ts +1 -0
snikket/Util.hx +22 -0
snikket/Version.hx +8 -0

diff --git a/browserjs.hxml b/browserjs.hxml
index cdfab9f..77f23e1 100644
--- a/browserjs.hxml
+++ b/browserjs.hxml
@@ -10,6 +10,7 @@
 
 snikket.Client
 snikket.Push
+snikket.Version
 
 -D js-es=6
 -D hxtsdgen_enums_ts
diff --git a/nodejs.hxml b/nodejs.hxml
index 6223ee5..7a6c171 100644
--- a/nodejs.hxml
+++ b/nodejs.hxml
@@ -11,6 +11,7 @@
 
 snikket.Client
 snikket.Push
+snikket.Version
 
 -D js-es=6
 -D hxtsdgen_enums_ts
diff --git a/npm/index.ts b/npm/index.ts
index fd63ff2..a22ae51 100644
--- a/npm/index.ts
+++ b/npm/index.ts
@@ -20,6 +20,7 @@ export import Participant = snikket.Participant;
 export import Push = snikket.Push;
 export import SerializedChat = snikket.SerializedChat;
 export import jingle = snikket.jingle;
+export const VERSION = snikket.Version.HUMAN;
 
 export import UiState = enums.UiState;
 export import MessageStatus = enums.MessageStatus;
diff --git a/snikket/Util.hx b/snikket/Util.hx
index da43ecf..0f76785 100644
--- a/snikket/Util.hx
+++ b/snikket/Util.hx
@@ -20,3 +20,25 @@ function setupTrace() {
 function downcast<T, S>(value: T, c: Class<S>): Null<S> {
 	return cast Std.downcast(cast value, cast c);
 }
+
+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
+}
diff --git a/snikket/Version.hx b/snikket/Version.hx
new file mode 100644
index 0000000..c01a954
--- /dev/null
+++ b/snikket/Version.hx
@@ -0,0 +1,8 @@
+package snikket;
+
+import snikket.Util;
+
+@:expose
+class Version {
+	@:keep public static var HUMAN(default, never):String = getGitVersion();
+}