Xmake 完全支持插件模式,我们可以很方便地扩展实现自己的插件,并且 Xmake 也提供了一些内建的插件可供使用。
我们可以执行下 xmake -h 看下当前支持的插件:
Plugins:
l, lua Run the lua script.
m, macro Run the given macro.
doxygen Generate the doxygen document.
hello Hello xmake!
project Create the project file.
接下来我们介绍下本文的重点,一个简单的 hello xmake 插件的开发,代码如下:
-- 定义一个名叫hello的插件任务
task("hello")
-- 设置类型为插件
set_category("plugin")
-- 插件运行的入口
on_run(function ()
-- 显示hello xmake!
print("hello xmake!")
end)
-- 设置插件的命令行选项,这里没有任何参数选项,仅仅显示插件描述
set_menu {
-- usage
usage = "xmake hello [options]"
-- description
, description = "Hello xmake!"
-- options
, options = {}
}
这个插件的文件结构如下:
plugins
|-- hello
| |-- xmake.lua
|...
| plugins目录下无需xmake.lua
现在一个最简单的插件写完了,那怎么让它被xmake检测到呢,有三种方式:
xmake/plugins,这个里面都是一些内建的插件~/.xmake/plugins 用户全局目录,这样对当前 xmake 全局生效./plugins 目录下,通过在工程描述文件 xmake.lua 中调用 add_plugindirs("plugins") 添加当前工程的插件搜索目录,这样只对当前工程生效接下来,我们尝试运行下这个插件:
xmake hello
显示结果:
hello xmake!
最后我们还可以在 target 自定义的脚本中运行这个插件:
target("demo")
-- 构建之后运行插件
after_build(function (target)
-- 导入task模块
import("core.project.task")
-- 运行插件任务
task.run("hello")
end)