Skip to content

Instantly share code, notes, and snippets.

@leidegre
Created June 29, 2024 08:37
Show Gist options
  • Save leidegre/070abc4c54df5e034104e42707ac25ed to your computer and use it in GitHub Desktop.
Save leidegre/070abc4c54df5e034104e42707ac25ed to your computer and use it in GitHub Desktop.
tasks.json and launch.json generation for Tundra2
module(..., package.seeall)
local native = require "tundra.native"
local nodegen = require "tundra.nodegen"
local path = require "tundra.path"
local util = require "tundra.util"
local ide_com = require "tundra.ide.ide-common"
local project_types = ide_com.project_types
local vscode_generator = {}
vscode_generator.__index = vscode_generator
local function to_json(obj, f, indent)
indent = indent or ""
if type(obj) ~= "table" then
if type(obj) == "string" then
f:write('"')
f:write(obj)
f:write('"')
else
f:write(tostring(obj)) -- boolean, number
end
else
-- table or array?
if obj[1] then
-- array
local indent2 = indent .. " "
f:write("[\n", indent2)
for i, v in ipairs(obj) do
if 1 < i then
f:write(",\n", indent2)
end
to_json(v, f, indent2)
end
f:write("\n", indent, "]")
else
local indent2 = indent .. " "
f:write(indent, "{\n", indent2)
local k, v = next(obj)
if k then
to_json(k, f, indent2)
f:write(": ")
to_json(v, f, indent2)
k, v = next(obj, k)
while k do
f:write(",\n", indent2)
to_json(k, f, indent2)
f:write(": ")
to_json(v, f, indent2)
k, v = next(obj, k)
end
end
f:write("\n", indent, "}")
end
end
end
function vscode_generator:generate_files(ngen, config_tuples, raw_nodes, env, default_nodes, hints, ide_script)
assert(config_tuples and #config_tuples > 0)
local units = util.filter(raw_nodes, function (u)
return u.Decl.Name and project_types[u.Keyword]
end)
-- DAG nodes have:
-- inputs
-- action
-- annotation
-- pass
-- aux_outputs
-- expensive
-- overwrite_outputs
-- deps
-- src_env
-- env
-- outputs
local tasks = {}
local configurations = {}
for _, unit in ipairs(units) do
local decl = unit.Decl
local dag_nodes = assert(decl.__DagNodes, "no dag nodes for " .. decl.Name)
for build_id, dag_node in pairs(dag_nodes) do
local label = decl.Name .. " (" .. build_id .. ")"
tasks[#tasks + 1] = {
type = "process",
label = label,
command = "tundra2.exe",
args = {
build_id,
decl.Name
},
problemMatcher = {
"$msCompile"
},
group = {
kind = "build",
isDefault = default_nodes[dag_node] or false
}
}
for _, output in ipairs(dag_node.outputs) do
if output:match("%.exe") then
configurations[#configurations + 1] = {
name = label,
type = "cppvsdbg",
request = "launch",
program = "${workspaceFolder}/" .. output:gsub("\\", "/"),
cwd = "${workspaceFolder}/t2-output/" .. build_id,
preLaunchTask = label,
visualizerFile = "${workspaceFolder}/game.natvis",
console = "integratedTerminal"
}
break
end
end
end
end
local tasks_json = assert(io.open(".vscode/tasks.json.tmp", 'wb'))
to_json({
["//"]= {"This file was generated by a tool. DO NOT EDIT.", "tundra2 -g vscode"},
version = "2.0.0",
tasks = tasks
}, tasks_json)
tasks_json:close()
ide_com.replace_if_changed(".vscode/tasks.json.tmp", ".vscode/tasks.json")
local launch_json = assert(io.open(".vscode/launch.json.tmp", 'wb'))
to_json({
["//"]= {"This file was generated by a tool. DO NOT EDIT.", "tundra2 -g vscode"},
version = "0.2.0",
configurations = configurations
}, launch_json)
launch_json:close()
ide_com.replace_if_changed(".vscode/launch.json.tmp", ".vscode/launch.json")
end
-- https://github.com/deplinenoise/tundra/blob/e48e03bbd8a193889337d25b42cfbd64c64c1c33/scripts/tundra/nodegen.lua#L816-L822
nodegen.set_ide_backend(function(...)
local state = setmetatable({}, vscode_generator)
state:generate_files(...)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment