summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2016-03-29 10:56:16 +0800
committerruki <[email protected]>2016-03-29 10:56:38 +0800
commit6e2aa359d0636c0224cccee04723408d9a3ae046 (patch)
tree3b0607a30f2116fcfca7f00c6c3108d0d43b9389
parent7edb31caaa3d1a0d58737324b6af85cb3b2d2c75 (diff)
add trycatch and remove on_failure for task
-rw-r--r--xmake/core/project/task.lua18
-rw-r--r--xmake/core/sandbox/modules/catch.lua31
-rw-r--r--xmake/core/sandbox/modules/finally.lua31
-rw-r--r--xmake/core/sandbox/modules/import/core/platform/tool.lua4
-rw-r--r--xmake/core/sandbox/modules/import/core/project/task.lua56
-rw-r--r--xmake/core/sandbox/modules/try.lua115
-rw-r--r--xmake/core/sandbox/sandbox.lua4
7 files changed, 240 insertions, 19 deletions
diff --git a/xmake/core/project/task.lua b/xmake/core/project/task.lua
index b4690c027..ee68c92c1 100644
--- a/xmake/core/project/task.lua
+++ b/xmake/core/project/task.lua
@@ -169,11 +169,10 @@ function task._interpreter()
-- register api: set_task_menu()
interp:api_register_set_values("task", "task", "menu")
- -- register api: on_task_run(), on_task_before(), on_task_after(), on_task_failure()
+ -- register api: on_task_run(), on_task_before(), on_task_after()
interp:api_register_on_script("task", "task", "run"
, "after"
- , "before"
- , "failure")
+ , "before")
-- set filter
interp:filter_set(filter.init(function (variable)
@@ -402,19 +401,6 @@ function task.run(name)
end
end
- -- run on_failure if be failed?
- if not ok and taskinfo.failure then
-
- -- call it in the sandbox
- ok, errors = sandbox.load(taskinfo.failure)
- if not ok then
- utils.error(errors)
- end
-
- -- failed
- ok = false
- end
-
-- ok?
return ok
end
diff --git a/xmake/core/sandbox/modules/catch.lua b/xmake/core/sandbox/modules/catch.lua
new file mode 100644
index 000000000..4f7b5a893
--- /dev/null
+++ b/xmake/core/sandbox/modules/catch.lua
@@ -0,0 +1,31 @@
+--!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 catch.lua
+--
+
+-- catch
+function sandbox_catch(block)
+
+ -- get the catch block function
+ return {catch = block[1]}
+end
+
+-- return module
+return sandbox_catch
diff --git a/xmake/core/sandbox/modules/finally.lua b/xmake/core/sandbox/modules/finally.lua
new file mode 100644
index 000000000..3ec05bf26
--- /dev/null
+++ b/xmake/core/sandbox/modules/finally.lua
@@ -0,0 +1,31 @@
+--!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 finally.lua
+--
+
+-- finally
+function sandbox_finally(block)
+
+ -- get the finally block function
+ return {finally = block[1]}
+end
+
+-- return module
+return sandbox_finally
diff --git a/xmake/core/sandbox/modules/import/core/platform/tool.lua b/xmake/core/sandbox/modules/import/core/platform/tool.lua
index 389400167..cc7811e21 100644
--- a/xmake/core/sandbox/modules/import/core/platform/tool.lua
+++ b/xmake/core/sandbox/modules/import/core/platform/tool.lua
@@ -48,7 +48,9 @@ function sandbox_core_platform_tool.run(name, ...)
end
-- run it
- return instance:main(...)
+ if not instance:main(...) then
+ raise("run tool: %s failed!", name)
+ end
end
-- return module
diff --git a/xmake/core/sandbox/modules/import/core/project/task.lua b/xmake/core/sandbox/modules/import/core/project/task.lua
new file mode 100644
index 000000000..18d595ac7
--- /dev/null
+++ b/xmake/core/sandbox/modules/import/core/project/task.lua
@@ -0,0 +1,56 @@
+--!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 task.lua
+--
+
+-- define module
+local sandbox_core_project_task = sandbox_core_project_task or {}
+
+-- load modules
+local os = require("base/os")
+local io = require("base/io")
+local string = require("base/string")
+local task = require("project/task")
+local raise = require("sandbox/modules/raise")
+
+-- run the given task
+function sandbox_core_project_task.run(taskname, args)
+
+ -- make command
+ local cmd = "xmake " .. (taskname or "")
+ for name, value in pairs(args) do
+ cmd = string.format("%s --%s=%s", cmd, name, tostring(value))
+ end
+
+ -- make temporary log file
+ log = os.tmpname()
+
+ -- run command
+ if 0 ~= os.execute(cmd .. string.format(" > %s 2>&1", log)) then
+ io.cat(log)
+ os.raise("run: %s failed!", taskname or cmd)
+ end
+
+ -- remove the temporary log file
+ os.rm(log)
+end
+
+-- return module
+return sandbox_core_project_task
diff --git a/xmake/core/sandbox/modules/try.lua b/xmake/core/sandbox/modules/try.lua
new file mode 100644
index 000000000..3e21a39df
--- /dev/null
+++ b/xmake/core/sandbox/modules/try.lua
@@ -0,0 +1,115 @@
+--!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 try.lua
+--
+
+-- load modules
+local table = require("base/table")
+local string = require("base/string")
+local option = require("base/option")
+
+-- define module
+local sandbox_try = sandbox_try or {}
+
+-- traceback
+function sandbox_try._traceback(errors)
+
+ -- not verbose?
+ if not option.get("verbose") then
+ if errors then
+ -- remove the prefix info
+ local _, pos = errors:find(":%d+: ")
+ if pos then
+ return errors:sub(pos + 1)
+ end
+ end
+ return errors
+ end
+
+ -- init results
+ local results = ""
+ if errors then
+ results = errors .. "\n"
+ end
+ results = results .. "stack traceback:\n"
+
+ -- make results
+ local level = 2
+ while true do
+
+ -- get debug info
+ local info = debug.getinfo(level, "Sln")
+
+ -- end?
+ if not info or (info.name and info.name == "xpcall") then
+ break
+ end
+
+ -- function?
+ if info.what == "C" then
+ results = results .. string.format(" [C]: in function '%s'\n", info.name)
+ elseif info.name then
+ results = results .. string.format(" [%s:%d]: in function '%s'\n", info.short_src, info.currentline, info.name)
+ elseif info.what == "main" then
+ results = results .. string.format(" [%s:%d]: in main chunk\n", info.short_src, info.currentline)
+ break
+ else
+ results = results .. string.format(" [%s:%d]:\n", info.short_src, info.currentline)
+ end
+
+ -- next
+ level = level + 1
+ end
+
+ -- ok?
+ return results
+end
+
+-- try
+function sandbox_try.try(block)
+
+ -- get the try function
+ local try = block[1]
+ assert(try)
+
+ -- get catch and finally functions
+ local funcs = block[2]
+ if funcs and block[3] then
+ table.join2(funcs, block[2])
+ end
+
+ -- try to call it
+ local ok, errors = xpcall(try, sandbox_try._traceback)
+ if not ok then
+
+ -- run the catch function
+ if funcs and funcs.catch then
+ funcs.catch(errors)
+ end
+ end
+
+ -- run the finally function
+ if funcs and funcs.finally then
+ funcs.finally()
+ end
+end
+
+-- return module
+return sandbox_try.try
diff --git a/xmake/core/sandbox/sandbox.lua b/xmake/core/sandbox/sandbox.lua
index 637c69ce9..5e457d590 100644
--- a/xmake/core/sandbox/sandbox.lua
+++ b/xmake/core/sandbox/sandbox.lua
@@ -20,7 +20,7 @@
-- @file sandbox.lua
--
--- define module: sandbox
+-- define module
local sandbox = sandbox or {}
-- load modules
@@ -307,5 +307,5 @@ function sandbox.instance()
return instance
end
--- return module: sandbox
+-- return module
return sandbox