summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2023-10-12 22:54:30 +0800
committerruki <[email protected]>2023-10-12 22:54:30 +0800
commit768183611c9334c7727233e5a8f7a78c38aa03db (patch)
treefb20b03d4c67d582ec304f79fa4b946aae885e26
parentacccacadd43f998d144865322eb2561698a2173d (diff)
load scope
-rw-r--r--tests/apis/custom_scopeapis/xmake.lua18
-rw-r--r--xmake/core/sandbox/modules/import/core/project/project.lua22
2 files changed, 40 insertions, 0 deletions
diff --git a/tests/apis/custom_scopeapis/xmake.lua b/tests/apis/custom_scopeapis/xmake.lua
index ae88c3f73..3f20836a5 100644
--- a/tests/apis/custom_scopeapis/xmake.lua
+++ b/tests/apis/custom_scopeapis/xmake.lua
@@ -1,13 +1,31 @@
add_rules("mode.debug", "mode.release")
interp_add_scopeapis("myscope.set_name", "myscope.add_list", {kind = "values"})
+interp_add_scopeapis("myscope.on_script", {kind = "script"})
myscope("hello")
set_name("foo")
add_list("value1", "value2")
+ on_script(function ()
+ print("hello")
+ end)
target("test")
set_kind("binary")
add_files("src/*.cpp")
+ on_config(function (target)
+ import("core.project.project")
+ local myscope = project.scope("myscope")
+ for name, scope in pairs(myscope) do
+ print("myscope(%s)", name)
+ print(" name: %s", scope:get("name"))
+ print(" list: %s", table.concat(scope:get("list"), ", "))
+ print(" script:")
+ local script = scope:get("script")
+ if script then
+ script()
+ end
+ end
+ end)
diff --git a/xmake/core/sandbox/modules/import/core/project/project.lua b/xmake/core/sandbox/modules/import/core/project/project.lua
index 6d35abaa0..b8feddefc 100644
--- a/xmake/core/sandbox/modules/import/core/project/project.lua
+++ b/xmake/core/sandbox/modules/import/core/project/project.lua
@@ -254,5 +254,27 @@ function sandbox_core_project.chdir(projectdir, projectfile)
config._DIRECTORY = nil
end
+-- get scope
+function sandbox_core_project.scope(scopename)
+ local cachekey = "scope." .. scopename
+ local scope = project._memcache():get(cachekey)
+ if not scope then
+
+ -- load the project file first if has not been loaded?
+ local ok, errors = project._load()
+ if not ok then
+ raise("load project failed, %s", errors or "unknown")
+ end
+
+ -- load scope
+ scope, errors = project._load_scope(scopename, true, false)
+ if not scope then
+ raise("load scope(%s) failed, %s", scopename, errors or "unknown")
+ end
+ project._memcache():set(cachekey, scope)
+ end
+ return scope
+end
+
-- return module
return sandbox_core_project