xmake插件開發之類庫使用

xmake經過import接口,能夠在自定義腳本中導入各類內置類庫和擴展類庫模塊,使得xmake的插件開發具備更多的靈活性,提供更豐富的功能。git

咱們先看下,目前xmake提供的一些類庫:github

.
├── _g.lua
├── assert.lua
├── catch.lua
├── coroutine.lua
├── debug.lua
├── finally.lua
├── format.lua
├── ifelse.lua
├── import
│   └── core
│       ├── base
│       │   └── option.lua
│       ├── platform
│       │   ├── environment.lua
│       │   ├── menu.lua
│       │   └── platform.lua
│       ├── project
│       │   ├── cache.lua
│       │   ├── config.lua
│       │   ├── global.lua
│       │   ├── history.lua
│       │   ├── menu.lua
│       │   ├── package.lua
│       │   ├── project.lua
│       │   ├── target.lua
│       │   ├── task.lua
│       │   └── template.lua
│       └── tool
│           ├── compiler.lua
│           ├── linker.lua
│           └── tool.lua
├── import.lua
├── inherit.lua
├── insert.lua
├── io.lua
├── ipairs.lua
├── math.lua
├── os.lua
├── pairs.lua
├── path.lua
├── print.lua
├── printf.lua
├── raise.lua
├── string.lua
├── table.lua
├── tonumber.lua
├── tostring.lua
├── try.lua
├── utils.lua
└── vformat.lua

在根目錄下的模塊和api都是屬於內建的,不須要import也能夠直接使用,屬於經常使用api,提供了xmake最基礎的特性。。shell

在子目錄下的是擴展模塊,須要import後才能使用,導入規則見http://www.javashuo.com/tag/import,例如:api

import("core.project.task")

須要注意的是:xmake對自定義的腳本採用了異常處理機制,大部分狀況下,調用的api是不須要判斷返回值狀態是否成功,若是出錯了,會當即中斷,而且顯示錯誤信息安全

這樣語法上更加的精簡可讀,而且更安全,全部api的輸入輸出,內部都有檢測,狀態不對會當即自動報錯。lua

固然若是咱們想要本身獲取這個異常的狀態,作一些邏輯上的處理,能夠經過try/catch來實現,使用起來也很是簡單。spa

下面簡單介紹下一些經常使用的內置模塊api,這些模塊不須要import就可使用的哦。:).net

os模塊

-- 運行shell命令,若是運行失敗直接中斷,並顯示出錯信息,咱們不須要判斷返回值
os.run("echo hello xmake!")

-- 複製文件
os.cp("/tmp/src", "/tmp/dst")

-- 刪除文件或者目錄
os.rm("/tmp/dir")

-- 移動文件
os.mv("/tmp/old", "/tmp/new")

-- 判斷文件是否存在
if os.isfile("/tmp/file") then
end

-- 判斷目錄是否存在
if os.isdir("/tmp/dir") then
end

-- 匹配遍歷文件,*爲非遞歸匹配,**爲遞歸匹配
for _, file in ipairs(os.match("src/*.c")) do
    print(file)
end

-- 匹配遍歷目錄,*爲非遞歸匹配,**爲遞歸匹配
for _, file in ipairs(os.match("src/*", true)) do
    print(file)
end

經常使用api

-- 拋出異常,當即中斷
raise()

-- 拋出異常,當即中斷,並拋出異常錯誤信息
raise("error info")

-- 拋出異常,當即中斷,並拋出異常錯誤代碼
raise(-1)

-- 顯示輸出並換行,支持格式化輸出,跟lua的print稍有不一樣
print("hello %s", "xmake")

-- 顯示輸出不換行
printf("hello %s", "xmake")

-- 格式化字符串
s = format("hello %s", "xmake")

異常捕獲api

try
{
    -- try塊,裏面拋出異常
    function ()
        raise("error")
    end,

    catch
    {
        -- catch塊,捕獲異常
        function (errors)
            print(errors)
        end
    }
}

-- 獲取try塊的返回值,若是沒有異常的話返回true
local ok = try
{
    -- try塊,裏面拋出異常
    function ()
        -- may be error
        return true
    end
}

try
{
    -- try塊,裏面拋出異常
    function ()
        raise("error")
    end,

    catch
    {
        -- catch塊,捕獲異常
        function (errors)
            print(errors)
        end
    },

    finally
    { 
        -- finally 塊
        function ()
        end
    }
}

path模塊

-- 獲取相對路徑
path.relative("/tmp/a")

-- 獲取絕對路徑
path.absolute("src")

-- 獲取目錄
path.directory("/tmp/a")

-- 獲取文件名 test.c
path.filename("/tmp/test.c")

-- 獲取base名 test
path.basename("/tmp/test.c")

-- 獲取擴展名
path.extension("/tmp/test.c")

-- 拼接路徑 /tmp/test.c
path.join("/tmp", "test.c")

io 模塊

-- 打開一個寫文件
file = io.open("/tmp/a", "w")

-- 寫文件數據
file:write("hello")

-- 寫文件格式化行
file:print("hello %s", "xmake")

-- 寫文件格式化不換行
file:printf("hello %s", "xmake")

-- 關閉文件
file:close()

-- 序列化寫一個lua對象到文件
io.save("/tmp/a", object)

-- 反序列化讀取一個文件對象
object = io.load("/tmp/a")

-- 讀取文件數據,並顯示
io.cat("/tmp/a")

-- 模式替換文件內容, 替換空格字符爲 "space"
io.gsub("/tmp/a", "%s", "space")

還有一些是lua的經常使用模塊,這裏就很少說了,例如:string, table, debug, coroutine, pairs, ipairs, tostring, tonumber 等等插件


相關文章
相關標籤/搜索