summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-04-23 16:03:06 +0800
committerGitHub <[email protected]>2024-04-23 16:03:06 +0800
commit0219243bddc2b742178c1b8fb7a77f1a6fdbf8f0 (patch)
tree0f2d79e66855dececb27a75df1e1e48bbc942425
parent316c9cdee35d47e57219412d7ad39326966626c0 (diff)
parentc5b45c157bba1c348d8d95dc103377859a8cdfe9 (diff)
Merge pull request #5005 from xmake-io/api
Show all apis
-rw-r--r--xmake/core/package/package.lua6
-rw-r--r--xmake/core/sandbox/modules/import/core/package/package.lua2
-rw-r--r--xmake/core/sandbox/modules/import/core/project/option.lua8
-rw-r--r--xmake/core/sandbox/modules/import/core/project/project.lua1
-rw-r--r--xmake/core/sandbox/modules/import/core/project/rule.lua14
-rw-r--r--xmake/core/sandbox/modules/import/core/project/target.lua14
-rw-r--r--xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua9
-rw-r--r--xmake/core/sandbox/modules/import/core/tool/toolchain.lua3
-rw-r--r--xmake/core/sandbox/sandbox.lua71
-rw-r--r--xmake/plugins/show/lists/apis.lua271
-rw-r--r--xmake/plugins/show/lists/rules.lua2
-rw-r--r--xmake/plugins/show/showlist.lua23
-rw-r--r--xmake/plugins/show/xmake.lua5
13 files changed, 357 insertions, 72 deletions
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua
index abad53326..e673f4345 100644
--- a/xmake/core/package/package.lua
+++ b/xmake/core/package/package.lua
@@ -2905,5 +2905,11 @@ function package.load_from_repository(packagename, packagedir, opt)
return instance
end
+-- new a package instance
+function package.new(...)
+ return _instance.new(...)
+end
+
+
-- return module
return package
diff --git a/xmake/core/sandbox/modules/import/core/package/package.lua b/xmake/core/sandbox/modules/import/core/package/package.lua
index 527ae5b64..a5aa2e9d5 100644
--- a/xmake/core/sandbox/modules/import/core/package/package.lua
+++ b/xmake/core/sandbox/modules/import/core/package/package.lua
@@ -32,6 +32,8 @@ sandbox_core_package_package.installdir = package.installdir
sandbox_core_package_package.searchdirs = package.searchdirs
sandbox_core_package_package.targetplat = package.targetplat
sandbox_core_package_package.targetarch = package.targetarch
+sandbox_core_package_package.apis = package.apis
+sandbox_core_package_package.new = package.new
-- load the package from the project file
function sandbox_core_package_package.load_from_project(packagename)
diff --git a/xmake/core/sandbox/modules/import/core/project/option.lua b/xmake/core/sandbox/modules/import/core/project/option.lua
index e96f79d4c..fe98ccb93 100644
--- a/xmake/core/sandbox/modules/import/core/project/option.lua
+++ b/xmake/core/sandbox/modules/import/core/project/option.lua
@@ -25,10 +25,10 @@ local sandbox_core_project_option = sandbox_core_project_option or {}
local option = require("project/option")
local raise = require("sandbox/modules/raise")
--- get the option interpreter
-function sandbox_core_project_option.interpreter()
- return option.interpreter()
-end
+-- inherit some builtin interfaces
+sandbox_core_project_option.interpreter = option.interpreter
+sandbox_core_project_option.new = option.new
+sandbox_core_project_option.apis = option.apis
-- return module
return sandbox_core_project_option
diff --git a/xmake/core/sandbox/modules/import/core/project/project.lua b/xmake/core/sandbox/modules/import/core/project/project.lua
index b8feddefc..c197ff770 100644
--- a/xmake/core/sandbox/modules/import/core/project/project.lua
+++ b/xmake/core/sandbox/modules/import/core/project/project.lua
@@ -70,6 +70,7 @@ sandbox_core_project.policy = project.policy
sandbox_core_project.tmpdir = project.tmpdir
sandbox_core_project.tmpfile = project.tmpfile
sandbox_core_project.is_loaded = project.is_loaded
+sandbox_core_project.apis = project.apis
-- check project options
function sandbox_core_project.check_options()
diff --git a/xmake/core/sandbox/modules/import/core/project/rule.lua b/xmake/core/sandbox/modules/import/core/project/rule.lua
index 1acca90a1..f40a69a01 100644
--- a/xmake/core/sandbox/modules/import/core/project/rule.lua
+++ b/xmake/core/sandbox/modules/import/core/project/rule.lua
@@ -28,15 +28,11 @@ local project = require("project/project")
local sandbox = require("sandbox/sandbox")
local raise = require("sandbox/modules/raise")
--- get the given global rule
-function sandbox_core_project_rule.rule(name)
- return rule.rule(name)
-end
-
--- get the all global rules
-function sandbox_core_project_rule.rules()
- return rule.rules()
-end
+-- inherit some builtin interfaces
+sandbox_core_project_rule.rule = rule.rule
+sandbox_core_project_rule.rules = rule.rules
+sandbox_core_project_rule.new = rule.new
+sandbox_core_project_rule.apis = rule.apis
-- return module
return sandbox_core_project_rule
diff --git a/xmake/core/sandbox/modules/import/core/project/target.lua b/xmake/core/sandbox/modules/import/core/project/target.lua
index 07424cad3..e953c3c0e 100644
--- a/xmake/core/sandbox/modules/import/core/project/target.lua
+++ b/xmake/core/sandbox/modules/import/core/project/target.lua
@@ -25,15 +25,11 @@ local sandbox_core_project_target = sandbox_core_project_target or {}
local target = require("project/target")
local raise = require("sandbox/modules/raise")
--- get the filename from the given name and kind
-function sandbox_core_project_target.filename(name, kind, opt)
- return target.filename(name, kind, opt)
-end
-
--- get the link name of the target file
-function sandbox_core_project_target.linkname(filename, opt)
- return target.linkname(filename, opt)
-end
+-- inherit some builtin interfaces
+sandbox_core_project_target.filename = target.filename
+sandbox_core_project_target.linkname = target.linkname
+sandbox_core_project_target.new = target.new
+sandbox_core_project_target.apis = target.apis
-- return module
return sandbox_core_project_target
diff --git a/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua b/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua
index c189f398e..688707e4c 100644
--- a/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua
+++ b/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua
@@ -127,16 +127,17 @@ end
-- get the filter of the current sandbox for the given script
function sandbox_core_sandbox.filter(script)
-
- -- get the current sandbox instance
local instance = sandbox.instance(script)
if not instance then
raise("cannot get sandbox instance!")
end
-
- -- get it
return instance:filter()
end
+-- get all builtin modules
+function sandbox_core_sandbox.builtin_modules()
+ return sandbox.builtin_modules()
+end
+
-- return module
return sandbox_core_sandbox
diff --git a/xmake/core/sandbox/modules/import/core/tool/toolchain.lua b/xmake/core/sandbox/modules/import/core/tool/toolchain.lua
index 667019b1a..46bd9fb1d 100644
--- a/xmake/core/sandbox/modules/import/core/tool/toolchain.lua
+++ b/xmake/core/sandbox/modules/import/core/tool/toolchain.lua
@@ -27,6 +27,9 @@ local toolchain = require("tool/toolchain")
local project = require("project/project")
local raise = require("sandbox/modules/raise")
+-- inherit some builtin interfaces
+sandbox_core_tool_toolchain.apis = toolchain.apis
+
-- get all toolchains list
function sandbox_core_tool_toolchain.list()
local names = table.copy(platform.toolchains())
diff --git a/xmake/core/sandbox/sandbox.lua b/xmake/core/sandbox/sandbox.lua
index 766e6061c..1e0817a4e 100644
--- a/xmake/core/sandbox/sandbox.lua
+++ b/xmake/core/sandbox/sandbox.lua
@@ -90,50 +90,10 @@ end
-- register api for builtin
function sandbox._api_register_builtin(self, name, func)
-
- -- check
assert(self and self._PUBLIC and func)
-
- -- register it
self._PUBLIC[name] = func
end
--- get builtin modules
-function sandbox._builtin_modules()
- local builtin_modules = sandbox._BUILTIN_MODULES
- if builtin_modules == nil then
- builtin_modules = {}
- local builtin_module_files = os.match(path.join(os.programdir(), "core/sandbox/modules/*.lua"))
- if builtin_module_files then
- for _, builtin_module_file in ipairs(builtin_module_files) do
-
- -- the module name
- local module_name = path.basename(builtin_module_file)
- assert(module_name)
-
- -- load script
- local script, errors = loadfile(builtin_module_file)
- if script then
-
- -- load module
- local ok, results = utils.trycall(script)
- if not ok then
- os.raise(results)
- end
-
- -- save module
- builtin_modules[module_name] = results
- else
- -- error
- os.raise(errors)
- end
- end
- end
- sandbox._BUILTIN_MODULES = builtin_modules
- end
- return builtin_modules
-end
-
-- new a sandbox instance
function sandbox._new()
@@ -145,7 +105,7 @@ function sandbox._new()
-- register the builtin modules
instance:_api_register_builtin("_g", {})
- for module_name, module in pairs(sandbox._builtin_modules()) do
+ for module_name, module in pairs(sandbox.builtin_modules()) do
instance:_api_register_builtin(module_name, module)
end
@@ -350,5 +310,34 @@ function sandbox.instance(script)
return instance
end
+-- get builtin modules
+function sandbox.builtin_modules()
+ local builtin_modules = sandbox._BUILTIN_MODULES
+ if builtin_modules == nil then
+ builtin_modules = {}
+ local builtin_module_files = os.files(path.join(os.programdir(), "core/sandbox/modules/*.lua"))
+ if builtin_module_files then
+ for _, builtin_module_file in ipairs(builtin_module_files) do
+ local module_name = path.basename(builtin_module_file)
+ assert(module_name)
+
+ local script, errors = loadfile(builtin_module_file)
+ if script then
+ local ok, results = utils.trycall(script)
+ if not ok then
+ os.raise(results)
+ end
+ builtin_modules[module_name] = results
+ else
+ os.raise(errors)
+ end
+ end
+ end
+ sandbox._BUILTIN_MODULES = builtin_modules
+ end
+ return builtin_modules
+end
+
+
-- return module
return sandbox
diff --git a/xmake/plugins/show/lists/apis.lua b/xmake/plugins/show/lists/apis.lua
new file mode 100644
index 000000000..272b614e0
--- /dev/null
+++ b/xmake/plugins/show/lists/apis.lua
@@ -0,0 +1,271 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file apis.lua
+--
+
+-- imports
+import("core.project.config")
+import("core.project.rule")
+import("core.project.target")
+import("core.project.project")
+import("core.project.option")
+import("core.package.package")
+import("core.sandbox.sandbox")
+import("core.sandbox.module")
+import("core.tool.toolchain")
+import(".showlist")
+
+function _is_callable(func)
+ if type(func) == "function" then
+ return true
+ elseif type(func) == "table" then
+ local meta = debug.getmetatable(func)
+ if meta and meta.__call then
+ return true
+ end
+ end
+end
+
+-- get project scope apis
+function project_scope_apis()
+ local result = {}
+ for _, names in pairs(project.apis()) do
+ for _, name in ipairs(names) do
+ if type(name) == "table" then
+ name = name[1]
+ end
+ table.insert(result, name)
+ end
+ end
+ return result
+end
+
+-- get target scope apis
+function target_scope_apis()
+ local result = {}
+ for _, names in pairs(target.apis()) do
+ for _, name in ipairs(names) do
+ table.insert(result, name)
+ end
+ end
+ return result
+end
+
+-- get target instance apis
+function target_instance_apis()
+ local result = {}
+ local instance = target.new()
+ for k, v in pairs(instance) do
+ if not k:startswith("_") and type(v) == "function" then
+ table.insert(result, "target:" .. k)
+ end
+ end
+ return result
+end
+
+-- get option scope apis
+function option_scope_apis()
+ local result = {}
+ for _, names in pairs(option.apis()) do
+ for _, name in ipairs(names) do
+ table.insert(result, name)
+ end
+ end
+ return result
+end
+
+-- get option instance apis
+function option_instance_apis()
+ local result = {}
+ local instance = option.new()
+ for k, v in pairs(instance) do
+ if not k:startswith("_") and type(v) == "function" then
+ table.insert(result, "option:" .. k)
+ end
+ end
+ return result
+end
+
+-- get rule scope apis
+function rule_scope_apis()
+ local result = {}
+ for _, names in pairs(rule.apis()) do
+ for _, name in ipairs(names) do
+ table.insert(result, name)
+ end
+ end
+ return result
+end
+
+-- get rule instance apis
+function rule_instance_apis()
+ local result = {}
+ local instance = rule.new()
+ for k, v in pairs(instance) do
+ if not k:startswith("_") and type(v) == "function" then
+ table.insert(result, "rule:" .. k)
+ end
+ end
+ return result
+end
+
+
+-- get package scope apis
+function package_scope_apis()
+ local result = {}
+ for _, names in pairs(package.apis()) do
+ for _, name in ipairs(names) do
+ if type(name) == "table" then
+ name = name[1]
+ end
+ table.insert(result, name)
+ end
+ end
+ return result
+end
+
+-- get package instance apis
+function package_instance_apis()
+ local result = {}
+ local instance = package.new()
+ for k, v in pairs(instance) do
+ if not k:startswith("_") and type(v) == "function" then
+ table.insert(result, "package:" .. k)
+ end
+ end
+ return result
+end
+
+-- get toolchain scope apis
+function toolchain_scope_apis()
+ local result = {}
+ for _, names in pairs(toolchain.apis()) do
+ for _, name in ipairs(names) do
+ table.insert(result, name)
+ end
+ end
+ return result
+end
+
+-- get toolchain instance apis
+function toolchain_instance_apis()
+ local result = {}
+ local instance = toolchain.load("clang")
+ for k, v in pairs(instance) do
+ if not k:startswith("_") and type(v) == "function" then
+ table.insert(result, "toolchain:" .. k)
+ end
+ end
+ return result
+end
+
+-- get scope apis
+function scope_apis()
+ local result = {}
+ table.join2(result, project_scope_apis())
+ table.join2(result, target_scope_apis())
+ table.join2(result, option_scope_apis())
+ table.join2(result, rule_scope_apis())
+ table.join2(result, package_scope_apis())
+ table.join2(result, toolchain_scope_apis())
+ table.sort(result)
+ return result
+end
+
+-- get instance apis
+function instance_apis()
+ local result = {}
+ table.join2(result, target_instance_apis())
+ table.join2(result, option_instance_apis())
+ table.join2(result, rule_instance_apis())
+ table.join2(result, package_instance_apis())
+ table.join2(result, toolchain_instance_apis())
+ table.sort(result)
+ return result
+end
+
+-- get builtin module apis
+function builtin_module_apis()
+ local builtin_modules = table.clone(sandbox.builtin_modules())
+ builtin_modules.pairs = nil
+ builtin_modules.ipairs = nil
+ local result = {}
+ for name, value in pairs(builtin_modules) do
+ if type(value) == "table" then
+ for k, v in pairs(value) do
+ if not k:startswith("_") and type(v) == "function" then
+ table.insert(result, name .. "." .. k)
+ end
+ end
+ elseif type(value) == "function" then
+ table.insert(result, name)
+ end
+ end
+ table.insert(result, "ipairs")
+ table.insert(result, "pairs")
+ table.sort(result)
+ return result
+end
+
+-- get import module apis
+function import_module_apis()
+ local result = {}
+ local moduledirs = module.directories()
+ for _, moduledir in ipairs(moduledirs) do
+ moduledir = path.absolute(moduledir)
+ local modulefiles = os.files(path.join(moduledir, "**.lua|private/**.lua|core/tools/**.lua|detect/tools/**.lua"))
+ if modulefiles then
+ for _, modulefile in ipairs(modulefiles) do
+ local modulename = path.relative(modulefile, moduledir)
+ if path.filename(modulename) == "main.lua" then
+ modulename = path.directory(modulename)
+ end
+ modulename = modulename:gsub("/", "."):gsub("%.lua", "")
+ local instance = import(modulename, {try = true, anonymous = true})
+ if _is_callable(instance) then
+ table.insert(result, modulename)
+ elseif type(instance) == "table" then
+ for k, v in pairs(instance) do
+ if not k:startswith("_") and type(v) == "function" then
+ table.insert(result, modulename .. "." .. k)
+ end
+ end
+ end
+ end
+ end
+ end
+ table.sort(result)
+ return result
+end
+
+-- get all apis
+function apis()
+ return {scope = scope_apis(),
+ instance = instance_apis(),
+ builtin_module = builtin_module_apis(),
+ import_module = import_module_apis()}
+end
+
+-- show all apis
+function main()
+ config.load()
+ local result = apis()
+ if result then
+ showlist(result)
+ end
+end
diff --git a/xmake/plugins/show/lists/rules.lua b/xmake/plugins/show/lists/rules.lua
index b6ab2f919..77c0ea5c2 100644
--- a/xmake/plugins/show/lists/rules.lua
+++ b/xmake/plugins/show/lists/rules.lua
@@ -23,7 +23,7 @@ import("core.project.config")
import("core.project.rule")
import(".showlist")
--- show all platforms
+-- show all rules
function main()
config.load()
local rules = {}
diff --git a/xmake/plugins/show/showlist.lua b/xmake/plugins/show/showlist.lua
index 923228e42..a80fd3b01 100644
--- a/xmake/plugins/show/showlist.lua
+++ b/xmake/plugins/show/showlist.lua
@@ -21,9 +21,9 @@
-- imports
import("core.base.option")
import("core.base.text")
+import("core.base.json")
--- show values
-function main(values)
+function _show_text(values)
local tbl = {align = 'l', sep = " "}
local row = {}
for _, value in ipairs(values) do
@@ -38,3 +38,22 @@ function main(values)
end
print(text.table(tbl))
end
+
+function _show_json(values)
+ print(json.encode(values))
+end
+
+function main(values)
+ if option.get("json") then
+ _show_json(values)
+ else
+ if table.is_dictionary(values) then
+ for k, v in pairs(values) do
+ cprint("${bright}%s:", k)
+ _show_text(v)
+ end
+ else
+ _show_text(values)
+ end
+ end
+end
diff --git a/xmake/plugins/show/xmake.lua b/xmake/plugins/show/xmake.lua
index f652ed620..bae15471c 100644
--- a/xmake/plugins/show/xmake.lua
+++ b/xmake/plugins/show/xmake.lua
@@ -25,11 +25,12 @@ task("show")
usage = "xmake show [options] [arguments]",
description = "Show the given project information.",
options = {
- {'l', "list", "kv", nil, "Show the values list of the given name.",
+ {'l', "list", "kv", nil, "Show the values list of the given name.",
values = function (complete, opt)
return import("list").lists()
end},
- {'t', "target", "kv", nil, "Show the information of the given target.",
+ {nil, "json", "k", false, "Show information with json format."},
+ {'t', "target", "kv", nil, "Show the information of the given target.",
values = function (complete, opt)
return import("private.utils.complete_helper.targets")(complete, opt)
end}