-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnippetsExport.lua
More file actions
103 lines (88 loc) · 2.67 KB
/
SnippetsExport.lua
File metadata and controls
103 lines (88 loc) · 2.67 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
98
99
100
101
102
103
require "lfs"
local function GetProjectPath()
local path = lfs.currentdir()
path = string.gsub(path, "\\", "/")
return path
end
local projectPath = GetProjectPath()
---需要导出的文件或路径
local filesPath = {
"/Examples"
}
local savePath = projectPath.."/lua.json"
local pattern = "function%s+(%w+)([%.:])(%w+)%("
local jsonTemplatePath = projectPath.."/snippetstemplate.json"
local function findindir (path, wefind, r_table, intofolder)
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
local f = path..'\\'..file
--print ("/t "..f)
if string.find(f, wefind) ~= nil then
--print("/t "..f)
table.insert(r_table, f)
end
local attr = lfs.attributes (f)
assert (type(attr) == "table")
if attr.mode == "directory" and intofolder then
findindir (f, wefind, r_table, intofolder)
else
--for name, value in pairs(attr) do
-- print (name, value)
--end
end
end
end
end
local function GetFileName(filepath, stripextension)
stripextension = stripextension or false
local filename = string.match(filepath, ".+/([^/]*%.%w+)$")
if stripextension == true then
local idx = filename:match(".+()%.%w+$")
if idx then
return filename:sub(1, idx-1)
end
end
return filename
end
local function Export(path)
local filename = GetFileName(path, true)
local f = io.open(path, "r")
local result = ""
for line in f:lines() do
for i, j, k in string.gmatch(line, pattern) do
local funcname = string.format("%s%s%s", filename, j, k)
result = result..string.format("\t%q:{\n\t\t\"body\": %q,\n\t\t\"description\": %q,\n\t\t\"prefix\": %q\n\t},\n", funcname, funcname.."()", funcname, funcname)
end
end
f:close()
return result
end
local function ExportAll()
local json = ""
for i, filepath in ipairs(filesPath) do
local path = string.format("%s%s", projectPath, filepath)
local allfile = {}
findindir(path, "%.lua$", allfile, true)
if #allfile > 0 then
for j, p in pairs(allfile) do
p = string.gsub(p, "\\", "/" )
json = json..Export(p)
end
else
json = json..Export(path)
end
end
print(json)
local f = io.open(jsonTemplatePath, "r")
local template = ""
for line in f:lines() do
template = template.."\n"..line
end
f:close()
template = string.gsub(template, "//replace with my snippets", json)
local savefile = io.open(savePath, "w")
savefile:write(template)
savefile:close()
print(string.format("save done ===> (%s).", savePath))
end
ExportAll()