update-apis.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import("plugins.show.lists.apis", {rootdir = os.programdir()})
  2. import("core.base.hashset")
  3. function _create_file(pth, api, opt)
  4. opt = opt or {}
  5. local s = opt.s or ([[---
  6. key: API_NAME
  7. name: API_NAME
  8. api: true
  9. ---
  10. ### API_NAME]]):gsub("\n%s+", "\n"):gsub("API_NAME", api)
  11. print("Creating markdown file for", api, "in", path.relative(pth, os.projectdir()))
  12. io.writefile(p, s)
  13. end
  14. -- do the update in the api/description/scopes folder of one locale
  15. function _update_description_scope_apis(opt)
  16. local apis = apis.description_scope_apis()
  17. local scopes_root = path.join(os.projectdir(), "doc", opt.locale, "api", "description", "scopes")
  18. for _, api in ipairs(apis) do
  19. local type, interface = table.unpack(api:split(".", {plain = true}))
  20. local p = path.join(scopes_root, type, interface .. ".md")
  21. if not os.exists(p) then
  22. _create_file(p, api)
  23. end
  24. end
  25. end
  26. -- do the update in the api/description/builtins folder of one locale
  27. function _update_description_builtin_apis(opt)
  28. local description_builtin = apis.description_builtin_apis()
  29. local script_builtin = hashset.from(apis.script_builtin_apis())
  30. local builtins_root = path.join(os.projectdir(), "doc", opt.locale, "api", "description", "builtins")
  31. for _, api in ipairs(description_builtin) do
  32. -- conditions are located in different places
  33. -- some in api/description/builtins
  34. -- others in api/description/condition
  35. -- so it's easier just to ignore them
  36. if api:startswith("is_") then
  37. goto continue
  38. end
  39. local p = path.join(builtins_root, api .. ".md")
  40. if not os.exists(p) then
  41. if script_builtin:has(api) then
  42. local s = format("${include api/script/builtins/%s.md}", api)
  43. _create_file(p, api, {s = s})
  44. else
  45. _create_file(p, api)
  46. end
  47. end
  48. ::continue::
  49. end
  50. end
  51. -- do the update in the api/script/builtins_modules folder of one locale
  52. function _update_description_builtin_modules_apis(opt)
  53. local description_builtin = apis.description_builtin_module_apis()
  54. local script_builtin = hashset.from(apis.script_builtin_module_apis())
  55. local builtins_root = path.join(os.projectdir(), "doc", opt.locale, "api", "description", "builtin_modules")
  56. for _, api in ipairs(description_builtin) do
  57. local type, interface = table.unpack(api:split(".", {plain = true}))
  58. local p = path.join(builtins_root, type, interface .. ".md")
  59. if not os.exists(p) then
  60. if script_builtin:has(api) then
  61. local s = format("${include api/script/builtin_modules/%s.md}", api)
  62. _create_file(p, api, {s = s})
  63. else
  64. _create_file(p, api)
  65. end
  66. end
  67. end
  68. end
  69. -- do the update in the api/script/instances folder of one locale
  70. function _update_script_instances_apis(opt)
  71. local apis = apis.script_instance_apis()
  72. local instances_root = path.join(os.projectdir(), "doc", opt.locale, "api", "script", "instances")
  73. for _, api in ipairs(apis) do
  74. local type, interface = table.unpack(api:split(":", {plain = true}))
  75. local p = path.join(instances_root, type, interface .. ".md")
  76. if not os.exists(p) then
  77. _create_file(p, api)
  78. end
  79. end
  80. end
  81. -- do the update in the api/script/builtins folder of one locale
  82. function _update_script_builtin_apis(opt)
  83. local apis = apis.script_builtin_apis()
  84. local builtins_root = path.join(os.projectdir(), "doc", opt.locale, "api", "script", "builtins")
  85. for _, api in pairs(apis) do
  86. -- conditions are located in different places
  87. -- some in api/description/builtins
  88. -- others in api/description/condition
  89. -- so it's easier just to ignore them
  90. if api:startswith("is_") then
  91. goto continue
  92. end
  93. local p = path.join(builtins_root, api .. ".md")
  94. if not os.exists(p) then
  95. _create_file(p, api, opt)
  96. end
  97. ::continue::
  98. end
  99. end
  100. -- do the update in the api/script/builtin_modules folder of one locale
  101. function _update_script_builtin_modules_apis(opt)
  102. local apis = apis.script_builtin_module_apis()
  103. local builtins_root = path.join(os.projectdir(), "doc", opt.locale, "api", "script", "builtin_modules")
  104. for _, api in ipairs(apis) do
  105. local type, interface = table.unpack(api:split(".", {plain = true}))
  106. local p = path.join(builtins_root, type, interface .. ".md")
  107. if not os.exists(p) then
  108. _create_file(p, api)
  109. end
  110. end
  111. end
  112. function main()
  113. for _, pagefile in ipairs(os.files(path.join(os.projectdir(), "doc", "*", "pages.lua"))) do
  114. local locale = path.basename(path.directory(pagefile))
  115. -- description
  116. _update_description_scope_apis({locale = locale})
  117. _update_description_builtin_apis({locale = locale})
  118. _update_description_builtin_modules_apis({locale = locale})
  119. -- script
  120. _update_script_instances_apis({locale = locale})
  121. _update_script_builtin_modules_apis({locale = locale})
  122. end
  123. end