diff --git a/src/awesomewm.d.tl/generator/init.tl b/src/awesomewm.d.tl/generator/init.tl index 5349d74..586a64b 100644 --- a/src/awesomewm.d.tl/generator/init.tl +++ b/src/awesomewm.d.tl/generator/init.tl @@ -1,5 +1,6 @@ return { 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", + teal_type_definitions = require "generator.teal_type_definitions", } diff --git a/src/awesomewm.d.tl/generator/module_init_definition.tl b/src/awesomewm.d.tl/generator/module_init_definition.tl new file mode 100644 index 0000000..20d8ed4 --- /dev/null +++ b/src/awesomewm.d.tl/generator/module_init_definition.tl @@ -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 + local tmpl_args = { + ipairs = ipairs, + requires = requires, + snippets = snippets, + } + return utils.do_or_fail(template.substitute, tmpl, tmpl_args) + +end + +return module + diff --git a/src/awesomewm.d.tl/init.tl b/src/awesomewm.d.tl/init.tl index 9c80692..b508aab 100644 --- a/src/awesomewm.d.tl/init.tl +++ b/src/awesomewm.d.tl/init.tl @@ -3,10 +3,12 @@ local filesystem = require "filesystem" local generator = require "generator" local List = require "pl.List" local logger = require "logger" +local Map = require "pl.Map" local Module_Doc = require "entity.Module_Doc" local Module_Info = require "entity.Module_Info" local property = require "property" local scraper = require "scraper" +local stringx = require "pl.stringx" local utils = require "utils" local log = logger.log("main") @@ -35,6 +37,34 @@ local function module_lists( return all_module_infos, module_infos, global_module_infos 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): Map> + local tree: Map> = 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( url: 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.ignored_modules) ) +local tree = modules_tree(module_infos) log:info( logger.message_with_metadata( @@ -62,7 +93,8 @@ log:info( { total_module_count = #all_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), 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 = 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