diff options
| author | ruki <[email protected]> | 2017-05-16 10:31:40 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-05-16 10:32:16 +0800 |
| commit | 76845e083b940445042896a87ebb482edcac8a36 (patch) | |
| tree | 9b966443c8ca343cba52f58a61eb14c3d12f488b | |
| parent | 79634bd7f65737d619f87f6b38710195429843d0 (diff) | |
improve import interfaces and add user externsion modules
| -rw-r--r-- | xmake/core/base/interpreter.lua | 6 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import.lua | 39 | ||||
| -rw-r--r-- | xmake/modules/lib/detect/find_ccache.lua | 33 | ||||
| -rw-r--r-- | xmake/plugins/lua/xmake.lua | 28 |
4 files changed, 85 insertions, 21 deletions
diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 1f31756a1..fdb8fbaa0 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -1084,7 +1084,7 @@ function interpreter:api_register_on_script(scope_kind, ...) script = function (...) -- import it - return import(modulename).main(...) + return import(modulename)(...) end end @@ -1119,7 +1119,7 @@ function interpreter:api_register_before_script(scope_kind, ...) script = function (...) -- import it - return import(modulename).main(...) + return import(modulename)(...) end end @@ -1154,7 +1154,7 @@ function interpreter:api_register_after_script(scope_kind, ...) script = function (...) -- import it - return import(modulename).main(...) + return import(modulename)(...) end end diff --git a/xmake/core/sandbox/modules/import.lua b/xmake/core/sandbox/modules/import.lua index 234e76925..56e517e5b 100644 --- a/xmake/core/sandbox/modules/import.lua +++ b/xmake/core/sandbox/modules/import.lua @@ -31,6 +31,7 @@ local path = require("base/path") local utils = require("base/utils") local table = require("base/table") local string = require("base/string") +local global = require("project/global") local sandbox = require("sandbox/sandbox") local raise = require("sandbox/modules/raise") @@ -104,16 +105,11 @@ function sandbox_import._find(dir, name) -- load the single module? local module = nil if os.isfile(path.join(dir, name .. ".lua")) then - - -- ok return true -- load modules elseif os.isdir(path.join(dir, name)) then - - -- ok return true - end -- not found @@ -269,15 +265,31 @@ function sandbox_import.import(name, args) local rootdir = args.rootdir or instance:rootdir() assert(rootdir) + -- the sandbox modules directory + local modules_sandbox_dir = path.join(xmake._CORE_DIR, "sandbox/modules/import") + + -- the extension modules directory + local modules_extension_dir = path.join(xmake._PROGRAM_DIR, "modules") + + -- the global modules directory for users + local modules_global_dir = path.join(global.directory(), "modules") + + -- init module directories + local modules_directories = + { + rootdir -- load module from the given root directory first + , path.join(global.directory(), "modules") -- load module from the user global modules directory + , path.join(xmake._PROGRAM_DIR, "modules") -- load module from the extension modules directory + , path.join(xmake._CORE_DIR, "sandbox/modules/import") -- load module from the sandbox core modules directory + } + -- load module local module = nil local errors = nil - if sandbox_import._find(rootdir, name) then - -- load module from the sandbox root directory - module, errors = sandbox_import._load(rootdir, name, instance) - else - -- load module from the sandbox core directory - module, errors = sandbox_import._load(path.join(xmake._CORE_DIR, "sandbox/modules/import"), name) + for idx, moduledir in ipairs(modules_directories) do + if sandbox_import._find(moduledir, name) then + module, errors = sandbox_import._load(moduledir, name, utils.ifelse(idx < #modules_directories, instance, nil)) -- last modules need not fork sandbox + end end -- check @@ -320,6 +332,11 @@ function sandbox_import.import(name, args) end + -- bind main entry + if module.main then + setmetatable(module, { __call = function (_, ...) return module.main(...) end}) + end + -- import this module into the parent scope scope_parent[imported_name] = module diff --git a/xmake/modules/lib/detect/find_ccache.lua b/xmake/modules/lib/detect/find_ccache.lua new file mode 100644 index 000000000..09364a5e5 --- /dev/null +++ b/xmake/modules/lib/detect/find_ccache.lua @@ -0,0 +1,33 @@ +--!The Make-like Build Utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2017, TBOOX Open Source Group. +-- +-- @author ruki +-- @file find_ccache.lua +-- + +-- find ccache +-- +-- @param ... detected dirs or files +-- +-- @return shellname, fullpath +-- +function main(...) + print("ccache") +end diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index 1c6b2bf27..ba872f9cf 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -35,7 +35,7 @@ task("lua") import("core.base.option") import("core.sandbox.sandbox") - -- list all scripts? + -- list all lua scripts? if option.get("list") then print("scripts:") local files = os.match(path.join(os.scriptdir(), "scripts/*.lua")) @@ -51,9 +51,18 @@ task("lua") -- import and run script if os.isfile(name) then - import(path.basename(name), {rootdir = path.directory(name)}).main(unpack(option.get("arguments") or {})) + + -- run the given lua script file (xmake lua /tmp/script.lua) + import(path.basename(name), {rootdir = path.directory(name)})(unpack(option.get("arguments") or {})) + + elseif os.isfile(path.join(os.scriptdir(), "scripts", name .. ".lua")) then + + -- run builtin lua script (xmake lua echo "hello xmake") + import("scripts." .. name)(unpack(option.get("arguments") or {})) else - import("scripts." .. name).main(unpack(option.get("arguments") or {})) + + -- run modules (xmake lua core.xxx.xxx) + import(name)(unpack(option.get("arguments") or {})) end else -- enter interactive mode @@ -75,10 +84,15 @@ task("lua") -- options , options = { - {'l', "list", "k", nil, "List all scripts." } - , {nil, "root", "k", nil, "Allow to run script as root." } - , {nil, "script", "v", nil, "Run the given lua script." } - , {nil, "arguments", "vs", nil, "The script arguments." } + {'l', "list", "k", nil, "List all scripts." } + , {nil, "root", "k", nil, "Allow to run script as root." } + , {nil, "script", "v", nil, "Run the given lua script name, file or module and enter interactive mode if no given script.", + ".e.g", + " - xmake lua (enter interactive mode)", + " - xmake lua /tmp/script.lua", + " - xmake lua echo 'hello xmake'", + " - xmake lua core.xxx.xxx" } + , {nil, "arguments", "vs", nil, "The script arguments." } } } |
