feat: implement `global_env_def`
ci/woodpecker/pr/build Pipeline failed Details
ci/woodpecker/pr/docker-build Pipeline was successful Details
ci/woodpecker/pr/lint Pipeline failed Details

This commit is contained in:
Aire-One 2022-12-28 18:54:17 +01:00
parent b8cd7e3db8
commit a2a063882a
5 changed files with 85 additions and 1 deletions

View File

@ -0,0 +1,55 @@
local List = require "pl.List"
local Module_Doc = require "entity.Module_Doc"
local template = require "pl.template"
local utils = require "utils"
local snippets = require "generator.snippets"
-- The long therm goal is to have so many `snippets.render_*` functions that
-- we can render the whole file with the smallest template possible.
local tmpl = [[
-- Auto generated file (Do not manually edit this file!)
# for module in modules:iter() do
global record $(module.record_name)
# if #module.signals ~= 0 then
$(snippets.indent(snippets.render_enum("Signal", module.signals)))
# end -- /signals
# if #module.methods ~= 0 then
-- Object methods
$(snippets.indent(snippets.render_record_functions(module.methods)))
# end -- /methods
# if #module.properties ~= 0 then
-- Object properties
$(snippets.indent(snippets.render_record_properties(module.properties)))
# end -- /properties
# if #module.constructors ~= 0 then
-- Constructors
$(snippets.indent(snippets.render_record_functions(module.constructors)))
# end -- /constructors
# if #module.static_functions ~= 0 then
-- Static functions
$(snippets.indent(snippets.render_record_functions(module.static_functions)))
# end -- /static_functions
end
# end -- /modules
]]
local module = {}
function module.generate_teal(data: List<Module_Doc.Module_Doc>): string
local tmpl_args = {
ipairs = ipairs,
modules = data,
snippets = snippets,
}
return utils.do_or_fail(template.substitute, tmpl, tmpl_args)
end
return module

View File

@ -1,4 +1,5 @@
return { return {
global_env_def = require "generator.global_env_def",
teal_type_definitions = require "generator.teal_type_definitions", teal_type_definitions = require "generator.teal_type_definitions",
snippets = require "generator.snippets", snippets = require "generator.snippets",
} }

View File

@ -1,11 +1,13 @@
local crawler = require "crawler" local crawler = require "crawler"
local filesystem = require "filesystem" local filesystem = require "filesystem"
local generator = require "generator"
local List = require "pl.List" local List = require "pl.List"
local logger = require "logger" local logger = require "logger"
local Module_Doc = require "entity.Module_Doc"
local Module_Info = require "entity.Module_Info" local Module_Info = require "entity.Module_Info"
local property = require "property" local property = require "property"
local scraper = require "scraper" local scraper = require "scraper"
local generator = require "generator" local utils = require "utils"
local log = logger.log("main") local log = logger.log("main")
@ -72,3 +74,24 @@ for module in module_infos:iter() do
property.out_directory .. "/" .. module.name:gsub("%.", "/") .. ".d.tl" property.out_directory .. "/" .. module.name:gsub("%.", "/") .. ".d.tl"
) )
end end
local global_env_def: List<Module_Doc.Module_Doc> = List()
for module in global_module_infos:iter() do
if module.name:gmatch(".*%sand%s.*") then
do_one_file(
property.base_url .. "/" .. module.uri,
module.name,
property.out_directory .. "/" .. module.name:gsub(".*%sand%s", ""):gsub("%.", "/") .. ".d.tl"
)
end
local html = crawler.fetch(property.base_url .. "/" .. module.uri)
local module_doc = scraper.module_doc.get_doc_from_page(html, (module.name:gsub("%sand%s.*", "")))
module_doc:fixup()
module_doc.record_name = utils.lowercase(module_doc.record_name)
global_env_def:append(module_doc)
end
filesystem.file_writer.write(
generator.global_env_def.generate_teal(global_env_def),
property.out_directory .. "/global_env.d.tl"
)

View File

@ -43,6 +43,10 @@ function utils.capitalize(s: string): string
return (s:gsub("^%l", string.upper)) return (s:gsub("^%l", string.upper))
end end
function utils.lowercase(s: string): string
return (s:gsub("^%L", string.lower))
end
function utils.is_empty(s: string): boolean function utils.is_empty(s: string): boolean
return s == nil or s == "" return s == nil or s == ""
end end

View File

@ -6,4 +6,5 @@ return {
"types", "types",
"generated", -- used to remove require error when visualizing the generated type definitions "generated", -- used to remove require error when visualizing the generated type definitions
}, },
global_env_def = "generated.global_env", -- used to remove require error when visualizing the generated type definitions
} }