/ nvim / lua / utils / taskfile.nvim
taskfile.nvim
  1  " All credits for this plugin goes to https://github.com/orgs/mistweaverco
  2  " for some reason maintainer decided to delete repo, so I've copied the last
  3  " version I had cached locally as-is right below.
  4  
  5  if exists("g:loaded_Taskfile")
  6          finish
  7  endif
  8  let g:loaded_Taskfile = 1
  9  
 10  let s:PluginName = "Taskfile.nvim"
 11  let s:taskList = [""]
 12  let s:TaskfileWindow = 0
 13  
 14  function! s:FileExists(filepath)
 15          if filereadable(a:filepath)
 16                  return 1
 17          else
 18                  return 0
 19          endif
 20  endfunction
 21  
 22  function! Taskfile#Run(...)
 23          let filepath = s:GetTaskfileAbsoluteFilepath()
 24          if s:FileExists(filepath) == 0
 25                  echo "No Taskfile found"
 26          else
 27                  let task = get(a:, 1, "")
 28                  let cmd =  s:GetTaskfileAbsoluteFilepath() . " " . task
 29                  call s:ExecExternalCommand(cmd)
 30          endif
 31  endfunction
 32  
 33  function! Taskfile#Reload()
 34          " TODO Cache Taskfile tasks and only reload the cache when this
 35          " function fires
 36  endfunction
 37  
 38  function! Taskfile#List()
 39          let cfg = s:GetAllTasks()
 40          let cmd = s.GetTaskfileFilename()
 41          call s:ExecExternalCommand(cmd)
 42  endfunction
 43  
 44  function! s:OnTermEventHandler(job_id, data, event) dict
 45          if a:event == 'stdout'
 46                  " do nothing
 47          elseif a:event == 'stderr'
 48                  execute s:TaskfileWindow . "windo startinsert!"
 49          elseif a:data == 0
 50                  execute s:TaskfileWindow . "windo startinsert!"
 51          else
 52                  execute s:TaskfileWindow . "windo startinsert!"
 53          endif
 54  endfunction
 55  
 56  function! s:OnJobEventHandler(job_id, data, event) dict
 57          if a:event == 'stdout'
 58                  let str = self.shell.' stdout: '.join(a:data)
 59          elseif a:event == 'stderr'
 60                  let str = self.shell.' stderr: '.join(a:data)
 61          else
 62                  let str = self.shell.' finished'
 63          endif
 64          echom str
 65  endfunction
 66  
 67  let s:termEventCallbacks = {
 68          \ 'on_stdout': function('s:OnTermEventHandler'),
 69          \ 'on_stderr': function('s:OnTermEventHandler'),
 70          \ 'on_exit': function('s:OnTermEventHandler')
 71  \ }
 72  
 73  let s:jobEventCallbacks = {
 74          \ 'on_stdout': function('s:OnJobEventHandler'),
 75          \ 'on_stderr': function('s:OnJobEventHandler'),
 76          \ 'on_exit': function('s:OnJobEventHandler')
 77  \ }
 78  
 79  function! s:taskListCompletion(ArgLead, CmdLine, CursorPos)
 80          let filepath = s:GetTaskfileAbsoluteFilepath()
 81          if s:FileExists(filepath) == 0
 82                  return ""
 83          endif
 84          let s:taskList = s:GetAllTasks()
 85          return filter(s:taskList, 'v:val =~ "^'. a:ArgLead .'"')
 86  endfunction
 87  
 88  function! s:GetAllTasks()
 89          let filepath = s:GetTaskfileAbsoluteFilepath()
 90          let tasklist = systemlist(filepath . " __show_tasks")
 91          return tasklist
 92  endfunction
 93  
 94  function! s:GetTaskfileAbsoluteFilepath()
 95          let filepath = getcwd() . "/" . s:GetTaskfileFilename()
 96          return filepath
 97  endfunction
 98  
 99  function! s:GetTaskfileFilename()
100          return "Taskfile"
101  endfunction
102  
103  function! s:ExecExternalCommand(command)
104          if has("nvim") == 1
105                  if exists("g:TaskfileAsynchronous")
106                          call jobstart(["bash", "-c", a:command])
107                  else
108                          execute "!" . a:command
109                  endif
110          elseif v:version >= 800
111                  if exists("g:TaskfileAsynchronous")
112                          call job_start("bash -c " . a:command)
113                  else
114                          execute "!" . a:command
115                  endif
116          else
117                  if exists("g:TaskfileAsynchronous")
118                          silent execute "!" . a:command
119                  else
120                          execute "!" . a:command
121                  endif
122          endif
123  endfunction
124  
125  function! s:GetBasename(...)
126          let buffername = bufname("%")
127          let filepath = get(a:, 1, buffername)
128          return fnamemodify(filepath, ":t")
129  endfunction
130  
131  function! s:ReadfileAsString(filepath)
132          return readfile(a:filepath)
133  endfunction
134  
135  function! s:GetCurrentFile()
136          return expand("%")
137  endfunction
138  
139  command! -bang -complete=customlist,s:taskListCompletion -nargs=* Task call Taskfile#Run(<f-args>)