summaryrefslogtreecommitdiff
path: root/xmake
diff options
context:
space:
mode:
authorruki <[email protected]>2017-02-08 10:30:48 +0800
committerruki <[email protected]>2017-02-08 10:30:48 +0800
commit0dd09d0248bb07ca897d666ff13feaae19a3d93e (patch)
tree96cb83fb812b0e995a34cbf0ed562e8f3c5b471a /xmake
parent495724f2c5c22501520de05715f6ef18f2bd484d (diff)
add os.argv
Diffstat (limited to 'xmake')
-rw-r--r--xmake/core/base/os.lua93
-rw-r--r--xmake/core/base/table.lua111
2 files changed, 81 insertions, 123 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 1b5c31736..f49346d55 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -28,6 +28,7 @@ local os = os or {}
-- load modules
local io = require("base/io")
local path = require("base/path")
+local table = require("base/table")
local utils = require("base/utils")
local string = require("base/string")
@@ -265,28 +266,14 @@ end
-- run shell
function os.run(cmd)
- -- make temporary log file
- local log = os.tmpfile()
-
- -- execute it
- local ok = os.exec(cmd, log, log)
- if ok ~= 0 then
-
- -- make errors
- local errors = io.readall(log)
-
- -- remove the temporary log file
- os.rm(log)
-
- -- failed
- return false, errors
+ -- parse arguments
+ local argv = os.argv(cmd)
+ if not argv or #argv <= 0 then
+ return false, string.format("invalid command: %s", cmd)
end
- -- remove the temporary log file
- os.rm(log)
-
- -- ok
- return true
+ -- run it
+ return os.runv(argv[1], table.slice(argv, 2))
end
-- run shell with arguments list
@@ -319,45 +306,14 @@ end
-- execute shell
function os.exec(cmd, outfile, errfile)
- -- open command
- local ok = -1
- local proc = process.open(cmd, outfile, errfile)
- if proc ~= nil then
-
- -- wait process
- local waitok = -1
- local status = -1
- if coroutine.running() then
-
- -- save the current directory
- local curdir = os.curdir()
-
- -- wait it
- repeat
- -- poll it
- waitok, status = process.wait(proc, 0)
- if waitok == 0 then
- waitok, status = coroutine.yield(proc)
- end
- until waitok ~= 0
-
- -- resume the current directory
- os.cd(curdir)
- else
- waitok, status = process.wait(proc, -1)
- end
-
- -- get status
- if waitok > 0 then
- ok = status
- end
-
- -- close process
- process.close(proc)
+ -- parse arguments
+ local argv = os.argv(cmd)
+ if not argv or #argv <= 0 then
+ return -1
end
- -- ok?
- return ok
+ -- run it
+ return os.execv(argv[1], table.slice(argv, 2), outfile, errfile)
end
-- execute shell with arguments list
@@ -407,23 +363,14 @@ end
-- run shell and return output and error data
function os.iorun(cmd)
- -- make temporary output and error file
- local outfile = os.tmpfile()
- local errfile = os.tmpfile()
-
- -- run command
- local ok = os.exec(cmd, outfile, errfile)
-
- -- get output and error data
- local outdata = io.readall(outfile)
- local errdata = io.readall(errfile)
-
- -- remove the temporary output and error file
- os.rm(outfile)
- os.rm(errfile)
+ -- parse arguments
+ local argv = os.argv(cmd)
+ if not argv or #argv <= 0 then
+ return false, string.format("invalid command: %s", cmd)
+ end
- -- ok?
- return ok == 0, outdata, errdata
+ -- run it
+ return os.iorunv(argv[1], table.slice(argv, 2))
end
-- run shell with arguments and return output and error data
diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua
index 6c37ae219..3767fc1c4 100644
--- a/xmake/core/base/table.lua
+++ b/xmake/core/base/table.lua
@@ -25,6 +25,18 @@
-- define module: table
local table = table or {}
+-- clear the table
+function table.clear(self)
+
+ -- check
+ assert(self and type(self) == "table")
+
+ -- clear it
+ for k in next, self do
+ rawset(self, k, nil)
+ end
+end
+
-- join all objects and tables
function table.join(...)
@@ -69,18 +81,6 @@ function table.join2(self, ...)
return self
end
--- clear the table
-function table.clear(self)
-
- -- check
- assert(self and type(self) == "table")
-
- -- clear it
- for k in next, self do
- rawset(self, k, nil)
- end
-end
-
-- copy the table to self
function table.copy(copied)
@@ -116,6 +116,55 @@ function table.copy2(self, copied)
end
+-- inherit interfaces and create a new instance
+function table.inherit(...)
+
+ -- init instance
+ local classes = {...}
+ local instance = {}
+ for _, clasz in ipairs(classes) do
+ for k, v in pairs(clasz) do
+ if type(v) == "function" then
+ instance[k] = v
+ end
+ end
+ end
+
+ -- ok?
+ return instance
+end
+
+-- inherit interfaces from the given class
+function table.inherit2(self, ...)
+
+ -- check
+ assert(self)
+
+ -- init instance
+ local classes = {...}
+ for _, clasz in ipairs(classes) do
+ for k, v in pairs(clasz) do
+ if type(v) == "function" and self[k] == nil then
+ self[k] = v
+ end
+ end
+ end
+
+ -- ok?
+ return self
+end
+
+-- slice table array
+function table.slice(self, first, last, step)
+
+ -- slice it
+ local sliced = {}
+ for i = first or 1, last or #self, step or 1 do
+ sliced[#sliced + 1] = self[i]
+ end
+ return sliced
+end
+
-- is array?
function table.is_array(self)
@@ -321,43 +370,5 @@ function table.unique(array)
return array
end
--- inherit interfaces and create a new instance
-function table.inherit(...)
-
- -- init instance
- local classes = {...}
- local instance = {}
- for _, clasz in ipairs(classes) do
- for k, v in pairs(clasz) do
- if type(v) == "function" then
- instance[k] = v
- end
- end
- end
-
- -- ok?
- return instance
-end
-
--- inherit interfaces from the given class
-function table.inherit2(self, ...)
-
- -- check
- assert(self)
-
- -- init instance
- local classes = {...}
- for _, clasz in ipairs(classes) do
- for k, v in pairs(clasz) do
- if type(v) == "function" and self[k] == nil then
- self[k] = v
- end
- end
- end
-
- -- ok?
- return self
-end
-
-- return module: table
return table