-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbazel.lua
217 lines (190 loc) · 6.15 KB
/
bazel.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
local bazel = require("bazel")
local M = {}
local function BufDir()
local bufnr = vim.fn.bufnr()
return vim.fn.expand(("#%d:p:h"):format(bufnr))
end
local function Split(s, delimiter)
local result = {}
for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
table.insert(result, match)
end
return result
end
local function StartDebugger(type, program, args, cwd, env, workspace)
require("dap").run({
name = "Launch",
type = type,
request = "launch",
program = function()
return program
end,
env = env,
args = args,
cwd = cwd,
runInTerminal = false,
stopOnEntry = false,
setupCommands = { { text = "-enable-pretty-printing", ignoreFailures = true } },
-- sourceFileMap = { ["/proc/self/cwd"] = workspace },
})
end
function M.YankLabel()
local label = vim.fn.GetLabel()
print("yanking " .. label .. ' to + and " register')
vim.fn.setreg("+", label)
vim.fn.setreg('"', label)
end
local function get_python_imports(bazel_info)
local command = "grep 'python_imports =' " .. bazel_info.executable .. [[ | sed "s|.*'\(.*\)'|\1|"]]
return vim.fn.trim(vim.fn.system(command))
end
local function get_python_executable(bazel_info)
local command = [[grep -oP "rel_path = '.*'" ]]
.. bazel_info.executable
.. [[ | grep -o "'.*'" | tail -c +2 | head -c -2]]
return bazel_info.runfiles .. "/" .. vim.fn.trim(vim.fn.system(command))
end
local function get_bazel_python_modules(bazel_info)
local imports = Split(get_python_imports(bazel_info), ":")
local extra_paths = { bazel_info.runfiles, bazel_info.runfiles .. "/" .. bazel_info.workspace_name }
for _, import in pairs(imports) do
table.insert(extra_paths, bazel_info.runfiles .. "/" .. import)
end
return extra_paths
end
local function construct_python_path(extra_paths)
local env = ""
local sep = ""
for _, extra_path in pairs(extra_paths) do
env = env .. sep .. extra_path
sep = ":"
end
return env
end
local function get_python_path(bazel_info)
local extra_paths = get_bazel_python_modules(bazel_info)
return construct_python_path(extra_paths)
end
local function save_pyright_config_json(extra_paths, include)
local config = { typeCheckingMode = "off", extraPaths = extra_paths, include = include }
local json = { vim.fn.json_encode(config) }
vim.fn.writefile(json, "pyrightconfig.json")
print("Created pyrightconfig.json")
end
local function get_keys(t)
local keys = {}
for key, _ in pairs(t) do
table.insert(keys, key)
end
return keys
end
function M.create_pyright_config(target, include)
local on_success = function(bazel_info)
local Path = require("plenary.path")
local extra_paths = {}
local ws_name = bazel_info.workspace_name
local workspace = bazel_info.workspace
for _, line in pairs(bazel_info.stdout) do
local depset = line:match("depset%(%[(.*)%]") or ""
for extra_path in depset:gmatch('"(.-)"') do
if extra_path:match("^" .. ws_name) then
for _, pattern in pairs({ workspace, workspace .. "/bazel-bin" }) do
local path = extra_path:gsub("^" .. ws_name, pattern)
if Path:new(path):is_dir() then
extra_paths[path] = true
end
end
else
extra_paths[workspace .. "/external/" .. extra_path] = true
end
end
end
save_pyright_config_json(get_keys(extra_paths), include)
end
bazel.cquery(
vim.g.bazel_config
.. " 'kind(py_.*,"
.. target
.. ")' --output starlark --starlark:expr 'providers(target)[\"PyInfo\"].imports'",
{ on_success = on_success }
)
end
function M.DebugBazelPyInput()
local args = ""
local command = vim.fn.input("add bazel debug without 'bazel run': ")
local split_command = vim.split(command, "--")
local start_debugger = function(bazel_info)
local cwd = bazel_info.runfiles .. "/" .. bazel_info.workspace_name
StartDebugger(type, get_program(bazel_info), args, cwd, get_env(bazel_info), bazel_info.workspace)
end
bazel.run("build", args, split_command[1], bazel.get_workspace(), { on_success = start_debugger })
end
function M.DebugBazel(type, bazel_config, get_program, args, get_env)
local start_debugger = function(bazel_info)
local cwd = bazel_info.runfiles .. "/" .. bazel_info.workspace_name
StartDebugger(type, get_program(bazel_info), args, cwd, get_env(bazel_info), bazel_info.workspace)
end
bazel.run_here("build", bazel_config, { on_success = start_debugger })
end
function M.DebugBazelPy(args)
local get_env = function(bazel_info)
return {
PYTHONPATH = get_python_path(bazel_info),
RUNFILES_DIR = bazel_info.runfiles,
LD_LIBRARY_PATH = "/home/q456457/.local/lib/python3.8/site-packages/tensorrt_libs",
ASSET_PATH = "/data/training/modelstore",
}
end
M.DebugBazel("python", vim.g.bazel_config, get_python_executable, args, get_env)
end
function M.DebugBazelPyRun()
M.DebugBazelPy(vim.g.debug_args or {})
end
function M.DebugBazelPyTest()
M.DebugBazelPy(require("bazel.pytest").get_test_filter_args())
end
local function default_program(bazel_info)
return bazel_info.executable
end
local function default_env(_)
return {}
end
function M.DebugGTest()
local args = require("bazel.gtest").get_gtest_filter_args()
M.DebugBazel("cppdbg", vim.g.bazel_config .. " --compilation_mode dbg --copt -O0", default_program, args, default_env)
end
function M.DebugTest()
if vim.bo.filetype == "python" then
M.DebugBazelPyTest()
elseif vim.bo.filetype == "cpp" then
M.DebugGTest()
else
print("Debugging not supported for this filetype")
end
end
function M.DebugRun()
if vim.bo.filetype == "python" then
M.DebugBazelPyRun()
else
local args = vim.g.debug_args or {}
M.DebugBazel(
"cppdbg",
vim.g.bazel_config .. " --compilation_mode dbg --copt=-O0",
default_program,
args,
default_env
)
end
end
local function split_by_space(input)
local chunks = {}
for substring in input:gmatch("%S+") do
table.insert(chunks, substring)
end
return chunks
end
function M.set_debug_args_from_input()
local args = vim.fn.input("args for debugging with bazel: ")
vim.g.debug_args = split_by_space(args)
end
return M