summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2016-03-03 16:45:16 +0800
committerruki <[email protected]>2016-03-03 16:45:16 +0800
commitb331a6e94a0ed82938a626b8aa82d3c8eb6a5199 (patch)
tree9516f518ba302f6d831fdeb4788a38e869af9cc5
parented661e8781d307227b928d8b7f801e4240a017f2 (diff)
add io for sandbox
-rw-r--r--xmake/core/base/io.lua16
-rw-r--r--xmake/core/sandbox/modules/io.lua149
-rw-r--r--xmake/core/sandbox/modules/os.lua14
3 files changed, 160 insertions, 19 deletions
diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua
index a59383d61..685b05bf8 100644
--- a/xmake/core/base/io.lua
+++ b/xmake/core/base/io.lua
@@ -97,22 +97,6 @@ function io._save(file, object)
return io._save_with_level(file, object, 0)
end
--- create directory and open a writable file
-function io.openmk(filepath)
-
- -- check
- assert(filepath)
-
- -- get the file directory
- local dir = path.directory(filepath)
-
- -- ensure the file directory
- if not os.isdir(dir) then os.mkdir(dir) end
-
- -- open it
- return io.open(filepath, "w")
-end
-
-- read all data from file
function io.readall(filepath)
diff --git a/xmake/core/sandbox/modules/io.lua b/xmake/core/sandbox/modules/io.lua
new file mode 100644
index 000000000..5c7d0fc27
--- /dev/null
+++ b/xmake/core/sandbox/modules/io.lua
@@ -0,0 +1,149 @@
+--!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 io.lua
+--
+
+-- load modules
+local io = require("base/io")
+local utils = require("base/utils")
+local raise = require("sandbox/modules/raise")
+local vformat = require("sandbox/modules/vformat")
+
+-- define module
+local sandbox_io = sandbox_io or {}
+
+-- gsub the given file and return replaced data
+function sandbox_io.gsub(filepath, pattern, replace)
+
+ -- check
+ assert(filepath)
+
+ -- format it first
+ filepath = vformat(filepath)
+
+ -- replace all
+ local data, count, errors = io.gsub(filepath, pattern, replace)
+ if not data then
+ raise(errors)
+ end
+
+ -- ok
+ return data, count
+end
+
+-- load object from the given file
+function sandbox_io.load(filepath)
+
+ -- check
+ assert(filepath)
+
+ -- format it first
+ filepath = vformat(filepath)
+
+ -- done
+ local result, errors = io.load(filepath)
+ if not result then
+ raise(errors)
+ end
+
+ -- ok
+ return result
+end
+
+-- save object the the given filepath
+function sandbox_io.save(filepath, object)
+
+ -- check
+ assert(filepath)
+
+ -- format it first
+ filepath = vformat(filepath)
+
+ -- done
+ local ok, errors = io.save(filepath, object)
+ if not ok then
+ raise(errors)
+ end
+end
+
+-- read all data from file
+function sandbox_io.read(filepath)
+
+ -- check
+ assert(filepath)
+
+ -- format it first
+ filepath = vformat(filepath)
+
+ -- done
+ local result, errors = io.read(filepath)
+ if not result then
+ raise(errors)
+ end
+
+ -- ok
+ return result
+end
+
+-- write all data to file
+function sandbox_io.write(filepath, data)
+
+ -- check
+ assert(filepath)
+
+ -- format it first
+ filepath = vformat(filepath)
+
+ -- done
+ local ok, errors = io.write(filepath, data)
+ if not ok then
+ raise(errors)
+ end
+end
+
+-- cat the given file
+function sandbox_io.cat(filepath, linecount)
+
+ -- check
+ assert(filepath)
+
+ -- format it first
+ filepath = vformat(filepath)
+
+ -- cat it
+ io.cat(filepath, linecount)
+end
+
+-- tail the given file
+function sandbox_io.tail(filepath, linecount)
+
+ -- check
+ assert(filepath)
+
+ -- format it first
+ filepath = vformat(filepath)
+
+ -- tail it
+ io.tail(filepath, linecount)
+end
+
+-- return module
+return sandbox_io
+
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index 89e07ca96..857ab0cf8 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -29,9 +29,6 @@ local vformat = require("sandbox/modules/vformat")
-- define module
local sandbox_os = sandbox_os or {}
--- inherit the public interfaces of os
-sandbox_os.curdir = os.curdir
-
-- copy file or directory
function sandbox_os.cp(src, dst)
@@ -136,6 +133,17 @@ function sandbox_os.rmdir(dir)
end
+-- get the current directory
+function sandbox_os.curdir()
+
+ -- get it
+ local curdir = os.curdir()
+ assert(curdir)
+
+ -- ok
+ return curdir
+end
+
-- run shell
function sandbox_os.run(cmd, ...)