Skip to content

Commit

Permalink
Allow to select shell in command line for token replacement.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Jul 14, 2023
1 parent 26725da commit 2df6c92
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
11 changes: 11 additions & 0 deletions src/_premake_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,17 @@
}
}

newoption
{
trigger = "shell",
value = "VALUE",
description = "Select shell (for command token substitution)",
allowed = {
{ "cmd", "Windows command shell" },
{ "posix", "For posix shells" },
}
}

newoption
{
trigger = "scripts",
Expand Down
24 changes: 17 additions & 7 deletions src/base/os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@
return table.contains(tags, id:lower())
end

--
-- Retrieve the current target shell ID string.
--

function os.shell()
return _OPTIONS.shell or iif(os.target() == "windows", "cmd", "posix")
end

---
-- Determine if a directory exists on the file system, and that it is a
Expand Down Expand Up @@ -599,7 +606,7 @@
---

os.commandTokens = {
_ = {
posix = {
chdir = function(v)
return "cd " .. path.normalize(v)
end,
Expand Down Expand Up @@ -631,7 +638,7 @@
return "touch " .. path.normalize(v)
end,
},
windows = {
cmd = {
chdir = function(v)
return "chdir " .. path.translate(path.normalize(v))
end,
Expand Down Expand Up @@ -681,9 +688,12 @@
}

function os.translateCommands(cmd, map)
map = map or os.target()
map = map or os.shell()
if type(map) == "string" then
map = os.commandTokens[map] or os.commandTokens["_"]
if map == "windows" then -- For retro compatibility
map = "cmd"
end
map = os.commandTokens[map] or os.commandTokens["posix"]
end

local processOne = function(cmd)
Expand All @@ -697,7 +707,7 @@

local token = cmd:sub(i + 1, j - 1):lower()
local args = cmd:sub(j + 2)
local func = map[token] or os.commandTokens["_"][token]
local func = map[token] or os.commandTokens["posix"][token]
if func then
cmd = cmd:sub(1, i -1) .. func(args)
end
Expand Down Expand Up @@ -725,7 +735,7 @@
-- Apply os slashes for decorated command paths.
---
function os.translateCommandAndPath(dir, map)
if map == 'windows' then
if map == 'windows' or map == 'cmd' then
return path.translate(dir)
end
return dir
Expand All @@ -737,7 +747,7 @@
function os.translateCommandsAndPaths(cmds, basedir, location, map)
local translatedBaseDir = path.getrelative(location, basedir)

map = map or os.target()
map = map or os.shell()

local translateFunction = function(value)
local result = path.join(translatedBaseDir, value)
Expand Down
6 changes: 3 additions & 3 deletions website/docs/Tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ file.extension -- (including '.'; eg ".cpp")

## Command Tokens

Command tokens represent a system level command in a platform-neutral way.
Command tokens represent a system level command in a shell-neutral way.

```lua
postbuildcommands {
Expand All @@ -92,11 +92,11 @@ You can use command tokens anywhere you specify a command line, including:
* [prelinkcommands](prelinkcommands.md)
* [rebuildcommands](rebuildcommands.md)

Command tokens are replaced with an appropriate command for the target platform. For Windows, path separators in the commmand arguments are converted to backslashes.
Command tokens are replaced with an appropriate command for the target shell. For Windows, path separators in the commmand arguments are converted to backslashes.

The available tokens, and their replacements:

| Token | DOS | Posix |
| Token | DOS/cmd | Posix |
|------------|---------------------------------------------|-----------------|
| {CHDIR} | chdir {args} | cd {args} |
| {COPYFILE} | copy /B /Y {args} | cp -f {args} |
Expand Down

0 comments on commit 2df6c92

Please sign in to comment.