feat(Module_Doc): manage requires
ci/woodpecker/pr/build Pipeline was successful Details
ci/woodpecker/pr/docker-build Pipeline was successful Details
ci/woodpecker/pr/lint Pipeline was successful Details

This commit is contained in:
Aire-One 2022-12-19 19:39:34 +01:00
parent 67ecf46058
commit dc8698270b
5 changed files with 93 additions and 2 deletions

View File

@ -1,7 +1,14 @@
local Function_Info = require "entity.Function_Info" local Function_Info = require "entity.Function_Info"
local List = require "pl.List" local List = require "pl.List"
local Map = require "pl.Map"
local Variable_Info = require "entity.Variable_Info" local Variable_Info = require "entity.Variable_Info"
local module_to_require <const> : Map<string, string> = Map({
Shape = "gears.shape",
Surface = "gears.surface",
Widget = "wibox.widget",
})
local record Module_Doc local record Module_Doc
metamethod __call: function(Module_Doc): Module_Doc metamethod __call: function(Module_Doc): Module_Doc
@ -16,7 +23,10 @@ local record Module_Doc
static_functions: List<Function_Info.Function_Info> static_functions: List<Function_Info.Function_Info>
signals: List<string> signals: List<string>
requires: Map<string, string>
fixup: function(Module_Doc) fixup: function(Module_Doc)
populate_requires: function(Module_Doc)
end end
local __Module_Doc: metatable<Module_Doc> = { local __Module_Doc: metatable<Module_Doc> = {
@ -27,6 +37,7 @@ local __Module_Doc: metatable<Module_Doc> = {
properties = List(), properties = List(),
static_functions = List(), static_functions = List(),
signals = List(), signals = List(),
requires = Map(),
fixup = function(self: Module_Doc) fixup = function(self: Module_Doc)
for c in self.constructors:iter() do for c in self.constructors:iter() do
@ -44,8 +55,60 @@ local __Module_Doc: metatable<Module_Doc> = {
for s in self.static_functions:iter() do for s in self.static_functions:iter() do
s:fixup() s:fixup()
end end
end, end,
populate_requires = function(self: Module_Doc)
-- TODO : Move this to other Entities. Can be a little tricky because we populate a map
for c in self.constructors:iter() do
for p in c.parameters:iter() do
for t in p.types:iter() do
local mod = module_to_require:get(t.name)
if mod then
self.requires:set(t.name, mod)
end
end
end
end
for m in self.methods:iter() do
for p in m.parameters:iter() do
for t in p.types:iter() do
local mod = module_to_require:get(t.name)
if mod then
self.requires:set(t.name, mod)
end
end
end
for t in m.return_types:iter() do
local mod = module_to_require:get(t.name)
if mod then
self.requires:set(t.name, mod)
end
end
end
for p in self.properties:iter() do
for t in p.types:iter() do
local mod = module_to_require:get(t.name)
if mod then
self.requires:set(t.name, mod)
end
end
end
for s in self.static_functions:iter() do
for p in s.parameters:iter() do
for t in p.types:iter() do
local mod = module_to_require:get(t.name)
if mod then
self.requires:set(t.name, mod)
end
end
end
for t in s.return_types:iter() do
local mod = module_to_require:get(t.name)
if mod then
self.requires:set(t.name, mod)
end
end
end
end
} }
end, end,
} }

View File

@ -1,13 +1,19 @@
local List = require "pl.List" local List = require "pl.List"
local Map = require "pl.Map" local Map = require "pl.Map"
local type_fix: Map<string, string> = Map({ local type_fix <const> : Map<string, string> = Map({
bool = "boolean", bool = "boolean",
client = "Client", client = "Client",
["gears.shape"] = "Shape",
["gears.surface"] = "Surface",
image = "Image", image = "Image",
int = "integer", int = "integer",
screen = "Screen", screen = "Screen",
shape = "Shape",
surface = "Surface",
tag = "Tag", tag = "Tag",
["wibox.widget"] = "Widget",
widget = "Widget",
}) })
local record Type_Info local record Type_Info

View File

@ -9,6 +9,7 @@ local Variable_Info = require "entity.Variable_Info"
local record Module local record Module
indent: function(str: string, level: number): string indent: function(str: string, level: number): string
render_requires: function(requires: Map<string, string>): string
render_typed_variable: function(name: string, types: List<Type_Info.Type_Info>): string render_typed_variable: function(name: string, types: List<Type_Info.Type_Info>): string
render_anonymous_function_signature: function(item: Function_Info.Function_Info): string render_anonymous_function_signature: function(item: Function_Info.Function_Info): string
render_record_functions: function(items: List<Function_Info.Function_Info>): string render_record_functions: function(items: List<Function_Info.Function_Info>): string
@ -26,6 +27,22 @@ function snippets.indent(str: string, level: number): string
return stringx.rstrip(stringx.indent(str, level, string.rep(" ", 3))) return stringx.rstrip(stringx.indent(str, level, string.rep(" ", 3)))
end end
function snippets.render_requires(requires: Map<string, string>): string
local tmpl = [[local $(name) = require "$(path)"]]
local require_statements <const> = List()
for name, path in requires:iter() do
local tmpl_args = {
name = name,
path = path,
}
require_statements:append(utils.do_or_fail(template.substitute, tmpl, tmpl_args))
end
return require_statements:concat("\n")
end
function snippets.render_typed_variable(name: string, types: List<Type_Info.Type_Info>): string function snippets.render_typed_variable(name: string, types: List<Type_Info.Type_Info>): string
local tmpl = local tmpl =
[[$(name): $(types)]] [[$(name): $(types)]]

View File

@ -8,6 +8,10 @@ local snippets = require "generator.snippets"
local tmpl = [[ local tmpl = [[
-- Auto generated file (Do not manually edit this file!) -- Auto generated file (Do not manually edit this file!)
# if module.requires:len() ~= 0 then
$(snippets.render_requires(module.requires))
# end -- /requires
local record $(module.record_name) local record $(module.record_name)
# if #module.signals ~= 0 then # if #module.signals ~= 0 then
$(snippets.indent(snippets.render_enum("Signal", module.signals))) $(snippets.indent(snippets.render_enum("Signal", module.signals)))

View File

@ -28,6 +28,7 @@ local function do_one_file(url: string, module_name: string, output: string)
local html = crawler.fetch(url) local html = crawler.fetch(url)
local module_doc = scraper.module_doc.get_doc_from_page(html, module_name) local module_doc = scraper.module_doc.get_doc_from_page(html, module_name)
module_doc:fixup() module_doc:fixup()
module_doc:populate_requires()
filesystem.file_writer.write( filesystem.file_writer.write(
generator.teal_type_definitions.generate_teal(module_doc), generator.teal_type_definitions.generate_teal(module_doc),
output output