Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- depends.lua v0.1
- -- by dlord
- CONFIG = {
- DEPENDENCIES_LOCATION = "/lib",
- DEPENDENCIES = {},
- STARTUP = ""
- }
- local CONFIG_FILE = "dependencies_conf"
- local CONFIG_TEMPLATE = [[-- dependencies_conf
- -- Configuration file for the depends api helper.
- -- You will need to provide a table of keys
- -- containing the API names, and their respective
- -- pastebin code.
- DEPENDENCIES_LOCATION = "${DEPENDENCIES_LOCATION}"
- DEPENDENCIES = ${DEPENDENCIES}
- -- One of the dependencies can be set to run on startup.
- STARTUP = "${STARTUP}"
- ]]
- local function interpolate(s, params)
- return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
- end
- local function saveConfig(config)
- if config == nil then
- config = depends.CONFIG or CONFIG
- end
- local h = fs.open("/"..CONFIG_FILE, "w")
- local configToSave = {
- DEPENDENCIES_LOCATION = config.DEPENDENCIES_LOCATION or "/lib",
- DEPENDENCIES = textutils.serialize(config.DEPENDENCIES or {}),
- STARTUP = config.STARTUP
- }
- h.write(interpolate(CONFIG_TEMPLATE, configToSave))
- h.close()
- end
- local function downloadFromPastebin(pastebinKey, path)
- if fs.exists(path) then
- print(path.." already exists.")
- print("If you are sure that you want to download a copy of this file, delete the existing one first, and try again.")
- error()
- end
- print("Retrieving from pastebin: "..pastebinKey)
- shell.run("pastebin", "get", pastebinKey, path)
- if fs.exists(path) == false then
- print("Unable to retrieve "..pastebinKey.." from pastebin.")
- print("Check your connection, and try again.")
- error()
- end
- end
- local function uploadDependencies()
- -- copy the existing dependencies, as we will mutate it.
- local dependencies = {}
- for k, v in pairs(depends.CONFIG.DEPENDENCIES) do
- dependencies[k] = v
- end
- for k, v in pairs(dependencies) do
- if v == "" then
- print("Uploading "..k)
- local h = fs.open(fs.combine(depends.CONFIG.DEPENDENCIES_LOCATION, k), "r")
- local text = h.readAll()
- h.close()
- local apiKey = "0ec2eb25b6166c0c27a394ae118ad829"
- local response = http.post(
- "http://pastebin.com/api/api_post.php",
- "api_option=paste&"..
- "api_dev_key="..apiKey.."&"..
- "api_paste_format=lua&"..
- "api_paste_name="..textutils.urlEncode(k).."&"..
- "api_paste_code="..textutils.urlEncode(text)
- )
- if response then
- local responseString = response.readAll()
- response.close()
- local pastebinKey = string.match(responseString, "[^/]+$")
- if pastebinKey == "Post limit, maximum pastes per 24h reached" then
- print("You have reached pastebin's post limits. Try again later, or use the web interface.")
- error()
- end
- depends.CONFIG.DEPENDENCIES[k] = pastebinKey
- saveConfig(depends.CONFIG)
- else
- print("Failed to upload "..k..". Check your internet connection, and try again.")
- error()
- end
- end
- end
- end
- -- main functions of depends.lua
- reload = function(configFile)
- local configToLoad = configFile or CONFIG_FILE
- os.unloadAPI(configToLoad)
- if os.loadAPI("/"..configToLoad) == false then
- error("Could not load the config file!")
- end
- depends.CONFIG = getfenv(0)[configToLoad]
- if depends.CONFIG == nil then
- print("I could not find the necessary config.")
- print("You probably screwed something up.")
- error()
- end
- end
- on = function(name, pastebinKey)
- local dependencyPath = depends.CONFIG.DEPENDENCIES_LOCATION.."/"..name
- local isAvailable, key = depends.check(name, pastebinKey)
- os.unloadAPI(name)
- if isAvailable == false then
- print(name.." not found. Attempting to download from pastebin: "..key)
- if depends.download(name, key) then
- isAvailable = true
- end
- end
- if isAvailable then
- if depends.CONFIG.DEPENDENCIES[name] == nil then
- depends.CONFIG.DEPENDENCIES[name] = key or ""
- saveConfig(depends.CONFIG)
- end
- os.loadAPI(dependencyPath)
- end
- end
- check = function(name, pastebinKey)
- if name == "depends" then
- error("depends is a reserved name.")
- end
- depends.reload()
- local dependencyPath = depends.CONFIG.DEPENDENCIES_LOCATION.."/"..name
- local isAvailable = fs.exists(dependencyPath)
- local key = depends.CONFIG.DEPENDENCIES[name]
- if isAvailable == false and (key == nil or key == "") then
- key = pastebinKey
- if key == nil or key == "" then
- error(name.." is not available. You will need to provide a pastebin key.")
- end
- end
- return isAvailable, key
- end
- download = function(name, pastebinKey)
- local key = depends.CONFIG.DEPENDENCIES[name] or pastebinKey
- if key == nil and (pastebinKey == nil or pastebinKey == "") then
- error(name.." was not declared as a dependency.")
- end
- if key == "" then
- key = pastebinKey
- if key == "" then
- print("No pastebin key defined. Skipping.")
- return false
- end
- end
- local dependencyPath = depends.CONFIG.DEPENDENCIES_LOCATION.."/"..name
- downloadFromPastebin(key, dependencyPath)
- return true
- end
- -- main functions of depends.lua
- local function setupAPI(force)
- local globalEnv = getfenv(0)
- if globalEnv.depends == nil or force then
- globalEnv.depends = {
- CONFIG = CONFIG,
- reload = reload,
- on = on,
- check = check,
- download = download
- }
- end
- end
- setupAPI()
- local function downloadConfig(pastebinKey)
- local configPath = "/"..CONFIG_FILE
- downloadFromPastebin(pastebinKey, configPath)
- end
- local function runStartup(startupFile, shellArgs)
- local fileToRun = depends.CONFIG.DEPENDENCIES_LOCATION.."/"..startupFile
- if fs.exists(startupFile) then
- fileToRun = startupFile
- else
- local isAvailable, key = depends.check(startupFile)
- if isAvailable == false then
- depends.download(startupFile, key)
- end
- if depends.CONFIG.DEPENDENCIES[startupFile] == nil then
- depends.CONFIG.DEPENDENCIES[startupFile] = key or ""
- end
- end
- depends.CONFIG.STARTUP = startupFile
- saveConfig(depends.CONFIG)
- shell.run(fileToRun, unpack(shellArgs))
- end
- local function run(shellArgs)
- local configFile = CONFIG_FILE
- local startupFile = nil
- local args = shellArgs
- if #shellArgs > 0 then
- if shellArgs[1] == "--reload" then
- setupAPI(true)
- elseif shellArgs[1] == "--run" and shellArgs[2] then
- print("Running "..shellArgs[2])
- startupFile = shellArgs[2]
- args = {}
- for i=3, #shellArgs do
- table.insert(args, shellArgs[i])
- end
- elseif shellArgs[1] == "--get-config" and shellArgs[2] then
- downloadConfig(shellArgs[2])
- return
- elseif shellArgs[1] == "--config" and shellArgs[2] then
- configFile = shellArgs[2]
- elseif shellArgs[1] == "--publish" then
- uploadDependencies()
- return
- end
- end
- if fs.exists(CONFIG_FILE) == false then
- print("I can't find the config file: "..CONFIG_FILE)
- print("Generating one for you.")
- saveConfig()
- print("")
- print("Configuration file is located at /"..CONFIG_FILE)
- return
- end
- depends.reload(configFile)
- startupFile = startupFile or depends.CONFIG.STARTUP
- if startupFile and startupFile ~= "" then
- runStartup(startupFile, args)
- else
- print("No startup dependency defined.")
- end
- end
- if shell then
- run({...})
- else
- print("Running depends.lua as an API")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement