diff options
| author | ruki <[email protected]> | 2016-02-27 20:11:22 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2016-02-27 20:11:22 +0800 |
| commit | dd8ae9c540d5da04a8ffc8a030dfbbd53b26daa9 (patch) | |
| tree | 6a0a9222796e509fffa5f2c3c53567a29be3921e | |
| parent | 4e56f11cf31ad1279505ee5ed7e29945c20f0639 (diff) | |
add raise interfaces for os
| -rw-r--r-- | xmake/core/base/deprecated/interpreter.lua | 9 | ||||
| -rw-r--r-- | xmake/core/base/filter.lua | 4 | ||||
| -rw-r--r-- | xmake/core/base/interpreter.lua | 15 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 15 | ||||
| -rw-r--r-- | xmake/core/base/sandbox.lua | 3 | ||||
| -rw-r--r-- | xmake/core/base/task.lua | 6 | ||||
| -rw-r--r-- | xmake/core/base/template.lua | 3 | ||||
| -rw-r--r-- | xmake/core/base/utils.lua | 10 | ||||
| -rw-r--r-- | xmake/core/sandbox/import.lua | 7 | ||||
| -rw-r--r-- | xmake/core/sandbox/os.lua | 28 | ||||
| -rw-r--r-- | xmake/core/sandbox/raise.lua | 25 |
11 files changed, 71 insertions, 54 deletions
diff --git a/xmake/core/base/deprecated/interpreter.lua b/xmake/core/base/deprecated/interpreter.lua index aa88fa7e0..91c734660 100644 --- a/xmake/core/base/deprecated/interpreter.lua +++ b/xmake/core/base/deprecated/interpreter.lua @@ -52,8 +52,7 @@ function deprecated_interpreter.api_register_set_scope(self, ...) -- check if not scope_for_kind[scope_name] then utils.error("set_%s(\"%s\") failed, %s not found!", scope_kind, scope_name, scope_name) - utils.error("please uses add_%s(\"%s\") first!", scope_kind, scope_name) - utils.abort() + os.raise("please uses add_%s(\"%s\") first!", scope_kind, scope_name) end -- init scope for name @@ -92,8 +91,7 @@ function deprecated_interpreter.api_register_add_scope(self, ...) -- check if scope_for_kind[scope_name] then utils.error("add_%s(\"%s\") failed, %s have been defined!", scope_kind, scope_name, scope_name) - utils.error("please uses set_%s(\"%s\")!", scope_kind, scope_name) - utils.abort() + os.raise("please uses set_%s(\"%s\")!", scope_kind, scope_name) end -- init scope for name @@ -126,8 +124,7 @@ function deprecated_interpreter.api_register_set_script(self, scope_kind, prefix -- bind script and get new script with sandbox local newscript, errors = sandbox.bind(script, self) if not newscript then - utils.error("set_%s(): %s", name, errors) - utils.abort() + os.raise("set_%s(): %s", name, errors) end -- update script? diff --git a/xmake/core/base/filter.lua b/xmake/core/base/filter.lua index 8d187c32c..faacbafc2 100644 --- a/xmake/core/base/filter.lua +++ b/xmake/core/base/filter.lua @@ -24,6 +24,7 @@ local filter = filter or {} -- load modules +local os = require("base/os") local table = require("base/table") local utils = require("base/utils") local string = require("base/string") @@ -78,8 +79,7 @@ function filter.handle(self, value) -- invalid builtin variable? if result == nil then - utils.error("invalid variable: $(%s)", variable) - utils.abort() + os.raise("invalid variable: $(%s)", variable) end -- ok? diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index e8cf8841e..9afa12f1e 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -232,15 +232,13 @@ function interpreter._api_builtin_add_subdirfiles(self, isdirs, ...) -- done interpreter local ok, errors = xpcall(script, interpreter._traceback) if not ok then - utils.error(errors) - utils.abort() + os.raise(errors) end -- get mtime of the file self._PRIVATE._MTIMES[path.relative(file, self._PRIVATE._ROOTDIR)] = os.mtime(file) else - utils.error(errors) - utils.abort() + os.raise(errors) end end end @@ -740,8 +738,7 @@ function interpreter.api_register_on_script(self, scope_kind, prefix, ...) -- bind script and get new script with sandbox local newscript, errors = sandbox.bind(script, self) if not newscript then - utils.error("on_%s(): %s", name, errors) - utils.abort() + os.raise("on_%s(): %s", name, errors) end -- update script? @@ -791,8 +788,7 @@ function interpreter.api_register_add_keyvalues(self, scope_kind, prefix, ...) -- check count if (count % 2) == 1 then - utils.error("add_%s() values must be key-value pair!", name) - utils.abort() + os.raise("add_%s() values must be key-value pair!", name) end -- done @@ -884,8 +880,7 @@ function interpreter.api_call(self, apiname, ...) -- get api function local apifunc = self._PUBLIC[apiname] if not apifunc then - utils.error("call %s() failed, the api %s not found!", apiname) - utils.abort() + os.raise("call %s() failed, the api %s not found!", apiname) end -- call api function diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 62b8043a0..78542d72f 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -233,5 +233,20 @@ function os.cd(dir) return true end +-- raise an exception and abort the current script +-- +-- the parent function will capture it if we uses pcall or xpcall +-- +function os.raise(msg, ...) + + -- raise it + if msg then + error(string.format(msg, ...)) + else + error() + end +end + + -- return module: os return os diff --git a/xmake/core/base/sandbox.lua b/xmake/core/base/sandbox.lua index 971feeb72..43a483526 100644 --- a/xmake/core/base/sandbox.lua +++ b/xmake/core/base/sandbox.lua @@ -111,8 +111,7 @@ function sandbox._init() -- load module local ok, results = xpcall(script, debug.traceback) if not ok then - utils.error(results) - utils.abort() + os.raise(results) end -- register module diff --git a/xmake/core/base/task.lua b/xmake/core/base/task.lua index 11643ee7e..9225fa7a8 100644 --- a/xmake/core/base/task.lua +++ b/xmake/core/base/task.lua @@ -200,8 +200,7 @@ function task.menu() end else -- errors - utils.error("taskmenu: %s", results) - utils.abort() + os.raise("taskmenu: %s", results) end else table.insert(options_full, option) @@ -243,8 +242,7 @@ function task.menu() local ok, results = task._call(description) if not ok then -- errors - utils.error("taskmenu: %s", results) - utils.abort() + os.raise("taskmenu: %s", results) end -- ok diff --git a/xmake/core/base/template.lua b/xmake/core/base/template.lua index 76f5398c5..b80e38baf 100644 --- a/xmake/core/base/template.lua +++ b/xmake/core/base/template.lua @@ -132,8 +132,7 @@ function template.templates(language) local results, errors = interp:load(templatefile, nil, true, true) if not results then -- trace - utils.error(errors) - utils.abort() + os.raise(errors) end -- save template directory diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua index f0664f294..a73034261 100644 --- a/xmake/core/base/utils.lua +++ b/xmake/core/base/utils.lua @@ -56,16 +56,6 @@ function utils.error(msg, ...) print("error: " .. string.format(msg, ...)) end --- the abort function, will raise an exception --- and the parent function can capture it --- --- so we do not use os.exit() -function utils.abort() - - -- trace - error() -end - -- the warning function function utils.warning(msg, ...) diff --git a/xmake/core/sandbox/import.lua b/xmake/core/sandbox/import.lua index 94b062894..27417a23b 100644 --- a/xmake/core/sandbox/import.lua +++ b/xmake/core/sandbox/import.lua @@ -21,6 +21,7 @@ -- -- load modules +local os = require("base/os") local utils = require("base/utils") -- import module @@ -29,8 +30,7 @@ function sandbox_import(name) -- load module local module = require("sandbox/import/" .. name) if not module then - utils.error("cannot import module: %s", name) - utils.abort() + os.raise("cannot import module: %s", name) end -- get the parent scope @@ -39,8 +39,7 @@ function sandbox_import(name) -- this module has been imported? if rawget(scope_parent, name) then - utils.error("this module: %s has been imported!", name) - utils.abort() + os.raise("this module: %s has been imported!", name) end -- import this module into the parent scope diff --git a/xmake/core/sandbox/os.lua b/xmake/core/sandbox/os.lua index 354e21b01..52945efbc 100644 --- a/xmake/core/sandbox/os.lua +++ b/xmake/core/sandbox/os.lua @@ -45,8 +45,7 @@ function sandbox_os.cp(src, dst) -- done local ok, errors = os.cp(src, dst) if not ok then - utils.error(errors) - utils.abort() + os.raise(errors) end end @@ -64,8 +63,7 @@ function sandbox_os.mv(src, dst) -- done local ok, errors = os.mv(src, dst) if not ok then - utils.error(errors) - utils.abort() + os.raise(errors) end end @@ -82,8 +80,7 @@ function sandbox_os.rm(file_or_dir, emptydir) -- done local ok, errors = os.rm(file_or_dir, emptydir) if not ok then - utils.error(errors) - utils.abort() + os.raise(errors) end end @@ -100,8 +97,7 @@ function sandbox_os.cd(dir) -- done local ok, errors = os.cd(dir) if not ok then - utils.error(errors) - utils.abort() + os.raise(errors) end end @@ -117,8 +113,7 @@ function sandbox_os.mkdir(dir) -- done if not os.mkdir(dir) then - utils.error("create directory: %s failed!", dir) - utils.abort() + os.raise("create directory: %s failed!", dir) end end @@ -135,8 +130,7 @@ function sandbox_os.rmdir(dir) -- done if os.isdir(dir) then if not os.rmdir(dir) then - utils.error("remove directory: %s failed!", dir) - utils.abort() + os.raise("remove directory: %s failed!", dir) end end @@ -154,8 +148,7 @@ function sandbox_os.run(cmd, ...) -- run command if 0 ~= os.execute(cmd .. string.format(" > %s 2>&1", log)) then io.cat(log) - utils.error("run: %s failed!", cmd) - utils.abort() + os.raise("run: %s failed!", cmd) end -- remove the temporary log file @@ -201,6 +194,13 @@ function sandbox_os.isfile(filepath) return os.isfile(filepath) end +-- raise an exception and abort the current script +function sandbox_os.raise(msg, ...) + + -- raise it + os.raise(msg, ...) +end + -- return module return sandbox_os diff --git a/xmake/core/sandbox/raise.lua b/xmake/core/sandbox/raise.lua new file mode 100644 index 000000000..fb1996082 --- /dev/null +++ b/xmake/core/sandbox/raise.lua @@ -0,0 +1,25 @@ +--!The Automatic Cross-platform Build Tool +-- +-- XMake is free software; you can redistribute it and/or modify +-- it under the terms of the GNU Lesser General Public License as published by +-- the Free Software Foundation; either version 2.1 of the License, or +-- (at your option) any later version. +-- +-- XMake is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Lesser General Public License for more details. +-- +-- You should have received a copy of the GNU Lesser General Public License +-- along with XMake; +-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a> +-- +-- Copyright (C) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file raise.lua +-- + +-- load module +return require("sandbox/os").raise + |
