summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-03-28 21:58:52 +0800
committerruki <[email protected]>2017-03-28 21:58:52 +0800
commit8e0c711fb2ea0fb3a54b9f2b41d1907fc4be8b02 (patch)
treec9f7fdc3dc1ea9155e8fd01e57b66c71f0abaf72
parent6672a212d0ea74c3d6c6425b58b4fbb1dea7999c (diff)
add sudo interface
-rw-r--r--xmake/actions/install/main.lua19
-rw-r--r--xmake/actions/uninstall/main.lua25
-rw-r--r--xmake/core/base/os.lua12
-rw-r--r--xmake/core/sandbox/modules/os.lua73
4 files changed, 79 insertions, 50 deletions
diff --git a/xmake/actions/install/main.lua b/xmake/actions/install/main.lua
index 5d4e33179..4b47ff7f0 100644
--- a/xmake/actions/install/main.lua
+++ b/xmake/actions/install/main.lua
@@ -40,6 +40,7 @@ function main()
try
{
function ()
+
-- install target
install.install(targetname)
@@ -52,25 +53,11 @@ function main()
-- failed or not permission? request administrator permission and install it again
function (errors)
- -- init argv
- local argv = {"xmake", "lua"}
- for _, name in ipairs({"file", "project", "backtrace", "verbose", "quiet"}) do
- local value = option.get(name)
- if type(value) == "string" then
- table.insert(argv, "--" .. name .. "=" .. value)
- elseif value then
- table.insert(argv, "--" .. name)
- end
- end
- table.insert(argv, path.join(os.scriptdir(), "install_admin.lua"))
- table.insert(argv, targetname)
- table.insert(argv, option.get("installdir"))
-
-- show tips
cprint("${bright red}error: ${default red}installation failed, may permission denied!")
-- continue to install with administrator permission?
- if os.sudo() then
+ if os.feature("sudo") then
-- show tips
cprint("${bright yellow}note: ${default yellow}try continue to install with administrator permission again?")
@@ -81,7 +68,7 @@ function main()
if io.read() == 'y' then
-- install target with administrator permission
- os.runv(os.sudo(), argv)
+ os.sudol(os.runv, path.join(os.scriptdir(), "install_admin.lua"), {targetname, option.get("installdir")})
-- trace
cprint("${bright}install ok!${clear}${ok_hand}")
diff --git a/xmake/actions/uninstall/main.lua b/xmake/actions/uninstall/main.lua
index 724621a3e..bc57156ac 100644
--- a/xmake/actions/uninstall/main.lua
+++ b/xmake/actions/uninstall/main.lua
@@ -40,8 +40,9 @@ function main()
try
{
function ()
+
-- uninstall target
- install.uninstall(targetname)
+ uninstall.uninstall(targetname)
-- trace
cprint("${bright}uninstall ok!${clear}${ok_hand}")
@@ -52,25 +53,11 @@ function main()
-- failed or not permission? request administrator permission and uninstall it again
function (errors)
- -- init argv
- local argv = {"xmake", "lua"}
- for _, name in ipairs({"file", "project", "backtrace", "verbose", "quiet"}) do
- local value = option.get(name)
- if type(value) == "string" then
- table.insert(argv, "--" .. name .. "=" .. value)
- elseif value then
- table.insert(argv, "--" .. name)
- end
- end
- table.insert(argv, path.join(os.scriptdir(), "uninstall_admin.lua"))
- table.insert(argv, targetname)
- table.insert(argv, option.get("installdir"))
-
-- show tips
cprint("${bright red}error: ${default red}failed to uninstall, may permission denied!")
- -- continue to install with administrator permission?
- if os.sudo() then
+ -- continue to uninstall with administrator permission?
+ if os.feature("sudo") then
-- show tips
cprint("${bright yellow}note: ${default yellow}try continue to uninstall with administrator permission again?")
@@ -80,8 +67,8 @@ function main()
io.flush()
if io.read() == 'y' then
- -- install target with administrator permission
- os.runv(os.sudo(), argv)
+ -- uninstall target with administrator permission
+ os.sudol(os.runv, path.join(os.scriptdir(), "uninstall_admin.lua"), {targetname, option.get("installdir")})
-- trace
cprint("${bright}uninstall ok!${clear}${ok_hand}")
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 542751d54..5fb51b194 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -590,17 +590,5 @@ function os.isexec(filepath)
return os.isfile(filepath)
end
--- get sudo program name for running program with administrator permission
-function os.sudo()
-
- -- on windows?
- if xmake._HOST == "windows" then
- -- TODO
- -- add sudo.bat
- else
- return "sudo"
- end
-end
-
-- return module
return os
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index 15f75ae08..94b78c6b8 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -26,6 +26,7 @@
local io = require("base/io")
local os = require("base/os")
local utils = require("base/utils")
+local option = require("base/option")
local sandbox = require("sandbox/sandbox")
local vformat = require("sandbox/modules/vformat")
@@ -41,6 +42,18 @@ sandbox_os.mtime = os.mtime
sandbox_os.mclock = os.mclock
sandbox_os.emptydir = os.emptydir
+-- get sudo program name for running program with administrator permission
+function sandbox_os._sudoname()
+
+ -- on windows?
+ if xmake._HOST == "windows" then
+ -- TODO
+ -- add sudo.bat
+ else
+ return "sudo"
+ end
+end
+
-- copy file or directory
function sandbox_os.cp(...)
@@ -445,9 +458,63 @@ function sandbox_os.uuid(name)
return uuid
end
--- get sudo program name for running program with administrator permission
-function sandbox_os.sudo()
- return os.sudo()
+-- get the feature of os
+function sandbox_os.feature(name)
+
+ -- has 'sudo' feature?
+ if name == "sudo" then
+ return sandbox_os._sudoname() ~= nil
+ end
+end
+
+-- sudo run shell with administrator permission
+--
+-- .e.g
+-- os.sudo(os.run, "echo", "hello xmake!")
+--
+function sandbox_os.sudo(runner, cmd, ...)
+
+ -- check
+ assert(sandbox_os.feature("sudo"), "no sudo!")
+
+ -- run it with administrator permission
+ runner(sandbox_os._sudoname() .. " " .. cmd, ...)
+end
+
+-- sudo run shell with administrator permission and arguments list
+--
+-- .e.g
+-- os.sudov(os.runv, {"echo", "hello xmake!"})
+--
+function sandbox_os.sudov(runner, shellname, argv)
+
+ -- check
+ assert(sandbox_os.feature("sudo"), "no sudo!")
+
+ -- run it with administrator permission
+ runner(sandbox_os._sudoname(), table.join(shellname, argv))
+end
+
+-- sudo run lua script with administrator permission and arguments list
+--
+-- .e.g
+-- os.sudol(os.runv, "xxx.lua", {"arg1", "arg2"})
+--
+function sandbox_os.sudol(runner, luafile, luaargv)
+
+ -- init argv
+ local argv = {"lua"}
+ for _, name in ipairs({"file", "project", "backtrace", "verbose", "quiet"}) do
+ local value = option.get(name)
+ if type(value) == "string" then
+ table.insert(argv, "--" .. name .. "=" .. value)
+ elseif value then
+ table.insert(argv, "--" .. name)
+ end
+ end
+
+ -- run it with administrator permission
+ sandbox_os.sudov(runner, "xmake", table.join(argv, luafile, luaargv))
end
-- return module