插件開發之hello xmake

xmake從v2.0開始,全面支持插件模式,咱們能夠很方便的擴展實現本身的插件,而且xmake也提供了一些內建的使用插件git

咱們能夠執行下 xmake -h 看下當前支持的插件:github

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插件的開發,代碼以下:xcode

-- 定義一個名叫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 = {}
                })

這個插件的文件結構以下:bash

hello
 - xmake.lua

如今一個最簡單的插件寫完了,那怎麼讓它被xmake檢測到呢,有三種方式:ui

  1. 把 hello 這個文件夾放置在 xmake的插件安裝目錄 xmake/plugins,這個裏面都是些內建的插件
  2. 把 hello 文件夾防止在 ~/.xmake/plugins 用戶全局目錄,這樣對當前xmake 全局生效
  3. 把 hello 文件夾防止在任意地方,經過在工程描述文件xmake.lua中調用add_plugindirs("./hello") 添加當前的工程的插件搜索目錄,這樣只對當前工程生效

接下來,咱們嘗試運行下這個插件:lua

xmake hello

顯示結果:.net

hello xmake!

固然你能夠經過set_menu中添加一些自定義的參數,這個等後續再詳細介紹插件

最後咱們還能夠在target自定義的腳本中運行這個插件:命令行

target("demo")

    -- 構建以後運行插件
    after_build(function (target)

        -- 導入task模塊
        import("core.project.task")

        -- 運行插件任務
        task.run("hello")
    end)

相關文章
相關標籤/搜索