summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2016-03-01 19:14:21 +0800
committerruki <[email protected]>2016-03-01 19:14:21 +0800
commit197c9a41fdb374f8338c6dd40af8df4eb5371b29 (patch)
treef0a686ce0615523f87848386973d2328afe89695
parentdb1a45865998fb00bf46baeaada017d9f17f9da4 (diff)
import package.module
-rwxr-xr-xxmake/actions/config/xmake.lua11
-rwxr-xr-xxmake/actions/create/xmake.lua4
-rwxr-xr-xxmake/actions/global/xmake.lua2
-rwxr-xr-xxmake/actions/package/xmake.lua2
-rw-r--r--xmake/core/base/path.lua8
-rw-r--r--xmake/core/base/sandbox.lua5
-rw-r--r--xmake/core/sandbox/import.lua161
7 files changed, 174 insertions, 19 deletions
diff --git a/xmake/actions/config/xmake.lua b/xmake/actions/config/xmake.lua
index 811393b8e..446ba5d4a 100755
--- a/xmake/actions/config/xmake.lua
+++ b/xmake/actions/config/xmake.lua
@@ -29,6 +29,9 @@ task("config")
-- on run
on_task_run(function ()
+ import("core.project")
+ print(project)
+
end)
-- set menu
@@ -54,7 +57,7 @@ task("config")
, function ()
-- import platform
- import("core/platform")
+ import("core.platform")
-- make description
local description = {}
@@ -71,7 +74,7 @@ task("config")
, function ()
-- import platform
- import("core/platform")
+ import("core.platform")
-- make description
local description = {}
@@ -99,7 +102,7 @@ task("config")
, function ()
-- import platform
- import("core/project")
+ import("core.project")
-- get menu for project
return project.menu()
@@ -147,7 +150,7 @@ task("config")
, function ()
-- import platform
- import("core/platform")
+ import("core.platform")
-- get menu for platform
return platform.menu("config")
diff --git a/xmake/actions/create/xmake.lua b/xmake/actions/create/xmake.lua
index e528bb842..9225ee771 100755
--- a/xmake/actions/create/xmake.lua
+++ b/xmake/actions/create/xmake.lua
@@ -44,7 +44,7 @@ task("create")
, function ()
-- import template
- import("core/template")
+ import("core.template")
-- make description
local description = {}
@@ -61,7 +61,7 @@ task("create")
, function ()
-- import template
- import("core/template")
+ import("core.template")
-- make description
local description = {}
diff --git a/xmake/actions/global/xmake.lua b/xmake/actions/global/xmake.lua
index 3161884a7..8c2b2495d 100755
--- a/xmake/actions/global/xmake.lua
+++ b/xmake/actions/global/xmake.lua
@@ -51,7 +51,7 @@ task("global")
, function ()
-- import platform
- import("core/platform")
+ import("core.platform")
-- get menu for platform
return platform.menu("global")
diff --git a/xmake/actions/package/xmake.lua b/xmake/actions/package/xmake.lua
index 305cd872d..c0fdb9771 100755
--- a/xmake/actions/package/xmake.lua
+++ b/xmake/actions/package/xmake.lua
@@ -47,7 +47,7 @@ task("package")
, function ()
-- import platform
- import("core/platform")
+ import("core.platform")
-- make description
local description = {}
diff --git a/xmake/core/base/path.lua b/xmake/core/base/path.lua
index 724a03d8b..0a9d6cf41 100644
--- a/xmake/core/base/path.lua
+++ b/xmake/core/base/path.lua
@@ -89,5 +89,13 @@ function path.join(p, ...)
return path.translate(p)
end
+-- split path by the separator
+function path.split(p)
+
+ -- split it
+ return p:split("/\\")
+end
+
+
-- return module: path
return path
diff --git a/xmake/core/base/sandbox.lua b/xmake/core/base/sandbox.lua
index 43a483526..49c863263 100644
--- a/xmake/core/base/sandbox.lua
+++ b/xmake/core/base/sandbox.lua
@@ -105,7 +105,7 @@ function sandbox._init()
assert(module_name)
-- load script
- local script = loadfile(builtin_module_file)
+ local script, errors = loadfile(builtin_module_file)
if script then
-- load module
@@ -116,6 +116,9 @@ function sandbox._init()
-- register module
self:_api_register_builtin(module_name, results)
+ else
+ -- error
+ os.raise(errors)
end
end
end
diff --git a/xmake/core/sandbox/import.lua b/xmake/core/sandbox/import.lua
index 236547eaf..c59cdbe87 100644
--- a/xmake/core/sandbox/import.lua
+++ b/xmake/core/sandbox/import.lua
@@ -20,22 +20,163 @@
-- @file import.lua
--
+-- define module
+local sandbox_import = sandbox_import or {}
+
-- load modules
-local os = require("base/os")
-local path = require("base/path")
-local utils = require("base/utils")
+local os = require("base/os")
+local path = require("base/path")
+local utils = require("base/utils")
+local string = require("base/string")
--- import module
-function sandbox_import(name)
+-- get module name
+function sandbox_import._modulename(name)
+
+ -- check
+ assert(name)
+
+ -- find modulename
+ local i = name:find_last(".", true)
+ if i then
+ name = name:sub(i + 1)
+ end
+
+ -- get it
+ return name
+end
+
+-- load module from file
+function sandbox_import._loadfile(filepath)
+
+ -- check
+ assert(filepath)
+
+ -- load module script
+ local script, errors = loadfile(filepath)
+ if not script then
+ return nil, errors
+ end
-- load module
- local module = require("sandbox/import/" .. name)
+ local ok, result = xpcall(script, debug.traceback)
+ if not ok then
+ return nil, result
+ end
+
+ -- ok?
+ return result
+end
+
+-- load module
+function sandbox_import._load(dir, name)
+
+ -- check
+ assert(dir and name)
+
+ -- replace "package.module" => "package/module"
+ name = (name:gsub("%.", "/"))
+ assert(name)
+
+ -- load the single module?
+ local module = nil
+ if os.isfile(path.join(dir, name .. ".lua")) then
+
+ -- load module
+ local result, errors = sandbox_import._loadfile(path.join(dir, name .. ".lua"))
+ if not result then
+ return nil, errors
+ end
+
+ -- save module
+ module = result
+
+ -- load modules
+ elseif os.isdir(path.join(dir, name)) then
+
+ -- get modulefiles
+ local moduleroot = path.join(path.join(dir, name))
+ local modulefiles = os.match(path.join(moduleroot, "**.lua"))
+ if modulefiles then
+ for _, modulefile in ipairs(modulefiles) do
+
+ -- load module
+ local result, errors = sandbox_import._loadfile(modulefile)
+ if not result then
+ return nil, errors
+ end
+
+ -- get the module path
+ local modulepath = path.relative(modulefile, moduleroot)
+ if not modulepath then
+ return nil, string.format("cannot get the path for module: %s", name)
+ end
+
+ -- init the root module
+ module = module or {}
+
+ -- save module
+ local scope = module
+ for _, modulename in ipairs(path.split(modulepath)) do
+
+ -- is end?
+ local pos = modulename:find(".lua", 1, true)
+ if pos then
+
+ -- get the module name
+ modulename = modulename:sub(1, pos - 1)
+ assert(modulename)
+
+ -- save module
+ scope[modulename] = result
+
+ -- is scope?
+ else
+
+ -- enter submodule
+ scope[modulename] = scope[modulename] or {}
+ scope = scope[modulename]
+ end
+ end
+ end
+ end
+ end
+
+ -- this module not found?
+ if not module then
+ return nil, string.format("module: %s not found!", name)
+ end
+
+ -- return it
+ return module
+end
+
+-- import module
+--
+-- .e.g
+--
+-- import("core.platform")
+-- => platform
+--
+-- import("core.platform", "p")
+-- => p
+--
+-- import("core")
+-- => core
+-- => core.platform
+--
+function sandbox_import.import(name, alias)
+
+ -- check
+ assert(name)
+
+ -- load module from the sandbox directory
+ local module, errors = sandbox_import._load(path.join(xmake._CORE_DIR, "sandbox/import"), name)
if not module then
- os.raise("cannot import module: %s", name)
+ os.raise("cannot import module: %s, %s", name, errors)
end
-- get module name
- local modulename = path.basename(name)
+ local modulename = sandbox_import._modulename(name)
if not modulename then
os.raise("cannot get module name for %s", name)
end
@@ -50,12 +191,12 @@ function sandbox_import(name)
end
-- import this module into the parent scope
- scope_parent[modulename] = module
+ scope_parent[alias or modulename] = module
-- return it
return module
end
-- load module
-return sandbox_import
+return sandbox_import.import