lua_runner.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. local platform = vim.uv.os_uname()
  2. local deps_install_dir = table.remove(_G.arg, 1)
  3. local subcommand = table.remove(_G.arg, 1)
  4. local suffix = (platform and platform.sysname:lower():find 'windows') and '.dll' or '.so'
  5. package.path = (deps_install_dir .. '/share/lua/5.1/?.lua;')
  6. .. (deps_install_dir .. '/share/lua/5.1/?/init.lua;')
  7. .. package.path
  8. package.cpath = deps_install_dir .. '/lib/lua/5.1/?' .. suffix .. ';' .. package.cpath
  9. local uv = vim.uv
  10. -- we use busted and luacheck and their lua dependencies
  11. -- But installing their binary dependencies with luarocks is very
  12. -- slow, replace them with vim.uv wrappers
  13. local system = {}
  14. package.loaded['system.core'] = system
  15. function system.monotime()
  16. uv.update_time()
  17. return uv.now() * 1e-3
  18. end
  19. function system.gettime()
  20. local sec, usec = uv.gettimeofday()
  21. return sec + usec * 1e-6
  22. end
  23. function system.sleep(sec)
  24. uv.sleep(sec * 1e3)
  25. end
  26. local term = {}
  27. package.loaded['term.core'] = term
  28. function term.isatty(_)
  29. return uv.guess_handle(1) == 'tty'
  30. end
  31. local lfs = { _VERSION = 'fake' }
  32. package.loaded['lfs'] = lfs
  33. function lfs.attributes(path, attr)
  34. local stat = uv.fs_stat(path)
  35. if attr == 'mode' then
  36. return stat and stat.type or ''
  37. elseif attr == 'modification' then
  38. if not stat then
  39. return nil
  40. end
  41. local mtime = stat.mtime
  42. return mtime.sec + mtime.nsec * 1e-9
  43. else
  44. error('not implemented')
  45. end
  46. end
  47. function lfs.currentdir()
  48. return uv.cwd()
  49. end
  50. function lfs.chdir(dir)
  51. local status, err = pcall(uv.chdir, dir)
  52. if status then
  53. return true
  54. else
  55. return nil, err
  56. end
  57. end
  58. function lfs.dir(path)
  59. local fs = uv.fs_scandir(path)
  60. return function()
  61. if not fs then
  62. return
  63. end
  64. return uv.fs_scandir_next(fs)
  65. end
  66. end
  67. function lfs.mkdir(dir)
  68. return uv.fs_mkdir(dir, 493) -- octal 755
  69. end
  70. if subcommand == 'busted' then
  71. require 'busted.runner'({ standalone = false })
  72. elseif subcommand == 'luacheck' then
  73. require 'luacheck.main'
  74. else
  75. error 'unknown subcommand'
  76. end