summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-05-31 08:34:16 +0800
committerruki <[email protected]>2017-05-31 08:34:16 +0800
commit72c1e27d86cb58656c52a0ae5df4a7b6ba94f240 (patch)
treeaef920425e1eea053ba9f8917c4153b3e3a13d57
parentf4c6af603c6b8deb2821600d02409a670748efcc (diff)
improve find file and path to support winreg
-rw-r--r--xmake/core/sandbox/modules/import/lib/detect/find_file.lua46
-rw-r--r--xmake/core/sandbox/modules/import/lib/detect/find_path.lua45
2 files changed, 71 insertions, 20 deletions
diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_file.lua b/xmake/core/sandbox/modules/import/lib/detect/find_file.lua
index 75b587172..ea3cc209b 100644
--- a/xmake/core/sandbox/modules/import/lib/detect/find_file.lua
+++ b/xmake/core/sandbox/modules/import/lib/detect/find_file.lua
@@ -28,15 +28,16 @@ local sandbox_lib_detect_find_file = sandbox_lib_detect_find_file or {}
-- load modules
local os = require("base/os")
local path = require("base/path")
+local utils = require("base/utils")
local table = require("base/table")
local raise = require("sandbox/modules/raise")
-- find file
--
--- @param name the file name
--- @param dirs the file directories
+-- @param name the file name
+-- @param pathes the program pathes (.e.g dirs, pathes, winreg pathes)
--
--- @return the file path
+-- @return the file path
--
-- @code
--
@@ -45,19 +46,44 @@ local raise = require("sandbox/modules/raise")
--
-- @endcode
--
-function sandbox_lib_detect_find_file.main(name, dirs)
+function sandbox_lib_detect_find_file.main(name, pathes)
-- find file
local result = nil
- for _, dir in ipairs(table.wrap(dirs)) do
+ for _, _path in ipairs(table.wrap(pathes)) do
- -- TODO
- -- dir is registry path?
+ -- handle winreg value .e.g [HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\XXXX;Name]\\dir\\file
+ _path = _path:gsub("%[(.*)%]", function (regpath)
+
+ -- get registry value
+ local value, errors = winreg.query(regpath)
+ if not value then
+ utils.verror(errors)
+ end
+
+ -- file path not exists? attempt to parse path from `"path" xxx`
+ if value and not os.exists(value) then
+ value = value:match("\"(.-)\"")
+ end
+
+ -- ok
+ return value
+ end)
+
+ -- get file path
+ local filepath = nil
+ if os.isfile(_path) then
+ filepath = _path
+ elseif os.isdir(_path) then
+ filepath = path.join(_path, name)
+ end
-- file exists?
- for _, file in ipairs(os.files(path.join(dir, name))) do
- result = file
- break
+ if filepath then
+ for _, file in ipairs(os.files(filepath)) do
+ result = file
+ break
+ end
end
end
diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_path.lua b/xmake/core/sandbox/modules/import/lib/detect/find_path.lua
index d4aaafaa8..10b1228b5 100644
--- a/xmake/core/sandbox/modules/import/lib/detect/find_path.lua
+++ b/xmake/core/sandbox/modules/import/lib/detect/find_path.lua
@@ -33,10 +33,10 @@ local raise = require("sandbox/modules/raise")
-- find path
--
--- @param name the path name
--- @param dirs the path directories
+-- @param name the path name
+-- @param pathes the program pathes (.e.g dirs, pathes, winreg pathes)
--
--- @return the path
+-- @return the path
--
-- @code
--
@@ -45,19 +45,44 @@ local raise = require("sandbox/modules/raise")
--
-- @endcode
--
-function sandbox_lib_detect_find_path.main(name, dirs)
+function sandbox_lib_detect_find_path.main(name, pathes)
-- find file
local result = nil
- for _, dir in ipairs(table.wrap(dirs)) do
+ for _, _path in ipairs(table.wrap(pathes)) do
- -- TODO
- -- dir is registry path?
+ -- handle winreg value .e.g [HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\XXXX;Name]\\dir\\file
+ _path = _path:gsub("%[(.*)%]", function (regpath)
+
+ -- get registry value
+ local value, errors = winreg.query(regpath)
+ if not value then
+ utils.verror(errors)
+ end
+
+ -- file path not exists? attempt to parse path from `"path" xxx`
+ if value and not os.exists(value) then
+ value = value:match("\"(.-)\"")
+ end
+
+ -- ok
+ return value
+ end)
+
+ -- get file path
+ local filepath = nil
+ if os.isfile(_path) then
+ filepath = _path
+ elseif os.isdir(_path) then
+ filepath = path.join(_path, name)
+ end
-- path exists?
- for _, p in ipairs(os.filedirs(path.join(dir, name))) do
- result = p
- break
+ if filepath then
+ for _, p in ipairs(os.filedirs(filepath)) do
+ result = p
+ break
+ end
end
end