summaryrefslogtreecommitdiff
path: root/xmake/modules
diff options
context:
space:
mode:
authorruki <[email protected]>2017-05-24 11:45:37 +0800
committerruki <[email protected]>2017-05-24 11:45:37 +0800
commit4e52205dc696ade5ba01c8d6ccf9831b59ad6196 (patch)
tree2a2e2065ed0f65b019168d2e3f099363d4b96a26 /xmake/modules
parent0041140dbfebce83293bcd3991760c8651f7a7ee (diff)
add privilege.sudo and find sudo modules
Diffstat (limited to 'xmake/modules')
-rw-r--r--xmake/modules/detect/tool/find_sudo.lua55
-rw-r--r--xmake/modules/privilege/sudo.lua146
2 files changed, 201 insertions, 0 deletions
diff --git a/xmake/modules/detect/tool/find_sudo.lua b/xmake/modules/detect/tool/find_sudo.lua
new file mode 100644
index 000000000..32bd86456
--- /dev/null
+++ b/xmake/modules/detect/tool/find_sudo.lua
@@ -0,0 +1,55 @@
+--!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_sudo.lua
+--
+
+-- imports
+import("lib.detect.find_program")
+import("lib.detect.find_programver")
+
+-- find sudo
+--
+-- @param opt the argument options, .e.g {version = true}
+--
+-- @return program, version
+--
+-- @code
+--
+-- local sudo = find_sudo()
+-- local sudo, version = find_sudo({version = true})
+--
+-- @endcode
+--
+function main(opt)
+
+ -- find program
+ local program = find_program("sudo", { "/usr/bin", "/usr/local/bin"})
+
+ -- find program version
+ local version = nil
+ if program and opt and opt.version then
+ version = find_programver(program)
+ end
+
+ -- ok?
+ return program, version
+end
diff --git a/xmake/modules/privilege/sudo.lua b/xmake/modules/privilege/sudo.lua
new file mode 100644
index 000000000..aba3a0362
--- /dev/null
+++ b/xmake/modules/privilege/sudo.lua
@@ -0,0 +1,146 @@
+--!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 sudo.lua
+--
+
+-- imports
+import("core.base.option")
+import("detect.tool.find_sudo")
+
+-- sudo run shell with administrator permission
+--
+-- .e.g
+-- _sudo(os.run, "echo", "hello xmake!")
+--
+function _sudo(runner, cmd, ...)
+
+ -- find sudo
+ local program = find_sudo()
+ assert(program, "sudo not found!")
+
+ -- TODO handle -E on ubuntu
+
+ -- run it with administrator permission and preserve parent environment
+ runner(program .. " -E " .. cmd, ...)
+end
+
+-- sudo run shell with administrator permission and arguments list
+--
+-- .e.g
+-- _sudov(os.runv, {"echo", "hello xmake!"})
+--
+function _sudov(runner, shellname, argv)
+
+ -- find sudo
+ local program = find_sudo()
+ assert(program, "sudo not found!")
+
+ -- run it with administrator permission and preserve parent environment
+ runner(program, table.join("-E", shellname, argv))
+end
+
+-- sudo run lua script with administrator permission and arguments list
+--
+-- .e.g
+-- _lua(os.runv, "xxx.lua", {"arg1", "arg2"})
+--
+function _lua(runner, luafile, luaargv)
+
+ -- init argv
+ local argv = {"lua", "--root"}
+ 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
+ _sudov(runner, "xmake", table.join(argv, luafile, luaargv))
+end
+
+-- has sudo?
+function has()
+ return find_sudo() ~= nil
+end
+
+-- sudo run shell
+function run(cmd, ...)
+ return _sudo(os.run, cmd, ...)
+end
+
+-- sudo run shell with arguments list
+function runv(shellname, argv)
+ return _sudo(os.run, shellname, argv)
+end
+
+-- sudo quietly run shell and echo verbose info if [-v|--verbose] option is enabled
+function vrun(cmd, ...)
+ return _sudo(os.vrun, cmd, ...)
+end
+
+-- sudo quietly run shell with arguments list and echo verbose info if [-v|--verbose] option is enabled
+function vrunv(shellname, argv)
+ return _sudo(os.vrunv, shellname, argv)
+end
+
+-- sudo run shell and return output and error data
+function iorun(cmd, ...)
+ return _sudo(os.iorun, cmd, ...)
+end
+
+-- sudo run shell and return output and error data
+function iorunv(shellname, argv)
+ return _sudo(os.iorunv, shellname, argv)
+end
+
+-- sudo execute shell
+function exec(cmd, ...)
+ return _sudo(os.exec, cmd, ...)
+end
+
+-- sudo execute shell with arguments list
+function execv(shellname, argv)
+ return _sudo(os.execv, shellname, argv)
+end
+
+-- sudo run lua script
+function runl(luafile, luaargv)
+ return _lua(os.runv, luafile, luaargv)
+end
+
+-- sudo quietly run lua script and echo verbose info if [-v|--verbose] option is enabled
+function vrunl(luafile, luaargv)
+ return _lua(os.vrunv, luafile, luaargv)
+end
+
+-- sudo run lua script and return output and error data
+function iorunl(luafile, luaargv)
+ return _lua(os.iorunv, luafile, luaargv)
+end
+
+-- sudo execute lua script
+function execl(luafile, luaargv)
+ return _lua(os.execv, luafile, luaargv)
+end