11local docs = require ("@config/docs" )
22local project = require ("@config/project" )
3+ local tables = require ("@libs/collections/tables" )
34local text = require ("@scripts/lib/text" )
45local reconstructRequirePath = require ("./reconstructRequirePath" )
56local sourcemap = require ("./sourcemap" )
67local path = require ("@std/path" )
8+ local tableext = require ("@std/tableext" )
79local filepaths = require ("./filepaths" )
810local moonwave = require ("./moonwave" )
911
10- export type Library = {
12+ export type Base = {
1113 name : string ,
1214 slug : string ,
1315 summary : string ,
@@ -17,50 +19,102 @@ export type Library = {
1719 sourceLine : number ,
1820 importPath : string ,
1921
20- outputDirectory : path .path ,
21- outputUrl : path .path ,
22+ url : path .path ,
2223}
2324
24- export type Mechanic = {}
25+ export type Class = Base & {
26+ directory : path .path ,
27+ methods : { Method },
28+ apiSignature : string ,
29+ }
30+
31+ export type Method = Base & {
32+ filepath : path .path ,
33+ parameters : { moonwave .FunctionParameter },
34+ returns : { moonwave .LuaType },
35+ signature : string ,
36+ }
2537
2638export type Model = {
27- libraries : { Library },
28- mechanics : { Mechanic },
39+ classes : { Class },
2940 sourcemapPaths : { [string ]: { string } },
3041 nameToUrls : { [string ]: string },
3142 nameToHyperlinks : { [string ]: string },
3243}
3344
34- local function modelLibrary (model : Model , library : moonwave .Class ): Library
35- local sourcePath = project .source .. "/" .. library .source .path
36- local slug = string.lower (library .name )
37- local url = path .join (docs .documentationPaths .libraries , slug )
38- model .nameToUrls [library .name ] = path .format (url )
39- model .nameToHyperlinks [library .name ] = `<a href={text .quote ("/" .. path .format (url ))}>{library .name }</a>`
45+ local function modelLink (model : Model , name : string , url : path .path )
46+ model .nameToUrls [name ] = path .format (url )
47+ model .nameToHyperlinks [name ] = `<a href={text .quote ("/" .. path .format (url ))}>{name }</a>`
48+ end
49+
50+ local function modelBase (model : Model , base : moonwave .Class | moonwave .Function )
51+ local sourcePath = project .source .. "/" .. base .source .path
52+ local slug = string.lower (base .name )
53+ local url = path .join (docs .documentationPaths .api , slug )
4054
4155 return {
42- name = library .name ,
56+ name = base .name ,
4357 slug = slug ,
44- summary = string.match (library .desc , "^[^\n ]+" ) or "" ,
45- description = library .desc ,
58+ summary = string.match (base .desc , "^[^\n ]+" ) or "" ,
59+ description = base .desc ,
4660
4761 sourcePath = sourcePath ,
48- sourceLine = library .source .line ,
62+ sourceLine = base .source .line ,
4963 importPath = table.concat (
5064 reconstructRequirePath (model .sourcemapPaths , sourcePath )
51- or error (`library { library .source .path } cannot be imported` ),
65+ or error (`{ base .source .path } cannot be imported; is it included under rojo? ` ),
5266 "/"
5367 ),
5468
55- outputDirectory = path .join (filepaths .libraries , slug ),
56- outputUrl = url ,
69+ url = url ,
5770 }
5871end
5972
73+ local function modelMethod (model : Model , class : Class , method : moonwave .Function )
74+ local base = modelBase (model , method ) :: Method
75+ base .filepath = path .join (class .directory , base .slug )
76+ base .url = path .join (class .url , base .slug )
77+ base .parameters = method .params
78+ base .returns = method .returns
79+
80+ local parameterSignature = tableext .map (base .parameters , function (p : moonwave .FunctionParameter )
81+ return `{p .name }: {p .lua_type }`
82+ end )
83+
84+ local returnTypes = tableext .map (base .returns , function (t : moonwave .LuaType )
85+ return t .lua_type
86+ end )
87+
88+ local returnBraces = if # returnTypes == 1 then "%s" else "(%s)"
89+ local returnSignature = string.format (returnBraces :: any , table.concat (returnTypes , ", " ))
90+
91+ base .signature = `({table.concat (parameterSignature , ", " )}) -> {returnSignature }`
92+
93+ return base
94+ end
95+
96+ local function modelClass (model : Model , class : moonwave .Class ): Class
97+ local base = modelBase (model , class ) :: Class
98+ modelLink (model , base .name , base .url )
99+ base .directory = path .join (filepaths .api , base .slug )
100+
101+ local apiSignature = {}
102+
103+ base .methods = tables .create (# class .functions )
104+ for index , method in class .functions do
105+ local methodModel = modelMethod (model , base , method )
106+ base .methods [index ] = methodModel
107+ table.insert (apiSignature , ` {methodModel .name }: {methodModel .signature },` )
108+ end
109+
110+ base .apiSignature = if # apiSignature > 1 then table.concat (apiSignature , "\n " ) else " -- No APIs implemented."
111+
112+ return base
113+ end
114+
60115local function moonwaveModel (classes : { moonwave .Class })
61116 local model : Model = {
62- libraries = {},
63- mechanics = {},
117+ classes = {},
64118 sourcemapPaths = sourcemap .process (),
65119 nameToUrls = {},
66120 nameToHyperlinks = {},
@@ -70,8 +124,8 @@ local function moonwaveModel(classes: { moonwave.Class })
70124 local tags : { string } = class .tags or {}
71125
72126 for _ , tag in tags do
73- if tag == "Library " then
74- table.insert (model .libraries , modelLibrary (model , class ))
127+ if tag == "Class " then
128+ table.insert (model .classes , modelClass (model , class ))
75129 break
76130 end
77131 end
0 commit comments