Shatur / neovim-tasks

A statefull task manager focused on integration with build systems.
GNU General Public License v3.0
108 stars 10 forks source link

Dynamic tasks #16

Closed undyamon closed 1 year ago

undyamon commented 1 year ago

Hi, this is less of an issue and more of a "I found out how to do this and maybe someone will find it useful".

So, the idea is that it is possible to have dynamic tasks as part of a module. When creating your own module, you can do

{
  params = ...,
  condition = ...,
  tasks = setmetatable({
      -- normal (static) commands
    }, {
    __index = function(tab, key)
      return function(module_config, _)
        -- return task configuration based on requested task 'key'...
      end
    end
  })
}

An example would be to wrap make in a module and support arbitrary targets.

{
  params = { 'cmd' },
  condition = function()
    return require('plenary.path'):new('Makefile'):exists()
  end,
  tasks = setmetatable({}, {
    __index = function(_, target)
      return function(module_config, _)
        return {
          cmd = module_config.cmd,
          args = module_config.args and module_config.args[target] or { target },
          -- Close quickfix on success
          after_success = function() vim.cmd.cclose() end
        }
      end
    end
  })
}

With this, calling Task start make install will run make install (i.e., the task is mapped to the make target). This behavior could also be customized if we have the following in our setup

{
  default_params = {
    make = {
      cmd = "make",
      args = {
        build = {},
        nuke = { "clean" }
      } 
  }
}

Now

By adding a metatable to default_params.make.args, it could be possible to add dynamic task parameters for dynamic tasks.

Now, the example is not very useful unless one wants to use make with the same interface used for other modules, but you get the gist of it.

Shatur commented 1 year ago

This does make sense! Would you like to submit a PR?

undyamon commented 1 year ago

For the make module itself?

Shatur commented 1 year ago

Yes, could be a good built-in module and an interesting example. But you can have it in a separate repo of course.

undyamon commented 1 year ago

Btw, I should mention that I very much like this plugin... simple, highly hackable and gets the job done. I have been running all sorts of tasks with it, from docker compose shenanigan to scripts on my personal notes.

Shatur commented 1 year ago

Thank you, I'm glad you found it useful!