[ZeroBrane Studio] Re: How to require a lua file from .zbstudio/packages?

  • From: Chronos Phaenon Eosphoros <cpeosphoros@xxxxxxxxx>
  • To: zerobrane@xxxxxxxxxxxxx
  • Date: Sun, 23 Jul 2017 14:35:22 -0300

self.path is nil when 'onRegister = function(self)' is called.
self:GetFilePath() is nil
ide:GetPackagePath(self.fname) works as intended, but 'require' still
fails, after populating self.path as instructed (either stripping
"$HOME/.zbstudios/" from the path or not). This is the 'require' stack:

no field package.preload['myplugin']
no file 'lualibs/myplugin.lua'
no file 'lualibs/myplugin/myplugin.lua'
no file 'lualibs/myplugin/init.lua'
no file 'lualibs/myplugin/myplugin/myplugin.lua'
no file 'lualibs/myplugin/myplugin/init.lua'
no file './myplugin.lua'
no file '/usr/local/share/luajit-2.0.4/myplugin.lua'
no file '/usr/local/share/lua/5.1/myplugin.lua'
no file '/usr/local/share/lua/5.1/myplugin/init.lua/packages/myplugin'
no file 'bin/linux/x64/myplugin.so'
no file 'bin/linux/x64/clibs/myplugin.so'
no file 'bin/linux/x64/clibs/myplugin.so'
no file './myplugin.so'
no file '/usr/local/lib/lua/5.1/myplugin.so'
no file '/usr/local/lib/lua/5.1/loadall.so'

Anyways, got a workaround emulating 'require' with:

local myplugin
local ok, res = pcall(loadfile, "myplugin.lua")
if ok then
ok, myplugin = pcall(res)
end
if ok then
-- use myplugin
end

I've actually developed a "metaplugin" for hot reloading plugins in
development without having to close/reopening the IDE.

It replicates all the passes done in zbstudio's packages.lua when loading
plugins on IDE close/opening.

I'd already made a pull request with a working version, using the
workaround. I just wanted a cleaner version with 'require'. But it works
fine as is.

Chronos Phaenon Eosphoros

Antes de imprimir, por favor reflita sobre a economia e o meio-ambiente –
Before printing, please consider the economy and environment.

On Sun, Jul 23, 2017 at 1:43 PM, Paul K <paul@xxxxxxxxxxxxx> wrote:

local myplugin = require "packages.myplugin"
Doesn't find myplugin.lua, which is in .zbstudio/packages.

This is not going to work for several reasons. First, if the plugin is
in packages, then you should reference it as `require "myplugin"`
(without `packages.`). Second, it will still not load as the packages/
folder is not in `packages.path`, so if you want to load packages from
that location, you need to explicitly add it. Third, the packages are
loaded by the IDE itself (as they have special methods), so loading
them with `require` may not have desired effect.

The packages should be loaded correctly from $HOME/.zbstudio/packages
folder.

If you want to load additional files (for example, some 3rd-party
modules that you want to use in the plugin), then the convention is to
put them in the same folder as the plugin name and load it from there
as part of onRegister callback. For example, if the plugin name is
myplugin, then packages/ folder will have myplugin.lua and myplugin/
folder with all other files/modules. You can get the path to that
location using `package:GetFilePath()` call, where `package` is the
object passed to onRegister event (it's the first parameter in all
event callbacks). You can then strip .lua from it and add it to
package.path; something like the following may work (not tested):

package.path = package.path .. ";" ..
package:GetFilePath():gsub("%.lua$", "/?.lua")
require "somethingelse" -- somethingelse is in myplugin/somethingelse.lua

Paul.

On Sun, Jul 23, 2017 at 8:41 AM, Chronos Phaenon Eosphoros
<cpeosphoros@xxxxxxxxx> wrote:

local myplugin = require "packages.myplugin"

Doesn't find myplugin.lua, which is in
.zbstudio/packages.

Thanks,

Chronos Phaenon Eosphoros

Antes de imprimir, por favor reflita sobre a economia e o meio-ambiente –
Before printing, please consider the economy and environment.


Other related posts: