diff options
| author | OpportunityLiu <[email protected]> | 2019-07-17 08:48:11 +0800 |
|---|---|---|
| committer | OpportunityLiu <[email protected]> | 2019-07-19 11:48:31 +0800 |
| commit | 0a771e41e5057d2f6dc9dce3cbe2078cbc19aa83 (patch) | |
| tree | 3478c553fbee0e2842dbe05196f5d742d518bf1b /xmake/core/base | |
| parent | ae533cdc5bcee78d7a24b64eac430d9197d0e043 (diff) | |
add path.splitenv
Diffstat (limited to 'xmake/core/base')
| -rw-r--r-- | xmake/core/base/os.lua | 35 | ||||
| -rw-r--r-- | xmake/core/base/path.lua | 27 |
2 files changed, 54 insertions, 8 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index ee5979b2e..fe88c65bb 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -943,34 +943,53 @@ function os.getenvs() return envs end --- set values to environment variable +-- set values to environment variable function os.setenv(name, ...) - return os._setenv(name, table.concat({...}, path.envsep())) + local values = {...} + if #values <= 1 then + -- keep compatible with original implementation + os._setenv(name, values[1] or "") + else + os._setenv(path.joinenv(values)) + end end --- add values to environment variable +-- add values to environment variable function os.addenv(name, ...) - local sep = path.envsep() local values = {...} if #values > 0 then - return os._setenv(name, table.concat(values, sep) .. sep .. (os.getenv(name) or "")) + local oldenv = os.getenv(name) + local appendenv = path.joinenv(values) + if oldenv == "" or oldenv == nil then + return os._setenv(name, appendenv) + else + return os._setenv(name, appendenv .. path.envsep() .. oldenv) + end else return true end end --- set values to environment variable with the given seperator +-- set values to environment variable with the given seperator function os.setenvp(name, values, sep) sep = sep or path.envsep() return os._setenv(name, table.concat(table.wrap(values), sep)) end --- add values to environment variable with the given seperator +-- add values to environment variable with the given seperator function os.addenvp(name, values, sep) sep = sep or path.envsep() values = table.wrap(values) if #values > 0 then - return os._setenv(name, table.concat(values, sep) .. sep .. (os.getenv(name) or "")) + local oldenv = os.getenv(name) + local appendenv = table.concat(values, sep) + local newenv + if oldenv == "" or oldenv == nil then + newenv = appendenv + else + newenv = appendenv .. sep .. oldenv + end + return os._setenv(name, newenv) else return true end diff --git a/xmake/core/base/path.lua b/xmake/core/base/path.lua index c1222ffc6..d15c42fc4 100644 --- a/xmake/core/base/path.lua +++ b/xmake/core/base/path.lua @@ -158,6 +158,33 @@ function path.splitenv(env_path) return result end +-- concat environment variable with `path.envsep()`, +-- also handles more speical cases such as posix flags and windows quoted pathes +function path.joinenv(env_table) + + -- check + env_table = env_table or {} + + local envsep = path.envsep() + + if xmake._HOST == "windows" then + local tab = {} + for _, v in ipairs(env_table) do + if v ~= "" then + if v:find(envsep, 1, true) then + v = '"' .. v .. '"' + end + table.insert(tab, v) + end + end + return table.concat(tab, envsep) + else + return table.concat(env_table, envsep) + end + + return result +end + -- the last character is the path seperator? function path.islastsep(p) |
