Merge command tokens feature

This commit is contained in:
Jason Perkins 2014-12-03 16:47:55 -05:00
commit 590981d94d
9 changed files with 139 additions and 17 deletions

View File

@ -1 +1 @@
61225861a7aa58fcee0cab894206fde9a6be7736 modules/xcode
3935627f6ef6f848afd1ac3b22cc1eaa7b34a582 modules/xcode

View File

@ -218,6 +218,7 @@
local steps = cfg[event .. "commands"]
local msg = cfg[event .. "message"]
if #steps > 0 then
steps = os.translateCommands(steps)
msg = msg or string.format("Running %s commands", event)
_p('\t@echo %s', msg)
_p('\t%s', table.implode(steps, "", "", "\n\t"))

View File

@ -161,7 +161,9 @@
end
_x('%s: %s', output, dependencies)
_p('\t@echo "%s"', filecfg.buildmessage or ("Building " .. filecfg.relpath))
for _, cmd in ipairs(filecfg.buildcommands) do
local cmds = os.translateCommands(filecfg.buildcommands)
for _, cmd in ipairs(cmds) do
_p('\t$(SILENT) %s', cmd)
end
_p('endif')

View File

@ -198,15 +198,17 @@
function cs2005.buildEvents(prj)
local function output(name, steps)
if #steps > 0 then
steps = os.translateCommands(steps, p.WINDOWS)
steps = table.implode(steps, "", "", "\r\n")
_x(2,'<%sBuildEvent>%s</%sBuildEvent>', name, steps, name)
end
end
if #prj.prebuildcommands > 0 or #prj.postbuildcommands > 0 then
local cfg = project.getfirstconfig(prj)
if #cfg.prebuildcommands > 0 or #cfg.postbuildcommands > 0 then
_p(1,'<PropertyGroup>')
output("Pre", prj.prebuildcommands)
output("Post", prj.postbuildcommands)
output("Pre", cfg.prebuildcommands)
output("Post", cfg.postbuildcommands)
_p(1,'</PropertyGroup>')
end
end

View File

@ -940,8 +940,8 @@
function m.buildCommandLine(cfg)
commands = table.concat(cfg.buildcommands, "\r\n")
p.x('BuildCommandLine="%s"', commands)
local cmds = os.translateCommands(cfg.buildcommands, p.WINDOWS)
p.x('BuildCommandLine="%s"', table.concat(cmds, "\r\n"))
end
@ -955,8 +955,9 @@
function m.cleanCommandLine(cfg)
commands = table.concat(cfg.cleancommands, "\r\n")
p.x('CleanCommandLine="%s"', commands)
local cmds = os.translateCommands(cfg.cleancommands, p.WINDOWS)
cmds = table.concat(cmds, "\r\n")
p.x('CleanCommandLine="%s"', cmds)
end
@ -969,6 +970,7 @@
if msg then
p.x('Description="%s"', msg)
end
steps = os.translateCommands(steps, p.WINDOWS)
p.x('CommandLine="%s"', table.implode(steps, "", "", "\r\n"))
end
end
@ -1030,7 +1032,8 @@
function m.customBuildTool(cfg)
local cfg, filecfg = config.normalize(cfg)
if filecfg and fileconfig.hasCustomBuildRule(filecfg) then
p.x('CommandLine="%s"', table.concat(filecfg.buildcommands,'\r\n'))
local cmds = os.translateCommands(filecfg.buildcommands, p.WINDOWS)
p.x('CommandLine="%s"', table.concat(cmds,'\r\n'))
local outputs = project.getrelative(filecfg.project, filecfg.buildoutputs)
p.x('Outputs="%s"', table.concat(outputs, ' '))

View File

@ -145,7 +145,8 @@
function m.commandLineTemplates(r)
if #r.buildcommands then
local cmds = table.concat(r.buildcommands, p.eol())
local cmds = os.translateCommands(r.buildcommands, p.WINDOWS)
cmds = table.concat(cmds, p.eol())
p.x('<CommandLineTemplate>%s</CommandLineTemplate>', cmds)
end
end

View File

@ -471,6 +471,7 @@
local msg = cfg[field .. "message"]
if #steps > 0 then
steps = os.translateCommands(steps, p.WINDOWS)
_p(2,'<%s>', name)
_x(3,'<Command>%s</Command>', table.implode(steps, "", "", "\r\n"))
if msg then
@ -644,7 +645,8 @@
if fileconfig.hasCustomBuildRule(filecfg) then
m.excludedFromBuild(cfg, filecfg)
local commands = table.concat(filecfg.buildcommands,'\r\n')
local commands = os.translateCommands(filecfg.buildcommands, p.WINDOWS)
commands = table.concat(commands,'\r\n')
m.element("Command", condition, '%s', commands)
local outputs = project.getrelative(prj, filecfg.buildoutputs)
@ -1288,6 +1290,7 @@
function m.nmakeCommandLine(cfg, commands, phase)
if #commands > 0 then
commands = os.translateCommands(commands, p.WINDOWS)
commands = table.concat(premake.esc(commands), p.eol())
_p(2, '<NMake%sCommandLine>%s</NMake%sCommandLine>', phase, commands, phase)
end

View File

@ -5,20 +5,22 @@
--
--
-- Add a path normalization step to the execution command to
-- replace special symbols and any scripted weirdness.
---
-- Extend Lua's built-in os.execute() with token expansion and
-- path normalization.
--
premake.override(os, "execute", function(base, cmd)
cmd = path.normalize(cmd)
cmd = os.translateCommands(cmd)
return base(cmd)
end)
--
---
-- Same as os.execute(), but accepts string formatting arguments.
--
---
function os.executef(cmd, ...)
cmd = string.format(cmd, unpack(arg))
@ -435,6 +437,98 @@
---
-- Translate command tokens into their OS or action specific equivalents.
---
os.commandTokens = {
_ = {
chdir = function(v)
return "cd " .. v
end,
copy = function(v)
return "cp -rf " .. v
end,
delete = function(v)
return "rm -f " .. v
end,
echo = function(v)
return "echo " .. v
end,
mkdir = function(v)
return "mkdir -p " .. v
end,
move = function(v)
return "mv -f " .. v
end,
rmdir = function(v)
return "rm -rf " .. v
end,
touch = function(v)
return "touch " .. v
end,
},
windows = {
chdir = function(v)
return "chdir " .. path.translate(v)
end,
copy = function(v)
return "xcopy /Q /E /Y /I " .. path.translate(v)
end,
delete = function(v)
return "del " .. path.translate(v)
end,
echo = function(v)
return "echo " .. v
end,
mkdir = function(v)
return "mkdir " .. path.translate(v)
end,
move = function(v)
return "move /Y " .. path.translate(v)
end,
rmdir = function(v)
return "rmdir /S /Q " .. path.translate(v)
end,
touch = function(v)
v = path.translate(v)
return string.format("type nul >> %s && copy /b %s+,, %s", v, v, v)
end,
}
}
function os.translateCommands(cmd, map)
map = map or os.get()
if type(map) == "string" then
map = os.commandTokens[map] or os.commandTokens["_"]
end
local processOne = function(cmd)
local token = cmd:match("^{.+}")
if token then
token = token:sub(2, #token - 1):lower()
local args = cmd:sub(#token + 4)
local func = map[token] or os.commandTokens["_"][token]
if func then
cmd = func(args)
end
end
return cmd
end
if type(cmd) == "table" then
local result = {}
for i = 1, #cmd do
result[i] = processOne(cmd[i])
end
return result
else
return processOne(cmd)
end
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.translateCommands("cp a b"))
end
function suite.translateCommand_callsProcessor()
os.commandTokens.test = {
copy = function(value) return "test " .. value end
}
test.isequal("test a b", os.translateCommands("{COPY} a b", "test"))
end