main.lua 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. local serpent = require 'serpent'
  2. -- Helpers
  3. local function copy(t)
  4. if type(t) ~= 'table' then return t end
  5. local result = {}
  6. for k, v in pairs(t) do
  7. result[k] = copy(v)
  8. end
  9. return result
  10. end
  11. local function unindent(code)
  12. local indent = code:match('^(% +)')
  13. if indent then
  14. return code:gsub('\n' .. indent, '\n'):gsub('^' .. indent, ''):gsub('%s*$', '')
  15. else
  16. return code
  17. end
  18. end
  19. local function unwrap(str)
  20. if not str then return str end
  21. str = unindent(str)
  22. return str:gsub('([^\n])\n(%S)', function(a, b)
  23. if b == '-' or b == '>' then
  24. return a .. '\n' .. b
  25. else
  26. return a .. ' ' .. b
  27. end
  28. end)
  29. :gsub('^%s+', '')
  30. :gsub('%s+$', '')
  31. end
  32. local function pluralify(t, key)
  33. t[key .. 's'] = t[key .. 's'] or (t[key] and { t[key] } or nil)
  34. t[key] = nil
  35. return t[key .. 's']
  36. end
  37. local lookup = {}
  38. local function track(obj)
  39. lookup[obj.key] = obj
  40. end
  41. local function warnIf(cond, s, ...)
  42. if cond then print(string.format(s, ...)) end
  43. end
  44. -- Objects that are lowercase need to be special-cased
  45. local objectSnowflakes = { vec3 = true, quat = true, mat4 = true }
  46. -- Processors
  47. local function processExample(example)
  48. if type(example) == 'string' then
  49. return {
  50. code = unindent(example)
  51. }
  52. else
  53. example.description = unwrap(example.description)
  54. example.code = unindent(example.code)
  55. end
  56. return example
  57. end
  58. local function processEnum(path, parent)
  59. local enum = require(path)
  60. enum.name = path:match('[^/]+$')
  61. enum.key = enum.name
  62. enum.module = parent.key
  63. enum.description = unwrap(enum.description)
  64. enum.notes = unwrap(enum.notes)
  65. for _, value in ipairs(enum.values) do
  66. value.description = unwrap(value.description)
  67. end
  68. track(enum)
  69. return enum
  70. end
  71. local function processFunction(path, parent)
  72. local fn = require(path)
  73. local isMethod = parent.name:match('^[A-Z]') or objectSnowflakes[parent.key]
  74. fn.name = path:match('[^/]+$')
  75. fn.key = isMethod and (parent.key .. ':' .. fn.name) or (path:gsub('/', '.'):gsub('callbacks%.', ''))
  76. fn.description = unwrap(fn.description)
  77. fn.module = parent.module or parent.key
  78. fn.notes = unwrap(fn.notes)
  79. fn.examples = pluralify(fn, 'example')
  80. for k, example in ipairs(fn.examples or {}) do
  81. fn.examples[k] = processExample(example)
  82. end
  83. if not fn.variants then
  84. fn.variants = {
  85. {
  86. arguments = fn.arguments,
  87. returns = fn.returns
  88. }
  89. }
  90. else
  91. for name, arg in pairs(fn.arguments) do
  92. arg.name = name
  93. end
  94. for name, ret in pairs(fn.returns) do
  95. ret.name = name
  96. end
  97. for _, variant in ipairs(fn.variants) do
  98. for i, name in ipairs(variant.arguments) do
  99. variant.arguments[i] = copy(fn.arguments[name])
  100. end
  101. for i, name in ipairs(variant.returns) do
  102. variant.returns[i] = copy(fn.returns[name])
  103. end
  104. end
  105. end
  106. for _, variant in ipairs(fn.variants) do
  107. local function processTable(t)
  108. if not t then return end
  109. for _, field in ipairs(t) do
  110. field.description = unwrap(field.description)
  111. processTable(field.table)
  112. end
  113. t.description = unwrap(t.description)
  114. end
  115. variant.description = unwrap(variant.description)
  116. for _, arg in ipairs(variant.arguments) do
  117. arg.description = unwrap(arg.description)
  118. processTable(arg.table)
  119. end
  120. for _, ret in ipairs(variant.returns) do
  121. ret.description = unwrap(ret.description)
  122. processTable(ret.table)
  123. end
  124. end
  125. fn.arguments = nil
  126. fn.returns = nil
  127. track(fn)
  128. return fn
  129. end
  130. local function processObject(path, parent)
  131. local object = require(path .. '.init')
  132. object.key = path:match('[^/]+$')
  133. object.name = object.key
  134. object.description = unwrap(object.description)
  135. object.summary = object.summary or object.description
  136. object.module = parent.key
  137. object.methods = {}
  138. object.constructors = pluralify(object, 'constructor')
  139. object.notes = unwrap(object.notes)
  140. object.examples = pluralify(object, 'example')
  141. if object.sections then
  142. for _, section in ipairs(object.sections) do
  143. section.description = unwrap(section.description)
  144. end
  145. end
  146. for k, example in ipairs(object.examples or {}) do
  147. object.examples[k] = processExample(example)
  148. end
  149. for _, file in ipairs(lovr.filesystem.getDirectoryItems(path)) do
  150. if file ~= 'init.lua' then
  151. table.insert(object.methods, processFunction(path .. '/' .. file:gsub('%..+$', ''), object))
  152. end
  153. end
  154. table.sort(object.methods, function(a, b) return a.key < b.key end)
  155. track(object)
  156. return object
  157. end
  158. local function processModule(path)
  159. local module = require(path .. '.init') -- So we avoid requiring the module itself
  160. module.key = module.external and path:match('[^/]+$') or path:gsub('/', '.')
  161. module.name = module.external and module.key or module.key:match('[^%.]+$')
  162. module.description = unwrap(module.description)
  163. module.functions = {}
  164. module.objects = {}
  165. module.enums = {}
  166. module.notes = unwrap(module.notes)
  167. if module.sections then
  168. for _, section in ipairs(module.sections) do
  169. section.description = unwrap(section.description)
  170. end
  171. end
  172. module.examples = pluralify(module, 'example')
  173. for k, example in ipairs(module.examples or {}) do
  174. module.examples[k] = processExample(example)
  175. end
  176. for _, file in ipairs(lovr.filesystem.getDirectoryItems(path)) do
  177. local childPath = path .. '/' .. file
  178. local childModule = childPath:gsub('%..+$', '')
  179. local isFile = lovr.filesystem.isFile(childPath)
  180. local capitalized = file:match('^[A-Z]')
  181. if file ~= 'init.lua' and not capitalized and isFile then
  182. table.insert(module.functions, processFunction(childModule, module))
  183. elseif capitalized and isFile then
  184. table.insert(module.enums, processEnum(childModule, module))
  185. elseif not isFile and (capitalized or objectSnowflakes[file]) then
  186. table.insert(module.objects, processObject(childModule, module))
  187. end
  188. end
  189. table.sort(module.functions, function(a, b) return a.key < b.key end)
  190. table.sort(module.objects, function(a, b) return a.key < b.key end)
  191. table.sort(module.enums, function(a, b) return a.key < b.key end)
  192. track(module)
  193. return module
  194. end
  195. -- Validation
  196. local function validateRelated(item)
  197. for _, key in ipairs(item.related or {}) do
  198. warnIf(not lookup[key], 'Related item for %s not found: %s', item.key, key)
  199. end
  200. end
  201. local function validateEnum(enum)
  202. validateRelated(enum)
  203. end
  204. local function validateObject(object)
  205. for _, constructor in ipairs(object.constructors or {}) do
  206. warnIf(not lookup[constructor], 'Constructor for %s not found: %s', object.key, constructor)
  207. end
  208. validateRelated(object)
  209. end
  210. local function validateFunction(fn)
  211. if fn.tag then
  212. local found = false
  213. for _, section in ipairs(lookup[fn.module].sections or {}) do
  214. if section.tag == fn.tag then found = true break end
  215. end
  216. warnIf(not found, 'Unknown tag %s for %s', fn.tag, fn.key)
  217. end
  218. for _, variant in ipairs(fn.variants) do
  219. for _, arg in ipairs(variant.arguments) do
  220. warnIf(not arg or not arg.name, 'Invalid argument for variant of %s', fn.key)
  221. end
  222. for _, ret in ipairs(variant.returns) do
  223. warnIf(not ret or not ret.name, 'Invalid return for variant of %s', fn.key)
  224. end
  225. end
  226. validateRelated(fn)
  227. end
  228. local function validateModule(module)
  229. for _, object in ipairs(module.objects) do
  230. validateObject(object)
  231. end
  232. for _, fn in ipairs(module.functions) do
  233. validateFunction(fn)
  234. end
  235. for _, fn in ipairs(module.enums) do
  236. validateEnum(fn)
  237. end
  238. end
  239. function lovr.load()
  240. local api = {
  241. modules = {},
  242. callbacks = {}
  243. }
  244. -- Modules
  245. table.insert(api.modules, processModule('lovr'))
  246. for _, file in ipairs(lovr.filesystem.getDirectoryItems('lovr')) do
  247. local path = 'lovr/' .. file
  248. if file ~= 'callbacks' and file:match('^[a-z]') and lovr.filesystem.isDirectory(path) then
  249. table.insert(api.modules, processModule(path))
  250. end
  251. end
  252. -- Callbacks
  253. local callbacks = 'lovr/callbacks'
  254. for _, file in ipairs(lovr.filesystem.getDirectoryItems(callbacks)) do
  255. table.insert(api.callbacks, processFunction(callbacks .. '/' .. file:gsub('%.lua', ''), api.modules[1]))
  256. end
  257. -- Validate
  258. for _, module in ipairs(api.modules) do
  259. validateModule(module)
  260. end
  261. -- Sort
  262. table.sort(api.modules, function(a, b) return a.key < b.key end)
  263. table.sort(api.callbacks, function(a, b) return a.key < b.key end)
  264. -- Serialize
  265. local file = io.open(lovr.filesystem.getSource() .. '/init.lua', 'w')
  266. assert(file, 'Could not open init.lua for writing')
  267. local keyPriority = {
  268. name = 1,
  269. tag = 2,
  270. summary = 3,
  271. type = 4,
  272. description = 5,
  273. key = 6,
  274. module = 7,
  275. arguments = 8,
  276. returns = 9
  277. }
  278. local function sort(keys, t)
  279. table.sort(keys, function(a, b) return (keyPriority[a] or 1000) < (keyPriority[b] or 1000) end)
  280. end
  281. local contents = 'return ' .. serpent.block(api, { comment = false, sortkeys = sort })
  282. file:write(contents)
  283. file:close()
  284. -- Bye
  285. lovr.event.quit()
  286. end