Refering to where I am

[Edit: moved to Macros]

In a macro, I would like to find the path to a directory at the same level as the file using the macro, so if the directory containing my source looks like this:

foo.swift    Shared

And if have a macro in foo.swift like this:

let stuff: [Int] = [ #mymacro("Shared/SandBox") ]

...the macro will get a useful version of the path to Shared, such that it can munge through the files there, and produce the replacement program text.

My first inclination was to use #filePath. But, apparently macros don't get expanded inside of other macros. (I'm not really surprised by that)

I tried getting the currentDirectory from FileManager,default, but surprisingly, that seems to be "/", instead of anything having to do with the source.

I finally figured out context.location(of: node), from which I can maybe figure things out, but I still wonder if this is the only way?

For anyone interested in what the heck I'm doing, I'm constructing an array of references to structs named /.+Play/, which are all Views. If there is a simpler Swift™ way of doing this, (i.e. not external) I'd love to know it.

2 Likes

A frustration: If I expand a macro, using the Xcode feature of right clicking on the macro and selecting expand macro, it seems that my current working directory is "/", instead of the directory of the project using the macro.

Worse, I thinks it's a sandbox. I really can't tell.