Snusmumriken

Lua launcher for shortcuts

Dec 13th, 2018 (edited)
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.04 KB | None | 0 0
  1. -- WTF: this is lua script launcher for shortcuts or debugging
  2.  
  3. -- Usage: run in shell:
  4. -- path/to/lua/executable /path/to/this/launcher.lua /path/to/script.lua
  5.  
  6. -- Example: notepad++ shortcut looks like this (alt + f1):
  7. -- <Command name="Lua" Ctrl="no" Alt="yes" Shift="no" Key="112">
  8. --   luajit c:\lua\launcher.lua $(FULL_CURRENT_PATH)
  9. -- </Command>
  10.  
  11. -- Features:
  12. -- 1. Console window doesn't disappear, we can debug script after launch
  13. -- 2. Package paths configured for [/path/to/script.lua], run it like project
  14. -- 3. Sweet traceback!
  15.  
  16. -- Tricks: write 'cont' on 'lua_debug' mode
  17.  
  18. -- Written by snus (C) for internal corporate usage, if you see it - go to jail now.
  19.  
  20. local path_sep   = package.config:sub(1, 1)
  21. local shared_ext = path_sep == '\\' and 'dll' or 'so'
  22.  
  23. -- shift args back
  24. for i = 0, #arg do
  25.     arg[i] = arg[i + 1]
  26. end
  27.  
  28. -- Init path
  29. local scrPath = arg[0] or os.exit()
  30. scrPath = scrPath:gsub('[\\/]', '/')
  31.  
  32. local __path, __name = scrPath:match'^(.*)/(.-).lua$'
  33. local __name_ext     = __name .. '.lua'
  34.  
  35. package.path  = __path..'/?/init.lua;' .. package.path
  36. package.path  = __path..'/?.lua;'      .. package.path
  37. package.cpath = __path..'/?/core.' .. shared_ext .. ';' .. package.cpath
  38. package.cpath = __path..'/?.'      .. shared_ext .. ';' .. package.cpath
  39.  
  40. package.path  = package.path:gsub('[\\/]', path_sep)
  41. package.cpath = package.cpath:gsub('[\\/]', path_sep)
  42.  
  43. local function errorprinter(msg, layer)
  44.   -- if it's nil or something
  45.     msg = tostring(msg)
  46.    
  47.     local trace = debug.traceback(msg, 1 + (layer or 1))
  48.     trace = trace:gsub('%[string "' .. __name_ext .. '"%]', __name_ext)
  49.    
  50.     -- cut four last lines of traceback
  51.     local lines = ('\n[^\n]+'):rep(4) .. '$'
  52.     trace = trace:gsub(lines, "")
  53.    
  54.     print("Error: " .. trace)
  55. end
  56.  
  57. xpcall(function()
  58.         local file = io.open(scrPath)
  59.         local code = file:read('*a'); file:close()
  60.         local chunk, err = loadstring(code, __name_ext)
  61.         if not chunk then error(err, 2) end
  62.         chunk()
  63.     end,
  64.     errorprinter
  65. )
  66.  
  67. -- term window is not closed at end of task
  68. debug.debug()
Add Comment
Please, Sign In to add comment