diff options
| author | Hennadii Chernyshchyk <genaloner@gmail.com> | 2022-09-10 14:05:32 +0300 |
|---|---|---|
| committer | Hennadii Chernyshchyk <genaloner@gmail.com> | 2022-09-10 17:48:06 +0300 |
| commit | fb10b4906aadaeff295883d171c05246943e5571 (patch) | |
| tree | eae589c4aeb613b8a2e8a1daf678f008f01069b9 /lua | |
| download | neovim-tasks-fb10b4906aadaeff295883d171c05246943e5571.tar.gz neovim-tasks-fb10b4906aadaeff295883d171c05246943e5571.zip | |
Initial commit
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/tasks/config.lua | 32 | ||||
| -rw-r--r-- | lua/tasks/constants.lua | 5 | ||||
| -rw-r--r-- | lua/tasks/init.lua | 142 | ||||
| -rw-r--r-- | lua/tasks/module/cargo.lua | 171 | ||||
| -rw-r--r-- | lua/tasks/module/cmake.lua | 272 | ||||
| -rw-r--r-- | lua/tasks/project_config.lua | 29 | ||||
| -rw-r--r-- | lua/tasks/runner.lua | 226 | ||||
| -rw-r--r-- | lua/tasks/subcommands.lua | 79 | ||||
| -rw-r--r-- | lua/tasks/utils.lua | 100 |
9 files changed, 1056 insertions, 0 deletions
diff --git a/lua/tasks/config.lua b/lua/tasks/config.lua new file mode 100644 index 0000000..0d5da61 --- /dev/null +++ b/lua/tasks/config.lua | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | local Path = require('plenary.path') | ||
| 2 | |||
| 3 | local config = { | ||
| 4 | defaults = { | ||
| 5 | default_params = { | ||
| 6 | cmake = { | ||
| 7 | cmd = 'cmake', | ||
| 8 | build_dir = tostring(Path:new('{cwd}', 'build', '{os}-{build_type}')), | ||
| 9 | build_type = 'Debug', | ||
| 10 | dap_name = 'lldb', | ||
| 11 | args = { | ||
| 12 | configure = { '-D', 'CMAKE_EXPORT_COMPILE_COMMANDS=1', '-G', 'Ninja' }, | ||
| 13 | }, | ||
| 14 | }, | ||
| 15 | cargo = { | ||
| 16 | dap_name = 'lldb', | ||
| 17 | }, | ||
| 18 | }, | ||
| 19 | save_before_run = true, | ||
| 20 | params_file = 'neovim.json', | ||
| 21 | quickfix = { | ||
| 22 | pos = 'botright', | ||
| 23 | height = 12, | ||
| 24 | only_on_error = false, | ||
| 25 | }, | ||
| 26 | dap_open_command = function() return require('dap').repl.open() end, | ||
| 27 | }, | ||
| 28 | } | ||
| 29 | |||
| 30 | setmetatable(config, { __index = config.defaults }) | ||
| 31 | |||
| 32 | return config | ||
diff --git a/lua/tasks/constants.lua b/lua/tasks/constants.lua new file mode 100644 index 0000000..4e236b2 --- /dev/null +++ b/lua/tasks/constants.lua | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | local constants = { | ||
| 2 | task_params = { 'args', 'env' }, -- Parameters available for all tasks | ||
| 3 | } | ||
| 4 | |||
| 5 | return constants | ||
diff --git a/lua/tasks/init.lua b/lua/tasks/init.lua new file mode 100644 index 0000000..4b6c893 --- /dev/null +++ b/lua/tasks/init.lua | |||
| @@ -0,0 +1,142 @@ | |||
| 1 | local config = require('tasks.config') | ||
| 2 | local runner = require('tasks.runner') | ||
| 3 | local constants = require('tasks.constants') | ||
| 4 | local utils = require('tasks.utils') | ||
| 5 | local ProjectConfig = require('tasks.project_config') | ||
| 6 | local tasks = {} | ||
| 7 | |||
| 8 | --- Apply user settings. | ||
| 9 | ---@param values table | ||
| 10 | function tasks.setup(values) setmetatable(config, { __index = vim.tbl_deep_extend('force', config.defaults, values) }) end | ||
| 11 | |||
| 12 | --- Execute a task from a module. | ||
| 13 | ---@param module_type string: Name of a module or `auto` string to pick a first module that match a condition. | ||
| 14 | ---@param task_name string | ||
| 15 | ---@vararg string additional arguments that will be passed to the last task. | ||
| 16 | function tasks.start(module_type, task_name, ...) | ||
| 17 | local current_job_name = runner.get_current_job_name() | ||
| 18 | if current_job_name then | ||
| 19 | utils.notify(string.format('Another job is currently running: "%s"', current_job_name), vim.log.levels.ERROR) | ||
| 20 | return | ||
| 21 | end | ||
| 22 | |||
| 23 | local module, module_name = utils.get_module(module_type) | ||
| 24 | if not module then | ||
| 25 | return | ||
| 26 | end | ||
| 27 | |||
| 28 | local commands = module.tasks[task_name] | ||
| 29 | if not commands then | ||
| 30 | utils.notify(string.format('Unable to find a task named "%s" in module "%s"', task_name, module_name), vim.log.levels.ERROR) | ||
| 31 | return | ||
| 32 | end | ||
| 33 | |||
| 34 | if config.save_before_run then | ||
| 35 | vim.api.nvim_command('silent! wall') | ||
| 36 | end | ||
| 37 | |||
| 38 | local project_config = ProjectConfig.new() | ||
| 39 | local module_config = project_config[module_name] | ||
| 40 | if not vim.tbl_islist(commands) then | ||
| 41 | commands = { commands } | ||
| 42 | end | ||
| 43 | runner.chain_commands(task_name, commands, module_config, { ... }) | ||
| 44 | end | ||
| 45 | |||
| 46 | --- Set a module-specific parameter. Settings will be stored on disk. | ||
| 47 | ---@param module_type string: Name of a module or `auto` string to pick a first module that match a condition. | ||
| 48 | ---@param param_name string | ||
| 49 | function tasks.set_module_param(module_type, param_name) | ||
| 50 | local module, module_name = utils.get_module(module_type) | ||
| 51 | if not module then | ||
| 52 | return | ||
| 53 | end | ||
| 54 | |||
| 55 | if not module then | ||
| 56 | return | ||
| 57 | end | ||
| 58 | |||
| 59 | local project_config = ProjectConfig.new() | ||
| 60 | local current_value = vim.tbl_get(project_config, module_name, param_name) | ||
| 61 | |||
| 62 | local param = module.params[param_name] | ||
| 63 | if not param then | ||
| 64 | if vim.tbl_contains(module.params, param_name) then | ||
| 65 | -- Contains a string without a value, request for input | ||
| 66 | vim.ui.input({ prompt = string.format('Set "%s" for module "%s"', param_name, module_name), default = current_value }, function(input) | ||
| 67 | project_config[module_name][param_name] = input | ||
| 68 | project_config:write() | ||
| 69 | end) | ||
| 70 | else | ||
| 71 | utils.notify(string.format('No such parameter "%s" for module "%s"', param_name, module_name), vim.log.levels.ERROR) | ||
| 72 | end | ||
| 73 | return | ||
| 74 | end | ||
| 75 | |||
| 76 | if vim.is_callable(param) then | ||
| 77 | param = param() | ||
| 78 | if not param then | ||
| 79 | return | ||
| 80 | end | ||
| 81 | end | ||
| 82 | |||
| 83 | -- Put current value first | ||
| 84 | if current_value then | ||
| 85 | for index, value in ipairs(param) do | ||
| 86 | if value == current_value then | ||
| 87 | table.remove(param, index) | ||
| 88 | break | ||
| 89 | end | ||
| 90 | end | ||
| 91 | table.insert(param, 1, current_value) | ||
| 92 | end | ||
| 93 | |||
| 94 | vim.ui.select(param, { prompt = string.format('Select "%s"', param_name) }, function(choice, idx) | ||
| 95 | if not idx then | ||
| 96 | return | ||
| 97 | end | ||
| 98 | if not project_config[module_name] then | ||
| 99 | project_config[module_name] = {} | ||
| 100 | end | ||
| 101 | project_config[module_name][param_name] = choice | ||
| 102 | project_config:write() | ||
| 103 | end) | ||
| 104 | end | ||
| 105 | |||
| 106 | --- Set a parameter for a module task. Settings will be stored on disk. | ||
| 107 | ---@param module_type string: Name of a module or `auto` string to pick a first module that match a condition. | ||
| 108 | ---@param task_name string | ||
| 109 | ---@param param_name string | ||
| 110 | function tasks.set_task_param(module_type, task_name, param_name) | ||
| 111 | local module, module_name = utils.get_module(module_type) | ||
| 112 | if not module then | ||
| 113 | return | ||
| 114 | end | ||
| 115 | if not vim.tbl_contains(constants.task_params, param_name) then | ||
| 116 | utils.notify(string.format('Unknown task parameter "%s"\nAvailable task parameters: %s', param_name, table.concat(constants.task_params, ', ')), vim.log.levels.ERROR) | ||
| 117 | return | ||
| 118 | end | ||
| 119 | |||
| 120 | local project_config = ProjectConfig.new() | ||
| 121 | local current_value = vim.tbl_get(project_config, module_name, param_name, task_name) | ||
| 122 | current_value = current_value and utils.join_args(current_value) or '' | ||
| 123 | vim.ui.input({ prompt = string.format('Set "%s" for task "%s" from module "%s": ', param_name, task_name, module_name), default = current_value, completion = 'file' }, function(input) | ||
| 124 | if not project_config[module_name] then | ||
| 125 | project_config[module_name] = {} | ||
| 126 | end | ||
| 127 | if not project_config[module_name][param_name] then | ||
| 128 | project_config[module_name][param_name] = {} | ||
| 129 | end | ||
| 130 | project_config[module_name][param_name][task_name] = utils.split_args(input) | ||
| 131 | project_config:write() | ||
| 132 | end) | ||
| 133 | end | ||
| 134 | |||
| 135 | --- Cancel last current task. | ||
| 136 | function tasks.cancel() | ||
| 137 | if not runner.cancel_job() then | ||
| 138 | utils.notify('No running process') | ||
| 139 | end | ||
| 140 | end | ||
| 141 | |||
| 142 | return tasks | ||
diff --git a/lua/tasks/module/cargo.lua b/lua/tasks/module/cargo.lua new file mode 100644 index 0000000..3e21371 --- /dev/null +++ b/lua/tasks/module/cargo.lua | |||
| @@ -0,0 +1,171 @@ | |||
| 1 | local utils = require('tasks.utils') | ||
| 2 | local Job = require('plenary.job') | ||
| 3 | local Path = require('plenary.path') | ||
| 4 | local cargo = {} | ||
| 5 | |||
| 6 | -- Modified version of `errorformat` from the official Rust plugin for Vim: | ||
| 7 | -- https://github.com/rust-lang/rust.vim/blob/4aa69b84c8a58fcec6b6dad6fe244b916b1cf830/compiler/rustc.vim#L32 | ||
| 8 | -- https://github.com/rust-lang/rust.vim/blob/4aa69b84c8a58fcec6b6dad6fe244b916b1cf830/compiler/cargo.vim#L35 | ||
| 9 | -- We display all lines (not only error messages) since we show output in quickfix. | ||
| 10 | -- Zero-width look-ahead regex is used to avoid marking general messages as errors: %\%%(ignored text%\)%\@!. | ||
| 11 | local errorformat = [[%Eerror: %\%%(aborting %\|could not compile%\)%\@!%m,]] | ||
| 12 | .. [[%Eerror[E%n]: %m,]] | ||
| 13 | .. [[%Inote: %m,]] | ||
| 14 | .. [[%Wwarning: %\%%(%.%# warning%\)%\@!%m,]] | ||
| 15 | .. [[%C %#--> %f:%l:%c,]] | ||
| 16 | .. [[%E left:%m,%C right:%m %f:%l:%c,%Z,]] | ||
| 17 | .. [[%.%#panicked at \'%m\'\, %f:%l:%c]] | ||
| 18 | |||
| 19 | --- Detects package name from command line arguments. | ||
| 20 | ---@param args table | ||
| 21 | ---@return string? | ||
| 22 | local function detect_package_name(args) | ||
| 23 | for index, value in ipairs(args) do | ||
| 24 | if value == '-p' or value == '--package' or value == '--bin' then | ||
| 25 | return args[index + 1] | ||
| 26 | end | ||
| 27 | end | ||
| 28 | return nil | ||
| 29 | end | ||
| 30 | |||
| 31 | --- Returns only a packages that can be executed. | ||
| 32 | ---@param packages table: Packages to filter. | ||
| 33 | ---@return table | ||
| 34 | local function find_executable_packages(packages) | ||
| 35 | local executables = {} | ||
| 36 | for _, line in pairs(packages) do | ||
| 37 | local package = vim.json.decode(line) | ||
| 38 | if package.executable and package.executable ~= vim.NIL then | ||
| 39 | table.insert(executables, package) | ||
| 40 | end | ||
| 41 | end | ||
| 42 | return executables | ||
| 43 | end | ||
| 44 | |||
| 45 | --- Finds executable package name from a list of packages. | ||
| 46 | ---@param packages table | ||
| 47 | ---@param args table?: Command line arguments that will be used to detect an executable if JSON message from cargo is missing this info. | ||
| 48 | ---@return table? | ||
| 49 | local function get_executable_package(packages, args) | ||
| 50 | local executable_packages = find_executable_packages(packages) | ||
| 51 | if #executable_packages == 1 then | ||
| 52 | return executable_packages[1] | ||
| 53 | end | ||
| 54 | |||
| 55 | -- Try to detect package name from arguments | ||
| 56 | local package_name = detect_package_name(args or {}) | ||
| 57 | if not package_name then | ||
| 58 | local available_names = {} | ||
| 59 | for _, package in ipairs(executable_packages) do | ||
| 60 | table.insert(available_names, package.target.name) | ||
| 61 | end | ||
| 62 | utils.notify( | ||
| 63 | 'Could not determine which binary to run\nUse the "--bin" or "--package" option to specify a binary\nAvailable binaries: ' .. table.concat(available_names, ', '), | ||
| 64 | vim.log.levels.ERROR | ||
| 65 | ) | ||
| 66 | return nil | ||
| 67 | end | ||
| 68 | |||
| 69 | for _, package in ipairs(executable_packages) do | ||
| 70 | if package.target.name == package_name then | ||
| 71 | return package | ||
| 72 | end | ||
| 73 | end | ||
| 74 | |||
| 75 | utils.notify(string.format('Unable to find package named "%s"', package_name), vim.log.levels.ERROR) | ||
| 76 | return nil | ||
| 77 | end | ||
| 78 | |||
| 79 | ---@return table: List of functions for each cargo subcommand that return a task table. | ||
| 80 | local function get_cargo_subcommands() | ||
| 81 | local cargo_subcommands = {} | ||
| 82 | |||
| 83 | local job = Job:new({ | ||
| 84 | command = 'cargo', | ||
| 85 | args = { '--list' }, | ||
| 86 | enabled_recording = true, | ||
| 87 | }) | ||
| 88 | job:sync() | ||
| 89 | |||
| 90 | if job.code ~= 0 or job.signal ~= 0 then | ||
| 91 | utils.notify('Unable to get list of available cargo subcommands', vim.log.levels.ERROR) | ||
| 92 | return {} | ||
| 93 | end | ||
| 94 | |||
| 95 | local start_offset = 5 | ||
| 96 | for index, line in ipairs(job:result()) do | ||
| 97 | if index ~= 1 and not line:find('alias:') then | ||
| 98 | local subcommand_end = line:find(' ', start_offset) | ||
| 99 | local subcommand = line:sub(start_offset, subcommand_end and subcommand_end - 1 or nil) | ||
| 100 | cargo_subcommands[subcommand] = | ||
| 101 | function(module_config, _) return { cmd = 'cargo', args = vim.list_extend({ subcommand }, utils.split_args(module_config.global_cargo_args)), errorformat = errorformat } end | ||
| 102 | end | ||
| 103 | end | ||
| 104 | |||
| 105 | return cargo_subcommands | ||
| 106 | end | ||
| 107 | |||
| 108 | --- Task | ||
| 109 | ---@return table? | ||
| 110 | local function build_test(module_config, _) | ||
| 111 | return { | ||
| 112 | cmd = 'cargo', | ||
| 113 | args = vim.list_extend({ 'test', '--no-run', '--message-format=json' }, utils.split_args(module_config.global_cargo_args)), | ||
| 114 | errorformat = errorformat, | ||
| 115 | ignore_stdout = true, | ||
| 116 | } | ||
| 117 | end | ||
| 118 | |||
| 119 | --- Task | ||
| 120 | ---@param module_config table | ||
| 121 | ---@param previous_job table | ||
| 122 | ---@return table? | ||
| 123 | local function debug_test(module_config, previous_job) | ||
| 124 | local package = get_executable_package(previous_job:result(), vim.tbl_get(module_config, 'args', 'debug_test')) | ||
| 125 | if not package then | ||
| 126 | return | ||
| 127 | end | ||
| 128 | |||
| 129 | return { | ||
| 130 | cmd = package.executable, | ||
| 131 | dap_name = module_config.dap_name, | ||
| 132 | errorformat = errorformat, | ||
| 133 | } | ||
| 134 | end | ||
| 135 | |||
| 136 | --- Task | ||
| 137 | ---@param module_config table | ||
| 138 | ---@return table? | ||
| 139 | local function build(module_config, _) | ||
| 140 | return { | ||
| 141 | cmd = 'cargo', | ||
| 142 | args = vim.list_extend({ 'build', '--message-format=json' }, utils.split_args(module_config.global_cargo_args)), | ||
| 143 | ignore_stdout = true, | ||
| 144 | } | ||
| 145 | end | ||
| 146 | |||
| 147 | --- Task | ||
| 148 | ---@param module_config table | ||
| 149 | ---@param previous_job table | ||
| 150 | ---@return table? | ||
| 151 | local function debug(module_config, previous_job) | ||
| 152 | local package = get_executable_package(previous_job:result(), vim.tbl_get(module_config, 'args', 'debug')) | ||
| 153 | if not package then | ||
| 154 | return | ||
| 155 | end | ||
| 156 | |||
| 157 | return { | ||
| 158 | cmd = package.executable, | ||
| 159 | dap_name = module_config.dap_name, | ||
| 160 | errorformat = errorformat, | ||
| 161 | } | ||
| 162 | end | ||
| 163 | |||
| 164 | cargo.params = { | ||
| 165 | 'dap_name', | ||
| 166 | 'global_cargo_args', | ||
| 167 | } | ||
| 168 | cargo.condition = function() return Path:new('Cargo.toml'):exists() end | ||
| 169 | cargo.tasks = vim.tbl_extend('force', get_cargo_subcommands(), { debug_test = { build_test, debug_test }, debug = { build, debug } }) | ||
| 170 | |||
| 171 | return cargo | ||
diff --git a/lua/tasks/module/cmake.lua b/lua/tasks/module/cmake.lua new file mode 100644 index 0000000..4900aec --- /dev/null +++ b/lua/tasks/module/cmake.lua | |||
| @@ -0,0 +1,272 @@ | |||
| 1 | local Path = require('plenary.path') | ||
| 2 | local utils = require('tasks.utils') | ||
| 3 | local scandir = require('plenary.scandir') | ||
| 4 | local ProjectConfig = require('tasks.project_config') | ||
| 5 | local os = require('ffi').os:lower() | ||
| 6 | local cmake = {} | ||
| 7 | |||
| 8 | --- Parses build dir expression. | ||
| 9 | ---@param dir string: Path with expressions to replace. | ||
| 10 | ---@param build_type string | ||
| 11 | ---@return table | ||
| 12 | local function parse_dir(dir, build_type) | ||
| 13 | local parsed_dir = dir:gsub('{cwd}', vim.loop.cwd()) | ||
| 14 | parsed_dir = parsed_dir:gsub('{os}', os) | ||
| 15 | parsed_dir = parsed_dir:gsub('{build_type}', build_type:lower()) | ||
| 16 | return Path:new(parsed_dir) | ||
| 17 | end | ||
| 18 | |||
| 19 | --- Returns reply directory that contains targets information. | ||
| 20 | ---@param build_dir table | ||
| 21 | ---@return unknown | ||
| 22 | local function get_reply_dir(build_dir) return build_dir / '.cmake' / 'api' / 'v1' / 'reply' end | ||
| 23 | |||
| 24 | --- Reads information about target. | ||
| 25 | ---@param codemodel_target table | ||
| 26 | ---@param reply_dir table | ||
| 27 | ---@return table | ||
| 28 | local function get_target_info(codemodel_target, reply_dir) return vim.json.decode((reply_dir / codemodel_target['jsonFile']):read()) end | ||
| 29 | |||
| 30 | --- Creates query files that to acess information about targets after CMake configuration. | ||
| 31 | ---@param build_dir table | ||
| 32 | ---@return boolean: Returns `true` on success. | ||
| 33 | local function make_query_files(build_dir) | ||
| 34 | local query_dir = build_dir / '.cmake' / 'api' / 'v1' / 'query' | ||
| 35 | if not query_dir:mkdir({ parents = true }) then | ||
| 36 | utils.notify(string.format('Unable to create "%s"', query_dir.filename), vim.log.levels.ERROR) | ||
| 37 | return false | ||
| 38 | end | ||
| 39 | |||
| 40 | local codemodel_file = query_dir / 'codemodel-v2' | ||
| 41 | if not codemodel_file:is_file() then | ||
| 42 | if not codemodel_file:touch() then | ||
| 43 | utils.notify(string.format('Unable to create "%s"', codemodel_file.filename), vim.log.levels.ERROR) | ||
| 44 | return false | ||
| 45 | end | ||
| 46 | end | ||
| 47 | return true | ||
| 48 | end | ||
| 49 | |||
| 50 | --- Reads targets information. | ||
| 51 | ---@param reply_dir table | ||
| 52 | ---@return table? | ||
| 53 | local function get_codemodel_targets(reply_dir) | ||
| 54 | local found_files = scandir.scan_dir(reply_dir.filename, { search_pattern = 'codemodel*' }) | ||
| 55 | if #found_files == 0 then | ||
| 56 | utils.notify('Unable to find codemodel file', vim.log.levels.ERROR) | ||
| 57 | return nil | ||
| 58 | end | ||
| 59 | local codemodel = Path:new(found_files[1]) | ||
| 60 | local codemodel_json = vim.json.decode(codemodel:read()) | ||
| 61 | return codemodel_json['configurations'][1]['targets'] | ||
| 62 | end | ||
| 63 | |||
| 64 | ---@return table? | ||
| 65 | local function get_target_names() | ||
| 66 | local project_config = ProjectConfig.new() | ||
| 67 | local build_dir = parse_dir(project_config.cmake.build_dir, project_config.cmake.build_type) | ||
| 68 | if not build_dir:is_dir() then | ||
| 69 | utils.notify(string.format('Build directory "%s" does not exist, you need to run "configure" task first', build_dir), vim.log.levels.ERROR) | ||
| 70 | return nil | ||
| 71 | end | ||
| 72 | |||
| 73 | local reply_dir = get_reply_dir(build_dir) | ||
| 74 | local codemodel_targets = get_codemodel_targets(reply_dir) | ||
| 75 | if not codemodel_targets then | ||
| 76 | return nil | ||
| 77 | end | ||
| 78 | |||
| 79 | local targets = {} | ||
| 80 | for _, target in ipairs(codemodel_targets) do | ||
| 81 | local target_info = get_target_info(target, reply_dir) | ||
| 82 | local target_name = target_info['name'] | ||
| 83 | if target_name:find('_autogen') == nil then | ||
| 84 | table.insert(targets, target_name) | ||
| 85 | end | ||
| 86 | end | ||
| 87 | |||
| 88 | return targets | ||
| 89 | end | ||
| 90 | |||
| 91 | --- Finds path to an executable. | ||
| 92 | ---@param build_dir table | ||
| 93 | ---@param name string | ||
| 94 | ---@param reply_dir table | ||
| 95 | ---@return unknown? | ||
| 96 | local function get_executable_path(build_dir, name, reply_dir) | ||
| 97 | for _, target in ipairs(get_codemodel_targets(reply_dir)) do | ||
| 98 | if name == target['name'] then | ||
| 99 | local target_info = get_target_info(target, reply_dir) | ||
| 100 | if target_info['type'] ~= 'EXECUTABLE' then | ||
| 101 | utils.notify(string.format('Specified target "%s" is not an executable', name), vim.log.levels.ERROR) | ||
| 102 | return nil | ||
| 103 | end | ||
| 104 | |||
| 105 | local target_path = Path:new(target_info['artifacts'][1]['path']) | ||
| 106 | if not target_path:is_absolute() then | ||
| 107 | target_path = build_dir / target_path | ||
| 108 | end | ||
| 109 | |||
| 110 | return target_path | ||
| 111 | end | ||
| 112 | end | ||
| 113 | |||
| 114 | utils.notify(string.format('Unable to find target named "%s"', name), vim.log.levels.ERROR) | ||
| 115 | return nil | ||
| 116 | end | ||
| 117 | |||
| 118 | --- Copies compile_commands.json file from build directory to the current working directory for LSP integration. | ||
| 119 | local function copy_compile_commands() | ||
| 120 | local project_config = ProjectConfig.new() | ||
| 121 | local filename = 'compile_commands.json' | ||
| 122 | local source = parse_dir(project_config.cmake.build_dir, project_config.cmake.build_type) / filename | ||
| 123 | local destination = Path:new(vim.loop.cwd(), filename) | ||
| 124 | source:copy({ destination = destination.filename }) | ||
| 125 | end | ||
| 126 | |||
| 127 | --- Task | ||
| 128 | ---@param module_config table | ||
| 129 | ---@return table? | ||
| 130 | local function configure(module_config, _) | ||
| 131 | local build_dir = parse_dir(module_config.build_dir, module_config.build_type) | ||
| 132 | build_dir:mkdir({ parents = true }) | ||
| 133 | if not make_query_files(build_dir) then | ||
| 134 | return nil | ||
| 135 | end | ||
| 136 | |||
| 137 | return { | ||
| 138 | cmd = module_config.cmd, | ||
| 139 | args = { '-B', build_dir.filename, '-D', 'CMAKE_BUILD_TYPE=' .. module_config.build_type }, | ||
| 140 | after_success = copy_compile_commands, | ||
| 141 | } | ||
| 142 | end | ||
| 143 | |||
| 144 | --- Task | ||
| 145 | ---@param module_config table | ||
| 146 | ---@return table | ||
| 147 | local function build(module_config, _) | ||
| 148 | local build_dir = parse_dir(module_config.build_dir, module_config.build_type) | ||
| 149 | |||
| 150 | local args = { '--build', build_dir.filename } | ||
| 151 | if module_config.target then | ||
| 152 | vim.list_extend(args, { '--target', module_config.target }) | ||
| 153 | end | ||
| 154 | |||
| 155 | return { | ||
| 156 | cmd = module_config.cmd, | ||
| 157 | args = args, | ||
| 158 | after_success = copy_compile_commands, | ||
| 159 | } | ||
| 160 | end | ||
| 161 | |||
| 162 | --- Task | ||
| 163 | ---@param module_config table | ||
| 164 | ---@return table | ||
| 165 | local function build_all(module_config, _) | ||
| 166 | local build_dir = parse_dir(module_config.build_dir, module_config.build_type) | ||
| 167 | |||
| 168 | return { | ||
| 169 | cmd = module_config.cmd, | ||
| 170 | args = { '--build', build_dir.filename }, | ||
| 171 | after_success = copy_compile_commands, | ||
| 172 | } | ||
| 173 | end | ||
| 174 | |||
| 175 | --- Task | ||
| 176 | ---@param module_config table | ||
| 177 | ---@return table? | ||
| 178 | local function run(module_config, _) | ||
| 179 | if not module_config.target then | ||
| 180 | utils.notify('No selected target, please set "target" parameter', vim.log.levels.ERROR) | ||
| 181 | return nil | ||
| 182 | end | ||
| 183 | |||
| 184 | local build_dir = parse_dir(module_config.build_dir, module_config.build_type) | ||
| 185 | if not build_dir:is_dir() then | ||
| 186 | utils.notify(string.format('Build directory "%s" does not exist, you need to run "configure" task first', build_dir), vim.log.levels.ERROR) | ||
| 187 | return nil | ||
| 188 | end | ||
| 189 | |||
| 190 | local target_path = get_executable_path(build_dir, module_config.target, get_reply_dir(build_dir)) | ||
| 191 | if not target_path then | ||
| 192 | return | ||
| 193 | end | ||
| 194 | |||
| 195 | if not target_path:is_file() then | ||
| 196 | utils.notify(string.format('Selected target "%s" is not built', target_path.filename), vim.log.levels.ERROR) | ||
| 197 | return nil | ||
| 198 | end | ||
| 199 | |||
| 200 | return { | ||
| 201 | cmd = target_path.filename, | ||
| 202 | cwd = target_path:parent().filename, | ||
| 203 | } | ||
| 204 | end | ||
| 205 | |||
| 206 | --- Task | ||
| 207 | ---@param module_config table | ||
| 208 | ---@return table? | ||
| 209 | local function debug(module_config, _) | ||
| 210 | if module_config.build_type ~= 'Debug' and module_config.build_type ~= 'RelWithDebInfo' then | ||
| 211 | utils.notify( | ||
| 212 | string.format('For debugging your "build_type" param should be set to "Debug" or "RelWithDebInfo", but your current build type is "%s"'), | ||
| 213 | module_config.build_type, | ||
| 214 | vim.log.levels.ERROR | ||
| 215 | ) | ||
| 216 | return nil | ||
| 217 | end | ||
| 218 | |||
| 219 | local command = run(module_config, nil) | ||
| 220 | if not command then | ||
| 221 | return nil | ||
| 222 | end | ||
| 223 | |||
| 224 | command.dap_name = module_config.dap_name | ||
| 225 | return command | ||
| 226 | end | ||
| 227 | |||
| 228 | --- Task | ||
| 229 | ---@param module_config table | ||
| 230 | ---@return table | ||
| 231 | local function clean(module_config, _) | ||
| 232 | local build_dir = parse_dir(module_config.build_dir, module_config.build_type) | ||
| 233 | |||
| 234 | return { | ||
| 235 | cmd = module_config.cmd, | ||
| 236 | args = { '--build', build_dir.filename, '--target', 'clean' }, | ||
| 237 | after_success = copy_compile_commands, | ||
| 238 | } | ||
| 239 | end | ||
| 240 | |||
| 241 | --- Task | ||
| 242 | ---@param module_config table | ||
| 243 | ---@return table | ||
| 244 | local function open_build_dir(module_config, _) | ||
| 245 | local build_dir = parse_dir(module_config.build_dir, module_config.build_type) | ||
| 246 | |||
| 247 | return { | ||
| 248 | cmd = os == 'windows' and 'start' or 'xdg-open', | ||
| 249 | args = { build_dir.filename }, | ||
| 250 | ignore_stdout = true, | ||
| 251 | ignore_stderr = true, | ||
| 252 | } | ||
| 253 | end | ||
| 254 | |||
| 255 | cmake.params = { | ||
| 256 | target = get_target_names, | ||
| 257 | build_type = { 'Debug', 'Release', 'RelWithDebInfo', 'MinSizeRel' }, | ||
| 258 | 'cmake', | ||
| 259 | 'dap_name', | ||
| 260 | } | ||
| 261 | cmake.condition = function() return Path:new('CMakeLists.txt'):exists() end | ||
| 262 | cmake.tasks = { | ||
| 263 | configure = configure, | ||
| 264 | build = build, | ||
| 265 | build_all = build_all, | ||
| 266 | run = { build, run }, | ||
| 267 | debug = debug, | ||
| 268 | clean = clean, | ||
| 269 | open_build_dir = open_build_dir, | ||
| 270 | } | ||
| 271 | |||
| 272 | return cmake | ||
diff --git a/lua/tasks/project_config.lua b/lua/tasks/project_config.lua new file mode 100644 index 0000000..2d366c6 --- /dev/null +++ b/lua/tasks/project_config.lua | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | local config = require('tasks.config') | ||
| 2 | local Path = require('plenary.path') | ||
| 3 | |||
| 4 | --- Contains all fields from configuration. | ||
| 5 | ---@class ProjectConfig | ||
| 6 | local ProjectConfig = {} | ||
| 7 | ProjectConfig.__index = ProjectConfig | ||
| 8 | |||
| 9 | --- Reads project configuration JSON into a table. | ||
| 10 | ---@return ProjectConfig | ||
| 11 | function ProjectConfig.new() | ||
| 12 | local project_config = {} | ||
| 13 | local params_file = Path:new(config.params_file) | ||
| 14 | if params_file:is_file() then | ||
| 15 | project_config = vim.json.decode(params_file:read()) | ||
| 16 | else | ||
| 17 | project_config = {} | ||
| 18 | end | ||
| 19 | project_config = vim.tbl_extend('keep', project_config, config.default_params) | ||
| 20 | return setmetatable(project_config, ProjectConfig) | ||
| 21 | end | ||
| 22 | |||
| 23 | --- Writes all values as JSON to disk. | ||
| 24 | function ProjectConfig:write() | ||
| 25 | local params_file = Path:new(config.params_file) | ||
| 26 | params_file:write(vim.json.encode(self), 'w') | ||
| 27 | end | ||
| 28 | |||
| 29 | return ProjectConfig | ||
diff --git a/lua/tasks/runner.lua b/lua/tasks/runner.lua new file mode 100644 index 0000000..21bf765 --- /dev/null +++ b/lua/tasks/runner.lua | |||
| @@ -0,0 +1,226 @@ | |||
| 1 | local config = require('tasks.config') | ||
| 2 | local Job = require('plenary.job') | ||
| 3 | local runner = {} | ||
| 4 | |||
| 5 | local last_job | ||
| 6 | |||
| 7 | ---@param lines table | ||
| 8 | ---@param errorformat string? | ||
| 9 | local function append_to_quickfix(lines, errorformat) | ||
| 10 | vim.fn.setqflist({}, 'a', { efm = errorformat, lines = lines }) | ||
| 11 | -- Scrolls the quickfix buffer if not active | ||
| 12 | if vim.bo.buftype ~= 'quickfix' then | ||
| 13 | vim.api.nvim_command('cbottom') | ||
| 14 | end | ||
| 15 | end | ||
| 16 | |||
| 17 | ---@param errorformat? string | ||
| 18 | ---@return function: A coroutine that reads job data into quickfix. | ||
| 19 | local function read_to_quickfix(errorformat) | ||
| 20 | -- Modified from https://github.com/nvim-lua/plenary.nvim/blob/968a4b9afec0c633bc369662e78f8c5db0eba249/lua/plenary/job.lua#L287 | ||
| 21 | -- We use our own implementation to process data in chunks because | ||
| 22 | -- default Plenary callback processes every line which is very slow for adding to quickfix. | ||
| 23 | return coroutine.wrap(function(err, data, is_complete) | ||
| 24 | -- We repeat forever as a coroutine so that we can keep calling this. | ||
| 25 | local lines = {} | ||
| 26 | local result_index = 1 | ||
| 27 | local result_line = nil | ||
| 28 | local found_newline = nil | ||
| 29 | |||
| 30 | while true do | ||
| 31 | if data then | ||
| 32 | data = data:gsub('\r', '') | ||
| 33 | |||
| 34 | local processed_index = 1 | ||
| 35 | local data_length = #data + 1 | ||
| 36 | |||
| 37 | repeat | ||
| 38 | local start = data:find('\n', processed_index, true) or data_length | ||
| 39 | local line = data:sub(processed_index, start - 1) | ||
| 40 | found_newline = start ~= data_length | ||
| 41 | |||
| 42 | -- Concat to last line if there was something there already. | ||
| 43 | -- This happens when "data" is broken into chunks and sometimes | ||
| 44 | -- the content is sent without any newlines. | ||
| 45 | if result_line then | ||
| 46 | result_line = result_line .. line | ||
| 47 | |||
| 48 | -- Only put in a new line when we actually have new data to split. | ||
| 49 | -- This is generally only false when we do end with a new line. | ||
| 50 | -- It prevents putting in a "" to the end of the results. | ||
| 51 | elseif start ~= processed_index or found_newline then | ||
| 52 | result_line = line | ||
| 53 | |||
| 54 | -- Otherwise, we don't need to do anything. | ||
| 55 | end | ||
| 56 | |||
| 57 | if found_newline then | ||
| 58 | if not result_line then | ||
| 59 | return vim.api.nvim_err_writeln('Broken data thing due to: ' .. tostring(result_line) .. ' ' .. tostring(data)) | ||
| 60 | end | ||
| 61 | |||
| 62 | table.insert(lines, err and err or result_line) | ||
| 63 | |||
| 64 | result_index = result_index + 1 | ||
| 65 | result_line = nil | ||
| 66 | end | ||
| 67 | |||
| 68 | processed_index = start + 1 | ||
| 69 | until not found_newline | ||
| 70 | end | ||
| 71 | |||
| 72 | if is_complete and not found_newline then | ||
| 73 | table.insert(lines, err and err or result_line) | ||
| 74 | end | ||
| 75 | |||
| 76 | if #lines ~= 0 then | ||
| 77 | -- Move lines to another variable and send them to quickfix | ||
| 78 | local processed_lines = lines | ||
| 79 | lines = {} | ||
| 80 | vim.schedule(function() append_to_quickfix(processed_lines, errorformat) end) | ||
| 81 | end | ||
| 82 | |||
| 83 | if data == nil or is_complete then | ||
| 84 | return | ||
| 85 | end | ||
| 86 | |||
| 87 | err, data, is_complete = coroutine.yield() | ||
| 88 | end | ||
| 89 | end) | ||
| 90 | end | ||
| 91 | |||
| 92 | --- Run specified commands in chain. | ||
| 93 | ---@param task_name string: Name of a task to read properties. | ||
| 94 | ---@param commands table: Commands to chain. | ||
| 95 | ---@param module_config table: Module configuration. | ||
| 96 | ---@param addition_args table?: Additional arguments that will be applied to the last command. | ||
| 97 | ---@param previous_job table?: Previous job to read data from, used by this function for recursion. | ||
| 98 | function runner.chain_commands(task_name, commands, module_config, addition_args, previous_job) | ||
| 99 | local command = commands[1] | ||
| 100 | if vim.is_callable(command) then | ||
| 101 | command = command(module_config, previous_job) | ||
| 102 | if not command then | ||
| 103 | return | ||
| 104 | end | ||
| 105 | end | ||
| 106 | |||
| 107 | local cwd = command.cwd or vim.loop.cwd() | ||
| 108 | local args = command.args and command.args or {} | ||
| 109 | local env = vim.tbl_extend('force', vim.loop.os_environ(), command.env and command.env or {}) | ||
| 110 | if #commands == 1 then | ||
| 111 | -- Apply task parameters only to the last command | ||
| 112 | vim.list_extend(args, addition_args) | ||
| 113 | vim.list_extend(args, vim.tbl_get(module_config, 'args', task_name) or {}) | ||
| 114 | env = vim.tbl_extend('force', env, vim.tbl_get(module_config, 'env', task_name) or {}) | ||
| 115 | end | ||
| 116 | |||
| 117 | if command.dap_name then | ||
| 118 | vim.schedule(function() | ||
| 119 | local dap = require('dap') | ||
| 120 | local dap_config = dap.configurations[command.dap_name] -- Try to get an existing configuration | ||
| 121 | dap.run(vim.tbl_extend('force', dap_config and dap_config or { type = command.dap_name }, { | ||
| 122 | name = command.cmd, | ||
| 123 | request = 'launch', | ||
| 124 | program = command.cmd, | ||
| 125 | args = args, | ||
| 126 | })) | ||
| 127 | if config.dap_open_command then | ||
| 128 | vim.api.nvim_command('cclose') | ||
| 129 | config.dap_open_command() | ||
| 130 | end | ||
| 131 | last_job = dap | ||
| 132 | end) | ||
| 133 | return | ||
| 134 | end | ||
| 135 | |||
| 136 | local quickfix_output = not command.ignore_stdout and not command.ignore_stderr | ||
| 137 | local job = Job:new({ | ||
| 138 | command = command.cmd, | ||
| 139 | args = args, | ||
| 140 | cwd = cwd, | ||
| 141 | env = env, | ||
| 142 | enable_recording = #commands ~= 1, | ||
| 143 | on_start = quickfix_output and vim.schedule_wrap(function() | ||
| 144 | vim.fn.setqflist({}, ' ', { title = command.cmd .. ' ' .. table.concat(args, ' ') }) | ||
| 145 | vim.api.nvim_command(string.format('%s copen %d', config.quickfix.pos, config.quickfix.height)) | ||
| 146 | vim.api.nvim_command('wincmd p') | ||
| 147 | end) or nil, | ||
| 148 | on_exit = vim.schedule_wrap(function(_, code, signal) | ||
| 149 | if quickfix_output then | ||
| 150 | append_to_quickfix({ 'Exited with code ' .. (signal == 0 and code or 128 + signal) }) | ||
| 151 | end | ||
| 152 | if code == 0 and signal == 0 and command.after_success then | ||
| 153 | command.after_success() | ||
| 154 | end | ||
| 155 | end), | ||
| 156 | }) | ||
| 157 | |||
| 158 | job:start() | ||
| 159 | if not command.ignore_stdout then | ||
| 160 | job.stdout:read_start(read_to_quickfix(command.errorformat)) | ||
| 161 | end | ||
| 162 | if not command.ignore_stderr then | ||
| 163 | job.stderr:read_start(read_to_quickfix(command.errorformat)) | ||
| 164 | end | ||
| 165 | |||
| 166 | if #commands ~= 1 then | ||
| 167 | job:after_success(function() runner.chain_commands(task_name, vim.list_slice(commands, 2), module_config, addition_args, job) end) | ||
| 168 | end | ||
| 169 | last_job = job | ||
| 170 | end | ||
| 171 | |||
| 172 | ---@return string? | ||
| 173 | function runner.get_current_job_name() | ||
| 174 | if not last_job then | ||
| 175 | return nil | ||
| 176 | end | ||
| 177 | |||
| 178 | -- Check if this job was run through debugger. | ||
| 179 | if last_job.session then | ||
| 180 | local session = last_job.session() | ||
| 181 | if not session then | ||
| 182 | return nil | ||
| 183 | end | ||
| 184 | return session.config.program | ||
| 185 | end | ||
| 186 | |||
| 187 | if last_job.is_shutdown then | ||
| 188 | return nil | ||
| 189 | end | ||
| 190 | |||
| 191 | return last_job.cmd | ||
| 192 | end | ||
| 193 | |||
| 194 | ---@return boolean: `true` if a job was canceled or `false` if there is no active job. | ||
| 195 | function runner.cancel_job() | ||
| 196 | if not last_job then | ||
| 197 | return false | ||
| 198 | end | ||
| 199 | |||
| 200 | -- Check if this job was run through debugger. | ||
| 201 | if last_job.session then | ||
| 202 | if not last_job.session() then | ||
| 203 | return false | ||
| 204 | end | ||
| 205 | last_job.terminate() | ||
| 206 | return true | ||
| 207 | end | ||
| 208 | |||
| 209 | if last_job.is_shutdown then | ||
| 210 | return false | ||
| 211 | end | ||
| 212 | |||
| 213 | last_job:shutdown(1, 9) | ||
| 214 | |||
| 215 | if vim.fn.has('win32') == 1 then | ||
| 216 | -- Kill all children. | ||
| 217 | for _, pid in ipairs(vim.api.nvim_get_proc_children(last_job.pid)) do | ||
| 218 | vim.loop.kill(pid, 9) | ||
| 219 | end | ||
| 220 | else | ||
| 221 | vim.loop.kill(last_job.pid, 9) | ||
| 222 | end | ||
| 223 | return true | ||
| 224 | end | ||
| 225 | |||
| 226 | return runner | ||
diff --git a/lua/tasks/subcommands.lua b/lua/tasks/subcommands.lua new file mode 100644 index 0000000..a4ec9ca --- /dev/null +++ b/lua/tasks/subcommands.lua | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | local tasks = require('tasks') | ||
| 2 | local utils = require('tasks.utils') | ||
| 3 | local constants = require('tasks.constants') | ||
| 4 | local subcommands = {} | ||
| 5 | |||
| 6 | --- Completes `:Task` command. | ||
| 7 | ---@param arg string: Current argument under cursor. | ||
| 8 | ---@param cmd_line string: All arguments. | ||
| 9 | ---@return table: List of all commands matched with `arg`. | ||
| 10 | function subcommands.complete(arg, cmd_line) | ||
| 11 | local matches = {} | ||
| 12 | |||
| 13 | local words = vim.split(cmd_line, ' ', { trimempty = true }) | ||
| 14 | if not vim.endswith(cmd_line, ' ') then | ||
| 15 | -- Last word is not fully typed, don't count it | ||
| 16 | table.remove(words, #words) | ||
| 17 | end | ||
| 18 | |||
| 19 | if #words == 1 then | ||
| 20 | for subcommand in pairs(tasks) do | ||
| 21 | if vim.startswith(subcommand, arg) and subcommand ~= 'setup' then | ||
| 22 | table.insert(matches, subcommand) | ||
| 23 | end | ||
| 24 | end | ||
| 25 | elseif #words == 2 then | ||
| 26 | if words[2] == 'start' or words[2] == 'set_task_param' or words[2] == 'set_module_param' then | ||
| 27 | local module_names = utils.get_module_names() | ||
| 28 | table.insert(module_names, 'auto') -- Special value for automatic module detection | ||
| 29 | for _, module_name in ipairs(module_names) do | ||
| 30 | if vim.startswith(module_name, arg) then | ||
| 31 | table.insert(matches, module_name) | ||
| 32 | end | ||
| 33 | end | ||
| 34 | end | ||
| 35 | elseif #words == 3 then | ||
| 36 | if words[2] == 'start' or words[2] == 'set_task_param' or words[2] == 'set_module_param' then | ||
| 37 | local ok, module = pcall(require, 'tasks.module.' .. words[3]) | ||
| 38 | if ok then | ||
| 39 | for key, value in pairs(words[2] == 'set_module_param' and module.params or module.tasks) do | ||
| 40 | local name = type(key) == 'number' and value or key -- Handle arrays | ||
| 41 | if vim.startswith(name, arg) then | ||
| 42 | table.insert(matches, name) | ||
| 43 | end | ||
| 44 | end | ||
| 45 | end | ||
| 46 | end | ||
| 47 | elseif #words == 4 then | ||
| 48 | if words[2] == 'set_task_param' then | ||
| 49 | for _, param_name in ipairs(constants.task_params) do | ||
| 50 | if vim.startswith(param_name, arg) then | ||
| 51 | table.insert(matches, param_name) | ||
| 52 | end | ||
| 53 | end | ||
| 54 | end | ||
| 55 | end | ||
| 56 | |||
| 57 | return matches | ||
| 58 | end | ||
| 59 | |||
| 60 | --- Run specified subcommand received from completion. | ||
| 61 | ---@param subcommand table | ||
| 62 | function subcommands.run(subcommand) | ||
| 63 | local subcommand_func = tasks[subcommand.fargs[1]] | ||
| 64 | if not subcommand_func then | ||
| 65 | utils.notify(string.format('No such subcommand named "%s"', subcommand.fargs[1]), vim.log.levels.ERROR) | ||
| 66 | return | ||
| 67 | end | ||
| 68 | local subcommand_info = debug.getinfo(subcommand_func) | ||
| 69 | if subcommand_info.isvararg and #subcommand.fargs - 1 < subcommand_info.nparams then | ||
| 70 | utils.notify(string.format('Subcommand %s should have at least %s argument(s)', subcommand.fargs[1], subcommand_info.nparams + 1), vim.log.levels.ERROR) | ||
| 71 | return | ||
| 72 | elseif not subcommand_info.isvararg and #subcommand.fargs - 1 ~= subcommand_info.nparams then | ||
| 73 | utils.notify(string.format('Subcommand %s should have %s argument(s)', subcommand.fargs[1], subcommand_info.nparams + 1), vim.log.levels.ERROR) | ||
| 74 | return | ||
| 75 | end | ||
| 76 | subcommand_func(unpack(subcommand.fargs, 2)) | ||
| 77 | end | ||
| 78 | |||
| 79 | return subcommands | ||
diff --git a/lua/tasks/utils.lua b/lua/tasks/utils.lua new file mode 100644 index 0000000..983bde7 --- /dev/null +++ b/lua/tasks/utils.lua | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | local scandir = require('plenary.scandir') | ||
| 2 | local Path = require('plenary.path') | ||
| 3 | local utils = {} | ||
| 4 | |||
| 5 | local args_regex = vim.regex([[\s\%(\%([^'"]*\(['"]\)[^'"]*\1\)*[^'"]*$\)\@=]]) | ||
| 6 | |||
| 7 | --- A small wrapper around `vim.notify` that adds plugin title. | ||
| 8 | ---@param msg string | ||
| 9 | ---@param log_level number | ||
| 10 | function utils.notify(msg, log_level) vim.notify(msg, log_level, { title = 'Tasks' }) end | ||
| 11 | |||
| 12 | --- Splits command line arguments respecting quotes. | ||
| 13 | ---@param args string? | ||
| 14 | ---@return table | ||
| 15 | function utils.split_args(args) | ||
| 16 | if not args then | ||
| 17 | return {} | ||
| 18 | end | ||
| 19 | |||
| 20 | -- Split on spaces unless in quotes. | ||
| 21 | local splitted_args = {} | ||
| 22 | local match_beg | ||
| 23 | while true do | ||
| 24 | match_beg = args_regex:match_str(args) | ||
| 25 | if match_beg then | ||
| 26 | table.insert(splitted_args, args:sub(1, match_beg)) | ||
| 27 | args = args:sub(match_beg + 2) | ||
| 28 | else | ||
| 29 | -- Insert last arg left. | ||
| 30 | table.insert(splitted_args, args) | ||
| 31 | break | ||
| 32 | end | ||
| 33 | end | ||
| 34 | |||
| 35 | -- Remove quotes | ||
| 36 | for i, arg in ipairs(splitted_args) do | ||
| 37 | splitted_args[i] = arg:gsub('"', ''):gsub("'", '') | ||
| 38 | end | ||
| 39 | return splitted_args | ||
| 40 | end | ||
| 41 | |||
| 42 | --- Joins command line arguments respecting spaces by putting double quotes around them. | ||
| 43 | ---@param args table? | ||
| 44 | ---@return string | ||
| 45 | function utils.join_args(args) | ||
| 46 | if not args then | ||
| 47 | return '' | ||
| 48 | end | ||
| 49 | |||
| 50 | -- Add quotes if argument contain spaces | ||
| 51 | for index, arg in ipairs(args) do | ||
| 52 | if arg:find(' ') then | ||
| 53 | args[index] = '"' .. arg .. '"' | ||
| 54 | end | ||
| 55 | end | ||
| 56 | |||
| 57 | return table.concat(args, ' ') | ||
| 58 | end | ||
| 59 | |||
| 60 | ---@return table | ||
| 61 | function utils.get_module_names() | ||
| 62 | local module_dir = Path:new(debug.getinfo(1).source:sub(2)):parent() / 'module' | ||
| 63 | |||
| 64 | local modules = {} | ||
| 65 | for _, entry in ipairs(scandir.scan_dir(module_dir.filename, { depth = 1 })) do | ||
| 66 | -- Strip full path and extension | ||
| 67 | local extension_len = 4 | ||
| 68 | local parent_offset = 2 | ||
| 69 | table.insert(modules, entry:sub(#Path:new(entry):parent().filename + parent_offset, #entry - extension_len)) | ||
| 70 | end | ||
| 71 | |||
| 72 | return modules | ||
| 73 | end | ||
| 74 | |||
| 75 | --- Find a module by name | ||
| 76 | ---@param module_type string name of a module or `auto` string to pick a first module that match a condition. | ||
| 77 | ---@return table?, string?: module and its name. | ||
| 78 | function utils.get_module(module_type) | ||
| 79 | if module_type == 'auto' then | ||
| 80 | for _, name in ipairs(utils.get_module_names()) do | ||
| 81 | local module = require('tasks.module.' .. name) | ||
| 82 | if module.condition() then | ||
| 83 | return module, name | ||
| 84 | end | ||
| 85 | end | ||
| 86 | |||
| 87 | utils.notify('Unable to autodetect module for this working directory', vim.log.levels.ERROR) | ||
| 88 | return nil, nil | ||
| 89 | end | ||
| 90 | |||
| 91 | local module = require('tasks.module.' .. module_type) | ||
| 92 | if not module then | ||
| 93 | utils.notify('Unable to find a module named ' .. module_type, vim.log.levels.ERROR) | ||
| 94 | return nil, nil | ||
| 95 | end | ||
| 96 | |||
| 97 | return module, module_type | ||
| 98 | end | ||
| 99 | |||
| 100 | return utils | ||
