awesomewm.d.tl/src/awesomewm-d-tl/init.tl

164 lines
5.6 KiB
Plaintext

if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
require("lldebugger").start()
end
local ast <const> = require("awesomewm-d-tl.ast")
local crawler <const> = require("awesomewm-d-tl.crawler")
local dag <const> = require("awesomewm-d-tl.dag")
local filesystem <const> = require("awesomewm-d-tl.filesystem")
local printer <const> = require("awesomewm-d-tl.printer")
local List <const> = require("pl.List")
local logger <const> = require("awesomewm-d-tl.logger")
local Map <const> = require("pl.Map")
local Module_Info <const> = require("awesomewm-d-tl.entity.Module_Info")
local module_dependencies <const> = require("awesomewm-d-tl.visitors.module_dependencies")
local type Node = require("awesomewm-d-tl.types.Node")
local property <const> = require("awesomewm-d-tl.property")
local scraper <const> = require("awesomewm-d-tl.scraper")
local stringx <const> = require("pl.stringx")
local type_mapping <const> = require("awesomewm-d-tl.visitors.type_mapping")
local utils <const> = require("awesomewm-d-tl.utils")
local log = logger.log("main")
log:info(logger.message_with_metadata("Start", { property = property }))
local function module_lists(
doc_index_url: string,
global_modules: List<string>,
ignored_modules: List<string>
): List<Module_Info.Module_Info>, List<Module_Info.Module_Info>, List<Module_Info.Module_Info>
local index = crawler.fetch(doc_index_url)
local all_module_infos = List(scraper.module_info_list.get_modules_from_index(index))
local global_module_infos: List<Module_Info.Module_Info> = List()
local module_infos: List<Module_Info.Module_Info> = List()
for module in all_module_infos:iter() do
if global_modules:contains(module.name) then
global_module_infos:append(module)
elseif not ignored_modules:contains(module.name) then
module_infos:append(module)
end
end
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 printer.
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
--- TODO : rewrite this to use the DAG
local function do_module_init_definition(
module_infos: List<Module_Info.Module_Info>
)
local tree = modules_tree(module_infos)
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(
printer.module_init_definition.generate_teal(requires),
property.out_directory .. "/" .. stringx.split(module, "."):slice(1, -1):join("/") .. "/init.d.tl"
)
end
end
--- TODO : rewrite the module_info thingy
local all_module_infos, module_infos, global_module_infos = module_lists(
property.base_url .. property.index_uri,
List(),-- List(property.capi_modules),
List(utils.spread(
property.ignored_modules,
{
-- Modules that broke the parser.
--- TODO : fix the parser
"awful.screenshot",
}))
)
log:info(
logger.message_with_metadata(
"Finished Module List scrapping",
{
total_module_count = #all_module_infos,
module_count = #module_infos,
global_module_count = #global_module_infos,
}
)
)
-- Build the DAG
local module_dag = dag.new()
for module in module_infos:iter() do
local module_ast, other_nodes = scraper.module_doc.get_doc_from_page(
crawler.fetch(property.base_url .. "/" .. module.uri),
(module.name:gsub(".*%sand%s", ""))
)
dag.push_module(module_dag, module_ast.module_path, module_ast)
dag.push_global_nodes(module_dag, other_nodes)
end
-- Run the visitors
for _,root in dag.iter_modules(module_dag) do
ast.in_order_visitor(root, function(node: Node)
type_mapping.visit(node)
end)
ast.in_order_visitor(root, function(node: Node)
module_dependencies.visit(node, root, module_dag)
end)
end
-- Build the global module from dag.global_nodes
--- TODO : todo
--- TODO : this is fun, but we need to do something with it
-- Write the DAG to a file
-- local inspect = require("inspect")
-- filesystem.file_writer.write(
-- inspect(module_dag, { newline = "\n", indent = " ", depth = 2 }),
-- "generated_dag.lua"
-- )
log:info("Preprocessing finished")
-- Write modules types definitions to files
for module_path, root in dag.iter_modules(module_dag) do
filesystem.file_writer.write(
printer.teal_type_definition.printer(root),
property.out_directory .. "/" .. module_path:gsub("%.", "/") .. ".d.tl"
)
end
do_module_init_definition(module_infos)
log:info("Module init files generated")