Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] new cmd path decorations with %[] syntax #769

Merged
merged 2 commits into from
May 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to agree with the code here, just wanted to know your thoughts on having the caller perform this? Would there be any benefit in this function being "flexible"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it matters much, whatever we prefer... it just moves this single line into 5-6 locations instead...


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
15 changes: 15 additions & 0 deletions tests/base/test_os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,18 @@
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are great, but I've noticed a bit of a trend of our Lua patterns not working with the same pattern twice. For example, what happens if you were to have cmdtool %[path1/] %[path2/]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can certainly add a test for that... the output should be cmdtool "../foo/path1/" "../foo/path2/"

We use this in several places like that... but adding a test for it is probably a good idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, I can never really tell with the Lua patterns. 👍