Debian Codegraph @

extra/haxelib_src/src/haxelib/client/Util.hx

raw
1 package haxelib.client;
2
3 using StringTools;
4
5 class Util {
6
7 macro static public function rethrow(e) {
8 return if (haxe.macro.Context.defined("neko"))
9 macro neko.Lib.rethrow(e);
10 else
11 macro throw e;
12 }
13
14 #if macro
15 static function readVersionFromHaxelibJson() {
16 return haxe.Json.parse(sys.io.File.getContent("haxelib.json")).version;
17 }
18 #end
19
20 macro static public function getHaxelibVersion() {
21 return macro $v{readVersionFromHaxelibJson()};
22 }
23
24 macro static public function getHaxelibVersionLong() {
25 var version:String = readVersionFromHaxelibJson();
26 var p;
27 try {
28 //check if the .git folder exist
29 //prevent getting the git info of a parent directory
30 if (!sys.FileSystem.isDirectory(".git"))
31 throw "Not a git repo.";
32
33 //get commit sha
34 p = new sys.io.Process("git", ["rev-parse", "HEAD"]);
35 var sha = p.stdout.readAll().toString().trim();
36 p.close();
37
38 //check to see if there is changes, staged or not
39 p = new sys.io.Process("git", ["status", "--porcelain"]);
40 var changes = p.stdout.readAll().toString().trim();
41 p.close();
42
43 version += switch(changes) {
44 case "":
45 ' ($sha)';
46 case _:
47 ' ($sha - dirty)';
48 }
49 return macro $v{version};
50 } catch(e:Dynamic) {
51 if (p != null) p.close();
52 return macro $v{version};
53 }
54 }
55 }