Skip to content

Commit

Permalink
simple universal file patching
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksanderwlodarczyk committed Jan 29, 2019
1 parent 04d816a commit 2ac7a01
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
7 changes: 7 additions & 0 deletions file_patcher.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
file_patcher = {
preprocessors = {}
}

require("file_patcher.mod")

return file_patcher
3 changes: 3 additions & 0 deletions file_patcher/add_preprocessor.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function file_patcher.add_preprocessor(fn)
table.insert(file_patcher.preprocessors, fn)
end
21 changes: 21 additions & 0 deletions file_patcher/load_and_patch.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function file_patcher.load_and_patch(name, pre_content_write, post_content_write)
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")
if file then

file_patcher.write_to_file(created_file, pre_content_write)
file_patcher.rewrite_file_content(created_file, modulepath)
file_patcher.write_to_file(created_file, post_content_write)

created_file:close()
local to_compile = io.open("module.lua", "rb")
return assert(load(assert(to_compile:read("*a")), modulepath))
end
return require(name)
end

file_patcher.add_preprocessor(file_patcher.load_and_patch)
4 changes: 4 additions & 0 deletions file_patcher/mod.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require("file_patcher.add_preprocessor")
require("file_patcher.load_and_patch")
require("file_patcher.rewrite_content")
require("file_patcher.write_to_file")
6 changes: 6 additions & 0 deletions file_patcher/rewrite_file_content.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function file_patcher.rewrite_file_content(created_file, modulepath)
for line in io.lines(modulepath .. ".lua") do
created_file:write("\n\t" .. line)
end
end
end
3 changes: 3 additions & 0 deletions file_patcher/write_to_file.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function file_patcher.write_to_file(created_file, content)
created_file:write(content)
end

0 comments on commit 2ac7a01

Please sign in to comment.