Add os.translateCommand() to expand tokens

This commit is contained in:
Jason Perkins 2014-11-27 12:56:48 -05:00
parent c08c1aaabf
commit 5d327279cc
2 changed files with 57 additions and 0 deletions

View File

@ -435,6 +435,47 @@
---
-- Translate command tokens into their OS or action specific equivalents.
---
os.commandTokens = {
copy = {
_ = function(v)
return "cp -r " .. v
end,
windows = function(v)
return "xcopy /S " .. v
end,
}
}
function os.translateCommand(cmd)
if type(cmd) == "table" then
local result = {}
for i = 1, #cmd do
result[i] = os.translateCommand(cmd[i])
end
return result
end
local token = cmd:match("^{.+}")
if token then
local value = cmd:sub(#token + 2)
token = token:sub(2, #token - 1):lower()
local processors = os.commandTokens[token]
local processor = processors[_ACTION] or processors[_OS] or processors["_"]
if processor then
return processor(value)
end
end
return cmd
end
--
-- Generate a UUID.
--

View File

@ -130,3 +130,19 @@
function suite.pathsearch_NilPathsAllowed()
test.isequal(_TESTS_DIR, os.pathsearch("_tests.lua", nil, _TESTS_DIR, nil))
end
--
-- os.translateCommand() tests
--
function suite.translateCommand_onNoToken()
test.isequal("cp a b", os.translateCommand("cp a b"))
end
function suite.translateCommand_callsProcessor()
os.commandTokens.copy.test = function(value)
return "test " .. value
end
test.isequal("test a b", os.translateCommand("{COPY} a b"))
end