Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

splitted into functions #211

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion src/handlers/bridge.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@

-- Patch require to log all executed modules
_G._require = require
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to have this while its on line 7 as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None, my mistake


local default_searchers = package.searchers[2]

_G._require = require
function _G.require (module_name)
if package.loaded[module_name] == nil then
Expand All @@ -8,6 +12,82 @@ function _G.require (module_name)
return _require(module_name)
end

package.searchers[2] = function(name)
package.preload[name] = function(modulename)
local created_file = io.open("module.lua", "w+")
local modulepath = string.gsub(modulename, "%.", "/")
local path = "/"
local filename = string.gsub(path, "%?", modulepath)
local file = io.open(filename, "rb")
modulepath = modulepath
if file then
process_module(modulepath, modulename, created_file)
-- Compile and return the module
local compiled = compile_module(modulepath)
os.execute("rm module.lua")
return compiled
end
end
return require(name)
end


function compile_module(modulepath)
local to_compile = io.open("module.lua", "rb")
local compiled = assert(load(assert(to_compile:read("*a")), modulepath))

return compiled
end

function count_lines(modulepath)
local counters = { }
-- count lines to check if last line isn't return
local line_count = 0;
local return_line_num = 0;
for line in io.lines(modulepath .. ".lua") do
if line ~= "" then line_count = line_count + 1 end
if string.find(line, "return") ~= nil then
return_line_num = line_count
end
end
counters[1] = line_count
counters[2] = return_line_num

return counters
end

function process_module(modulepath, modulename, created_file)
created_file:write("local module_timer = os.clock()")

line_nums = count_lines(modulepath)
-- count lines to check if last line isn't return
local line_count = line_nums[1];
local return_line_num = line_nums[2];

local return_on_last_line = false;
if return_line_num == line_count then return_on_last_line = true end
local last_line_return = ""

-- rewrite file content
local line_num = 0
for line in io.lines(modulepath .. ".lua") do

if line ~= "" then line_num = line_num + 1 end
if return_on_last_line == false or line_num ~= line_count then
created_file:write(line .. "\n\t")
else
last_line_return = line
end
end

created_file:write("\n\tlocal elapsed = (os.clock() - module_timer) * 1000")
created_file:write("\n\r_log.info(\"" .. modulename .. " done in \" .. elapsed .. \" milliseconds\")")
if return_on_last_line then
created_file:write("\n" .. last_line_return)
end
created_file:close()
end

xpcall(function ()
local init_f, err = loadfile(torchbear.init_filename)
if not init_f then error(err) end
Expand All @@ -26,4 +106,4 @@ end)

if not torchbear.handler then
_log.debug("No handler specified")
end
end