Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for reading the long git sha1 and the current git tag. #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions example/Example.hx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class Example
var date = CompileTime.buildDate(); // Equivalent of writing `new Date(2012,11,25,20,48,15);`
var dateAsString = CompileTime.buildDateString(); // A string saying "2012-12-25 20:48:15"
var gitsha = CompileTime.buildGitCommitSha(); // A string that might say '104ad4e'
var gitshaLong = CompileTime.buildGitCommitShaLong(); // A string that might say '104ad4e8128dd7fae6d3f1c8a066e3570db99b2e'
var gitTagDesrc = CompileTime.buildGitTagDescription(); // A string that might say 'v1.0.2' or 'v1.0.2-104ad4e'
var gitTag = CompileTime.buildGitTag(); // A string that might say 'v1.0.2'
var file = CompileTime.readFile("README.md"); // Reads the contents of README.md as a String.
var name="Jason", age=25;
var greeting = CompileTime.interpolateFile("test.txt"); // Reads the contents of test.txt, and interpolates local values, similar to single quotes
Expand Down
33 changes: 33 additions & 0 deletions src/CompileTime.hx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,39 @@ class CompileTime
return toExpr(sha1);
}

/** Returns a string of the current git full sha1 */
macro public static function buildGitCommitShaLong():ExprOf<String> {
var proc = new sys.io.Process('git', ['log', "--pretty=format:'%H'", '-n', '1']);
var sha1 = proc.stdout.readLine();
return toExpr(sha1);
}

/** Returns a string of the current git tag description.
If there is a tag on the current commit, the tag is returned.
If there is no tag on the current commit, a combination of `TAG-SHA1` from the latest tag is returned instead.
If there are no tags on the repository, `null` is returned instead. */
macro public static function buildGitTagDescription():ExprOf<String> {
var proc = new sys.io.Process('git', ['describe', '--tags']);
var tag = null;
try {
tag = proc.stdout.readLine();
}
catch (e: Dynamic) { }
return toExpr(tag);
}

/** Returns a string of the current git tag.
If there is no tag on the current commit, `null` is returned instead. */
macro public static function buildGitTag():ExprOf<String> {
var proc = new sys.io.Process('git', ['describe', '--tags', '--exact-match' ]);
var tag = null;
try {
tag = proc.stdout.readLine();
}
catch (e: Dynamic) { }
return toExpr(tag);
}

/** Reads a file at compile time, and inserts the contents into your code as a string. The file path is resolved using `Context.resolvePath`, so it will search all your class paths */
macro public static function readFile(path:String):ExprOf<String> {
return toExpr(loadFileAsString(path));
Expand Down