Advertisement
TheProdigy22

Untitled

Dec 2nd, 2020
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.52 KB | None | 0 0
  1. -- get make_package so we can overwrite it
  2. local make_package = dofile("rom/modules/main/cc/require.lua").make
  3.  
  4. -- make some upvalues
  5. -- tProgramStack is from the OG shell; keeps track of the stack of running programs
  6. local tProgramStack = {}
  7. local userPaths_mt = {
  8.     __index=userPaths,
  9.     __newindex=function() error("Use customPackage.addPackagePath() to add to the userPaths list.", 2) end,
  10.     __metatable="Dude, what are you trying to do?"
  11. }
  12. -- make a global variable we can access from any program
  13. _G.customPackage = {
  14.     userPaths = setmetatable({}, userPaths_mt)
  15. }
  16. --
  17. customPackage.addPackagePath = function(path)
  18.     if not fs.isDir(path) then
  19.         error(path.." is not a directory",2)
  20.     end
  21.     if path:sub(1,1) ~= "/" then
  22.         error(path.." is not absolute (Make sure there's a leading slash)",2)
  23.     end
  24.     -- add trailing slash if there isn't one
  25.     rawset(customPackage.userPaths, #customPackage.userPaths + 1, (path:sub(-1,-1) == "/") and path or path.."/")
  26. end
  27.  
  28. local function createShellEnv(dir)
  29.     local env = { shell = shell, multishell = multishell }
  30.     env.require, env.package = make_package(env, dir)
  31.     return env
  32. end
  33.  
  34. function shell.execute(command, ...)
  35.     local sPath = shell.resolveProgram(command)
  36.     if sPath ~= nil then
  37.         tProgramStack[#tProgramStack + 1] = sPath
  38.         if multishell then
  39.             local sTitle = fs.getName(sPath)
  40.             if sTitle:sub(-4) == ".lua" then
  41.                 sTitle = sTitle:sub(1, -5)
  42.             end
  43.             multishell.setTitle(multishell.getCurrent(), sTitle)
  44.         end
  45.        
  46.         local sDir = fs.getDir(sPath)
  47.         local env = createShellEnv(sDir)
  48.         env.arg = { [0] = command, ... }
  49.         for _, path in ipairs(customPackage.userPaths) do
  50.             env.package.path = path.."?;"..path.."?.lua;"..path.."?/init.lua;"..env.package.path
  51.         end
  52.         local result = os.run(env, sPath, ...)
  53.        
  54.         tProgramStack[#tProgramStack] = nil
  55.         if multishell then
  56.             if #tProgramStack > 0 then
  57.                 local sTitle = fs.getName(tProgramStack[#tProgramStack])
  58.                 if sTitle:sub(-4) == ".lua" then
  59.                     sTitle = sTitle:sub(1, -5)
  60.                 end
  61.                 multishell.setTitle(multishell.getCurrent(), sTitle)
  62.             else
  63.                 multishell.setTitle(multishell.getCurrent(), "shell")
  64.             end
  65.         end
  66.         return result
  67.     else
  68.         printError("No such program")
  69.         return false
  70.     end
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement