From 6e12c6485f05015f62b390fb9faa23287bf2aab7 Mon Sep 17 00:00:00 2001 From: Jason Perkins Date: Sat, 29 Nov 2014 14:51:49 -0500 Subject: [PATCH] Move command token expansion into actions, to allow more control at point of export --- .hgsubstate | 2 +- src/_premake_init.lua | 6 --- src/actions/make/_make.lua | 1 + src/actions/make/make_cpp.lua | 4 +- src/actions/vstudio/vs2005_csproj.lua | 1 + src/actions/vstudio/vs200x_vcproj.lua | 13 +++--- src/actions/vstudio/vs2010_rules_props.lua | 3 +- src/actions/vstudio/vs2010_vcxproj.lua | 5 ++- src/base/os.lua | 50 ++++++++++++---------- src/base/oven.lua | 7 --- tests/base/test_os.lua | 10 ++--- 11 files changed, 52 insertions(+), 50 deletions(-) diff --git a/.hgsubstate b/.hgsubstate index f0d6de90..83a0246f 100644 --- a/.hgsubstate +++ b/.hgsubstate @@ -1 +1 @@ -61225861a7aa58fcee0cab894206fde9a6be7736 modules/xcode +3935627f6ef6f848afd1ac3b22cc1eaa7b34a582 modules/xcode diff --git a/src/_premake_init.lua b/src/_premake_init.lua index d1d5293e..f8c36079 100644 --- a/src/_premake_init.lua +++ b/src/_premake_init.lua @@ -68,7 +68,6 @@ scope = { "config", "rule" }, kind = "list:string", tokens = true, - commands = true, } api.alias("buildcommands", "buildCommands") @@ -129,7 +128,6 @@ scope = "config", kind = "list:string", tokens = true, - commands = true, } @@ -574,7 +572,6 @@ scope = "config", kind = "list:string", tokens = true, - commands = true, } api.register { @@ -589,7 +586,6 @@ scope = "config", kind = "list:string", tokens = true, - commands = true, } api.register { @@ -604,7 +600,6 @@ scope = "config", kind = "list:string", tokens = true, - commands = true, } api.register { @@ -625,7 +620,6 @@ scope = "config", kind = "list:string", tokens = true, - commands = true, } api.register { diff --git a/src/actions/make/_make.lua b/src/actions/make/_make.lua index 00713a17..46954814 100644 --- a/src/actions/make/_make.lua +++ b/src/actions/make/_make.lua @@ -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")) diff --git a/src/actions/make/make_cpp.lua b/src/actions/make/make_cpp.lua index bbcc290d..bf570a6e 100644 --- a/src/actions/make/make_cpp.lua +++ b/src/actions/make/make_cpp.lua @@ -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') diff --git a/src/actions/vstudio/vs2005_csproj.lua b/src/actions/vstudio/vs2005_csproj.lua index 6b77dcf5..ea9f88c2 100644 --- a/src/actions/vstudio/vs2005_csproj.lua +++ b/src/actions/vstudio/vs2005_csproj.lua @@ -198,6 +198,7 @@ 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', name, steps, name) end diff --git a/src/actions/vstudio/vs200x_vcproj.lua b/src/actions/vstudio/vs200x_vcproj.lua index 6eede15d..33ef8948 100644 --- a/src/actions/vstudio/vs200x_vcproj.lua +++ b/src/actions/vstudio/vs200x_vcproj.lua @@ -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, ' ')) diff --git a/src/actions/vstudio/vs2010_rules_props.lua b/src/actions/vstudio/vs2010_rules_props.lua index f2300ee0..9cfa8bae 100644 --- a/src/actions/vstudio/vs2010_rules_props.lua +++ b/src/actions/vstudio/vs2010_rules_props.lua @@ -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('%s', cmds) end end diff --git a/src/actions/vstudio/vs2010_vcxproj.lua b/src/actions/vstudio/vs2010_vcxproj.lua index 0164a981..f086e664 100644 --- a/src/actions/vstudio/vs2010_vcxproj.lua +++ b/src/actions/vstudio/vs2010_vcxproj.lua @@ -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,'%s', 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, '%s', phase, commands, phase) end diff --git a/src/base/os.lua b/src/base/os.lua index 00014df1..df97e67e 100644 --- a/src/base/os.lua +++ b/src/base/os.lua @@ -442,38 +442,42 @@ --- os.commandTokens = { - copy = { - _ = function(v) - return "cp -r " .. v - end, - windows = function(v) - return "xcopy /Q /E /Y " .. path.translate(v) - end, + _ = { + copy = function(v) return "cp -r " .. v end, + }, + windows = { + copy = function(v) return "xcopy /Q /E /Y " .. path.translate(v) end, } } - function os.translateCommand(cmd) + 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] = os.translateCommand(cmd[i]) + result[i] = processOne(cmd[i]) end return result + else + return processOne(cmd) 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.get()] or processors["_"] - if processor then - return processor(value) - end - end - - return cmd end diff --git a/src/base/oven.lua b/src/base/oven.lua index e32ee285..2e08bdc0 100644 --- a/src/base/oven.lua +++ b/src/base/oven.lua @@ -489,13 +489,6 @@ ctx.location = ctx.location or prj.location context.basedir(ctx, ctx.location) - -- Translate any tokens in command line fields - for f in p.field.each() do - if f.commands then - ctx[f.name] = os.translateCommand(ctx[f.name]) - end - end - -- Fill in a few calculated for the configuration, including the long -- and short names and the build and link target. diff --git a/tests/base/test_os.lua b/tests/base/test_os.lua index ce1a6c96..2d506cd6 100644 --- a/tests/base/test_os.lua +++ b/tests/base/test_os.lua @@ -137,12 +137,12 @@ -- function suite.translateCommand_onNoToken() - test.isequal("cp a b", os.translateCommand("cp a b")) + test.isequal("cp a b", os.translateCommands("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")) + os.commandTokens.test = { + copy = function(value) return "test " .. value end + } + test.isequal("test a b", os.translateCommands("{COPY} a b", "test")) end \ No newline at end of file