|
| 1 | +local dkjson = require "dkjson" |
| 2 | + |
| 3 | +local function fetchBuilds(path, buildList) |
| 4 | + buildList = buildList or {} |
| 5 | + for file in lfs.dir(path) do |
| 6 | + if file ~= "." and file ~= ".." then |
| 7 | + local f = path .. '/' .. file |
| 8 | + local attr = lfs.attributes(f) |
| 9 | + assert(type(attr) == "table") |
| 10 | + if attr.mode == "directory" then |
| 11 | + fetchBuilds(f, buildList) |
| 12 | + else |
| 13 | + if file:match("^.+(%..+)$") == ".json" then |
| 14 | + local fileHnd, errMsg = io.open(f, "r") |
| 15 | + if not fileHnd then |
| 16 | + return nil, errMsg |
| 17 | + end |
| 18 | + local fileText = fileHnd:read("*a") |
| 19 | + fileHnd:close() |
| 20 | + local fullCharData = dkjson.decode(fileText) |
| 21 | + buildList[f] = fullCharData.character |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + end |
| 26 | + return buildList |
| 27 | +end |
| 28 | + |
| 29 | +function buildTable(tableName, values, string, indent) |
| 30 | + string = string or "" |
| 31 | + indent = indent or 0 |
| 32 | + local indentStr = string.rep("\t", indent) |
| 33 | + local nextIndentStr = string.rep("\t", indent + 1) |
| 34 | + string = string .. indentStr .. tableName .. " = {\n" |
| 35 | + for key, value in pairsSortByKey(values) do |
| 36 | + if type(value) == "table" then |
| 37 | + string = buildTable("[\"" .. key .. "\"]", value, string, indent + 1) .. ",\n" |
| 38 | + elseif type(value) == "boolean" then |
| 39 | + string = string .. nextIndentStr .. "[\"" .. key .. "\"] = " .. (value and "true" or "false") .. ",\n" |
| 40 | + elseif type(value) == "string" then |
| 41 | + string = string .. nextIndentStr .. "[\"" .. key .. "\"] = \"" .. value .. "\",\n" |
| 42 | + else |
| 43 | + string = string .. nextIndentStr .. "[\"" .. key .. "\"] = " .. round(value, 4) .. ",\n" |
| 44 | + end |
| 45 | + end |
| 46 | + string = string .. indentStr .. "}" |
| 47 | + return string |
| 48 | +end |
| 49 | + |
| 50 | +local function formatXmlFile(filepath) |
| 51 | + local command = "xmllint --c14n " .. filepath |
| 52 | + |
| 53 | + -- Open the command process for reading ('r') |
| 54 | + local handle = io.popen(command, 'r') |
| 55 | + if not handle then |
| 56 | + return nil, "Failed to run xmllint. Is it installed and in your PATH?" |
| 57 | + end |
| 58 | + |
| 59 | + -- Read the entire output from the command |
| 60 | + local result = handle:read("*a") |
| 61 | + handle:close() |
| 62 | + |
| 63 | + local fileHnd, errMsg = io.open(filepath:gsub("-unformatted", ""), "w") |
| 64 | + fileHnd:write(result) |
| 65 | + fileHnd:close() |
| 66 | +end |
| 67 | + |
| 68 | +expose("generate all builds", function() |
| 69 | + local buildList = fetchBuilds("../spec/TestBuilds") |
| 70 | + for filename, testBuild in pairs(buildList) do |
| 71 | + local buildName = filename:gsub("^.*/([^/]+/[^/]+)%.[^.]+$", "%1") |
| 72 | + |
| 73 | + it("on build: " .. buildName, function() |
| 74 | + -- Load the build and calculate stats |
| 75 | + print("Loading build: " .. buildName) |
| 76 | + loadBuildFromJSON(testBuild, testBuild) |
| 77 | + |
| 78 | + print("Saving main output") |
| 79 | + -- Save the calculated output to a Lua file in the generated sub-folder |
| 80 | + -- Get rid of circular reference issues first |
| 81 | + build.calcsTab.mainOutput.ReqDexItem = nil |
| 82 | + build.calcsTab.mainOutput.ReqIntItem = nil |
| 83 | + build.calcsTab.mainOutput.ReqStrItem = nil |
| 84 | + local outputFile = filename:gsub("^(.+)%..+$", "%1.lua") |
| 85 | + outputFile = outputFile:gsub("^(.+)/([^/]+)$", "%1/generated/%2") |
| 86 | + local fileHnd, errMsg = io.open(outputFile, "w+") |
| 87 | + fileHnd:write(buildTable("output", build.calcsTab.mainOutput)) |
| 88 | + fileHnd:close() |
| 89 | + |
| 90 | + -- Save the XML DB file in the generated sub-folder |
| 91 | + print("Saving xml db file") |
| 92 | + build.dbFileName = outputFile:gsub("^(.+)%..+$", "%1-unformatted.xml") |
| 93 | + build:SaveDBFile() |
| 94 | + -- Format/order the XML file to easily see differences with previous generations |
| 95 | + formatXmlFile(build.dbFileName) |
| 96 | + -- Remove the unformatted XML file |
| 97 | + os.remove(build.dbFileName) |
| 98 | + end) |
| 99 | + end |
| 100 | +end) |
0 commit comments