Skip to content

Commit

Permalink
Merge pull request #20 from A2va/update-apis
Browse files Browse the repository at this point in the history
Update apis task
  • Loading branch information
waruqi authored Nov 8, 2024
2 parents 0373303 + 8929dd9 commit 7b0cbc2
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 8 deletions.
8 changes: 7 additions & 1 deletion doc/en-us/api/description/builtins/is_config.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
${include api/script/builtins/is_config.md}
---
key: is_config
name: is_config
api: true
---

### is_config
8 changes: 1 addition & 7 deletions doc/en-us/api/script/builtins/is_config.md
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
---
key: is_config
name: is_config
api: true
---

### is_config
${include api/description/builtins/is_config.md}
150 changes: 150 additions & 0 deletions modules/update-apis.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import("plugins.show.lists.apis", {rootdir = os.programdir()})
import("core.base.hashset")

function _create_file(pth, api, opt)
opt = opt or {}
local s = opt.s or ([[---
key: API_NAME
name: API_NAME
api: true
---
### API_NAME]]):gsub("\n%s+", "\n"):gsub("API_NAME", api)

print("Creating markdown file for", api, "in", path.relative(pth, os.projectdir()))
io.writefile(p, s)
end

-- do the update in the api/description/scopes folder of one locale
function _update_description_scope_apis(opt)
local apis = apis.description_scope_apis()

local scopes_root = path.join(os.projectdir(), "doc", opt.locale, "api", "description", "scopes")
for _, api in ipairs(apis) do
local type, interface = table.unpack(api:split(".", {plain = true}))
local p = path.join(scopes_root, type, interface .. ".md")

if not os.exists(p) then
_create_file(p, api)
end
end
end

-- do the update in the api/description/builtins folder of one locale
function _update_description_builtin_apis(opt)
local description_builtin = apis.description_builtin_apis()
local script_builtin = hashset.from(apis.script_builtin_apis())

local builtins_root = path.join(os.projectdir(), "doc", opt.locale, "api", "description", "builtins")
for _, api in ipairs(description_builtin) do
-- conditions are located in different places
-- some in api/description/builtins
-- others in api/description/condition
-- so it's easier just to ignore them
if api:startswith("is_") then
goto continue
end

local p = path.join(builtins_root, api .. ".md")
if not os.exists(p) then
if script_builtin:has(api) then
local s = format("${include api/script/builtins/%s.md}", api)
_create_file(p, api, {s = s})
else
_create_file(p, api)
end
end
::continue::
end

end

-- do the update in the api/script/builtins_modules folder of one locale
function _update_description_builtin_modules_apis(opt)
local description_builtin = apis.description_builtin_module_apis()
local script_builtin = hashset.from(apis.script_builtin_module_apis())

local builtins_root = path.join(os.projectdir(), "doc", opt.locale, "api", "description", "builtin_modules")

for _, api in ipairs(description_builtin) do
local type, interface = table.unpack(api:split(".", {plain = true}))
local p = path.join(builtins_root, type, interface .. ".md")

if not os.exists(p) then
if script_builtin:has(api) then
local s = format("${include api/script/builtin_modules/%s.md}", api)
_create_file(p, api, {s = s})
else
_create_file(p, api)
end
end
end
end

-- do the update in the api/script/instances folder of one locale
function _update_script_instances_apis(opt)
local apis = apis.script_instance_apis()

local instances_root = path.join(os.projectdir(), "doc", opt.locale, "api", "script", "instances")
for _, api in ipairs(apis) do
local type, interface = table.unpack(api:split(":", {plain = true}))
local p = path.join(instances_root, type, interface .. ".md")

if not os.exists(p) then
_create_file(p, api)
end
end
end

-- do the update in the api/script/builtins folder of one locale
function _update_script_builtin_apis(opt)
local apis = apis.script_builtin_apis()

local builtins_root = path.join(os.projectdir(), "doc", opt.locale, "api", "script", "builtins")
for _, api in pairs(apis) do
-- conditions are located in different places
-- some in api/description/builtins
-- others in api/description/condition
-- so it's easier just to ignore them
if api:startswith("is_") then
goto continue
end

local p = path.join(builtins_root, api .. ".md")
if not os.exists(p) then
_create_file(p, api, opt)
end
::continue::
end
end

-- do the update in the api/script/builtin_modules folder of one locale
function _update_script_builtin_modules_apis(opt)
local apis = apis.script_builtin_module_apis()

local builtins_root = path.join(os.projectdir(), "doc", opt.locale, "api", "script", "builtin_modules")
for _, api in ipairs(apis) do
local type, interface = table.unpack(api:split(".", {plain = true}))
local p = path.join(builtins_root, type, interface .. ".md")

if not os.exists(p) then
_create_file(p, api)
end
end
end

function main()

for _, pagefile in ipairs(os.files(path.join(os.projectdir(), "doc", "*", "pages.lua"))) do
local locale = path.basename(path.directory(pagefile))

-- description
_update_description_scope_apis({locale = locale})
_update_description_builtin_apis({locale = locale})
_update_description_builtin_modules_apis({locale = locale})

-- script
_update_script_instances_apis({locale = locale})
_update_script_builtin_modules_apis({locale = locale})
end
end
8 changes: 8 additions & 0 deletions xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ task("opendoc")
}
}

task("update-apis")
on_run("update-apis")
set_menu {
usage = "xmake update-apis",
description = "Create markdown files based on the new available apis in xmake.",
options = nil
}

0 comments on commit 7b0cbc2

Please sign in to comment.