Find files relative to build macro caller
This commit is contained in:
@@ -5,6 +5,7 @@ import haxe.macro.Expr;
|
|||||||
import haxe.macro.Context;
|
import haxe.macro.Context;
|
||||||
import haxe.macro.PositionTools;
|
import haxe.macro.PositionTools;
|
||||||
import sys.io.File;
|
import sys.io.File;
|
||||||
|
import haxe.io.Path;
|
||||||
#end
|
#end
|
||||||
import kiss.Kiss;
|
import kiss.Kiss;
|
||||||
import kiss.cloner.Cloner;
|
import kiss.cloner.Cloner;
|
||||||
@@ -50,6 +51,8 @@ class EmbeddedScript {
|
|||||||
public static function build(dslFile:String, scriptFile:String):Array<Field> {
|
public static function build(dslFile:String, scriptFile:String):Array<Field> {
|
||||||
var k = Kiss.defaultKissState();
|
var k = Kiss.defaultKissState();
|
||||||
|
|
||||||
|
var classPath = Context.getPosInfos(Context.currentPos()).file;
|
||||||
|
var loadingDirectory = Path.directory(classPath);
|
||||||
var classFields = Context.getBuildFields();
|
var classFields = Context.getBuildFields();
|
||||||
|
|
||||||
var commandList:Array<Expr> = [];
|
var commandList:Array<Expr> = [];
|
||||||
@@ -57,6 +60,7 @@ class EmbeddedScript {
|
|||||||
// This brings in the DSL's functions and global variables.
|
// This brings in the DSL's functions and global variables.
|
||||||
// As a side-effect, it also fills the KissState with the macros and reader macros that make the DSL syntax
|
// As a side-effect, it also fills the KissState with the macros and reader macros that make the DSL syntax
|
||||||
classFields = classFields.concat(Kiss.build(dslFile, k));
|
classFields = classFields.concat(Kiss.build(dslFile, k));
|
||||||
|
scriptFile = Path.join([loadingDirectory, scriptFile]);
|
||||||
|
|
||||||
Reader.readAndProcess(Stream.fromFile(scriptFile), k, (nextExp) -> {
|
Reader.readAndProcess(Stream.fromFile(scriptFile), k, (nextExp) -> {
|
||||||
var fields = Kiss.readerExpToFields(nextExp, k, false);
|
var fields = Kiss.readerExpToFields(nextExp, k, false);
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import kiss.cloner.Cloner;
|
|||||||
using kiss.Helpers;
|
using kiss.Helpers;
|
||||||
using kiss.Reader;
|
using kiss.Reader;
|
||||||
using tink.MacroApi;
|
using tink.MacroApi;
|
||||||
|
using haxe.io.Path;
|
||||||
|
|
||||||
typedef ExprConversion = (ReaderExp) -> Expr;
|
typedef ExprConversion = (ReaderExp) -> Expr;
|
||||||
|
|
||||||
@@ -91,13 +92,17 @@ class Kiss {
|
|||||||
/**
|
/**
|
||||||
Build macro: add fields to a class from a corresponding .kiss file
|
Build macro: add fields to a class from a corresponding .kiss file
|
||||||
**/
|
**/
|
||||||
public static function build(kissFile:String, ?k:KissState, useClassFields = true):Array<Field> {
|
public static function build(?kissFile:String, ?k:KissState, useClassFields = true):Array<Field> {
|
||||||
|
var classPath = Context.getPosInfos(Context.currentPos()).file;
|
||||||
|
// (load... ) relative to the original file
|
||||||
|
var loadingDirectory = Path.directory(classPath);
|
||||||
|
if (kissFile == null) {
|
||||||
|
kissFile = classPath.withoutDirectory().withoutExtension().withExtension("kiss");
|
||||||
|
}
|
||||||
|
|
||||||
return _try(() -> {
|
return _try(() -> {
|
||||||
var classFields:Array<Field> = if (useClassFields) Context.getBuildFields() else [];
|
var classFields:Array<Field> = if (useClassFields) Context.getBuildFields() else [];
|
||||||
var stream = Stream.fromFile(kissFile);
|
var stream = Stream.fromFile(Path.join([loadingDirectory, kissFile]));
|
||||||
|
|
||||||
// (load... ) relative to the original file
|
|
||||||
var loadingDirectory = Path.directory(kissFile);
|
|
||||||
|
|
||||||
if (k == null)
|
if (k == null)
|
||||||
k = defaultKissState();
|
k = defaultKissState();
|
||||||
@@ -112,13 +117,12 @@ class Kiss {
|
|||||||
nextExp.checkNumArgs(1, 1, "(load \"[file]\")");
|
nextExp.checkNumArgs(1, 1, "(load \"[file]\")");
|
||||||
switch (loadArgs[0].def) {
|
switch (loadArgs[0].def) {
|
||||||
case StrExp(otherKissFile):
|
case StrExp(otherKissFile):
|
||||||
var filePath = Path.join([loadingDirectory, otherKissFile]);
|
if (!k.loadedFiles.exists(otherKissFile)) {
|
||||||
if (!k.loadedFiles.exists(filePath)) {
|
var loadedFields = Kiss.build(otherKissFile, k, false);
|
||||||
var loadedFields = Kiss.build(filePath, k, false);
|
|
||||||
for (field in loadedFields) {
|
for (field in loadedFields) {
|
||||||
classFields.push(field);
|
classFields.push(field);
|
||||||
}
|
}
|
||||||
k.loadedFiles[filePath] = true;
|
k.loadedFiles[otherKissFile] = true;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
throw CompileError.fromExp(loadArgs[0], "only argument to load should be a string literal");
|
throw CompileError.fromExp(loadArgs[0], "only argument to load should be a string literal");
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import haxe.ds.Option;
|
|||||||
|
|
||||||
using StringTools;
|
using StringTools;
|
||||||
|
|
||||||
@:build(kiss.Kiss.build("kiss/src/test/cases/BasicTestCase.kiss"))
|
@:build(kiss.Kiss.build())
|
||||||
class BasicTestCase extends Test {
|
class BasicTestCase extends Test {
|
||||||
function testStaticVar() {
|
function testStaticVar() {
|
||||||
Assert.equals("Howdy", BasicTestCase.message);
|
Assert.equals("Howdy", BasicTestCase.message);
|
||||||
|
|||||||
@@ -5,5 +5,5 @@ import utest.Test;
|
|||||||
import kiss.Prelude;
|
import kiss.Prelude;
|
||||||
import kiss.List;
|
import kiss.List;
|
||||||
|
|
||||||
@:build(kiss.Kiss.build("kiss/src/test/cases/CommentAtEndOfListTestCase.kiss"))
|
@:build(kiss.Kiss.build())
|
||||||
class CommentAtEndOfListTestCase extends Test {}
|
class CommentAtEndOfListTestCase extends Test {}
|
||||||
|
|||||||
@@ -15,5 +15,5 @@ class DSLTestCase extends Test {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@:build(kiss.EmbeddedScript.build("kiss/src/test/cases/DSL.kiss", "kiss/src/test/cases/DSLScript.dsl"))
|
@:build(kiss.EmbeddedScript.build("DSL.kiss", "DSLScript.dsl"))
|
||||||
class DSLScript extends EmbeddedScript {}
|
class DSLScript extends EmbeddedScript {}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import haxe.ds.Option;
|
|||||||
|
|
||||||
using StringTools;
|
using StringTools;
|
||||||
|
|
||||||
@:build(kiss.Kiss.build("kiss/src/test/cases/MacroTestCase.kiss"))
|
@:build(kiss.Kiss.build())
|
||||||
class MacroTestCase extends Test {
|
class MacroTestCase extends Test {
|
||||||
function testMultipleFieldForms() {
|
function testMultipleFieldForms() {
|
||||||
Assert.equals(5, myVar);
|
Assert.equals(5, myVar);
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import utest.Test;
|
|||||||
import utest.Assert;
|
import utest.Assert;
|
||||||
import kiss.Prelude;
|
import kiss.Prelude;
|
||||||
|
|
||||||
@:build(kiss.Kiss.build("kiss/src/test/cases/ReaderMacroTestCase.kiss"))
|
@:build(kiss.Kiss.build())
|
||||||
class ReaderMacroTestCase extends Test {
|
class ReaderMacroTestCase extends Test {
|
||||||
function testReadBang() {
|
function testReadBang() {
|
||||||
Assert.equals("String that takes the rest of the line", ReaderMacroTestCase.myLine());
|
Assert.equals("String that takes the rest of the line", ReaderMacroTestCase.myLine());
|
||||||
|
|||||||
Reference in New Issue
Block a user