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/tasks/module | |
| download | neovim-tasks-fb10b4906aadaeff295883d171c05246943e5571.tar.gz neovim-tasks-fb10b4906aadaeff295883d171c05246943e5571.zip | |
Initial commit
Diffstat (limited to 'lua/tasks/module')
| -rw-r--r-- | lua/tasks/module/cargo.lua | 171 | ||||
| -rw-r--r-- | lua/tasks/module/cmake.lua | 272 |
2 files changed, 443 insertions, 0 deletions
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 | ||
