Define modules init.d.tl file (#61) #69

Merged
Aire-One merged 4 commits from feat/#61 into master 2022-12-30 20:54:55 +01:00
3 changed files with 79 additions and 2 deletions

View File

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

View File

@ -0,0 +1,31 @@
local Map = require "pl.Map"
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!)
return {
# for name, path in requires:iter() do
$(name) = require "$(path)",
# end -- /for
}
]]
local module = {}
function module.generate_teal(requires: Map<string, string>): string
local tmpl_args = {
ipairs = ipairs,
requires = requires,
snippets = snippets,
}
return utils.do_or_fail(template.substitute, tmpl, tmpl_args)
end
return module

View File

@ -3,10 +3,12 @@ local filesystem = require "filesystem"
local generator = require "generator" local generator = require "generator"
local List = require "pl.List" local List = require "pl.List"
local logger = require "logger" local logger = require "logger"
local Map = require "pl.Map"
local Module_Doc = require "entity.Module_Doc" 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 stringx = require "pl.stringx"
local utils = require "utils" local utils = require "utils"
local log = logger.log("main") local log = logger.log("main")
@ -35,6 +37,34 @@ local function module_lists(
return all_module_infos, module_infos, global_module_infos return all_module_infos, module_infos, global_module_infos
end end
-- The module's children list produced can contain duplicates.
-- We ignore them for now because they are dismissed when building a Map for the generator.
local function modules_tree(modules: List<Module_Info.Module_Info>): Map<string, List<string>>
local tree: Map<string, List<string>> = Map()
for module in modules:iter() do
local parent = module.name:gmatch("(.*)%.(.*)$")()
if parent then
local ancestors = stringx.split(parent, ".")
for i = 1, #ancestors - 1 do
local ancestor = ancestors:slice(1, i):join(".")
if not tree:get(ancestor) then
tree:set(ancestor, List())
end
if not tree:get(ancestor):contains(parent) then
tree:get(ancestor):append(parent)
end
end
local parent_node = tree:get(parent)
if not parent_node then
tree:set(parent, List())
parent_node = tree:get(parent)
end
parent_node:append(module.name)
end
end
return tree
end
local function do_one_file( local function do_one_file(
url: string, url: string,
record_name: string, record_name: string,
@ -55,6 +85,7 @@ local all_module_infos, module_infos, global_module_infos = module_lists(
List(property.capi_modules), List(property.capi_modules),
List(property.ignored_modules) List(property.ignored_modules)
) )
local tree = modules_tree(module_infos)
log:info( log:info(
logger.message_with_metadata( logger.message_with_metadata(
@ -62,7 +93,8 @@ log:info(
{ {
total_module_count = #all_module_infos, total_module_count = #all_module_infos,
module_count = #module_infos, module_count = #module_infos,
global_module_count = #global_module_infos global_module_count = #global_module_infos,
tree_items = tree:len(),
} }
) )
) )
@ -95,3 +127,16 @@ filesystem.file_writer.write(
generator.global_env_def.generate_teal(global_env_def), generator.global_env_def.generate_teal(global_env_def),
property.out_directory .. "/global_env.d.tl" property.out_directory .. "/global_env.d.tl"
) )
for module, children in tree:iter() do
-- TODO : this map should be coupled with the all_module_infos list
local requires: Map<string, string> = Map()
for child in children:iter() do
local name = child:gmatch(".*%.(.*)$")()
requires:set(name, child)
end
filesystem.file_writer.write(
generator.module_init_definition.generate_teal(requires),
property.out_directory .. "/" .. stringx.split(module, "."):slice(1, -1):join("/") .. "/init.d.tl"
)
end