Advertisement
dlord

depends.lua

Jun 2nd, 2014
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.21 KB | None | 0 0
  1. -- depends.lua v0.1
  2. --     by dlord
  3.  
  4.  
  5. CONFIG = {
  6.     DEPENDENCIES_LOCATION = "/lib",
  7.     DEPENDENCIES = {},
  8.     STARTUP = ""
  9. }
  10. local CONFIG_FILE = "dependencies_conf"
  11.  
  12. local CONFIG_TEMPLATE = [[-- dependencies_conf
  13. -- Configuration file for the depends api helper.
  14. -- You will need to provide a table of keys
  15. -- containing the API names, and their respective
  16. -- pastebin code.
  17. DEPENDENCIES_LOCATION = "${DEPENDENCIES_LOCATION}"
  18.  
  19. DEPENDENCIES = ${DEPENDENCIES}
  20.  
  21. -- One of the dependencies can be set to run on startup.
  22. STARTUP = "${STARTUP}"
  23. ]]
  24.  
  25. local function interpolate(s, params)
  26.     return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
  27. end
  28.  
  29. local function saveConfig(config)
  30.     if config == nil then
  31.         config = depends.CONFIG or CONFIG
  32.     end
  33.     local h = fs.open("/"..CONFIG_FILE, "w")
  34.     local configToSave = {
  35.         DEPENDENCIES_LOCATION = config.DEPENDENCIES_LOCATION or "/lib",
  36.         DEPENDENCIES = textutils.serialize(config.DEPENDENCIES or {}),
  37.         STARTUP = config.STARTUP
  38.     }
  39.  
  40.     h.write(interpolate(CONFIG_TEMPLATE, configToSave))
  41.     h.close()
  42. end
  43.  
  44. local function downloadFromPastebin(pastebinKey, path)
  45.     if fs.exists(path) then
  46.         print(path.." already exists.")
  47.         print("If you are sure that you want to download a copy of this file, delete the existing one first, and try again.")
  48.         error()
  49.     end
  50.  
  51.     print("Retrieving from pastebin: "..pastebinKey)
  52.     shell.run("pastebin", "get", pastebinKey, path)
  53.  
  54.     if fs.exists(path) == false then
  55.         print("Unable to retrieve "..pastebinKey.." from pastebin.")
  56.         print("Check your connection, and try again.")
  57.         error()
  58.     end
  59. end
  60.  
  61. local function uploadDependencies()
  62.     -- copy the existing dependencies, as we will mutate it.
  63.     local dependencies = {}
  64.     for k, v in pairs(depends.CONFIG.DEPENDENCIES) do
  65.         dependencies[k] = v
  66.     end
  67.  
  68.     for k, v in pairs(dependencies) do
  69.         if v == "" then
  70.             print("Uploading "..k)
  71.  
  72.             local h = fs.open(fs.combine(depends.CONFIG.DEPENDENCIES_LOCATION, k), "r")
  73.             local text = h.readAll()
  74.             h.close()
  75.  
  76.             local apiKey = "0ec2eb25b6166c0c27a394ae118ad829"
  77.             local response = http.post(
  78.                 "http://pastebin.com/api/api_post.php",
  79.                 "api_option=paste&"..
  80.                 "api_dev_key="..apiKey.."&"..
  81.                 "api_paste_format=lua&"..
  82.                 "api_paste_name="..textutils.urlEncode(k).."&"..
  83.                 "api_paste_code="..textutils.urlEncode(text)
  84.             )
  85.  
  86.             if response then
  87.                 local responseString = response.readAll()
  88.                 response.close()
  89.  
  90.                 local pastebinKey = string.match(responseString, "[^/]+$")
  91.  
  92.                 if pastebinKey == "Post limit, maximum pastes per 24h reached" then
  93.                     print("You have reached pastebin's post limits. Try again later, or use the web interface.")
  94.                     error()
  95.                 end
  96.  
  97.                 depends.CONFIG.DEPENDENCIES[k] = pastebinKey
  98.                 saveConfig(depends.CONFIG)
  99.             else
  100.                 print("Failed to upload "..k..". Check your internet connection, and try again.")
  101.                 error()
  102.             end
  103.         end
  104.     end
  105. end
  106.  
  107. -- main functions of depends.lua
  108. reload = function(configFile)
  109.     local configToLoad = configFile or CONFIG_FILE
  110.     os.unloadAPI(configToLoad)
  111.  
  112.     if os.loadAPI("/"..configToLoad) == false then
  113.         error("Could not load the config file!")
  114.     end
  115.  
  116.     depends.CONFIG = getfenv(0)[configToLoad]
  117.  
  118.     if depends.CONFIG == nil then
  119.         print("I could not find the necessary config.")
  120.         print("You probably screwed something up.")
  121.         error()
  122.     end
  123. end
  124.  
  125. on = function(name, pastebinKey)
  126.     local dependencyPath = depends.CONFIG.DEPENDENCIES_LOCATION.."/"..name
  127.     local isAvailable, key = depends.check(name, pastebinKey)
  128.  
  129.     os.unloadAPI(name)
  130.  
  131.     if isAvailable == false then
  132.         print(name.." not found. Attempting to download from pastebin: "..key)
  133.  
  134.         if depends.download(name, key) then
  135.             isAvailable = true
  136.         end
  137.     end
  138.  
  139.     if isAvailable then
  140.         if depends.CONFIG.DEPENDENCIES[name] == nil then
  141.             depends.CONFIG.DEPENDENCIES[name] = key or ""
  142.             saveConfig(depends.CONFIG)
  143.         end
  144.  
  145.         os.loadAPI(dependencyPath)
  146.     end
  147. end
  148.  
  149. check = function(name, pastebinKey)
  150.     if name == "depends" then
  151.         error("depends is a reserved name.")
  152.     end
  153.     depends.reload()
  154.  
  155.     local dependencyPath = depends.CONFIG.DEPENDENCIES_LOCATION.."/"..name
  156.     local isAvailable = fs.exists(dependencyPath)
  157.     local key = depends.CONFIG.DEPENDENCIES[name]
  158.  
  159.     if isAvailable == false and (key == nil or key == "") then
  160.         key = pastebinKey
  161.  
  162.         if key == nil or key == "" then
  163.             error(name.." is not available. You will need to provide a pastebin key.")
  164.         end
  165.     end
  166.  
  167.     return isAvailable, key
  168. end
  169.  
  170. download = function(name, pastebinKey)
  171.     local key = depends.CONFIG.DEPENDENCIES[name] or pastebinKey
  172.  
  173.     if key == nil and (pastebinKey == nil or pastebinKey == "") then
  174.         error(name.." was not declared as a dependency.")
  175.     end
  176.  
  177.     if key == "" then
  178.         key = pastebinKey
  179.  
  180.         if key == "" then
  181.             print("No pastebin key defined. Skipping.")
  182.             return false
  183.         end
  184.     end
  185.  
  186.     local dependencyPath = depends.CONFIG.DEPENDENCIES_LOCATION.."/"..name
  187.  
  188.     downloadFromPastebin(key, dependencyPath)
  189.  
  190.     return true
  191. end
  192. -- main functions of depends.lua
  193.  
  194. local function setupAPI(force)
  195.     local globalEnv = getfenv(0)
  196.     if globalEnv.depends == nil or force then
  197.         globalEnv.depends = {
  198.             CONFIG = CONFIG,
  199.             reload = reload,
  200.             on = on,
  201.             check = check,
  202.             download = download
  203.         }
  204.     end
  205. end
  206.  
  207. setupAPI()
  208.  
  209. local function downloadConfig(pastebinKey)
  210.     local configPath = "/"..CONFIG_FILE
  211.  
  212.     downloadFromPastebin(pastebinKey, configPath)
  213. end
  214.  
  215. local function runStartup(startupFile, shellArgs)
  216.     local fileToRun = depends.CONFIG.DEPENDENCIES_LOCATION.."/"..startupFile
  217.  
  218.     if fs.exists(startupFile) then
  219.         fileToRun = startupFile
  220.     else
  221.         local isAvailable, key = depends.check(startupFile)
  222.         if isAvailable == false then
  223.             depends.download(startupFile, key)
  224.         end
  225.  
  226.         if depends.CONFIG.DEPENDENCIES[startupFile] == nil then
  227.             depends.CONFIG.DEPENDENCIES[startupFile] = key or ""
  228.         end
  229.     end
  230.  
  231.     depends.CONFIG.STARTUP = startupFile
  232.    
  233.     saveConfig(depends.CONFIG)
  234.  
  235.     shell.run(fileToRun, unpack(shellArgs))
  236. end
  237.  
  238. local function run(shellArgs)
  239.     local configFile = CONFIG_FILE
  240.     local startupFile = nil
  241.     local args = shellArgs
  242.  
  243.     if #shellArgs > 0 then
  244.         if shellArgs[1] == "--reload" then
  245.             setupAPI(true)
  246.         elseif shellArgs[1] == "--run" and shellArgs[2] then
  247.             print("Running "..shellArgs[2])
  248.  
  249.             startupFile = shellArgs[2]
  250.             args = {}
  251.             for i=3, #shellArgs do
  252.                 table.insert(args, shellArgs[i])
  253.             end
  254.         elseif shellArgs[1] == "--get-config" and shellArgs[2] then
  255.             downloadConfig(shellArgs[2])
  256.             return
  257.         elseif shellArgs[1] == "--config" and shellArgs[2] then
  258.             configFile = shellArgs[2]
  259.         elseif shellArgs[1] == "--publish" then
  260.             uploadDependencies()
  261.             return
  262.         end
  263.     end
  264.  
  265.     if fs.exists(CONFIG_FILE) == false then
  266.         print("I can't find the config file: "..CONFIG_FILE)
  267.         print("Generating one for you.")
  268.  
  269.         saveConfig()
  270.  
  271.         print("")
  272.         print("Configuration file is located at /"..CONFIG_FILE)
  273.         return
  274.     end
  275.  
  276.     depends.reload(configFile)
  277.  
  278.     startupFile = startupFile or depends.CONFIG.STARTUP
  279.  
  280.     if startupFile and startupFile ~= "" then
  281.         runStartup(startupFile, args)
  282.     else
  283.         print("No startup dependency defined.")
  284.     end
  285. end
  286.  
  287. if shell then
  288.     run({...})
  289. else
  290.     print("Running depends.lua as an API")
  291. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement