Luarocks: Creating a rock from package with multiple files

▼魔方 西西 提交于 2019-12-24 16:14:51

问题


Using Luarocks, I've followed the tutorial on Creating a rock. However, I'm having difficulty creating a rock of a package that contains multiple files. What do I have to do differently from the tutorial when my package is spread out across multiple files?

Example - One File

Say I have the following file testrock.lua:

module('testrock')
function add(a, b)
    return a+b
end

and in testrock-scm-1.rockspec

package = "testrock"
version = "scm-1"

source = ...
description = ...
dependencies = ...

build = {
   type = "builtin",
   modules = {
       testrock = "testrock.lua"
   }
}

I then run luarocks make and it installs testrock just fine (I can go to another directory and run require 'testrock').

Example - Two Files

However, let's say I want to add another file foo.lua:

function testrock.sub(a, b)
    return a - b
end

I add the following to the end of testrock.lua:

require('foo')

and run luarocks make. make works, but when I then go to another directory and run `require 'testrock`` I get the following error:

/home/<username>/torch/install/share/lua/5.1/testrock.lua:7: attempt to call global 'require' (a nil value)

and so it's complaining about the require('foo') statement. Any advice?


回答1:


The call to module('testrock') hides all global variables, including the global require function. You can either call require before the call to module, or create a local alias (local require = require) before the call to module, or use the package.seeall option (module('testrock', package.seeall)).

Adding the foo module to your rockspec, so that it is installed together with your testrock.lua file, is straightforward:

-- ...
build = {
   type = "builtin",
   modules = {
       testrock = "testrock.lua",
       foo = "foo.lua"
   }
}
-- ...



回答2:


The solution came with realized that I don't need to build anything because I'm only using .lua files. Therefore, the following rockspec works:

package = "testrock"
version = "scm-1"

source = ...
description = ...
dependencies = ...

build = {
   type = "none",
   install = {
       lua = {
           "testrock.lua",
           "foo.lua"
       }
   }
}

This copies the testrock.lua and foo.lua to /home/<username>/torch/install/share/lua/5.1/.



来源:https://stackoverflow.com/questions/34966948/luarocks-creating-a-rock-from-package-with-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!