-
Notifications
You must be signed in to change notification settings - Fork 72
/
commands.lua
488 lines (424 loc) · 15 KB
/
commands.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
local lazy = require("flutter-tools.lazy")
local Job = require("plenary.job") ---@module "plenary.job"
local ui = lazy.require("flutter-tools.ui") ---@module "flutter-tools.ui"
local utils = lazy.require("flutter-tools.utils") ---@module "flutter-tools.utils"
local devices = lazy.require("flutter-tools.devices") ---@module "flutter-tools.devices"
local config = lazy.require("flutter-tools.config") ---@module "flutter-tools.config"
local executable = lazy.require("flutter-tools.executable") ---@module "flutter-tools.executable"
local dev_tools = lazy.require("flutter-tools.dev_tools") ---@module "flutter-tools.dev_tools"
local lsp = lazy.require("flutter-tools.lsp") ---@module "flutter-tools.lsp"
local job_runner = lazy.require("flutter-tools.runners.job_runner") ---@module "flutter-tools.runners.job_runner"
local debugger_runner = lazy.require("flutter-tools.runners.debugger_runner") ---@module "flutter-tools.runners.debugger_runner"
local dev_log = lazy.require("flutter-tools.log") ---@module "flutter-tools.log"
local M = {}
---@alias RunOpts {cli_args: string[]?, args: string[]?, device: Device?}
---@type table?
local current_device = nil
---@class flutter.Runner
---@field is_running fun(runner: flutter.Runner):boolean
---@field run fun(runner: flutter.Runner, paths:table, args:table, cwd:string, on_run_data:fun(is_err:boolean, data:string), on_run_exit:fun(data:string[], args: table))
---@field cleanup fun(funner: flutter.Runner)
---@field send fun(runner: flutter.Runner, cmd:string, quiet: boolean?)
---@type flutter.Runner?
local runner = nil
local function use_debugger_runner()
local dap_ok, dap = pcall(require, "dap")
if not config.debugger.run_via_dap then return false end
if dap_ok then return true end
ui.notify(
utils.join({ "debugger runner was request but nvim-dap is not installed!", dap }),
ui.ERROR
)
return false
end
function M.current_device() return current_device end
function M.is_running() return runner ~= nil and runner:is_running() end
local function match_error_string(line)
if not line then return false end
-- match the error string if no devices are setup
if line:match("No supported devices connected") ~= nil then
-- match the error string returned if multiple devices are matched
return true, "Choose a device"
elseif line:match("More than one device connected") ~= nil then
return true, "Choose a device"
end
end
---@param lines string[]
---@return boolean, string?
local function has_recoverable_error(lines)
for _, line in pairs(lines) do
local match, msg = match_error_string(line)
if match then return match, msg end
end
return false, nil
end
---Handle output from flutter run command
---@param is_err boolean if this is stdout or stderr
local function on_run_data(is_err, data)
if is_err and config.dev_log.notify_errors then ui.notify(data, ui.ERROR, { timeout = 5000 }) end
dev_log.log(data, config.dev_log)
end
local function shutdown()
if runner then runner:cleanup() end
runner = nil
current_device = nil
utils.emit_event(utils.events.PROJECT_CONFIG_CHANGED)
dev_tools.on_flutter_shutdown()
end
---Handle a finished flutter run command
---@param result string[]
local function on_run_exit(result, cli_args)
local matched_error, msg = has_recoverable_error(result)
if matched_error then
local lines = devices.to_selection_entries(result)
ui.select({
title = ("Flutter run (%s)"):format(msg),
lines = lines,
on_select = function(device) devices.select_device(device, cli_args) end,
})
end
shutdown()
end
--- Take arguments from the commandline and pass
--- them to the run command
---@param args string
function M.run_command(args)
args = args and args ~= "" and vim.split(args, " ") or nil
M.run({ args = args })
end
---@param callback fun(project_config: flutter.ProjectConfig?)
local function select_project_config(callback)
local project_config = config.project --[=[@as flutter.ProjectConfig[]]=]
if #project_config <= 1 then
utils.emit_event(utils.events.PROJECT_CONFIG_CHANGED, { data = project_config[1] })
return callback(project_config[1])
end
vim.ui.select(project_config, {
prompt = "Select a project configuration",
format_item = function(item)
if item.name then return item.name end
return vim.inspect(item)
end,
}, function(selected)
if selected then
utils.emit_event(utils.events.PROJECT_CONFIG_CHANGED, { data = selected })
callback(selected)
end
end)
end
---@param opts RunOpts
---@param conf flutter.ProjectConfig?
---@return string[]
local function get_run_args(opts, conf)
local args = {}
local cmd_args = opts.args
local device = conf and conf.device or (opts.device and opts.device.id)
local flavor = conf and conf.flavor
local target = conf and conf.target
local dart_defines = conf and conf.dart_define
local dart_define_from_file = conf and conf.dart_define_from_file
local flutter_mode = conf and conf.flutter_mode
local web_port = conf and conf.web_port
local dev_url = dev_tools.get_url()
if not use_debugger_runner() then vim.list_extend(args, { "run" }) end
if not cmd_args and device then vim.list_extend(args, { "-d", device }) end
if web_port then vim.list_extend(args, { "--web-port", web_port }) end
if cmd_args then vim.list_extend(args, cmd_args) end
if flavor then vim.list_extend(args, { "--flavor", flavor }) end
if target then vim.list_extend(args, { "--target", target }) end
if dart_define_from_file then
vim.list_extend(args, { "--dart-define-from-file", dart_define_from_file })
end
if dart_defines then
for key, value in pairs(dart_defines) do
vim.list_extend(args, { "--dart-define", ("%s=%s"):format(key, value) })
end
end
if flutter_mode then
if flutter_mode == "profile" then
vim.list_extend(args, { "--profile" })
elseif flutter_mode == "release" then
vim.list_extend(args, { "--release" })
end -- else default to debug
end
if dev_url then vim.list_extend(args, { "--devtools-server-address", dev_url }) end
return args
end
--- @param args string[]
--- @return Device?
local function get_device_from_args(args)
for i = 1, #args - 1 do
if args[i] == "-d" then return { id = args[i + 1] } end
end
end
---@param opts RunOpts
---@param project_conf flutter.ProjectConfig?
local function run(opts, project_conf)
opts = opts or {}
executable.get(function(paths)
local args = opts.cli_args or get_run_args(opts, project_conf)
current_device = opts.device or get_device_from_args(args)
ui.notify("Starting flutter project...")
runner = use_debugger_runner() and debugger_runner or job_runner
runner:run(paths, args, lsp.get_lsp_root_dir(), on_run_data, on_run_exit)
end)
end
---Run the flutter application
---@param opts RunOpts
function M.run(opts)
if M.is_running() then return ui.notify("Flutter is already running!") end
select_project_config(function(project_conf) run(opts, project_conf) end)
end
---@param cmd string
---@param quiet boolean?
---@param on_send function|nil
local function send(cmd, quiet, on_send)
if M.is_running() and runner then
runner:send(cmd, quiet)
if on_send then on_send() end
elseif not quiet then
ui.notify("Sorry! Flutter is not running")
end
end
---@param quiet boolean?
function M.reload(quiet) send("reload", quiet) end
---@param quiet boolean?
function M.restart(quiet)
send("restart", quiet, function()
if not quiet then ui.notify("Restarting...", nil, { timeout = 1500 }) end
end)
end
---@param quiet boolean?
function M.quit(quiet)
send("quit", quiet, function()
if not quiet then
ui.notify("Closing flutter application...", nil, { timeout = 1500 })
shutdown()
end
end)
end
---@param quiet boolean?
function M.visual_debug(quiet) send("visual_debug", quiet) end
---@param quiet boolean?
function M.performance_overlay(quiet) send("performance_overlay", quiet) end
---@param quiet boolean?
function M.repaint_rainbow(quiet) send("repaint_rainbow", quiet) end
---@param quiet boolean?
function M.slow_animations(quiet) send("slow_animations", quiet) end
---@param quiet boolean?
function M.detach(quiet) send("detach", quiet) end
function M.copy_profiler_url()
if not M.is_running() then
ui.notify("You must run the app first!")
return
end
local url, is_running = dev_tools.get_profiler_url()
if url then
vim.cmd("let @+='" .. url .. "'")
ui.notify("Profiler url copied to clipboard!")
elseif is_running then
ui.notify("Wait while the app starts", "please try again later")
else
ui.notify("You must start the DevTools server first!")
end
end
---@param quiet boolean?
function M.open_dev_tools(quiet) send("open_dev_tools", quiet) end
---@param quiet boolean
function M.generate(quiet) send("generate", quiet) end
---@param quiet boolean
function M.widget_inspector(quiet) send("inspect", quiet) end
---@param quiet boolean
function M.construction_lines(quiet) send("construction_lines", quiet) end
-----------------------------------------------------------------------------//
-- Pub commands
-----------------------------------------------------------------------------//
---Print result of running pub get
---@param result string[]
local function on_pub_get(result, err)
local timeout = err and 10000 or nil
ui.notify(utils.join(result), nil, { timeout = timeout })
end
---@type Job?
local pub_get_job = nil
function M.pub_get()
if not pub_get_job then
executable.flutter(function(cmd)
pub_get_job = Job:new({
command = cmd,
args = { "pub", "get" },
-- stylua: ignore
cwd = lsp.get_lsp_root_dir() --[[@as string]],
})
pub_get_job:after_success(vim.schedule_wrap(function(j)
on_pub_get(j:result())
pub_get_job = nil
end))
pub_get_job:after_failure(vim.schedule_wrap(function(j)
on_pub_get(j:stderr_result(), true)
pub_get_job = nil
end))
pub_get_job:start()
end)
end
end
---@type Job?
local pub_upgrade_job = nil
--- Take arguments from the commandline and pass
--- them to the pub upgrade command
---@param args string
function M.pub_upgrade_command(args)
args = args and args ~= "" and vim.split(args, " ") or nil
M.pub_upgrade(args)
end
function M.pub_upgrade(cmd_args)
if not pub_upgrade_job then
executable.flutter(function(cmd)
local notify_timeout = 10000
local args = { "pub", "upgrade" }
if cmd_args then vim.list_extend(args, cmd_args) end
pub_upgrade_job = Job:new({
command = cmd,
args = args,
-- stylua: ignore
cwd = lsp.get_lsp_root_dir() --[[@as string]],
})
pub_upgrade_job:after_success(vim.schedule_wrap(function(j)
ui.notify(utils.join(j:result()), nil, { timeout = notify_timeout })
pub_upgrade_job = nil
end))
pub_upgrade_job:after_failure(vim.schedule_wrap(function(j)
ui.notify(utils.join(j:stderr_result()), nil, { timeout = notify_timeout })
pub_upgrade_job = nil
end))
pub_upgrade_job:start()
end)
end
end
-----------------------------------------------------------------------------//
-- FVM commands
-----------------------------------------------------------------------------//
---@type Job?
local fvm_list_job = nil
--- Returns table<{name: string, status: active|global|nil}>
function M.fvm_list(callback)
if not fvm_list_job then
-- Example output:
--
-- Cache Directory: /Users/rjm/fvm/versions
--
-- master (active)
-- beta
-- stable (global)
fvm_list_job = Job:new({ command = "fvm", args = { "list" } })
fvm_list_job:after_success(vim.schedule_wrap(function(j)
local out = j:result()
local sdks_out = { unpack(out, 3, #out) }
local sdks = {}
for _, sdk_out in pairs(sdks_out) do
-- matches: "<name> (<status>)"
local name, status = sdk_out:match("(.*)%s%((%w+)%)")
name = name or sdk_out
table.insert(sdks, { name = name, status = status })
end
callback(sdks)
fvm_list_job = nil
end))
fvm_list_job:after_failure(vim.schedule_wrap(function(j)
ui.notify(utils.join(j:stderr_result()), ui.ERROR)
fvm_list_job = nil
end))
fvm_list_job:start()
end
end
---@type Job?
local fvm_use_job = nil
function M.fvm_use(sdk_name)
if not fvm_use_job then
fvm_use_job = Job:new({ command = "fvm", args = { "use", sdk_name } })
fvm_use_job:after_success(vim.schedule_wrap(function(j)
ui.notify(utils.join(j:result()))
shutdown()
executable.reset_paths()
lsp.restart()
fvm_use_job = nil
end))
fvm_use_job:after_failure(vim.schedule_wrap(function(j)
ui.notify(utils.join(j:result()), nil, { timeout = 10000 }) -- FVM doesn't output to stderr, nice
fvm_use_job = nil
end))
fvm_use_job:start()
end
end
---@param args string[]
---@param project_conf flutter.ProjectConfig?
---@return string[]
local function set_args_from_project_config(args, project_conf)
local flavor = project_conf and project_conf.flavor
local device = project_conf and project_conf.device
if flavor then vim.list_extend(args, { "--flavor", flavor }) end
if device then vim.list_extend(args, { "-d", device }) end
end
---@type Job?
local install_job = nil
function M.install()
if not install_job then
select_project_config(function(project_conf)
local args = { "install" }
set_args_from_project_config(args, project_conf)
ui.notify("Installing the app...")
executable.flutter(function(cmd)
local notify_timeout = 10000
install_job = Job:new({
command = cmd,
args = args,
-- stylua: ignore
cwd = lsp.get_lsp_root_dir() --[[@as string]],
})
install_job:after_success(vim.schedule_wrap(function(j)
ui.notify(utils.join(j:result()), nil, { timeout = notify_timeout })
install_job = nil
end))
install_job:after_failure(vim.schedule_wrap(function(j)
ui.notify(utils.join(j:result()), nil, { timeout = notify_timeout })
install_job = nil
end))
install_job:start()
end)
end)
end
end
---@type Job?
local uninstall_job = nil
function M.uninstall()
if not uninstall_job then
select_project_config(function(project_conf)
local args = { "install", "--uninstall-only" }
set_args_from_project_config(args, project_conf)
ui.notify("Uninstalling the app...")
executable.flutter(function(cmd)
local notify_timeout = 10000
uninstall_job = Job:new({
command = cmd,
args = args,
-- stylua: ignore
cwd = lsp.get_lsp_root_dir() --[[@as string]],
})
uninstall_job:after_success(vim.schedule_wrap(function(j)
ui.notify(utils.join(j:result()), nil, { timeout = notify_timeout })
uninstall_job = nil
end))
uninstall_job:after_failure(vim.schedule_wrap(function(j)
ui.notify(utils.join(j:result()), nil, { timeout = notify_timeout })
uninstall_job = nil
end))
uninstall_job:start()
end)
end)
end
end
if __TEST then
M.__run = run
M.__get_run_args = get_run_args
end
return M