-
Notifications
You must be signed in to change notification settings - Fork 91
/
localcheck.lua
executable file
·246 lines (209 loc) · 6.96 KB
/
localcheck.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
-- If you want to hardcode toc or slug in and not enter it, do so here
local ADDON_SLUG
local TOC_FILE
-- Automatically identify the slug based on the .git or .svn configuration data, change the path if it's not in root, or set to false to disable
local AUTO_IDENTIFY_SLUG = "./"
-- Automatically find the TOC in the given path, set to false to disable
local AUTO_FIND_TOC = "./"
-- Only necessary if you aren't auto identify the slug, curseforge or wowace.
local SITE_LOCATION = nil
-- Personally I keep the api key in another file and just have this reference that to get it
-- If you want to do this, create the file with CURSE_API_KEY = "<key>" in it and set the path here
-- set this to nil and it will ask you for your API key
local API_KEY_FILE = "../TestCode/api-key.lua"
-- Patterns that should not be scrapped, case-insensitive
-- Anything between the no-lib-strip is automatically ignored
local FILE_BLACKLIST = {"^localization", "^lib"}
-- 1 = English
DEFAULT_LANGUAGE = "1"
-- Removes phrases that are not found through this
DELETE_UNIMPORTED = true
-- No more modifying!
local OS_TYPE = os.getenv("HOME") and "linux" or "windows"
-- Mak sure we have LuaSockets
local _, http = pcall(require, "socket.http")
local _, ltn = pcall(require, "ltn12")
if( not http ) then
print("Failed to find socket.http, did you install LuaSockets?")
elseif( not ltn ) then
print("Failed to find ltn12, did you install LuaSockets?")
end
-- Figure out the API key
if( API_KEY_FILE ) then
local file = io.open(API_KEY_FILE)
if( not file ) then
print(string.format("It appears the API key file %s does not exist.", API_KEY_FILE))
else
file:close()
dofile(API_KEY_FILE)
if( not CURSE_API_KEY ) then
print("You did not define CURSE_API_KEY in your key file, make sure it does not have local next to it.")
end
end
end
if( not CURSE_API_KEY ) then
while( not CURSE_API_KEY ) do
io.stdout:write("Enter API key: ")
CURSE_API_KEY = io.stdin:read("*line")
CURSE_API_KEY = CURSE_API_KEY ~= "" and CURSE_API_KEY or nil
end
end
-- Attempt to automatically identify the addon slug
if( AUTO_IDENTIFY_SLUG ) then
local git = io.open(AUTO_IDENTIFY_SLUG .. ".git/config")
local svn = io.open(AUTO_IDENTIFY_SLUG .. ".svn/entries")
if( git ) then
local contents = git:read("*all")
git:close()
SITE_LOCATION, ADDON_SLUG = string.match(contents, "git%.(.-)%.com:wow/(.-)/mainline%.git")
elseif( svn ) then
local contents = svn:read("*all")
svn:close()
SITE_LOCATION, ADDON_SLUG = string.match(contents, "svn%.(.-)%.com/wow/(.-)/mainline")
end
if( not ADDON_SLUG ) then
print("Failed to identify addon slug.")
elseif( not SITE_LOCATION ) then
print("Failed to identify site location.")
end
end
if( not SITE_LOCATION ) then
while( not SITE_LOCATION ) do
io.stdout:write("Site location [wowace/curseforge]: ")
SITE_LOCATION = io.stdin:read("*line")
SITE_LOCATION = ( SITE_LOCATION == "wowace" or SITE_LOCATION == "curseforge" ) and SITE_LOCATION or nil
end
end
-- Manually ask for it
if( not ADDON_SLUG ) then
while( not ADDON_SLUG ) do
io.stdout:write("Enter slug: ")
ADDON_SLUG = io.stdin:read("*line")
ADDON_SLUG = ADDON_SLUG ~= "" and ADDON_SLUG or nil
end
end
print(string.format("Using addon slug: %s", ADDON_SLUG))
-- Find the TOC now
if( AUTO_FIND_TOC ) then
local pipe = OS_TYPE == "windows" and io.popen(string.format("dir /B \"%s\"", AUTO_FIND_TOC)) or io.popen(string.format("ls -1 \"%s\"", AUTO_FIND_TOC))
if( type(pipe) == "userdata" ) then
for file in pipe:lines() do
if( string.match(file, "(.+)%.toc") ) then
TOC_FILE = file
break
end
end
pipe:close()
if( not TOC_FILE ) then print("Failed to auto detect toc file.") end
else
print("Failed to auto find toc, cannot run dir /B or ls -1")
end
end
if( not TOC_FILE ) then
while( not TOC_FILE ) do
io.stdout:write("TOC path: ")
TOC_FILE = io.stdin:read("*line")
TOC_FILE = TOC_FILE ~= "" and TOC_FILE or nil
if( TOC_FILE ) then
local file = io.open(TOC_FILE)
if( file ) then
file:close()
break
else
print(string.format("%s does not exist.", TOC_FILE))
end
end
end
end
print(string.format("Using TOC file %s", TOC_FILE))
print("")
-- Parse through the TOC file so we know what to scan
local ignore
local localizedKeys = {}
for line in io.lines(TOC_FILE) do
line = string.gsub(line, "\r", "")
if( string.match(line, "#@no%-lib%-strip@") ) then
ignore = true
elseif( string.match(line, "#@end%-no%-lib%-strip@") ) then
ignore = nil
end
if( not ignore and string.match(line, "%.lua") ) then
-- Make sure it's a valid file
local blacklist
for _, check in pairs(FILE_BLACKLIST) do
if( string.match(string.lower(line), check) ) then
blacklist = true
break
end
end
-- File checks out, scrap everything
if( not blacklist ) then
-- Fix slashes
if( OS_TYPE == "linux" ) then
line = string.gsub(line, "\\", "/")
end
local keys = 0
local contents = io.open(line):read("*all")
for match in string.gmatch(contents, "L%[\"(.-)%\"]") do
if( not localizedKeys[match] ) then keys = keys + 1 end
localizedKeys[match] = true
end
print(string.format("%s (%d keys)", line, keys))
end
end
end
-- Compile all of the localization we found into string form
local totalLocalizedKeys = 0
local localization = ""
for key in pairs(localizedKeys) do
localization = string.format("%s\nL[\"%s\"] = true", localization, key, key)
totalLocalizedKeys = totalLocalizedKeys + 1
end
if( totalLocalizedKeys == 0 ) then
print("Warning, failed to find any localizations, perhaps you messed up a configuration variable?")
return
end
print(string.format("Found %d keys total", totalLocalizedKeys))
local addonData = {
["format"] = "lua_additive_table",
["language"] = DEFAULT_LANGUAGE,
["delete_unimported"] = DELETE_UNIMPORTED and "y" or "n",
["text"] = localization,
}
-- Send it off
local boundary = string.format("-------%s", os.time())
local source = {}
local body = ""
for key, data in pairs(addonData) do
body = string.format("%s--%s\r\n", body, boundary)
body = string.format("%sContent-Disposition: form-data; name=\"%s\"\r\n\r\n", body, key)
body = string.format("%s%s\r\n", body, data)
end
body = string.format("%s--%s\r\n", body, boundary)
http.request({
method = "POST",
url = string.format("http://www.%s.com/addons/%s/localization/import/?api-key=%s", SITE_LOCATION, ADDON_SLUG, CURSE_API_KEY),
sink = ltn12.sink.table(source),
source = ltn12.source.string(body),
headers = {
["Content-Type"] = string.format("multipart/form-data; boundary=\"%s\"", boundary),
["Content-Length"] = string.len(body),
},
})
local contents = table.concat(source, "\n")
local errors = {}
for line in string.gmatch(contents, "(.-)\n") do
local msg = string.match(line, "<span class=\"error\">(.-)</p>")
if( msg ) then
table.insert(errors, msg)
end
end
print("")
if( #(errors) == 0 ) then
print(string.format("Updated localization for %s on %s!", ADDON_SLUG, SITE_LOCATION))
else
print("Localization failed:")
for _, line in pairs(errors) do
print(line)
end
end