Skip to content

Commit 292b685

Browse files
committed
feat: type sigs
1 parent e555e41 commit 292b685

14 files changed

Lines changed: 145 additions & 131 deletions

File tree

.config/docs.luau

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ return {
55
-- paths are assumed to start from docs
66
documentationPaths = {
77
mechanics = "reference/mechanics",
8-
libraries = "reference/libraries",
8+
api = "reference/api",
99
},
1010

1111
header = "<!-- This file was automatically @generated and should not be modified manually. -->",

.lute/refgen/filepaths.luau

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ local docs = require("@config/docs")
22
local path = require("@std/path")
33

44
return {
5-
libraries = path.join(docs.directory, docs.documentationPaths.libraries),
5+
api = path.join(docs.directory, docs.documentationPaths.api),
66
mechanics = path.join(docs.directory, docs.documentationPaths.mechanics),
77
}

.lute/refgen/init.luau

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local pp = require("@batteries/pp")
21
local docs = require("@config/docs")
32
local project = require("@config/project")
43
local clearDirectory = require("@scripts/lib/clearDirectory")
@@ -20,35 +19,41 @@ local raw = process.run({
2019

2120
local model = moonwaveModel(json.deserialize(raw) :: moonwave.Extracted)
2221

23-
clearDirectory(filepaths.libraries)
22+
clearDirectory(filepaths.api)
2423

2524
local libraryNav: { { [string]: { string } } } = {}
2625

27-
for _, library in model.libraries do
28-
local libraryContents = templates.Library:gsub("{(.-)}", library :: { [string]: string })
26+
local function linkNames(content: string, hyperlinks: { [string]: string })
27+
for name, url in hyperlinks do
28+
content = string.gsub(content, "%f[%w]" .. name .. "%f[%W]", url)
29+
end
30+
return content
31+
end
32+
33+
for _, class in model.classes do
34+
local contents = string.gsub(templates.Class, "{([^{}]-)}", class :: { [string]: string })
2935

30-
local hyperlink = model.nameToHyperlinks[library.name]
31-
model.nameToHyperlinks[library.name] = nil
32-
libraryContents = libraryContents:gsub("%w*", model.nameToHyperlinks)
33-
model.nameToHyperlinks[library.name] = hyperlink
36+
local nameToHyperlinks = table.clone(model.nameToHyperlinks)
37+
nameToHyperlinks[class.name] = nil
38+
contents = linkNames(contents, nameToHyperlinks)
3439

35-
local index = path.join(library.outputDirectory, "index.md")
36-
print("Writing library", library.name)
37-
fs.createdirectory(library.outputDirectory, { makeparents = true })
38-
fs.writestringtofile(index, libraryContents)
40+
local index = path.join(class.directory, "index.md")
41+
print("Writing api class", class.name)
42+
fs.createdirectory(class.directory, { makeparents = true })
43+
fs.writestringtofile(index, contents)
3944

40-
table.insert(libraryNav, { [library.name] = { path.format(path.join(library.outputUrl, "index.md")) } })
45+
table.insert(libraryNav, { [class.name] = { path.format(path.join(class.url, "index.md")) } })
4146
end
4247

4348
local zensical = fs.readfiletostring(docs.zensical)
4449
zensical = text.replaceSectionedBlock(
4550
zensical,
4651
-- json is close enough lmao
4752
-- + toml breaks anyway
48-
'{ "Libraries" = '
53+
'{ "API" = '
4954
.. json.serialize(libraryNav :: any):gsub(":", "=")
5055
.. " }",
51-
"# refgen: libraries start",
52-
"# refgen: libraries end"
56+
"# refgen: api start",
57+
"# refgen: api end"
5358
)
5459
fs.writestringtofile(docs.zensical, zensical)

.lute/refgen/moonwaveModel.luau

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
local docs = require("@config/docs")
22
local project = require("@config/project")
3+
local tables = require("@libs/collections/tables")
34
local text = require("@scripts/lib/text")
45
local reconstructRequirePath = require("./reconstructRequirePath")
56
local sourcemap = require("./sourcemap")
67
local path = require("@std/path")
8+
local tableext = require("@std/tableext")
79
local filepaths = require("./filepaths")
810
local 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

2638
export 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
}
5871
end
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+
60115
local 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
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
<!--
1+
return [[<!--
22
This file was automatically @generated using Welcome To Hell's `refgen`.
33
As changes may be overriden, this file should not be manually modified.
44
-->
55
6-
# Mechanic
6+
# {name}
77
88
```luau
9-
local Mechanic = require("@game/ReplicatedStorage/Libs/Mechanic")
9+
{name} = {
10+
{apiSignature}
11+
}
1012
```
1113
12-
14+
{description}
1315
1416
## API
1517
1618
{api}
1719
20+
]]

.lute/refgen/templates/init.luau

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
return {
22
ApiItem = require("@self/ApiItem"),
33
ApiList = require("@self/ApiLIst"),
4+
Class = require("@self/Class"),
45
Function = require("@self/Function"),
56
FunctionParameter = require("@self/FunctionParameter"),
67
FunctionReturn = require("@self/FunctionReturn"),
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
# Level
77

88
```luau
9-
local Level = require("@game/ReplicatedStorage/Libs/Level")
9+
Level = {
10+
-- No APIs implemented.
11+
}
1012
```
1113

1214
Orchestrator of scripts and mechanics in levels.
@@ -15,7 +17,7 @@ Levels can be traversed by the player with mechanics, level scripts, and
1517
some starting positions. Alongside towers, Levels are also used to represent
1618
Chapters in Welcome To Hell's Campaign.
1719

18-
This class should not be constructed by tower scripts. Many <a href="/reference/libraries/mechanic">Mechanic</a>
20+
This class should not be constructed by tower scripts. Many Mechanic
1921
methods provide a handle to a tower's Level as the last argument.
2022

2123
## API
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!--
2+
This file was automatically @generated using Welcome To Hell's `refgen`.
3+
As changes may be overriden, this file should not be manually modified.
4+
-->
5+
6+
# ScriptCollector
7+
8+
```luau
9+
ScriptCollector = {
10+
new: () -> (),
11+
clone: (self: ScriptCollector) -> ScriptCollector,
12+
collectModuleScript: (self: ScriptCollector, instance: ModuleScript) -> (),
13+
collectInstance: (self: ScriptCollector, instance: Instance) -> (),
14+
collectInstanceAsync: (self: ScriptCollector, instance: Instance) -> Promise,
15+
collectInstancesAsync: (self: ScriptCollector, instances: { Instance }) -> Promise,
16+
}
17+
```
18+
19+
Collects mechanic implementations from module scripts for use in levels.
20+
21+
## API
22+
23+
{api}
24+

docs/reference/libraries/life/index.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

docs/reference/libraries/scriptcollector/index.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)