summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-09-29 00:35:13 +0800
committerruki <[email protected]>2020-09-29 00:35:13 +0800
commit675fc8fca1e62f24e505523eb1b8e0523a9ffb94 (patch)
tree61da761bba308ade0ce8455c674e792822c71c86
parentd662c54d57accd1382c2368e3c7f27832d9ef10d (diff)
improve cmake/install and add string.replace
-rw-r--r--tests/modules/string/test.lua11
-rw-r--r--xmake/core/base/io.lua27
-rw-r--r--xmake/core/base/string.lua15
-rw-r--r--xmake/core/sandbox/modules/io.lua17
-rw-r--r--xmake/modules/package/tools/cmake.lua25
5 files changed, 87 insertions, 8 deletions
diff --git a/tests/modules/string/test.lua b/tests/modules/string/test.lua
index 09a0f5a3e..3f52dc593 100644
--- a/tests/modules/string/test.lua
+++ b/tests/modules/string/test.lua
@@ -95,3 +95,14 @@ function test_lastof(t)
t:are_equal(("/home/file.txt"):lastof('/home', true), 1)
t:are_equal(("/home/file.txt"):lastof('[/\\]home'), 1)
end
+
+function test_replace(t)
+ t:are_equal(("123xyz456xyz789xyz"):replace("123", "000"), "000xyz456xyz789xyz")
+ t:are_equal(("123xyz456xyz789xyz"):replace("xyz", "000"), "123000456000789000")
+ t:are_equal(("123xyz456xyz789xyz"):replace("123", "000", {plain = true}), "000xyz456xyz789xyz")
+ t:are_equal(("123xyz456xyz789xyz"):replace("xyz", "000", {plain = true}), "123000456000789000")
+ t:are_equal(("123$xyz456xyz789xyz"):replace("123$", "000"), "123$xyz456xyz789xyz")
+ t:are_equal(("123xyz$456xyz$789xyz$"):replace("xyz$", "000"), "123xyz$456xyz$789xyz$")
+ t:are_equal(("123$xyz456xyz789xyz"):replace("123$", "000", {plain = true}), "000xyz456xyz789xyz")
+ t:are_equal(("123xyz$456xyz$789xyz$"):replace("xyz$", "000", {plain = true}), "123000456000789000")
+end
diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua
index 001b659ea..c18422a51 100644
--- a/xmake/core/base/io.lua
+++ b/xmake/core/base/io.lua
@@ -678,8 +678,33 @@ function io.gsub(filepath, pattern, replace, opt)
local ok, errors = io.writefile(filepath, data, opt)
if not ok then return nil, 0, errors end
end
+ return data, count
+end
- -- ok
+-- replace text of the given file and return replaced data
+function io.replace(filepath, pattern, replace, opt)
+
+ -- init option
+ opt = opt or {}
+
+ -- read all data from file
+ local data, errors = io.readfile(filepath, opt)
+ if not data then return nil, 0, errors end
+
+ -- replace it
+ local count = 0
+ if type(data) == "string" then
+ data, count = data:replace(pattern, replace, opt)
+ 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.writefile(filepath, data, opt)
+ if not ok then return nil, 0, errors end
+ end
return data, count
end
diff --git a/xmake/core/base/string.lua b/xmake/core/base/string.lua
index 9adf9e748..3a4eab821 100644
--- a/xmake/core/base/string.lua
+++ b/xmake/core/base/string.lua
@@ -137,6 +137,21 @@ function string:decode()
return (self:gsub("%%(%x%x)", function (w) return string.char(tonumber(w, 16)) end))
end
+-- replace text
+function string:replace(old, new, opt)
+ if opt and opt.plain then
+ local b, e = self:find(old, 1, true)
+ if b == nil then
+ return self, 0
+ else
+ local str, count = self:sub(e + 1):replace(old, new, opt)
+ return (self:sub(1, b - 1) .. new .. str), count + 1
+ end
+ else
+ return self:gsub(old, new)
+ end
+end
+
-- try to format
function string.tryformat(format, ...)
diff --git a/xmake/core/sandbox/modules/io.lua b/xmake/core/sandbox/modules/io.lua
index 6a65d11a3..c01df2214 100644
--- a/xmake/core/sandbox/modules/io.lua
+++ b/xmake/core/sandbox/modules/io.lua
@@ -173,8 +173,23 @@ function sandbox_io.gsub(filepath, pattern, replace, opt)
if not data then
raise(errors)
end
+ return data, count
+end
- -- ok
+-- replace text of the given file and return replaced data
+function sandbox_io.replace(filepath, pattern, replace, opt)
+
+ -- check
+ assert(filepath)
+
+ -- format it first
+ filepath = vformat(filepath)
+
+ -- replace all
+ local data, count, errors = io.replace(filepath, pattern, replace, opt)
+ if not data then
+ raise(errors)
+ end
return data, count
end
diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua
index 9ae25066f..a121a475c 100644
--- a/xmake/modules/package/tools/cmake.lua
+++ b/xmake/modules/package/tools/cmake.lua
@@ -332,6 +332,22 @@ function _install_for_msvc(package, configs, opt)
end
end
+-- install files for cmake
+function _install_files_for_cmake(package)
+
+ -- patch _IMPORT_PREFIX to the real path
+ --
+ -- e.g. set(_IMPORT_PREFIX "/Users/ruki/.xmake/cache/packages/2009/x/xxx/master/source/xxx/build_C8AA35F0/install")
+ --
+ for _, cmakefile in ipairs(os.files("install/lib/cmake/*/*.cmake")) do
+ local prefixdir_old = path.absolute("install")
+ local prefixdir_new = package:installdir()
+ io.replace(cmakefile, prefixdir_old, prefixdir_new, {plain = true})
+ end
+ os.trycp("install/lib", package:installdir())
+ os.trycp("install/include", package:installdir())
+end
+
-- do install for make
function _install_for_make(package, configs, opt)
local njob = tostring(math.ceil(os.cpuinfo().ncpu * 3 / 2))
@@ -351,8 +367,7 @@ function _install_for_make(package, configs, opt)
os.vrunv("make", argv)
os.vrunv("make", {"install"})
end
- os.trycp("install/lib", package:installdir())
- os.trycp("install/include", package:installdir())
+ _install_files_for_cmake(package)
end
-- do install for ninja
@@ -366,16 +381,14 @@ function _install_for_ninja(package, configs, opt)
table.insert(argv, "-j")
table.insert(argv, njob)
os.vrunv(ninja.program, argv)
- os.trycp("install/lib", package:installdir())
- os.trycp("install/include", package:installdir())
+ _install_files_for_cmake(package)
end
-- do install for cmake/build
function _install_for_cmakebuild(package, configs, opt)
os.vrunv("cmake", {"--build", os.curdir()}, {envs = opt.envs or buildenvs(package)})
os.vrunv("cmake", {"--install", os.curdir()})
- os.trycp("install/lib", package:installdir())
- os.trycp("install/include", package:installdir())
+ _install_files_for_cmake(package)
end
-- build package