Skip to content

Commit 84951ae

Browse files
committed
Added API for source deprecation
1 parent 7f2a0c8 commit 84951ae

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

src/base/action.lua

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
--
3939

4040
action._list = {}
41+
action._aliases = {}
42+
action._deprecatedaliases = {}
4143

4244

4345
---
@@ -68,6 +70,20 @@
6870
end
6971

7072
action._list[act.trigger] = act
73+
74+
-- Add aliases table
75+
if act.aliases then
76+
table.foreachi(act.aliases, function(alias)
77+
action._aliases[alias] = act.trigger
78+
end)
79+
end
80+
81+
-- Add deprecated aliases table
82+
if act.deprecatedaliases then
83+
for key, value in pairs(act.deprecatedaliases) do
84+
action._deprecatedaliases[key] = value
85+
end
86+
end
7187
end
7288

7389

@@ -145,6 +161,11 @@
145161
end
146162

147163

164+
function action.resolvealias(name)
165+
return action._aliases[name] or name
166+
end
167+
168+
148169
---
149170
-- Retrieve an action by name.
150171
--
@@ -155,7 +176,13 @@
155176
---
156177

157178
function action.get(name)
158-
return action._list[name]
179+
local resolved = action.resolvealias(name)
180+
return action._list[resolved]
181+
end
182+
183+
184+
function action.deprecatedalias(name)
185+
return action._deprecatedaliases[name]
159186
end
160187

161188

@@ -209,10 +236,22 @@
209236
---
210237

211238
function action.set(name)
212-
_ACTION = name
239+
-- If the action is an alias, resolve it to the real action
240+
local resolved = action._aliases[name] or name
241+
242+
-- If the action is a deprecated alias, warn the user
243+
local deprecated = action._deprecatedaliases[name]
244+
if deprecated then
245+
local onaction = deprecated["action"]
246+
if onaction ~= nil and type(onaction) == "function" then
247+
onaction()
248+
end
249+
end
250+
251+
_ACTION = resolved
213252

214253
-- Some actions imply a particular operating system or architecture
215-
local act = action.get(name)
254+
local act = action.get(resolved)
216255
if act then
217256
_TARGET_OS = act.targetos or _TARGET_OS
218257
_TARGET_ARCH = act.targetarch or _TARGET_ARCH

src/base/criteria.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@
117117
end
118118
end
119119

120+
-- Check if the prefix is an action
121+
if prefix == "action" or prefix == "_action" then
122+
local actname = word[1]
123+
-- Resolve the action alias
124+
word[1] = p.action.resolvealias(actname)
125+
126+
-- Check if the action was deprecated
127+
local actiondeprecation = p.action.deprecatedalias(actname)
128+
if actiondeprecation ~= nil and actiondeprecation.filter then
129+
actiondeprecation.filter()
130+
end
131+
end
132+
120133
table.insert(pattern, word)
121134
end
122135

website/docs/newaction.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ newaction { description }
2828
| onCleanProject | A callback for each project, when the clean action is selected. |
2929
| onCleanTarget | A callback for each target, when the clean action is selected. |
3030
| pathVars | A map of Premake tokens to toolset specific identifiers. |
31+
| aliases | A list of action names to alias to this action. |
32+
| deprecatedaliases | A table containing a mapping of aliases to callbacks to invoke on action invocation and filters containing the deprecated alias. Each value in the deprecatedaliases table is a table optionally containing an "action" and "filter" key. The values in this table are functions taking zero arguments. See the example below. |
3133

3234
The callbacks will fire in this order:
3335

@@ -66,6 +68,26 @@ newaction {
6668
}
6769
```
6870

71+
Register a new action with aliases and deprecations.
72+
73+
```lua
74+
newaction {
75+
trigger = "myaction",
76+
description = "Custom action",
77+
aliases = { "myalias", "deprecatedalias" },
78+
deprecatedaliases = {
79+
["deprecatedalias" ] = {
80+
[ "action" ] = function()
81+
p.warn("Use myaction instead of deprecatedalias.")
82+
end,
83+
[ "filter" ] = function()
84+
p.warn("deprecatedalias has been deprecated. Filter on myaction instead.")
85+
end
86+
}
87+
}
88+
}
89+
```
90+
6991
### See Also ###
7092

7193
* [Command Line Arguments](Command-Line-Arguments.md)

0 commit comments

Comments
 (0)