Skip to content

Commit

Permalink
build: add packing scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
futrime committed Jan 22, 2024
1 parent 54bb431 commit 8fe06c3
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 12 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ jobs:
with:
name: legacy-script-engine-${{ matrix.backend }}-windows-x64-${{ github.sha }}
path: |
build/windows/x64/release/legacy-script-engine-${{ matrix.backend }}.dll
build/windows/x64/release/legacy-script-engine-${{ matrix.backend }}.pdb
bin/
check-style:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
with:
name: legacy-script-engine-${{ matrix.backend }}-windows-x64-${{ github.sha }}
path: |
build/windows/x64/release/legacy-script-engine-${{ matrix.backend }}.dll
bin/
upload-to-release:
needs:
Expand Down
5 changes: 5 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "${pluginName}",
"entry": "${pluginFile}",
"type": "native"
}
118 changes: 118 additions & 0 deletions scripts/after_build.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
function beautify_json(value, indent)
import("core.base.json")
local json_text = ""
local stack = {}

local function escape_str(s)
return string.gsub(s, '[%c\\"]', function(c)
local replacements = {['\b'] = '\\b', ['\f'] = '\\f', ['\n'] = '\\n', ['\r'] = '\\r', ['\t'] = '\\t', ['"'] = '\\"', ['\\'] = '\\\\'}
return replacements[c] or string.format('\\u%04x', c:byte())
end)
end

local function is_null(v)
return v == json.null
end

local function is_empty_table(t)
if type(t) ~= 'table' then return false end
for _ in pairs(t) do
return false
end
return true
end

local function is_array(t)
return type(t) == 'table' and json.is_marked_as_array(t) or #t > 0
end

local function serialize(val, level)
local spaces = string.rep(" ", level * indent)

if type(val) == "table" and not stack[val] then
if is_empty_table(val) then
json_text = json_text .. (is_array(val) and "[]" or "{}")
return
end

stack[val] = true
local isArray = is_array(val)
json_text = json_text .. (isArray and "[\n" or "{\n")

local keys = isArray and {} or {}
for k in pairs(val) do
table.insert(keys, k)
end
if not isArray then
table.sort(keys)
end

for _, k in ipairs(keys) do
local v = val[k]
json_text = json_text .. spaces .. (isArray and "" or '"' .. escape_str(tostring(k)) .. '": ')
serialize(v, level + 1)
json_text = json_text .. ",\n"
end

json_text = string.sub(json_text, 1, -3) .. "\n" .. string.rep(" ", (level - 1) * indent) .. (isArray and "]" or "}")
stack[val] = nil
elseif type(val) == "string" then
json_text = json_text .. '"' .. escape_str(val) .. '"'
elseif type(val) == "number" then
if val % 1 == 0 then
json_text = json_text .. tostring(math.floor(val))
else
json_text = json_text .. tostring(val)
end
elseif type(val) == "boolean" then
json_text = json_text .. tostring(val)
elseif is_null(val) then
json_text = json_text .. "null"
else
error("Invalid value type: " .. type(val))
end
end
serialize(value, 1)
return json_text
end

function string_formatter(str, variables)
return str:gsub("%${(.-)}", function(var)
return variables[var] or "${" .. var .. "}"
end)
end

function pack_plugin(target,plugin_define)
import("lib.detect.find_file")

local manifest_path = find_file("manifest.json", os.projectdir())
if manifest_path then
local manifest = io.readfile(manifest_path)
local bindir = path.join(os.projectdir(), "bin")
local outputdir = path.join(bindir, plugin_define.pluginName)
local targetfile = path.join(outputdir, plugin_define.pluginFile)
local pdbfile = path.join(outputdir, path.basename(plugin_define.pluginFile) .. ".pdb")
local manifestfile = path.join(outputdir, "manifest.json")
local oritargetfile = target:targetfile()
local oripdbfile = path.join(path.directory(oritargetfile), path.basename(oritargetfile) .. ".pdb")

os.mkdir(outputdir)
os.cp(oritargetfile, targetfile)
if os.isfile(oripdbfile) then
os.cp(oripdbfile, pdbfile)
end

formattedmanifest = string_formatter(manifest, plugin_define)
io.writefile(manifestfile,formattedmanifest)
cprint("${bright green}[Plugin Packer]: ${reset}plugin already generated to " .. outputdir)
else
cprint("${bright yellow}warn: ${reset}not found manifest.json in root dir!")
end
end


return {
pack_plugin = pack_plugin,
beautify_json = beautify_json,
string_formatter = string_formatter
}
22 changes: 13 additions & 9 deletions xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,9 @@ target("legacy-script-engine")
)
add_defines(
"_HAS_CXX23=1", -- To enable C++23 features.
"_WIN32_WINNT=0x0601",
"_AMD64_",
"_CONSOLE",
"_WINDLL",
"_UNICODE",
"CPPHTTPLIB_OPENSSL_SUPPORT", -- To enable SSL support for cpp-httplib.
"NDEBUG",
"NOMINMAX",
"UNICODE",
"ENTT_PACKED_PAGE=128"
"NOMINMAX", -- To avoid conflicts with std::min and std::max.
"UNICODE" -- To enable Unicode support.
)
add_files(
"src/**.cpp"
Expand Down Expand Up @@ -122,3 +115,14 @@ target("legacy-script-engine")
"LLSE_BACKEND_QUICKJS"
)
end

after_build(function (target)
local plugin_packer = import("scripts.after_build")

local plugin_define = {
pluginName = target:basename(),
pluginFile = path.filename(target:targetfile()),
}

plugin_packer.pack_plugin(target,plugin_define)
end)

0 comments on commit 8fe06c3

Please sign in to comment.