-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace.lua
More file actions
97 lines (81 loc) · 1.7 KB
/
Copy pathreplace.lua
File metadata and controls
97 lines (81 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
local function single(name)
return require(name)
end
local function batch(names)
local sources = {}
for index = 1, #names do
sources[index] = require(names[index])
end
return sources
end
local IMPORT_MAPPINGS = {
["string"] = single,
["table"] = batch,
}
path = {}
function path.import(names, path)
if not names then
return
end
local type = type(names)
if type == "table" and not next(names) then
return
end
local import = assert(IMPORT_MAPPINGS[type], type)
if path then
path, package.path = package.path, path .. "?.lua"
end
local state, sources = xpcall(import, debug.traceback, names)
if not state then
print(sources)
end
if path then
package.path = path
end
return sources
end
function path.current()
local info = debug.getinfo(2, "S")
local path = string.sub(info.source, 2)
return string.match(path, "^.*[/\\]")
end
local config = path.import("config", path.current())
local state, file = xpcall(io.open, debug.traceback, config.src)
if not state then
print(file)
end
local range = config.range
local from, to = range[1], range[2]
local src = {}
for index = 1, from - 1 do
file:read()
end
for index = from, to do
table.insert(src, file:read())
end
file:close()
local begin = config.to - 1
for _, name in ipairs(config.dest) do
local state, file = xpcall(io.open, debug.traceback, name)
if not state then
print(file)
break
end
local dest = {}
for line in file:lines() do
table.insert(dest, line)
end
file:close()
for index = 1, #src do
dest[begin + index] = src[index]
end
state, file = xpcall(io.open, debug.traceback, name, "w")
if not state then
print(file)
break
end
for index = 1, #dest do
file:write(dest[index], '\n')
end
file:close()
end