Last active
December 24, 2015 12:49
-
-
Save weswigham/6799855 to your computer and use it in GitHub Desktop.
Pull Lua dependencies straight from the web! Now adds a searcher to require, instead of other things!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local socket = require("socket") | |
local http = require("socket.http") | |
--Safety is overrated. Pull scripts form the web! | |
local function webclude(target, ...) | |
local body, status, res_head, res_stat = http.request(target, ...) | |
if body then | |
return body | |
else | |
local str = "Could not include from web file: "..target.."\n" | |
.."Status: "..status.."\n" | |
.."Response Headers: "..tostring(res_head).."\n" | |
.."Response Status: "..res_stat.."\n" | |
return nil, str | |
end | |
end | |
local function webclude_req(modulename) | |
-- Find source | |
local src, err = webclude(modulename) | |
if not src then | |
-- Return a partial message that require will aggregate | |
return "\n\tNo module '"..modulename.."' did not resolve; " ..err | |
else | |
-- Compile and return the module | |
local load = load | |
if loadstring then --5.1 | |
load = loadstring | |
end | |
return assert(load(src)) | |
end | |
end | |
table.insert(package.loaders, webclude_req) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment