main.lua 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. assert(fn.arguments, string.format('Function %q with variants does not have arguments list', fn.key))
  92. for name, arg in pairs(fn.arguments) do
  93. arg.name = name
  94. end
  95. assert(fn.returns, string.format('Function %q with variants does not have returns list', fn.key))
  96. for name, ret in pairs(fn.returns) do
  97. ret.name = name
  98. end
  99. for _, variant in ipairs(fn.variants) do
  100. for i, name in ipairs(variant.arguments) do
  101. variant.arguments[i] = copy(fn.arguments[name])
  102. end
  103. for i, name in ipairs(variant.returns) do
  104. variant.returns[i] = copy(fn.returns[name])
  105. end
  106. end
  107. end
  108. for _, variant in ipairs(fn.variants) do
  109. local function processTable(t)
  110. if not t then return end
  111. for _, field in ipairs(t) do
  112. field.description = unwrap(field.description)
  113. processTable(field.table)
  114. end
  115. t.description = unwrap(t.description)
  116. end
  117. variant.description = unwrap(variant.description)
  118. for _, arg in ipairs(variant.arguments) do
  119. arg.description = unwrap(arg.description)
  120. processTable(arg.table)
  121. end
  122. for _, ret in ipairs(variant.returns) do
  123. ret.description = unwrap(ret.description)
  124. processTable(ret.table)
  125. end
  126. end
  127. fn.arguments = nil
  128. fn.returns = nil
  129. track(fn)
  130. return fn
  131. end
  132. local function processObject(path, parent)
  133. local object = require(path .. '.init')
  134. object.key = path:match('[^/]+$')
  135. object.name = object.key
  136. object.description = unwrap(object.description)
  137. object.summary = object.summary or object.description
  138. object.module = parent.key
  139. object.methods = {}
  140. object.constructors = pluralify(object, 'constructor')
  141. object.notes = unwrap(object.notes)
  142. object.examples = pluralify(object, 'example')
  143. if object.sections then
  144. for _, section in ipairs(object.sections) do
  145. section.description = unwrap(section.description)
  146. end
  147. end
  148. for k, example in ipairs(object.examples or {}) do
  149. object.examples[k] = processExample(example)
  150. end
  151. for _, file in ipairs(lovr.filesystem.getDirectoryItems(path)) do
  152. if file ~= 'init.lua' then
  153. table.insert(object.methods, processFunction(path .. '/' .. file:gsub('%..+$', ''), object))
  154. end
  155. end
  156. table.sort(object.methods, function(a, b) return a.key < b.key end)
  157. track(object)
  158. return object
  159. end
  160. local function processModule(path)
  161. local module = require(path .. '.init') -- So we avoid requiring the module itself
  162. module.key = module.external and path:match('[^/]+$') or path:gsub('/', '.')
  163. module.name = module.external and module.key or module.key:match('[^%.]+$')
  164. module.description = unwrap(module.description)
  165. module.functions = {}
  166. module.objects = {}
  167. module.enums = {}
  168. module.notes = unwrap(module.notes)
  169. if module.sections then
  170. for _, section in ipairs(module.sections) do
  171. section.description = unwrap(section.description)
  172. end
  173. end
  174. module.examples = pluralify(module, 'example')
  175. for k, example in ipairs(module.examples or {}) do
  176. module.examples[k] = processExample(example)
  177. end
  178. for _, file in ipairs(lovr.filesystem.getDirectoryItems(path)) do
  179. local childPath = path .. '/' .. file
  180. local childModule = childPath:gsub('%..+$', '')
  181. local isFile = lovr.filesystem.isFile(childPath)
  182. local capitalized = file:match('^[A-Z]')
  183. if file ~= 'init.lua' and not capitalized and isFile then
  184. table.insert(module.functions, processFunction(childModule, module))
  185. elseif capitalized and isFile then
  186. table.insert(module.enums, processEnum(childModule, module))
  187. elseif not isFile and (capitalized or objectSnowflakes[file]) then
  188. table.insert(module.objects, processObject(childModule, module))
  189. end
  190. end
  191. table.sort(module.functions, function(a, b) return a.key < b.key end)
  192. table.sort(module.objects, function(a, b) return a.key < b.key end)
  193. table.sort(module.enums, function(a, b) return a.key < b.key end)
  194. track(module)
  195. return module
  196. end
  197. -- Validation
  198. local function validateRelated(item)
  199. for _, key in ipairs(item.related or {}) do
  200. warnIf(not lookup[key], 'Related item for %s not found: %s', item.key, key)
  201. end
  202. end
  203. local function validateEnum(enum)
  204. validateRelated(enum)
  205. end
  206. local function validateObject(object)
  207. for _, constructor in ipairs(object.constructors or {}) do
  208. warnIf(not lookup[constructor], 'Constructor for %s not found: %s', object.key, constructor)
  209. end
  210. validateRelated(object)
  211. end
  212. local function validateFunction(fn)
  213. if fn.tag then
  214. local found = false
  215. for _, section in ipairs(lookup[fn.module].sections or {}) do
  216. if section.tag == fn.tag then found = true break end
  217. end
  218. warnIf(not found, 'Unknown tag %s for %s', fn.tag, fn.key)
  219. end
  220. for _, variant in ipairs(fn.variants) do
  221. for _, arg in ipairs(variant.arguments) do
  222. warnIf(not arg or not arg.name, 'Invalid argument for variant of %s', fn.key)
  223. end
  224. for _, ret in ipairs(variant.returns) do
  225. warnIf(not ret or not ret.name, 'Invalid return for variant of %s', fn.key)
  226. end
  227. end
  228. validateRelated(fn)
  229. end
  230. local function validateModule(module)
  231. for _, object in ipairs(module.objects) do
  232. validateObject(object)
  233. end
  234. for _, fn in ipairs(module.functions) do
  235. validateFunction(fn)
  236. end
  237. for _, fn in ipairs(module.enums) do
  238. validateEnum(fn)
  239. end
  240. end
  241. function lovr.load()
  242. local api = {
  243. modules = {},
  244. callbacks = {}
  245. }
  246. -- Modules
  247. table.insert(api.modules, processModule('lovr'))
  248. for _, file in ipairs(lovr.filesystem.getDirectoryItems('lovr')) do
  249. local path = 'lovr/' .. file
  250. if file ~= 'callbacks' and file:match('^[a-z]') and lovr.filesystem.isDirectory(path) then
  251. table.insert(api.modules, processModule(path))
  252. end
  253. end
  254. -- Callbacks
  255. local callbacks = 'lovr/callbacks'
  256. for _, file in ipairs(lovr.filesystem.getDirectoryItems(callbacks)) do
  257. table.insert(api.callbacks, processFunction(callbacks .. '/' .. file:gsub('%.lua', ''), api.modules[1]))
  258. end
  259. -- Validate
  260. for _, module in ipairs(api.modules) do
  261. validateModule(module)
  262. end
  263. -- Sort
  264. table.sort(api.modules, function(a, b) return a.key < b.key end)
  265. table.sort(api.callbacks, function(a, b) return a.key < b.key end)
  266. -- Serialize
  267. local file = io.open(lovr.filesystem.getSource() .. '/init.lua', 'w')
  268. assert(file, 'Could not open init.lua for writing')
  269. local keyPriority = {
  270. name = 1,
  271. tag = 2,
  272. summary = 3,
  273. type = 4,
  274. description = 5,
  275. key = 6,
  276. module = 7,
  277. arguments = 8,
  278. returns = 9
  279. }
  280. local function sort(keys, t)
  281. table.sort(keys, function(a, b) return (keyPriority[a] or 1000) < (keyPriority[b] or 1000) end)
  282. end
  283. local contents = 'return ' .. serpent.block(api, { comment = false, sortkeys = sort })
  284. file:write(contents)
  285. file:close()
  286. -- Bye
  287. lovr.event.quit()
  288. end