|
| 1 | +-- |
| 2 | +-- Name: vscode.lua |
| 3 | +-- Purpose: Define the vscode action(s). |
| 4 | +-- Author: Ryan Pusztai |
| 5 | +-- Modified by: Andrea Zanellato |
| 6 | +-- Andrew Gough |
| 7 | +-- Manu Evans |
| 8 | +-- Jason Perkins |
| 9 | +-- Yehonatan Ballas |
| 10 | +-- Jan "GamesTrap" Schürkamp |
| 11 | +-- Created: 2013/05/06 |
| 12 | +-- Updated: 2022/12/29 |
| 13 | +-- Copyright: (c) 2008-2020 Jason Perkins and the Premake project |
| 14 | +-- (c) 2022-2023 Jan "GamesTrap" Schürkamp |
| 15 | +-- |
| 16 | + |
| 17 | +local p = premake |
| 18 | + |
| 19 | +p.modules.vscode = {} |
| 20 | +p.modules.vscode._VERSION = p._VERSION |
| 21 | + |
| 22 | +local vscode = p.modules.vscode |
| 23 | +local project = p.project |
| 24 | + |
| 25 | +p.api.register { |
| 26 | + name = "vscode_makefile", |
| 27 | + scope = "workspace", |
| 28 | + display = "[vscode] makefile location", |
| 29 | + description = "Location of the makefile", |
| 30 | + kind = "string", |
| 31 | + tokens = true, |
| 32 | +} |
| 33 | + |
| 34 | +p.api.register { |
| 35 | + name = "vscode_solution", |
| 36 | + scope = "workspace", |
| 37 | + display = "[VSCode] sln location", |
| 38 | + description = "Location of the solution file", |
| 39 | + kind = "string", |
| 40 | + tokens = true, |
| 41 | +} |
| 42 | + |
| 43 | +p.api.register { |
| 44 | + name = "vscode_launch_visualizerFile", |
| 45 | + scope = "project", |
| 46 | + display = "[VSCode] launchdebugger NatVis file", |
| 47 | + description = "Debugger NatVis file", |
| 48 | + kind = "string", |
| 49 | + tokens = true, |
| 50 | +} |
| 51 | + |
| 52 | +p.api.register { |
| 53 | + name = "vscode_launch_cwd", |
| 54 | + scope = "config", |
| 55 | + display = "[VSCode] launch working directory", |
| 56 | + description = "Working directory of launched app", |
| 57 | + kind = "string", |
| 58 | + tokens = true, |
| 59 | +} |
| 60 | + |
| 61 | +p.api.register { |
| 62 | + name = "vscode_launch_environment", |
| 63 | + scope = "config", |
| 64 | + display = "[VSCode] launch environment strings", |
| 65 | + description = "Environment strings of launched app", |
| 66 | + kind = "table", |
| 67 | + tokens = true, |
| 68 | +} |
| 69 | + |
| 70 | +p.api.register { |
| 71 | + name = "vscode_launch_args", |
| 72 | + scope = "config", |
| 73 | + display = "[VSCode] launch arguments", |
| 74 | + description = "Launched app arguments", |
| 75 | + kind = "list:string", |
| 76 | + tokens = true, |
| 77 | +} |
| 78 | + |
| 79 | +local function splitStr(instr, separator) |
| 80 | + if separator == nil then |
| 81 | + separator = "%s" |
| 82 | + end |
| 83 | + local t = {} |
| 84 | + for str in string.gmatch(instr, "([^".. separator .. "]+)") do |
| 85 | + table.insert(t, str) |
| 86 | + end |
| 87 | + return t |
| 88 | +end |
| 89 | + |
| 90 | +local function longestCommonPath(prj) |
| 91 | + local files = {} |
| 92 | + for _, f in ipairs(prj.files) do |
| 93 | + table.insert(files, f) |
| 94 | + end |
| 95 | + if #files == 0 then return './' end |
| 96 | + |
| 97 | + local commonParts = splitStr(files[1], "/") |
| 98 | + for i = 2, #files do |
| 99 | + local parts = splitStr(files[i], "/") |
| 100 | + for j = #commonParts, 1, -1 do |
| 101 | + if parts[j] ~= commonParts[j] then |
| 102 | + commonParts[j] = nil |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + local commonPartsClean = {} |
| 107 | + for _, v in ipairs(commonParts) do |
| 108 | + if v ~= nil then |
| 109 | + table.insert(commonPartsClean, v) |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + local result = table.concat(commonPartsClean, "/") |
| 114 | + if files[1]:sub(1, 1) == "/" then |
| 115 | + result = "/" .. result |
| 116 | + end |
| 117 | + |
| 118 | + return result |
| 119 | +end |
| 120 | + |
| 121 | +function vscode.generateWorkspace(wks) |
| 122 | + -- Only create workspace file if it doesnt already exist |
| 123 | + |
| 124 | + local codeWorkspaceFile = io.open(wks.location .. "/" .. wks.name .. ".code-workspace", "w") |
| 125 | + codeWorkspaceFile:write('{\n\t"folders":\n\t[') |
| 126 | + codeWorkspaceFile:write('\n\t\t{\n\t\t\t"path": ".",\n\t\t},') |
| 127 | + |
| 128 | + local makefileLocation = '' |
| 129 | + if wks.vscode_makefile ~= nil then |
| 130 | + makefileLocation = '/'..wks.vscode_makefile |
| 131 | + end |
| 132 | + |
| 133 | + -- Create tasks.json file |
| 134 | + local tasksFile = io.open(wks.location .. "/.vscode/tasks.json", "w") |
| 135 | + tasksFile:write('{\n\t"version": "2.0.0",\n\t"options":\n\t{\n') |
| 136 | + tasksFile:write(string.format('\t\t"cwd": "${workspaceRoot}%s",\n\t},\n', makefileLocation)) |
| 137 | + tasksFile:write('\t"tasks":\n\t[\n') |
| 138 | + |
| 139 | + -- Create launch.json file |
| 140 | + local launchFile = io.open(wks.location .. "/.vscode/launch.json", "w") |
| 141 | + launchFile:write('{\n\t"version": "2.0.0",\n\t"configurations":\n\t[\n') |
| 142 | + |
| 143 | + -- Create c_cpp_properties.json file |
| 144 | + local propsFile = io.open(wks.location .. "/.vscode/c_cpp_properties.json", "w") |
| 145 | + propsFile:write('{\n\t"version": 4,\n\t"configurations":\n\t[\n') |
| 146 | + |
| 147 | + -- For each project |
| 148 | + for prj in p.workspace.eachproject(wks) do |
| 149 | + if project.isc(prj) or project.iscpp(prj) then |
| 150 | + vscode.project.vscode_tasks(prj, tasksFile) |
| 151 | + vscode.project.vscode_launch(prj, launchFile) |
| 152 | + vscode.project.vscode_c_cpp_properties(prj, propsFile) |
| 153 | + |
| 154 | + --local sourcePath = path.getrelative(prj.workspace.location, longestCommonPath(prj)) |
| 155 | + --codeWorkspaceFile:write(string.format('\n\t\t{\n\t\t\t"name": "%s",\n\t\t\t"path": "%s",\n\t\t},', prj.name, sourcePath)) |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + propsFile:write('\t]\n}\n') |
| 160 | + propsFile:close() |
| 161 | + |
| 162 | + launchFile:write('\t]\n}\n') |
| 163 | + launchFile:close() |
| 164 | + |
| 165 | + tasksFile:write('\t]\n}\n') |
| 166 | + tasksFile:close() |
| 167 | + |
| 168 | + codeWorkspaceFile:write('\n\t],\n}\n') |
| 169 | + codeWorkspaceFile:close() |
| 170 | +end |
| 171 | + |
| 172 | +include("vscode_project.lua") |
| 173 | + |
| 174 | +include("_preload.lua") |
| 175 | + |
| 176 | +return vscode |
0 commit comments