Skip to content

Commit

Permalink
Merge pull request #769 from Blizzard/cmdpath-token
Browse files Browse the repository at this point in the history
[core] new cmd path decorations with %[] syntax
  • Loading branch information
tvandijck authored May 4, 2017
2 parents 6475e2a + f1d461f commit 863b896
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/actions/make/_make.lua
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
local steps = cfg[event .. "commands"]
local msg = cfg[event .. "message"]
if #steps > 0 then
steps = os.translateCommands(steps)
steps = os.translateCommandsAndPaths(steps, cfg.project.basedir, cfg.project.location)
msg = msg or string.format("Running %s commands", event)
_p('\t@echo %s', msg)
_p('\t%s', table.implode(steps, "", "", "\n\t"))
Expand Down
2 changes: 1 addition & 1 deletion src/actions/make/make_cpp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
_p('%s: %s', output, dependencies)
_p('\t@echo "%s"', filecfg.buildmessage or ("Building " .. filecfg.relpath))

local cmds = os.translateCommands(filecfg.buildcommands)
local cmds = os.translateCommandsAndPaths(filecfg.buildcommands, cfg.project.basedir, cfg.project.location)
for _, cmd in ipairs(cmds) do
if cfg.bindirs and #cfg.bindirs > 0 then
_p('\t$(SILENT) $(EXE_PATHS) %s', cmd)
Expand Down
4 changes: 2 additions & 2 deletions src/actions/make/make_makefile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
_p(' define BUILDCMDS')
local steps = cfg.buildcommands
if #steps > 0 then
steps = os.translateCommands(steps)
steps = os.translateCommandsAndPaths(steps, cfg.project.basedir, cfg.project.location)
_p('\t@echo Running build commands')
_p('\t%s', table.implode(steps, "", "", "\n\t"))
end
Expand All @@ -84,7 +84,7 @@
_p(' define CLEANCMDS')
local steps = cfg.cleancommands
if #steps > 0 then
steps = os.translateCommands(steps)
steps = os.translateCommandsAndPaths(steps, cfg.project.basedir, cfg.project.location)
_p('\t@echo Running clean commands')
_p('\t%s', table.implode(steps, "", "", "\n\t"))
end
Expand Down
2 changes: 1 addition & 1 deletion src/actions/vstudio/vs2005_csproj.lua
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
function cs2005.buildEvents(prj)
local function output(name, steps)
if #steps > 0 then
steps = os.translateCommands(steps, p.WINDOWS)
steps = os.translateCommandsAndPaths(steps, prj.basedir, prj.location)
steps = table.implode(steps, "", "", "\r\n")
_x(2,'<%sBuildEvent>%s</%sBuildEvent>', name, steps, name)
end
Expand Down
8 changes: 4 additions & 4 deletions src/actions/vstudio/vs200x_vcproj.lua
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@


function m.buildCommandLine(cfg)
local cmds = os.translateCommands(cfg.buildcommands, p.WINDOWS)
local cmds = os.translateCommandsAndPaths(cfg.buildcommands, cfg.project.basedir, cfg.project.location)
p.x('BuildCommandLine="%s"', table.concat(cmds, "\r\n"))
end

Expand All @@ -960,7 +960,7 @@


function m.cleanCommandLine(cfg)
local cmds = os.translateCommands(cfg.cleancommands, p.WINDOWS)
local cmds = os.translateCommandsAndPaths(cfg.cleancommands, cfg.project.basedir, cfg.project.location)
cmds = table.concat(cmds, "\r\n")
p.x('CleanCommandLine="%s"', cmds)
end
Expand All @@ -975,7 +975,7 @@
if msg then
p.x('Description="%s"', msg)
end
steps = os.translateCommands(steps, p.WINDOWS)
steps = os.translateCommandsAndPaths(steps, cfg.project.basedir, cfg.project.location)
p.x('CommandLine="%s"', table.implode(steps, "", "", "\r\n"))
end
end
Expand Down Expand Up @@ -1045,7 +1045,7 @@
function m.customBuildTool(cfg)
local cfg, filecfg = config.normalize(cfg)
if filecfg and fileconfig.hasCustomBuildRule(filecfg) then
local cmds = os.translateCommands(filecfg.buildcommands, p.WINDOWS)
local cmds = os.translateCommandsAndPaths(filecfg.buildcommands, filecfg.project.basedir, filecfg.project.location)
p.x('CommandLine="%s"', table.concat(cmds,'\r\n'))

local outputs = project.getrelative(filecfg.project, filecfg.buildoutputs)
Expand Down
6 changes: 3 additions & 3 deletions src/actions/vstudio/vs2010_vcxproj.lua
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@
local msg = cfg[field .. "message"]

if #steps > 0 then
steps = os.translateCommands(steps, p.WINDOWS)
steps = os.translateCommandsAndPaths(steps, cfg.project.basedir, cfg.project.location)
p.push('<%s>', name)
p.x('<Command>%s</Command>', table.implode(steps, "", "", "\r\n"))
if msg then
Expand Down Expand Up @@ -1131,7 +1131,7 @@


function m.buildCommands(fcfg, condition)
local commands = os.translateCommands(fcfg.buildcommands, p.WINDOWS)
local commands = os.translateCommandsAndPaths(fcfg.buildcommands, fcfg.project.basedir, fcfg.project.location)
commands = table.concat(commands,'\r\n')
m.element("Command", condition, '%s', commands)
end
Expand Down Expand Up @@ -1768,7 +1768,7 @@

function m.nmakeCommandLine(cfg, commands, phase)
if #commands > 0 then
commands = os.translateCommands(commands, p.WINDOWS)
commands = os.translateCommandsAndPaths(commands, cfg.project.basedir, cfg.project.location)
commands = table.concat(p.esc(commands), p.eol())
p.w('<NMake%sCommandLine>%s</NMake%sCommandLine>', phase, commands, phase)
end
Expand Down
41 changes: 41 additions & 0 deletions src/base/os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,47 @@



---
-- Translate decorated command paths into their OS equivalents.
---

function os.translateCommandsAndPaths(cmds, basedir, location, map)
local translatedBaseDir = path.getrelative(location, basedir)

map = map or os.target()

local translateFunction = function(value)
local result = path.join(translatedBaseDir, value)
if value:endswith('/') or value:endswith('\\') or
value:endswith('/"') or value:endswith('\\"') then
result = result .. '/'
end
if map == 'windows' then
result = path.translate(result)
end
return result
end

local processOne = function(cmd)
local replaceFunction = function(value)
value = value:sub(3, #value - 1)
return '"' .. translateFunction(value) .. '"'
end
return string.gsub(cmd, "%%%[[^%]\r\n]*%]", replaceFunction)
end

if type(cmds) == "table" then
local result = {}
for i = 1, #cmds do
result[i] = processOne(cmds[i])
end
return os.translateCommands(result, map)
else
return os.translateCommands(processOne(cmds), map)
end
end


--
-- Generate a UUID.
--
Expand Down
19 changes: 19 additions & 0 deletions tests/base/test_os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,22 @@
local version = os.getversion();
test.istrue(version ~= nil)
end



--
-- os.translateCommandsAndPaths.
--

function suite.translateCommandsAndPaths()
test.isequal('cmdtool "../foo/path1"', os.translateCommandsAndPaths("cmdtool %[path1]", '../foo', '.', 'osx'))
end

function suite.translateCommandsAndPaths_PreserveSlash()
test.isequal('cmdtool "../foo/path1/"', os.translateCommandsAndPaths("cmdtool %[path1/]", '../foo', '.', 'osx'))
end

function suite.translateCommandsAndPaths_MultipleTokens()
test.isequal('cmdtool "../foo/path1" "../foo/path2/"', os.translateCommandsAndPaths("cmdtool %[path1] %[path2/]", '../foo', '.', 'osx'))
end

0 comments on commit 863b896

Please sign in to comment.