feat(build): 为示例项目添加打包时资源复制功能

为 push_box、hello_world、collision_demo 和 spatial_index_demo 示例项目添加以下功能:
1. 构建后生成 NRO 文件并打印日志
2. 打包时将 NRO 文件复制到 package 目录
3. 改进资源复制逻辑,支持子目录递归复制
4. 打包时将资源文件复制到 package 目录
This commit is contained in:
ChestnutYueyue 2026-02-10 20:46:53 +08:00
parent 23647b6458
commit b4036cd8dd
4 changed files with 132 additions and 9 deletions

View File

@ -20,6 +20,7 @@ target("collision_demo")
set_toolchains("switch") set_toolchains("switch")
set_targetdir("../../build/examples/collision_demo") set_targetdir("../../build/examples/collision_demo")
-- 构建后生成 NRO 文件
after_build(function (target) after_build(function (target)
local devkitPro = os.getenv("DEVKITPRO") or "C:/devkitPro" local devkitPro = os.getenv("DEVKITPRO") or "C:/devkitPro"
local elf_file = target:targetfile() local elf_file = target:targetfile()
@ -37,6 +38,17 @@ target("collision_demo")
else else
os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file}) os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file})
end end
print("Generated NRO: " .. nro_file)
end
end)
-- 打包时将 NRO 文件复制到 package 目录
after_package(function (target)
local nro_file = path.join(target:targetdir(), "collision_demo.nro")
local package_dir = target:packagedir()
if os.isfile(nro_file) and package_dir then
os.cp(nro_file, package_dir)
print("Copied NRO to package: " .. package_dir)
end end
end) end)
@ -46,20 +58,39 @@ target("collision_demo")
set_targetdir("../../build/examples/collision_demo") set_targetdir("../../build/examples/collision_demo")
add_ldflags("-mwindows", {force = true}) add_ldflags("-mwindows", {force = true})
-- 复制资源 -- 复制资源到输出目录
after_build(function (target) after_build(function (target)
local romfs = path.join(example_dir, "romfs") local romfs = path.join(example_dir, "romfs")
if os.isdir(romfs) then if os.isdir(romfs) then
local target_dir = path.directory(target:targetfile()) local target_dir = path.directory(target:targetfile())
local assets_dir = path.join(target_dir, "assets") local assets_dir = path.join(target_dir, "assets")
-- 创建 assets 目录
if not os.isdir(assets_dir) then if not os.isdir(assets_dir) then
os.mkdir(assets_dir) os.mkdir(assets_dir)
end end
os.cp(path.join(romfs, "assets/*"), assets_dir)
-- 复制所有资源文件(包括子目录)
os.cp(path.join(romfs, "assets/**"), assets_dir)
print("Copied assets from " .. romfs .. " to " .. assets_dir) print("Copied assets from " .. romfs .. " to " .. assets_dir)
else else
print("Warning: romfs directory not found at " .. romfs) print("Warning: romfs directory not found at " .. romfs)
end end
end) end)
-- 打包时将资源复制到 package 目录
after_package(function (target)
local target_dir = path.directory(target:targetfile())
local assets_dir = path.join(target_dir, "assets")
local package_dir = target:packagedir()
if os.isdir(assets_dir) and package_dir then
local package_assets = path.join(package_dir, "assets")
if not os.isdir(package_assets) then
os.mkdir(package_assets)
end
os.cp(path.join(assets_dir, "**"), package_assets)
print("Copied assets to package: " .. package_assets)
end
end)
end end
target_end() target_end()

View File

@ -20,7 +20,7 @@ target("hello_world")
set_toolchains("switch") set_toolchains("switch")
set_targetdir("../../build/examples/hello_world") set_targetdir("../../build/examples/hello_world")
-- 生成 NRO -- 构建后生成 NRO 文件
after_build(function (target) after_build(function (target)
local devkitPro = os.getenv("DEVKITPRO") or "C:/devkitPro" local devkitPro = os.getenv("DEVKITPRO") or "C:/devkitPro"
local elf_file = target:targetfile() local elf_file = target:targetfile()
@ -38,6 +38,17 @@ target("hello_world")
else else
os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file}) os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file})
end end
print("Generated NRO: " .. nro_file)
end
end)
-- 打包时将 NRO 文件复制到 package 目录
after_package(function (target)
local nro_file = path.join(target:targetdir(), "hello_world.nro")
local package_dir = target:packagedir()
if os.isfile(nro_file) and package_dir then
os.cp(nro_file, package_dir)
print("Copied NRO to package: " .. package_dir)
end end
end) end)
@ -47,20 +58,39 @@ target("hello_world")
set_targetdir("../../build/examples/hello_world") set_targetdir("../../build/examples/hello_world")
add_ldflags("-mwindows", {force = true}) add_ldflags("-mwindows", {force = true})
-- 复制资源 -- 复制资源到输出目录
after_build(function (target) after_build(function (target)
local romfs = path.join(example_dir, "romfs") local romfs = path.join(example_dir, "romfs")
if os.isdir(romfs) then if os.isdir(romfs) then
local target_dir = path.directory(target:targetfile()) local target_dir = path.directory(target:targetfile())
local assets_dir = path.join(target_dir, "assets") local assets_dir = path.join(target_dir, "assets")
-- 创建 assets 目录
if not os.isdir(assets_dir) then if not os.isdir(assets_dir) then
os.mkdir(assets_dir) os.mkdir(assets_dir)
end end
os.cp(path.join(romfs, "assets/*"), assets_dir)
-- 复制所有资源文件(包括子目录)
os.cp(path.join(romfs, "assets/**"), assets_dir)
print("Copied assets from " .. romfs .. " to " .. assets_dir) print("Copied assets from " .. romfs .. " to " .. assets_dir)
else else
print("Warning: romfs directory not found at " .. romfs) print("Warning: romfs directory not found at " .. romfs)
end end
end) end)
-- 打包时将资源复制到 package 目录
after_package(function (target)
local target_dir = path.directory(target:targetfile())
local assets_dir = path.join(target_dir, "assets")
local package_dir = target:packagedir()
if os.isdir(assets_dir) and package_dir then
local package_assets = path.join(package_dir, "assets")
if not os.isdir(package_assets) then
os.mkdir(package_assets)
end
os.cp(path.join(assets_dir, "**"), package_assets)
print("Copied assets to package: " .. package_assets)
end
end)
end end
target_end() target_end()

View File

@ -20,6 +20,7 @@ target("push_box")
set_toolchains("switch") set_toolchains("switch")
set_targetdir("../../build/examples/push_box") set_targetdir("../../build/examples/push_box")
-- 构建后生成 NRO 文件
after_build(function (target) after_build(function (target)
local devkitPro = os.getenv("DEVKITPRO") or "C:/devkitPro" local devkitPro = os.getenv("DEVKITPRO") or "C:/devkitPro"
local elf_file = target:targetfile() local elf_file = target:targetfile()
@ -37,6 +38,17 @@ target("push_box")
else else
os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file}) os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file})
end end
print("Generated NRO: " .. nro_file)
end
end)
-- 打包时将 NRO 文件复制到 package 目录
after_package(function (target)
local nro_file = path.join(target:targetdir(), "push_box.nro")
local package_dir = target:packagedir()
if os.isfile(nro_file) and package_dir then
os.cp(nro_file, package_dir)
print("Copied NRO to package: " .. package_dir)
end end
end) end)
@ -46,20 +58,39 @@ target("push_box")
set_targetdir("../../build/examples/push_box") set_targetdir("../../build/examples/push_box")
add_ldflags("-mwindows", {force = true}) add_ldflags("-mwindows", {force = true})
-- 复制资源 -- 复制资源到输出目录
after_build(function (target) after_build(function (target)
local romfs = path.join(example_dir, "romfs") local romfs = path.join(example_dir, "romfs")
if os.isdir(romfs) then if os.isdir(romfs) then
local target_dir = path.directory(target:targetfile()) local target_dir = path.directory(target:targetfile())
local assets_dir = path.join(target_dir, "assets") local assets_dir = path.join(target_dir, "assets")
-- 创建 assets 目录
if not os.isdir(assets_dir) then if not os.isdir(assets_dir) then
os.mkdir(assets_dir) os.mkdir(assets_dir)
end end
os.cp(path.join(romfs, "assets/*"), assets_dir)
-- 复制所有资源文件(包括子目录)
os.cp(path.join(romfs, "assets/**"), assets_dir)
print("Copied assets from " .. romfs .. " to " .. assets_dir) print("Copied assets from " .. romfs .. " to " .. assets_dir)
else else
print("Warning: romfs directory not found at " .. romfs) print("Warning: romfs directory not found at " .. romfs)
end end
end) end)
-- 打包时将资源复制到 package 目录
after_package(function (target)
local target_dir = path.directory(target:targetfile())
local assets_dir = path.join(target_dir, "assets")
local package_dir = target:packagedir()
if os.isdir(assets_dir) and package_dir then
local package_assets = path.join(package_dir, "assets")
if not os.isdir(package_assets) then
os.mkdir(package_assets)
end
os.cp(path.join(assets_dir, "**"), package_assets)
print("Copied assets to package: " .. package_assets)
end
end)
end end
target_end() target_end()

View File

@ -20,6 +20,7 @@ target("spatial_index_demo")
set_toolchains("switch") set_toolchains("switch")
set_targetdir("../../build/examples/spatial_index_demo") set_targetdir("../../build/examples/spatial_index_demo")
-- 构建后生成 NRO 文件
after_build(function (target) after_build(function (target)
local devkitPro = os.getenv("DEVKITPRO") or "C:/devkitPro" local devkitPro = os.getenv("DEVKITPRO") or "C:/devkitPro"
local elf_file = target:targetfile() local elf_file = target:targetfile()
@ -37,6 +38,17 @@ target("spatial_index_demo")
else else
os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file}) os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file})
end end
print("Generated NRO: " .. nro_file)
end
end)
-- 打包时将 NRO 文件复制到 package 目录
after_package(function (target)
local nro_file = path.join(target:targetdir(), "spatial_index_demo.nro")
local package_dir = target:packagedir()
if os.isfile(nro_file) and package_dir then
os.cp(nro_file, package_dir)
print("Copied NRO to package: " .. package_dir)
end end
end) end)
@ -46,20 +58,39 @@ target("spatial_index_demo")
set_targetdir("../../build/examples/spatial_index_demo") set_targetdir("../../build/examples/spatial_index_demo")
add_ldflags("-mwindows", {force = true}) add_ldflags("-mwindows", {force = true})
-- 复制资源 -- 复制资源到输出目录
after_build(function (target) after_build(function (target)
local romfs = path.join(example_dir, "romfs") local romfs = path.join(example_dir, "romfs")
if os.isdir(romfs) then if os.isdir(romfs) then
local target_dir = path.directory(target:targetfile()) local target_dir = path.directory(target:targetfile())
local assets_dir = path.join(target_dir, "assets") local assets_dir = path.join(target_dir, "assets")
-- 创建 assets 目录
if not os.isdir(assets_dir) then if not os.isdir(assets_dir) then
os.mkdir(assets_dir) os.mkdir(assets_dir)
end end
os.cp(path.join(romfs, "assets/*"), assets_dir)
-- 复制所有资源文件(包括子目录)
os.cp(path.join(romfs, "assets/**"), assets_dir)
print("Copied assets from " .. romfs .. " to " .. assets_dir) print("Copied assets from " .. romfs .. " to " .. assets_dir)
else else
print("Warning: romfs directory not found at " .. romfs) print("Warning: romfs directory not found at " .. romfs)
end end
end) end)
-- 打包时将资源复制到 package 目录
after_package(function (target)
local target_dir = path.directory(target:targetfile())
local assets_dir = path.join(target_dir, "assets")
local package_dir = target:packagedir()
if os.isdir(assets_dir) and package_dir then
local package_assets = path.join(package_dir, "assets")
if not os.isdir(package_assets) then
os.mkdir(package_assets)
end
os.cp(path.join(assets_dir, "**"), package_assets)
print("Copied assets to package: " .. package_assets)
end
end)
end end
target_end() target_end()