-
Notifications
You must be signed in to change notification settings - Fork 1
/
rename.lua
208 lines (166 loc) · 4.77 KB
/
rename.lua
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
--[[ renameLowercase.sh
luajit-2 ./rename.lua '@find . -path "./src/*[A-Z]*.h" -or -path "./src/*[A-Z]*.cpp" -or -path "./share/*[A-Z]*.layout"' \
'!return table.concat(f.PathSplit, "/", 1, #f.PathSplit-1) .. "/" .. f.PathSplit[#f.PathSplit]:lower()' \
'@find . -path "./src/*.h" -or -path "./src/*.cpp" -or -path "./share/*.xml"' \
'hg mv "%s" "%s"'
]]
local replacementPatterns = {'"%s"', '/%s"', '/%s/', '"%s"', '/%s"'}
local function split(str, pat)
local t = {}
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
local function splitPaths(State, Name, SplitName)
for _,v in ipairs(State) do
v[SplitName] = split(v[Name], '/+')
end
end
local function handleArg(Arg, State, Name)
assert(Arg, "Missing argument " .. Name)
if Arg:sub(1,1) == "!" then
local func, err = loadstring(Arg:sub(2))
if func == nil then
print(Arg, ": ", err)
return false
end
local e = getfenv(func)
for _,v in ipairs(State) do
e.f = v
v[Name] = func()
end
return true
end
local hFile = nil
if Arg:sub(1,1) == "@" then
hFile = io.popen(Arg:sub(2), "r")
else
hFile = io.open(Arg, "r")
end
local i = 1;
for l in hFile:lines() do
if State[i] == nil then
State[i] = {}
end
State[i][Name] = l
i = i +1
end
hFile:close()
return true
end
local function buildLookup(State, SplitName, LookupName)
local count = 0
for _,v in ipairs(State) do
local res = {}
for i = #v[SplitName],1,-1 do
for _,p in ipairs(replacementPatterns) do
res[#res+1] = p:format(table.concat(v[SplitName], "/", i))
count = count + 1
end
end
v[LookupName] = res
end
return count
end
local function readFile(Path)
local f = io.open(Path, "r")
local s = f:read("*a")
f:close()
return s
end
local function writeFile(Path, Content)
local f = io.open(Path, "w")
f:write(Content)
f:close()
end
local function replaceStr(str, Look, Replacement)
local c = 0
local res = ""
local i = 1
local s,e = str:find(Look, i, true)
while s do
res = res .. str:sub(i,s-1) .. Replacement
i = e + 1
s,e = str:find(Look, i, true)
c = c + 1
end
res = res .. str:sub(i)
return res, c
end
local function processContent(Content, fileRename)
local updates = 0
for _,v in ipairs(fileRename) do
for i = 1,#v.PathLookup,1 do
local count
Content, count = replaceStr(Content, v.PathLookup[i], v.NewPathLookup[i])
updates = updates + count
end
end
print("", updates, "Updates\n")
return Content
end
local function processFiles(fileEdit, fileRename)
local i = 1
for _,v in ipairs(fileEdit) do
print(("Editing file %4i/%i"):format(i, #fileEdit), v["EditPath"])
local content = readFile(v["EditPath"])
local newcontent = processContent(content, fileRename)
if content ~= newcontent then
writeFile(v["EditPath"], newcontent)
end
i = i + 1
end
end
local function renameFiles(State, Command)
Command = Command or 'mv "%s" "%s"'
local i = 1
for _,v in ipairs(State) do
local cmd = Command:format(v.Path, v.NewPath)
print(("Renaming file %4i/%i"):format(i, #State), cmd)
i = i + 1
os.execute(cmd)
end
end
local function main(Args)
if #Args < 3 then
print[[
rename.lua <Path> <NewPath> <EditPath> [<Command>]
<Path> list of files/directories which will be renamed during the process
<NewPath> equivalent list of paths with new names for the files/directories
<EditPath> list of files which will be updated to incorporate the new filenames
<Command> optional parameter to specify the shellcommand used to rename the files, defaults to "mv"
path parameters:
'pathlist.txt' use the content of the file as paths
'@find . -path "*.cpp"' use the output of any command as paths
'!return f.path:lower()' use a lua script to build the new filenames from f.Path and f.PathSplit respectively
]]
return -1
end
local fileRename = {}
local fileEdit = {}
handleArg(Args[1], fileRename, "Path")
splitPaths(fileRename, "Path", "PathSplit")
handleArg(Args[2], fileRename, "NewPath")
splitPaths(fileRename, "NewPath", "NewPathSplit")
handleArg(Args[3], fileEdit, "EditPath")
table.sort(fileRename, function (a,b) return #a.PathSplit < #b.PathSplit or (#a.PathSplit == #b.PathSplit and a.Path < b.Path) end)
table.sort(fileEdit, function (a,b) return a.EditPath < b.EditPath end)
local p = buildLookup(fileRename, "PathSplit", "PathLookup")
buildLookup(fileRename, "NewPathSplit", "NewPathLookup")
print(#fileRename, "files to rename with", p, "patterns to look for.")
processFiles(fileEdit, fileRename)
renameFiles(fileRename, Args[4])
end
main(arg)