Skip to content

Commit

Permalink
feat: build class to generate json schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Sep 7, 2022
1 parent bc65c64 commit d9b25d6
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions lua/lsp-settings/build.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
local util = require("lsp-settings.util")

local M = {}

function M.get_schema(package_json)
local json = util.json_decode(util.fetch(package_json)) or {}
local config = json.contributes and json.contributes.configuration or json.properties and json

local properties = {}

if vim.tbl_islist(config) then
for _, c in pairs(config) do
vim.list_extend(properties, c.properties)
end
elseif config.properties then
properties = config.properties
end
return {
["$schema"] = "http://json-schema.org/draft-07/schema#",
description = json.description,
properties = properties,
}
end

function M.clean()
---@diagnostic disable-next-line: param-type-mismatch
for _, f in pairs(vim.fn.expand("schemas/*.json", false, true)) do
vim.loop.fs_unlink(f)
end
end

function M.update_index()
local url = "https://gist.githubusercontent.mirror.nvdadr.com/williamboman/a01c3ce1884d4b57cc93422e7eae7702/raw/lsp-packages.json"
local index = util.fetch(url)
util.write_file("schemas/index.json", index)
util.write_file("lua/lsp-settings/schemas.lua", "return " .. vim.inspect(util.json_decode(index)))
end

function M.index()
return util.json_decode(util.read_file("schemas/index.json"))
end

function M.update_schemas()
for name, url in pairs(M.index()) do
print(("Generating schema for %s"):format(name))
local schema_file = ("schemas/%s.json"):format(name)

if not (util.exists(schema_file) and os.time() - util.mtime(schema_file) < 3600) then
local schema = M.get_schema(url)
util.write_file(schema_file, vim.fn.json_encode(schema))
end
end
end

function M.build()
-- M.clean()
M.update_index()
M.update_schemas()
end

M.build()

return M

0 comments on commit d9b25d6

Please sign in to comment.