We ran into an issue when we were creating unit tests for one of our projects. We had a project hierarchy that was like the following:
- Root
–Source files here…
–Tests/
----Tile/
-----main.lua
In which case, we did not want to copy Tile.lua to the Tests/Tile directory and keep both copies updated. Symlinks also did not solve this problem. We finally found a great solution. We could modify the package.path to change where requires looks for files. Each unit test’s main.lua would have something similar to this at the top:
[lua]local sourceEnd = package.path:find(“Tests”) - 1 – Get the location of Tests directory in string
local basePath = package.path:sub(0, sourceEnd); – Get the path to the root of the source code
package.path = basePath … “?.lua;” … package.path – Add root to the base package.path
package.path = basePath … “Common/?.lua;” … package.path – Add the Common[/lua]
So this was great and works perfectly for unit tests. So there ends the preface and comes the new problem: We got a thirdparty library that was two lua files. We placed it in a sub directory of its own and accessed the client api file with the following:
[lua]local thirdParty = require(“thirdPartyDirectory.thirdpartyOne”)[/lua]
However we ran into a problem because the file thirdpartyOne.lua requires thirdpartyTwo.lua. So we thought that instead of doing the hackish thing and modifying the third party library that we could do something more elegant and change the path to include the thirdPartyDirectory.
This works great on simulator, but running on device causes issues:
: Lua Runtime Error: lua\_pcall failed with status: 2, error message is: module 'thirdpartyTwo' not found:resource (thirdpartyTwo.lu) does not exist in archive
I don’t know much about how Corona’s device build process works but is there a way to get this to work? It really makes moving source files to different directories much easier. If there isn’t, could this be a feature that Corona could implement?
Thanks!
TL;DR
I want to change the package.path so that I can change where require looks for files. This way I can have 2 lua files in the same subdirectory that reference each other but don’t need the full path in the require statement. This makes moving source files easier and reduces the need to modify thirdparty lua files.
[import]uid: 94248 topic_id: 28397 reply_id: 328397[/import]