-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloveheaders.lua
178 lines (140 loc) · 4.89 KB
/
loveheaders.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
local lfs = require 'lfs'
getmetatable('').__mod = function (s, t)
return s:gsub('%$(%w+)', t)
end
local loader = {
appDir = lfs.currentdir(),
apiDirName = 'api',
headers = {},
headersIndex = {},
headersResolve = {},
headersDir = './headers/$version'
}
function loader:load()
local argp = require 'argparse'('loveheaders', 'build love headers file from love2d-community/love-api')
argp:argument('version', 'Build headers for version')
local args = argp:parse()
if args.version == nil then
error('version agrument is required')
end
local sourceDir = './'..self.apiDirName
os.execute('rm -rf '..sourceDir)
os.execute(
'git clone https://github.com/love2d-community/love-api --depth 1 -b $version ' % {version = args.version} ..
self.apiDirName
)
if not io.open(sourceDir) then
error("Can't load version #$version" % {version = args.version})
end
local api = require(self.apiDirName..'.love_api')
assert(api, 'LÖVE api not found.')
print 'Generating LOVE snippets ...'
local versionName = api.version
if api.version == nil then
versionName = args.version
end
loader:loadModule(api, nil, 0)
local apiDir = self.headersDir % {version = versionName}
if not io.open(apiDir) and not lfs.mkdir(apiDir) then
error("Can't create directory: $dir" % {dir = apiDir})
end
self:saveTo(apiDir..'/love.lua')
end
function loader:addHeader(module, header)
local moduleIdx
if self.headersResolve[module] == nil then
moduleIdx = #self.headers + 1
self.headersIndex[moduleIdx] = module
self.headersResolve[module] = moduleIdx
self.headers[moduleIdx] = {}
else
moduleIdx = self.headersResolve[module]
end
table.insert(self.headers[moduleIdx], header)
end
function loader:loadModule(ref, parentModule)
local moduleName = ref.name or 'love'
if parentModule then
moduleName = parentModule .. '.' .. moduleName
end
if ref.functions ~= nil then
self:loadFunctions(moduleName, ref.functions)
end
if ref.modules ~= nil then
for _, submoduleRef in ipairs(ref.modules) do
self:loadModule(submoduleRef, moduleName)
end
end
end
function loader:loadFunctions(module, functions)
for _, fn in ipairs(functions) do
self:loadFunction(module, fn)
end
end
function loader:loadFunction(module, fn)
local doc = {fn.description}
local args = {}
if fn.variants ~= nil then
for _, fnVariant in ipairs(fn.variants) do
for type, list in pairs(fnVariant) do
if type == 'description' then
table.insert(doc, list)
else
local docName
if type == 'arguments' then
docName = 'param'
elseif type == 'returns' then
docName = 'return'
end
for _, entry in ipairs(list) do
local default = ''
if entry.default ~= nil then
default = ' --[[='..entry.default..']]'
end
if type == 'arguments' then
local argName = entry.name
if argName == '...' then
argName = '___'
end
if argName:sub(1, 1) == '"' then
argName = 'str_' .. argName:gsub('"', '')
end
table.insert(args, '--[['..entry.type..']] ' .. argName .. default)
end
table.insert(doc, '@' .. docName .. ' ' .. entry.type .. ' ' .. entry.name .. ' ' .. entry.description)
end
end
end
end
end
self:addHeader(module, table.concat({
'--[[',
'$doc',
']]--',
'$name = function($args) end'
}, "\n") % {
name = fn.name,
module = module,
args = table.concat(args, ', '),
doc = ' '..table.concat(doc, "\n ")
})
end
function loader:saveTo(file)
local headers = ''
for moduleIdx, moduleHeaders in ipairs(self.headers) do
local module = self.headersIndex[moduleIdx]
headers = headers .. module .. " = {\n"
local headersLen = #moduleHeaders
for headerNum, header in ipairs(moduleHeaders) do
headers = headers .. "\n" .. header
if headerNum ~= headersLen then
headers = headers .. ','
end
headers = headers .. "\n"
end
headers = headers .. "}\n"
end
headers = headers .. "return love\n"
io.output(file):write(headers)
end
return setmetatable(loader, {__call = loader.load})