67 lines
1.9 KiB
Lua
67 lines
1.9 KiB
Lua
#!/usr/bin/env lua
|
|
-- Generate plc.json from data_types folder
|
|
-- Usage: lua generate_plc_json.lua [--root <path>] [--output <file>]
|
|
|
|
local args = {...}
|
|
local script_dir = debug.getinfo(1, "S").source:gsub("^@", ""):match("(.*/)") or ""
|
|
if script_dir == "" then script_dir = "." end
|
|
|
|
local cwd = io.popen("pwd"):read("*l")
|
|
if script_dir:sub(1, 1) == "/" then
|
|
script_dir = script_dir:gsub("/+$", "")
|
|
elseif cwd then
|
|
script_dir = cwd .. "/" .. script_dir
|
|
script_dir = script_dir:gsub("/+", "/"):gsub("/$", "")
|
|
else
|
|
script_dir = "."
|
|
end
|
|
|
|
package.path = package.path .. ";" .. script_dir .. "/?.lua;" .. script_dir .. "/src/?.lua"
|
|
|
|
local json = dofile(script_dir .. "/src/json.lua")
|
|
local plc_json = dofile(script_dir .. "/src/plc_json.lua")
|
|
|
|
local root_dir = script_dir
|
|
local output_file = script_dir .. "/plc.data.json"
|
|
|
|
for i, arg in ipairs(args) do
|
|
if arg == "--root" and args[i+1] then
|
|
root_dir = args[i+1]
|
|
elseif arg == "--output" and args[i+1] then
|
|
output_file = args[i+1]
|
|
end
|
|
end
|
|
|
|
local types = plc_json.load_types_from_workspace(root_dir)
|
|
|
|
if next(types) == nil then
|
|
print("No types found in " .. root_dir)
|
|
os.exit(1)
|
|
end
|
|
|
|
local dataTypes = {}
|
|
for _, typ in pairs(types) do
|
|
if typ.kind == "struct" then
|
|
local members = {}
|
|
for _, field in ipairs(typ.fields) do
|
|
table.insert(members, { name = field.name, dataTypeName = field.type })
|
|
end
|
|
table.insert(dataTypes, { name = typ.name, struct = members })
|
|
elseif typ.kind == "alias" then
|
|
table.insert(dataTypes, { name = typ.name, baseType = typ.base_type or "UNKNOWN" })
|
|
end
|
|
end
|
|
|
|
local output = { plcProject = { dataTypes = dataTypes } }
|
|
local content = json.encode(output)
|
|
|
|
local file = io.open(output_file, "w")
|
|
if file then
|
|
file:write(content)
|
|
file:close()
|
|
print("Generated " .. output_file .. " with " .. #dataTypes .. " types")
|
|
else
|
|
print("Failed to write " .. output_file)
|
|
os.exit(1)
|
|
end
|