diff options
Diffstat (limited to 'lua/tasks/init.lua')
-rw-r--r-- | lua/tasks/init.lua | 142 |
1 files changed, 142 insertions, 0 deletions
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 | ||