Skip to content
Open
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
17 changes: 17 additions & 0 deletions src/CompileTime.hx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class CompileTime
return toExpr(loadFileAsString(path));
}

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

/** Reads a file at compile time, and inserts the contents into your code as an interpolated string, similar to using 'single $quotes'. */
macro public static function interpolateFile(path:String):ExprOf<String> {
return Format.format( toExpr(loadFileAsString(path)) );
Expand Down Expand Up @@ -166,6 +171,18 @@ class CompileTime
}
}

static function loadFileAsBase64(path:String) {
try {
var p = Context.resolvePath(path);
Context.registerModuleDependency(Context.getLocalModule(),p);
var file = sys.io.File.getBytes(p);
return haxe.crypto.Base64.encode(file);
}
catch(e:Dynamic) {
return haxe.macro.Context.error('Failed to load file $path: $e', Context.currentPos());
}
}

static function isSameClass(a:ClassType, b:ClassType):Bool {
return (
a.pack.join(".") == b.pack.join(".")
Expand Down