diff options
| author | ruki <[email protected]> | 2015-06-20 20:11:59 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2015-06-20 20:11:59 +0800 |
| commit | 8452df53aca437c49963afdf9738022c198365a5 (patch) | |
| tree | 799eb694225028702484301434c8690a1d6efcc8 | |
| parent | cd3922c36588023c8e0ffaeccb2fc2c95b05dd20 (diff) | |
replace the project target name
| -rw-r--r-- | xmake/scripts/base/io.lua | 66 | ||||
| -rw-r--r-- | xmake/templates/c/console/_template.lua | 4 | ||||
| -rw-r--r-- | xmake/templates/c/console/project/xmake.lua | 2 |
3 files changed, 71 insertions, 1 deletions
diff --git a/xmake/scripts/base/io.lua b/xmake/scripts/base/io.lua index c6be077be..9c0ece688 100644 --- a/xmake/scripts/base/io.lua +++ b/xmake/scripts/base/io.lua @@ -113,6 +113,46 @@ function io.openmk(filepath) return io.open(filepath, "w") end +-- read all data from file +function io.readall(filepath) + + -- open file + local file = io.open(filepath, "r") + if not file then + -- error + return nil, string.format("open %s failed!", filepath) + end + + -- read all + local data = file:read("*all") + + -- exit file + file:close() + + -- ok? + return data +end + +-- write all data to file +function io.writall(filepath, data) + + -- open file + local file = io.open(filepath, "w") + if not file then + -- error + return false, string.format("open %s failed!", filepath) + end + + -- write all + file:write(data) + + -- exit file + file:close() + + -- ok? + return true +end + -- save object the the given filepath function io.save(filepath, object) @@ -179,6 +219,32 @@ function io.load(filepath) return result, errors end +-- gsub the given file and return replaced data +function io.gsub(filepath, pattern, replace) + + -- read all data from file + local data, errors = io.readall(filepath) + if not data then return nil, 0, errors end + + -- replace it + local count = 0 + if type(data) == "string" then + data, count = data:gsub(pattern, replace) + else + return nil, 0, string.format("data is not string!") + end + + -- replace ok? + if count ~= 0 then + -- write all data to file + local ok, errors = io.writall(filepath, data) + if not ok then return nil, 0, errors end + end + + -- ok + return data, count +end + -- cat the given file function io.cat(filepath, linecount) diff --git a/xmake/templates/c/console/_template.lua b/xmake/templates/c/console/_template.lua index e1475e286..8a1668fbf 100644 --- a/xmake/templates/c/console/_template.lua +++ b/xmake/templates/c/console/_template.lua @@ -24,6 +24,7 @@ local _template = _template or {} -- load modules +local io = require("base/io") local os = require("base/os") local path = require("base/path") local utils = require("base/utils") @@ -37,6 +38,9 @@ function _template.done(targetname, projectdir) -- check assert(targetname and projectdir) + -- replace the target name + io.gsub(projectdir .. "/xmake.lua", "%[targetname%]", targetname) + -- ok return true end diff --git a/xmake/templates/c/console/project/xmake.lua b/xmake/templates/c/console/project/xmake.lua index d4f8fd029..30aca7364 100644 --- a/xmake/templates/c/console/project/xmake.lua +++ b/xmake/templates/c/console/project/xmake.lua @@ -22,7 +22,7 @@ if modes("release") then end -- add target -add_target("demo") +add_target("[targetname]") -- set kind set_kind("binary") |
