1--
2-- action.lua
3-- Work with the list of registered actions.
4-- Copyright (c) 2002-2009 Jason Perkins and the Premake project
5--
6
7	premake.action = { }
8
9
10--
11-- The list of registered actions.
12--
13
14	premake.action.list = { }
15
16
17--
18-- Register a new action.
19--
20-- @param a
21--    The new action object.
22--
23
24	function premake.action.add(a)
25		-- validate the action object, at least a little bit
26		local missing
27		for _, field in ipairs({"description", "trigger"}) do
28			if (not a[field]) then
29				missing = field
30			end
31		end
32
33		if (missing) then
34			error("action needs a " .. missing, 3)
35		end
36
37		-- add it to the master list
38		premake.action.list[a.trigger] = a
39	end
40
41
42--
43-- Trigger an action.
44--
45-- @param name
46--    The name of the action to be triggered.
47-- @returns
48--    None.
49--
50
51	function premake.action.call(name)
52		local a = premake.action.list[name]
53		for sln in premake.solution.each() do
54			if a.onsolution then
55				a.onsolution(sln)
56			end
57			if sln.postsolutioncallbacks then
58				for _,cb in ipairs(sln.postsolutioncallbacks) do
59					cb(sln)
60				end
61			end
62
63			for prj in premake.solution.eachproject(sln) do
64				if a.onproject then
65					a.onproject(prj)
66				end
67				if prj.postprojectcallbacks then
68					for _,cb in ipairs(prj.postprojectcallbacks) do
69						cb(prj)
70					end
71				end
72			end
73		end
74
75		if a.execute then
76			a.execute()
77		end
78	end
79
80
81--
82-- Retrieve the current action, as determined by _ACTION.
83--
84-- @return
85--    The current action, or nil if _ACTION is nil or does not match any action.
86--
87
88	function premake.action.current()
89		return premake.action.get(_ACTION)
90	end
91
92
93--
94-- Retrieve an action by name.
95--
96-- @param name
97--    The name of the action to retrieve.
98-- @returns
99--    The requested action, or nil if the action does not exist.
100--
101
102	function premake.action.get(name)
103		return premake.action.list[name]
104	end
105
106
107--
108-- Iterator for the list of actions.
109--
110
111	function premake.action.each()
112		-- sort the list by trigger
113		local keys = { }
114		for _, action in pairs(premake.action.list) do
115			table.insert(keys, action.trigger)
116		end
117		table.sort(keys)
118
119		local i = 0
120		return function()
121			i = i + 1
122			return premake.action.list[keys[i]]
123		end
124	end
125
126
127--
128-- Activates a particular action.
129--
130-- @param name
131--    The name of the action to activate.
132--
133
134	function premake.action.set(name)
135		_ACTION = name
136		-- Some actions imply a particular operating system
137		local action = premake.action.get(name)
138		if action then
139			_OS = action.os or _OS
140		end
141	end
142
143
144--
145-- Determines if an action supports a particular language or target type.
146--
147-- @param action
148--    The action to test.
149-- @param feature
150--    The feature to check, either a programming language or a target type.
151-- @returns
152--    True if the feature is supported, false otherwise.
153--
154
155	function premake.action.supports(action, feature)
156		if not action then
157			return false
158		end
159		if action.valid_languages then
160			if table.contains(action.valid_languages, feature) then
161				return true
162			end
163		end
164		if action.valid_kinds then
165			if table.contains(action.valid_kinds, feature) then
166				return true
167			end
168		end
169		return false
170	end
171