lua_runner.lua 2.0 KB

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