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

Allow tokens to start with a ! to disable making paths relative. #880

Merged
merged 1 commit into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
66 changes: 39 additions & 27 deletions src/base/detoken.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,15 @@
setmetatable(environ, {__index = _G})

function expandtoken(token, e)
-- convert the token into a function to execute
local func, err = load("return " .. token, nil, 't', e)
if not func then
return nil, "load error: " .. err
end

-- run it and get the result
local success, result = pcall(func)
if not success then
err = result
result = nil
else
err = nil
result = result or ""
end

-- If the result is an absolute path, and it is being inserted into
-- a NON-path value, I need to make it relative to the project that
-- will contain it. Otherwise I ended up with an absolute path in
-- the generated project, and it can no longer be moved around.

local isAbs = false

if result ~= nil then
isAbs = path.isabsolute(result)
if isAbs and not field.paths and basedir then
result = path.getrelative(basedir, result)
end
local err
local result
local success

-- if the token starts with a !, don't try making it relative.
local dontMakeRelative = token:startswith('!')
if dontMakeRelative then
token = token:sub(2, -1)
end

-- If this token is in my path variable mapping tables, replace the
Expand All @@ -100,6 +81,37 @@
else
isAbs = path.isabsolute(result)
end
else
-- convert the token into a function to execute
local func
func, err = load("return " .. token, nil, 't', e)
if not func then
return nil, "load error: " .. err
end

-- run it and get the result
success, result = pcall(func)
if not success then
err = result
result = nil
else
err = nil
result = result or ""
end

if result ~= nil then
-- ensure we got a string.
result = tostring(result)

-- If the result is an absolute path, and it is being inserted into
-- a NON-path value, I need to make it relative to the project that
-- will contain it. Otherwise I ended up with an absolute path in
-- the generated project, and it can no longer be moved around.
isAbs = path.isabsolute(result)
if isAbs and not field.paths and basedir and not dontMakeRelative then
result = path.getrelative(basedir, result)
end
end
end

-- If the result is an absolute path, and it is being inserted into
Expand Down
11 changes: 11 additions & 0 deletions tests/base/test_detoken.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@
test.isequal("cd ..", x)
end

--
-- but not if it is prefixed with a !
--

function suite.canExpandWithExclamationMark()
local cwd = os.getcwd()
environ.cfg = { basedir = path.getdirectory(cwd) }
x = detoken.expand("%{!cfg.basedir}", environ, {}, cwd)
test.isequal(path.getdirectory(os.getcwd()), x)
end


--
-- If the value being expanded is a table, iterate over all of its values.
Expand Down