summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-07-24 21:54:10 +0800
committerruki <[email protected]>2017-07-24 21:54:10 +0800
commitafd783741790c86eee1481f5a04ec82e994c215c (patch)
tree79c7cb050c80b0587f47782d70767205a270fc0f
parent8489683bc32d7d6f20ee3b663b1d1a505cc71221 (diff)
load module with multi-dirs
-rw-r--r--docs/zh/plugins.md6
-rw-r--r--xmake/core/sandbox/modules/import/core/sandbox/module.lua40
2 files changed, 31 insertions, 15 deletions
diff --git a/docs/zh/plugins.md b/docs/zh/plugins.md
index fa1d493c4..36b1a9485 100644
--- a/docs/zh/plugins.md
+++ b/docs/zh/plugins.md
@@ -68,9 +68,9 @@ hello
现在一个最简单的插件写完了,那怎么让它被xmake检测到呢,有三种方式:
-1. 把 hello 这个文件夹放置在 xmake的插件安装目录 xmake/plugins,这个里面都是些内建的插件
-2. 把 hello 文件夹防止在 ~/.xmake/plugins 用户全局目录,这样对当前xmake 全局生效
-3. 把 hello 文件夹防止在任意地方,通过在工程描述文件xmake.lua中调用`add_plugindirs("./hello")` 添加当前的工程的插件搜索目录,这样只对当前工程生效
+1. 把 hello 这个文件夹放置在 xmake的插件安装目录 `xmake/plugins`,这个里面都是些内建的插件
+2. 把 hello 文件夹放置在 `~/.xmake/plugins` 用户全局目录,这样对当前xmake 全局生效
+3. 把 hello 文件夹放置在任意地方,通过在工程描述文件xmake.lua中调用`add_plugindirs("./hello")` 添加当前的工程的插件搜索目录,这样只对当前工程生效
#### 运行插件
diff --git a/xmake/core/sandbox/modules/import/core/sandbox/module.lua b/xmake/core/sandbox/modules/import/core/sandbox/module.lua
index 53b814218..b0aa2c1ef 100644
--- a/xmake/core/sandbox/modules/import/core/sandbox/module.lua
+++ b/xmake/core/sandbox/modules/import/core/sandbox/module.lua
@@ -133,15 +133,15 @@ function core_sandbox_module._find(dir, name)
-- the single module?
if os.isfile(key .. ".lua") then
- return path.absolute(key)
+ return path.absolute(key), false
-- modules?
elseif os.isdir(key) then
- return path.absolute(key)
+ return path.absolute(key), true
end
end
-- load module
-function core_sandbox_module._load(dir, name, instance)
+function core_sandbox_module._load(dir, name, instance, module)
-- check
assert(dir and name)
@@ -151,10 +151,12 @@ function core_sandbox_module._load(dir, name, instance)
assert(name)
-- load the single module?
- local module = nil
local script = nil
if os.isfile(path.join(dir, name .. ".lua")) then
+ -- check
+ assert(not module)
+
-- load module
local result, errors = core_sandbox_module._loadfile(path.join(dir, name .. ".lua"), instance)
if not result then
@@ -267,7 +269,7 @@ function core_sandbox_module.find(name)
-- find it from the module directories
for _, moduledir in ipairs(core_sandbox_module.directories()) do
- if core_sandbox_module._find(moduledir, name) then
+ if (core_sandbox_module._find(moduledir, name)) then
return true
end
end
@@ -348,10 +350,12 @@ function core_sandbox_module.import(name, opt)
local errors = nil
local module = nil
local modulekey = nil
+ local isdirs = false
+ local loadnext = false
for idx, moduledir in ipairs(modules_directories) do
-- find module and key
- modulekey = core_sandbox_module._find(moduledir, name)
+ modulekey, isdirs = core_sandbox_module._find(moduledir, name)
if modulekey then
-- load it from cache first
@@ -360,18 +364,25 @@ function core_sandbox_module.import(name, opt)
module = moduleinfo[1]
errors = moduleinfo[2]
else
+
-- load it from the script file
- module, errors = core_sandbox_module._load(moduledir, name, utils.ifelse(idx < #modules_directories, instance, nil)) -- last modules need not fork sandbox
+ module, errors = core_sandbox_module._load( moduledir, name
+ , utils.ifelse(idx < #modules_directories, instance, nil) -- last modules need not fork sandbox
+ , module)
- -- cache this module
- if not opt.nocache then
- modules[modulekey] = {module, errors}
+ -- continue to load?
+ if module and isdirs then
+ loadnext = true
end
end
- -- end
+ -- found
found = true
- break
+
+ -- end?
+ if not loadnext then
+ break
+ end
end
end
@@ -389,6 +400,11 @@ function core_sandbox_module.import(name, opt)
raise("cannot import module: %s, %s", name, errors)
end
+ -- cache this module
+ if not opt.nocache then
+ modules[modulekey] = modules[modulekey] or {module, errors}
+ end
+
-- get module script
local script = errors