Pre-processing project files in lifecycle hook editor script

Hello! I’m trying to augment build pipeline with some project files pre-processing by third-party tool. Looks like the best place for this is lifecycle hook script. The problem is that I can’t get the paths of files in project directory since the hook script is being run in /opt/Defold directory, where my Defold installation resides; this is confirmed by the following simple hook script:

local M = {}
function M.on_build_started(opts)
    local r = io.popen("pwd")
    error(r:read("*a"))
end
return M

However, I’d need a full paths to (some) files in project in order to feed those to that third-party tool. How can I acheive that? I’ve looked at opts argument and even at editor built-in, but none of those contain the project path to build the file paths upon it.

I haven’t used our editor script extension system enough to give good support I’m afraid. I know @Insality and @Jerakin are frequent users though. Perhaps they can help?

1 Like

Not sure I am much help here either unfortunately. I have tried to get the “absolute” project path in the past but never succeeded, however I have found in most cases I don’t need to.

io.popen("pwd") reports something “unexpected” for me too however normally if I would get a file with

-- usually this is a resource not a file already, like your selection
local root = editor.get("/game.project", "path") 

local file_handle = io.open("." .. root, "r")

While the editor.get does return a relative path io.open is fine with that and will read/write to the expected location with it. So lua does have the project path internally, not 100% sure how to get that though.

In the past I have passed these paths a long to python and it worked fine doing that too.

Probably, for your help I can share example of editor script from Druid:

It’s works fine with files inside project root where editor script is started. As I remember, the relative paths is working.

I think hooks.editor_script should work with similar way. For example in my hook script I have the string like:

local file = io.open(".check_file", "r")

And it open file correctly from project root folder

1 Like

Thanks for helpful pointers guys! The io.open indeed does work for the files in project. The only question left, is there a way to programmatically enumerate all the files in the project in the lifecycle hook, possibly matching an extension, so I can feed those I need to that third-party preprocessor tool?

I don’t think that possible with a strict .editor_script. What I would probably do something that I did in the link I posted above (this commit but the removed code), I would get the path to game.project within the editor script then send that into python (as the link) then gather your files there and pass it along. Not pretty but would get the job done.

Got it, thanks!
I ended up using the regular editor script, since it kind of feets my needs a little bit better anyway. Thanks for the tips!