Xmake 支持自定义任务和插件,它们都基于 task 任务系统实现。任务可以用于自动化构建流程、代码生成、文件处理等各种项目需求。
set_category("plugin") 分类set_menu() 设置,让任务可以通过命令行直接调用task("taskname")
on_run(function ()
-- 任务执行逻辑
print("任务执行中...")
end)
task("hello")
on_run(function ()
print("hello xmake!")
end)
这个任务只能在 xmake.lua 中通过 task.run() 调用:
target("test")
after_build(function (target)
import("core.project.task")
task.run("hello")
end)
通过 set_menu() 可以让任务通过命令行直接调用:
task("echo")
on_run(function ()
import("core.base.option")
-- 获取参数内容并显示
local contents = option.get("contents") or {}
local color = option.get("color") or "black"
cprint("${%s}%s", color, table.concat(contents, " "))
end)
set_menu {
usage = "xmake echo [options]",
description = "显示指定信息",
options = {
{'c', "color", "kv", "black", "设置输出颜色"},
{nil, "contents", "vs", nil, "要显示的内容"}
}
}
现在可以通过命令行调用:
$ xmake echo -c red hello xmake!
task("myplugin")
set_category("plugin") -- 分类为插件
on_run(function ()
print("这是一个插件")
end)
分类说明:
task("example")
on_run(function ()
import("core.base.option")
-- 获取不同类型的参数
local verbose = option.get("verbose") -- 布尔值
local color = option.get("color") -- 键值对
local files = option.get("files") -- 多值参数
local args = {...} -- 可变参数
end)
set_menu {
options = {
{'v', "verbose", "k", nil, "启用详细输出"}, -- 布尔选项
{'c', "color", "kv", "red", "设置颜色"}, -- 键值选项
{'f', "files", "vs", nil, "文件列表"}, -- 多值选项
{nil, "args", "vs", nil, "其他参数"} -- 可变参数
}
}
target("test")
set_kind("binary")
add_files("src/*.cpp")
after_build(function (target)
import("core.project.task")
-- 构建完成后运行代码生成任务
task.run("generate-code")
-- 运行测试任务
task.run("run-tests")
end)
-- 代码生成任务
task("generate-code")
on_run(function ()
print("生成代码...")
-- 执行代码生成逻辑
os.exec("protoc --cpp_out=src proto/*.proto")
end)
-- 测试任务
task("run-tests")
on_run(function ()
print("运行测试...")
os.exec("xmake run test")
end)
task("process-assets")
on_run(function ()
import("core.base.option")
local input_dir = option.get("input") or "assets"
local output_dir = option.get("output") or "build/assets"
-- 处理资源文件
os.mkdir(output_dir)
os.cp(path.join(input_dir, "*.png"), output_dir)
os.cp(path.join(input_dir, "*.json"), output_dir)
print("资源文件处理完成")
end)
set_menu {
usage = "xmake process-assets [options]",
description = "处理项目资源文件",
options = {
{'i', "input", "kv", "assets", "输入目录"},
{'o', "output", "kv", "build/assets", "输出目录"}
}
}
task("format")
on_run(function ()
import("core.base.option")
import("lib.detect.find_tool")
local tool = find_tool("clang-format")
if not tool then
raise("clang-format not found!")
end
local files = option.get("files") or {"src/**/*.cpp", "src/**/*.h"}
for _, pattern in ipairs(files) do
local filelist = os.files(pattern)
for _, file in ipairs(filelist) do
os.execv(tool.program, {"-i", file})
print("格式化文件:", file)
end
end
end)
set_menu {
usage = "xmake format [options]",
description = "格式化代码文件",
options = {
{'f', "files", "vs", nil, "要格式化的文件模式"}
}
}
task("clean-all")
on_run(function ()
local patterns = {
"build/**",
"*.log",
"*.tmp",
"*.o",
"*.a",
"*.so",
"*.dylib",
"*.exe"
}
for _, pattern in ipairs(patterns) do
os.tryrm(pattern)
end
print("项目清理完成")
end)
set_menu {
usage = "xmake clean-all",
description = "清理所有构建文件和临时文件"
}
$ xmake taskname [options] [args...]
import("core.project.task")
-- 调用任务
task.run("taskname")
-- 传递参数
task.run("taskname", {option1 = "value1"}, "arg1", "arg2")
target("test")
before_build(function (target)
task.run("prepare")
end)
after_build(function (target)
task.run("post-process")
end)
pcall 包装任务逻辑progress.show() 显示执行进度