Skip to content

Commit

Permalink
Allow to select shell from command line for token replacement.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Mar 9, 2024
1 parent af1c1b3 commit de5357b
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 @@ -1876,6 +1876,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 @@ -735,7 +745,7 @@
-- Translate decorated command paths into their OS equivalents.
---
function os.translateCommandsAndPaths(cmds, basedir, location, map)
map = map or os.target()
map = map or os.shell()
location = path.getabsolute(location)
basedir = path.getabsolute(basedir)

Expand Down
6 changes: 3 additions & 3 deletions website/docs/Tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ The paths are expanded relative to premake script, to obtain absolute paths, you

## 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 @@ -94,11 +94,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 de5357b

Please sign in to comment.