diff options
Diffstat (limited to 'xmake')
| -rw-r--r-- | xmake/core/base/os.lua | 5 | ||||
| -rw-r--r-- | xmake/core/base/table.lua | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import.lua | 41 | ||||
| -rw-r--r-- | xmake/plugins/lua/xmake.lua | 6 |
4 files changed, 45 insertions, 9 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 5aad87d26..7827064e3 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -216,6 +216,11 @@ function os.match(pattern, mode) end end + -- remove "./" or '.\\' prefix + if pattern:sub(1, 2):find('%.[/\\]') then + pattern = pattern:sub(3) + end + -- get the root directory local rootdir = pattern local starpos = pattern:find("%*") diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua index b7f5719a9..7e8438b8d 100644 --- a/xmake/core/base/table.lua +++ b/xmake/core/base/table.lua @@ -259,7 +259,7 @@ function table._dump(self, exclude, level) end io.write("}\n") else - io.write("<" .. tostring(v) .. ">") + io.write("<" .. tostring(self) .. ">") end end diff --git a/xmake/core/sandbox/modules/import.lua b/xmake/core/sandbox/modules/import.lua index c4011f7a4..921d3889a 100644 --- a/xmake/core/sandbox/modules/import.lua +++ b/xmake/core/sandbox/modules/import.lua @@ -51,6 +51,32 @@ function sandbox_import._modulename(name) return name end +-- get module path from name +function sandbox_import._modulepath(name) + + -- translate module path + -- + -- "package.module" => "package/module" + -- "..package.module" => "../../package/module" + -- + local startdots = true + local modulepath = name:gsub(".", function(c) + if c == '.' then + if startdots then + return ".." .. path.seperator() + else + return path.seperator() + end + else + startdots = false + return c + end + end) + + -- return module path + return modulepath +end + -- load module from file function sandbox_import._loadfile(filepath, instance) @@ -98,8 +124,8 @@ function sandbox_import._find(dir, name) -- check assert(dir and name) - -- replace "package.module" => "package/module" - name = (name:gsub("%.", "/")) + -- get module path + name = sandbox_import._modulepath(name) assert(name) -- load the single module? @@ -123,8 +149,8 @@ function sandbox_import._load(dir, name, instance) -- check assert(dir and name) - -- replace "package.module" => "package/module" - name = (name:gsub("%.", "/")) + -- get module path + name = sandbox_import._modulepath(name) assert(name) -- load the single module? @@ -233,6 +259,9 @@ end -- import("core.platform", {inherit = true}) -- => inherit the all interfaces of core.platform to the current scope -- +-- local test = import("test", {rootdir = "/tmp/xxx", anonymous = true}) +-- test() +-- -- @note the polymiorphism is not supported for import.inherit mode now. -- function sandbox_import.import(name, args) @@ -343,7 +372,9 @@ function sandbox_import.import(name, args) end -- import this module into the parent scope - scope_parent[imported_name] = module + if not args.anonymous then + scope_parent[imported_name] = module + end -- return it return module diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index 2789bf26a..78cd176ab 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -60,16 +60,16 @@ task("lua") if os.isfile(script) then -- run the given lua script file (xmake lua /tmp/script.lua) - import(path.basename(script), {rootdir = path.directory(script)})(unpack(option.get("arguments") or {})) + import(path.basename(script), {rootdir = path.directory(script), anonymous = true})(unpack(option.get("arguments") or {})) elseif os.isfile(path.join(os.scriptdir(), "scripts", script .. ".lua")) then -- run builtin lua script (xmake lua echo "hello xmake") - import("scripts." .. script)(unpack(option.get("arguments") or {})) + import("scripts." .. script, {anonymous = true})(unpack(option.get("arguments") or {})) else -- run modules (xmake lua core.xxx.xxx) - print(import(script)(unpack(option.get("arguments") or {}))) + print(import(script, {anonymous = true})(unpack(option.get("arguments") or {}))) end else -- enter interactive mode |
