插件开发之hello xmake

xmake从v2.0开始,全面支持插件模式,我们可以很方便的扩展实现自己的插件,并且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.
  • lua: 运行lua脚本的插件
  • macro: 这个很实用,宏脚本插件,可以手动录制多条xmake命令并且回放,也可以通过脚本实现一些复杂的宏脚本,这个我们后续会更加详细的介绍
  • doxygen:一键生成doxygen文档的插件
  • hello: 插件demo,仅仅显示一句话:'hello xmake!'
  • project: 生成工程文件的插件,目前仅支持(makefile),后续还会支持(vs,xcode等工程)的生成

接下来我们介绍下本文的重点,一个简单的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 = {}
                }) 

这个插件的文件结构如下:

hello
 - xmake.lua

现在一个最简单的插件写完了,那怎么让它被xmake检测到呢,有三种方式:

  1. 把 hello 这个文件夹放置在 xmake的插件安装目录 xmake/plugins,这个里面都是些内建的插件
  2. 把 hello 文件夹防止在 ~/.xmake/plugins 用户全局目录,这样对当前xmake 全局生效
  3. 把 hello 文件夹防止在任意地方,通过在工程描述文件xmake.lua中调用add_plugindirs("./hello") 添加当前的工程的插件搜索目录,这样只对当前工程生效

接下来,我们尝试运行下这个插件:

xmake hello

显示结果:

hello xmake!

当然你可以通过set_menu中添加一些自定义的参数,这个等后续再详细介绍

最后我们还可以在target自定义的脚本中运行这个插件:


target("demo")

    -- 构建之后运行插件
    after_build(function (target)

        -- 导入task模块
        import("core.project.task")

        -- 运行插件任务
        task.run("hello")
    end)

转载于:https://my.oschina.net/tboox/blog/694396

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值