diff options
| author | ruki <[email protected]> | 2016-03-18 10:22:32 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2016-03-18 10:22:32 +0800 |
| commit | bbb0873d55375f69edd6c54a9e128bada3878379 (patch) | |
| tree | ae51be862246da213673928ed5932c1116082172 | |
| parent | 2b233ade0aba7cb061d7b576357a462910c4b62f (diff) | |
hook io file
| -rwxr-xr-x | xmake/actions/config/config_h.lua | 32 | ||||
| -rw-r--r-- | xmake/core/base/io.lua | 82 |
2 files changed, 96 insertions, 18 deletions
diff --git a/xmake/actions/config/config_h.lua b/xmake/actions/config/config_h.lua index 1cf3d8c38..9d3c25494 100755 --- a/xmake/actions/config/config_h.lua +++ b/xmake/actions/config/config_h.lua @@ -39,24 +39,24 @@ function _make_for_target(files, targetname, target) local file = files[config_h] or io.open(config_h, "w") -- make the head - if files[config_h] then file:write("\n") end - file:write(format("#ifndef %s_H\n", prefix)) - file:write(format("#define %s_H\n", prefix)) - file:write("\n") + if files[config_h] then file:print("") end + file:print("#ifndef %s_H", prefix) + file:print("#define %s_H", prefix) + file:print("") -- make version if target.version then - file:write("// version\n") - file:write(format("#define %s_VERSION \"%s\"\n", prefix, target.version)) + file:print("// version") + file:print("#define %s_VERSION \"%s\"", prefix, target.version) local i = 1 local m = {"MAJOR", "MINOR", "ALTER"} for v in target.version:gmatch("%d+") do - file:write(format("#define %s_VERSION_%s %s\n", prefix, m[i], v)) + file:print("#define %s_VERSION_%s %s", prefix, m[i], v) i = i + 1 if i > 3 then break end end - file:write(format("#define %s_VERSION_BUILD %s\n", prefix, os.date("%Y%m%d%H%M", os.time()))) - file:write("\n") + file:print("#define %s_VERSION_BUILD %s", prefix, os.date("%Y%m%d%H%M", os.time())) + file:print("") end -- make the defines @@ -84,24 +84,24 @@ function _make_for_target(files, targetname, target) -- make the defines if #defines ~= 0 then - file:write("// defines\n") + file:print("// defines") for _, define in ipairs(defines) do - file:write(format("#define %s 1\n", define:gsub("=", " "):gsub("%$%((.-)%)", function (w) if w == "prefix" then return prefix end end))) + file:print("#define %s 1", define:gsub("=", " "):gsub("%$%((.-)%)", function (w) if w == "prefix" then return prefix end end)) end - file:write("\n") + file:print("") end -- make the undefines if #undefines ~= 0 then - file:write("// undefines\n") + file:print("// undefines") for _, undefine in ipairs(undefines) do - file:write(format("#undef %s\n", undefine:gsub("%$%((.-)%)", function (w) if w == "prefix" then return prefix end end))) + file:print("#undef %s", undefine:gsub("%$%((.-)%)", function (w) if w == "prefix" then return prefix end end)) end - file:write("\n") + file:print("") end -- make the tail - file:write("#endif\n") + file:print("#endif") -- cache the file files[config_h] = file diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua index 26c9163e2..0f884db54 100644 --- a/xmake/core/base/io.lua +++ b/xmake/core/base/io.lua @@ -21,7 +21,8 @@ -- -- define module -local io = io or {} +local io = io or {} +local _file = _file or {} -- load modules local path = require("base/path") @@ -30,6 +31,66 @@ local utils = require("base/utils") -- save original open io._open = io._open or io.open +-- read file +function _file.read(self, ...) + + -- check + assert(self._FILE) + + -- read it + return self._FILE:read(...) +end + +-- write file +function _file.write(self, ...) + + -- check + assert(self._FILE) + + -- write it + return self._FILE:write(...) +end + +-- print file +function _file.print(self, ...) + + -- check + assert(self._FILE) + + -- print it + return self._FILE:write(string.format(...) .. "\n") +end + +-- printf file +function _file.printf(self, ...) + + -- check + assert(self._FILE) + + -- printf it + return self._FILE:write(string.format(...)) +end + +-- get lines +function _file.lines(self) + + -- check + assert(self._FILE) + + -- get it + return self._FILE:lines() +end + +-- close file +function _file.close(self) + + -- check + assert(self._FILE) + + -- close it + return self._FILE:close() +end + -- save object with the level function io._save_with_level(file, object, level) @@ -158,8 +219,25 @@ function io.open(filepath, mode) end end + -- init file instance + local file = {} + for k, v in pairs(_file) do + if type(v) == "function" then + file[k] = v + end + end + -- open it - return io._open(path.translate(filepath), mode) + local handle, errors = io._open(path.translate(filepath), mode) + if not handle then + return nil, errors + end + + -- save file handle + file._FILE = handle + + -- ok? + return file, errors end -- save object the the given filepath |
