From ef4fe60273da742248fe9111e9141df51185c204 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 11 Jun 2015 15:08:30 +0800 Subject: open new file and create directory --- xmake/scripts/base/io.lua | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'xmake/scripts/base/io.lua') diff --git a/xmake/scripts/base/io.lua b/xmake/scripts/base/io.lua index 7be2d6d3a..db7c2b934 100644 --- a/xmake/scripts/base/io.lua +++ b/xmake/scripts/base/io.lua @@ -24,6 +24,7 @@ local io = io or {} -- load modules +local path = require("base/path") local utils = require("base/utils") -- save object with the level @@ -96,11 +97,27 @@ 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 + -- save object the the given filepath function io.save(filepath, object) -- open the file - local file = io.open(filepath, "w") + local file = io.openmk(filepath) if not file then -- error return false, string.format("open %s failed!", filepath) -- cgit v1.3.1