Lua包管理工具Luarocks详解

来源:互联网 发布:艺术品网络拍卖 编辑:程序博客网 时间:2024/05/15 11:10

高效Lua包管理工具Luarocks

讨论话题

  1. 什么是Luarocks
  2. 源码安装部署Luarocks
  3. 注册Luarocks为Lua社区贡献自己的代码
  4. Luarocks使用初探

什么是Luarocks

Luarocks是一个Lua包管理器,基于Lua语言开发,提供一个命令行的方式来管理Lua包依赖、安装第三方Lua包等,社区比较流行的包管理器之一,另还有一个LuaDist,Luarocks的包数量比LuaDist多,更细节的两者对比可参阅这里。

源码安装部署Luarocks(为何使用源码见此文)

wget http://luarocks.org/releases/luarocks-2.2.2.tar.gztar zxvf luarocks-2.2.2.tar.gzcd luarocks-2.2.2./configure --help

configure help查看所支持的安装配置,这里我们主要关注以下两个

--prefix=DIR                Prefix where LuaRocks should be installed.                            Default is /usr/local--with-lua=PREFIX           Use Lua from given prefix.                            Default is auto-detected (the parent directory of $LUA_BINDIR).

–prefix设置Luarocks安装路径,–with-lua指定Luarocks依赖的Lua安装路径。

为何强调设置–prefix

设置prefix会自动将Luarocks以及往后使用Luarocks安装的Lua包,LuaC包都安装到Luarocks安装路径下的相应位置,否则相关的包文件散落在文件系统中,显得杂乱不便于管理,如果所安装的Lua模板包含bin文件,则会自动安装到此目录下的bin路径,与Luarocks可执行文件同一路径,更便于管理、使用。

./configure --prefix=/usr/local/luarocks-2.2.2 --with-lua=/usr/local/lua-5.1.5make buildmake install

安装完成后,直接运行luarocks即可使用。

 // luarocksLuaRocks 2.2.2, a module deployment system for LuaNAME  /usr/local/bin/luarocks - LuaRocks main command-line interface

注册Luarocks为Lua社区贡献自己的代码

当前IT技术发展惊人的快,很大程度上与开源社区的快速发展有直接关系,大家一起分享知识、经验、解决方案,互相支持、协作。我们从社区汲取养分,学习成长,回过头来我们也需要回馈社区,这才是一个良性循环。

使用Luarocks为Lua社区贡献代码非常简单,只需如下几步:
1. 到官网注册一个luarocks账号
2. 在官网设置页面生成API-Keys(用作包提交的验证)
3. 在项目根目录根据rockspec文件格式书写你自己的rockspec文件(或者在项目根目录运行luarocks write_rockspec命令自动生成)
4. 使用API-key上传你的项目到luarocks.org(命令行运行luarocks upload --api-key=
5. 在你的项目页面点击下图所示的Add To Manifest将你的项目添加到相关分类方便分类检索,默认已经在root里。

将你的项目添加到相关分类

Luarocks使用初探

命令行运行luarocks,或者luarocks help能看到相关luarocks的详细信息,大致分为以下6个段。
1. NAME/名称 显示Luarocks说明信息 - LuaRocks main command-line interface
2. SYNOPSIS/概要 显示luarocks命令参数使用格式:
luarocks [--from=<server> | --only-from=<server>] [--to=<tree>] [VAR=VALUE]... <command> [<argument>]
3. GENERAL OPTIONS/通用选项 被所有命令所支持的选项,包含指定搜索rocks/rockspecs的server,默认的server搜寻顺序为:
~
https://luarocks.org
https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/
http://luafr.org/moonrocks/
http://luarocks.logiceditor.com/rocks
~
另外选项还设置是否仅仅下载源码、是否显示安装过程、指定超时时间等。
4. VARIABLES/变量 Variables from the “variables” table of the configuration file can be overriden with VAR=VALUE assignments.
5. COMMANDS/命令列表 luarocks的常规操作命令install、search、list等
6. CONFIGURATION/相关配置信息 Lua版本,rocks trees等安装luarocks时的配置

在luarocks使用中我们主要关注GENERAL OPTIONS、和COMMANDS两项。GENERAL OPTIONS与其他COMMANDS配合使用。以搜索香草/vanilla框架(一个基于Lua开发的Openresty Web应用开发框架)为例:

 // luarocks search vanilla        #直接搜索vanillaSearch results:===============Rockspecs and source rocks:---------------------------vanilla   0.1.0-1 (rockspec) - https://luarocks.org   0.1.0-1 (src) - https://luarocks.org   0.0.1-1 (rockspec) - https://luarocks.org   0.0.1-1 (src) - https://luarocks.org

也可以在命令后加上一个GENERAL OPTIONS/通用选项比如–verbose来显示整个命令运行过程(这对排查安装中遇到的问题非常有用)。

 // luarocks search vanilla --verbose      #添加参数显示整个搜索vanilla的过程os.execute:   'curl' -k -f -L --user-agent 'LuaRocks/2.2.2 macosx-x86_64 via curl' --connect-timeout 30 'https://luarocks.org/manifest-5.1.zip' 2> /dev/null 1> '/Users/zhoujing/.cache/luarocks/https___luarocks.org/manifest-5.1.zip'Results: 1  1 (number): 0os.execute:   unzip -n '/Users/zhoujing/.cache/luarocks/https___luarocks.org/manifest-5.1.zip'Archive:  /Users/zhoujing/.cache/luarocks/https___luarocks.org/manifest-5.1.zip  inflating: manifest-5.1Results: 1  1 (number): 0Search results:===============Rockspecs and source rocks:---------------------------vanilla   0.1.0-1 (rockspec) - https://luarocks.org   0.1.0-1 (src) - https://luarocks.org   0.0.1-1 (rockspec) - https://luarocks.org   0.0.1-1 (src) - https://luarocks.org

命令的执行参数追加的格式如同SYNOPSIS描述的那样:
luarocks [--from=<server> | --only-from=<server>] [--to=<tree>] [VAR=VALUE]... <command> [<argument>]
可以使用luarocks <command> help查看command更细节的帮助文档,这也非常有用。

几个常用的luarocks命令详解

上面演示了search命令,看字面意思就很明白是用来搜索模块的,使用相对比较简单也容易理解,类似的命令还有download、help、install、list、new_version、remove、upload等,但是余下的几个命令也很重要,使用起来就不是那么好理解,下面以一个test的项目重点演示说明(每个命令使用时都建议先执行luarocks <command> help查看详细文档,下面仅截取各命令文档的NAME和SYNOPSIS段并详述、演示使用过程)。

 ~/Desktop/ tree ./t-luarocks./t-luarocks├── test│   ├── spec│   │   └── init.lua│   ├── sys│   │   ├── config.lua│   │   └── vanilla.lua│   └── v│       ├── application.lua│       ├── bootstrap.lua│       ├── controller.lua│       └── dispatcher.lua└── test-0.0.rc1-1.rockspec
build
 ~/Desktop/t-luarocks/ luarocks help buildNAME  /usr/local/bin/luarocks build - Build/compile a rock.SYNOPSIS  /usr/local/bin/luarocks build [--pack-binary-rock] [--keep] {<rockspec>|<rock>|<name> [<version>]}

build命令用来基于rock二进制文件安装Lua包,前提rock文件基于规范的rockspec文件打包,且rockspec包含正确的build段描述。

doc
 ~/Desktop/t-luarocks/ luarocks help docNAME  /usr/local/bin/luarocks doc - Shows documentation for an installed rock.SYNOPSIS  /usr/local/bin/luarocks doc <argument>

doc命令用来显示本地已经安装的Lua包根目录下所有的Markdown文档列表,并默认自动打开README.md文件,如果相应的Lua包本地并没有安装,则会到服务器上搜寻。

 ~/Desktop/t-luarocks/ luarocks doc vanillaDocumentation files for vanilla 0.1.0-1---------------------------------------/usr/local/luarocks-2.2.2/lib/luarocks/rocks/vanilla/0.1.0-1/doc/  CHANGELOG.md  LICENSE.md  README-zh.md  README.mdOpening /usr/local/luarocks-2.2.2/lib/luarocks/rocks/vanilla/0.1.0-1/doc/README.md ...
lint
 ~/Desktop/t-luarocks/ luarocks help lintNAME  /usr/local/bin/luarocks lint - Check syntax of a rockspec.SYNOPSIS  /usr/local/bin/luarocks lint <rockspec>

lint命令用来检查当前目录rockspec文件(rockspec文件为Luarocks包管理的描述文件,细节写法会另起文档描述)的格式,如果格式没问题则不会有任何显示,否则则暴露细节错误,指导修改。

make
 ~/Desktop/t-luarocks/ luarocks help makeNAME  /usr/local/bin/luarocks make - Compile package in current directory using a rockspec.SYNOPSIS  /usr/local/bin/luarocks make [--pack-binary-rock] [<rockspec>]

make命令基于一个rockspec文件安装Lua包,这个文件必须是本地的一个文件,而不像install那样可以是一个网络文件。这个命令还可以通过追加参数--pack-binary-rock而仅仅编译生成rock文件,make命令与install命令的区别在于install基于rock文件,而make必须基于rockspec文件。

pack
 ~/Desktop/t-luarocks/ luarocks help packNAME  /usr/local/bin/luarocks pack - Create a rock, packing sources or binaries.SYNOPSIS  /usr/local/bin/luarocks pack {<rockspec>|<name> [<version>]}

pack命令根据本地rockspec文件将Lua源码包打包成二进制的rock文件(install命令基于rock文件进行安装)。

path
 ~/Desktop/t-luarocks/ luarocks help pathNAME  /usr/local/bin/luarocks path - Return the currently configured package path.SYNOPSIS  /usr/local/bin/luarocks path

path命令显示当前Luarocks所配置的LUA_PATH和LUA_CPATH值

purge
 ~/Desktop/t-luarocks/ luarocks help purgeNAME  /usr/local/bin/luarocks purge - Remove all installed rocks from a tree.SYNOPSIS  /usr/local/bin/luarocks purge --tree=<tree> [--old-versions]

purge命令必须传入一个--tree参数,这里的tree其实是一个路径,luarocks安装Lua包所在路径,purge命令将这个路径下面所有的包都remove。这个命令完全挽救了没有指定–prefix而安装luarocks的同学,在没有指定–prefix的时候luarocks将默认的安装到/usr/local路径下,基于此安装的包也将散落在整个/usr/local目录,真是太乱。这时候在命令行执行luarocks purge --tree=/usr/local整个世界都干净了。

show
 ~/Desktop/t-luarocks/ luarocks help showNAME  /usr/local/bin/luarocks show - Shows information about an installed rock.SYNOPSIS  /usr/local/bin/luarocks show <argument>

show 命令可用来显示包信息,这些信息来自于当前Lua包的rockspec文件中。

unpack
 ~/Desktop/t-luarocks/ luarocks help unpackNAME  /usr/local/bin/luarocks unpack - Unpack the contents of a rock.SYNOPSIS  /usr/local/bin/luarocks unpack [--force] {<rock>|<name> [<version>]}

unpack命令将pack命令打包的rock解开。

upload
 ~/Desktop/t-luarocks/ luarocks help uploadNAME  /usr/local/bin/luarocks upload - Upload a rockspec to the public rocks repository.SYNOPSIS  /usr/local/bin/luarocks upload [--skip-pack] [--api-key=<key>] [--force] <rockspec>

upload命令会根据rockspec文件将当前Lua包打包成rock文件然后上传至luarocks.org,上传需要添加一个在官网配置过的api-key参数进行验证。

write_rockspec
 ~/Desktop/t-luarocks/ luarocks help write_rockspecNAME  /usr/local/bin/luarocks write_rockspec - Write a template for a rockspec file.SYNOPSIS  /usr/local/bin/luarocks write_rockspec [--output=<file> ...] [<name>] [<version>] {<url>|<path>}

write_rockspec命令在当前目录自动生成一个rockspec文件骨架,一个template,注意仅仅是一个骨架,有些参数必须落实,否则无法正常使用,比如source、description段的配置。

下面给出一个简单的rockspec文件模板,包含了rockspec文件的基本必选项:

package ="vanilla"version ="0.1.0-1"source ={    url ="git://github.com/idevz/vanilla.git" #必须为可访问的在线库地址}description ={    summary       ="A Lightweight Openresty Web Framework",    homepage      ="http://idevz.github.io/vanilla",    maintainer    ="zhoujing<zhoujing00k@gmail.com>",    license       ="MIT"                    #必须指明所基于的开源协议}dependencies ={    "lua=5.1",    ... ...    "lua-resty-http=0.06-0"}build ={    type ="builtin",    modules ={        ["vanilla.v.view"]                      ="vanilla/v/view.lua",        ... ...        ["vanilla.v.views.rtpl"]                ="vanilla/v/views/rtpl.lua",    },    install ={        bin ={ "bin/vanilla" }    },}
1 0
原创粉丝点击