Luarocks:从包含多个文件的程序包中创建岩石 [英] Luarocks: Creating a rock from package with multiple files

查看:135
本文介绍了Luarocks:从包含多个文件的程序包中创建岩石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Luarocks,遵循了创建岩石上的教程>.但是,我很难创建包含多个文件的程序包.当我的程序包分散在多个文件中时,与本教程有何不同?

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?

说我有以下文件testrock.lua:

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

testrock-scm-1.rockspec

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

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

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

然后我运行luarocks make并安装testrock就好了(我可以转到另一个目录并运行require 'testrock').

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

但是,假设我要添加另一个文件foo.lua:

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

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

我在testrock.lua的末尾添加以下内容:

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

require('foo')

并运行luarocks make. make有效,但是当我转到另一个目录并运行`require'testrock`时,出现以下错误:

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)

,因此它抱怨require('foo')语句.有什么建议吗?

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

推荐答案

module('testrock')的调用将隐藏所有全局变量,包括全局require函数.您可以在调用module之前先调用require,或者在调用module之前创建本地别名(local require = require),或者使用

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)).

将foo模块添加到rockspec中,以便与testrock.lua文件一起安装,很简单:

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"
   }
}
-- ...

这篇关于Luarocks:从包含多个文件的程序包中创建岩石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆