Welcome to mirror list, hosted at ThFree Co, Russian Federation.

unload.c « script « src - github.com/windirstat/premake-4.x-stable.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 70e4f248deb41d9d878d789477f104f5981bf66f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
 * \file   unload.c
 * \brief  Unload project objects from the scripting environment.
 * \author Copyright (c) 2002-2008 Jason Perkins and the Premake project
 */

#include <assert.h>
#include "premake.h"
#include "script/script_internal.h"


static int unload_blocks(lua_State* L, struct UnloadFuncs* funcs, Blocks blocks);
static int unload_projects(lua_State* L, struct UnloadFuncs* funcs, Solution sln);


/**
 * Copy project information out of the scripting environment and into C objects that
 * can be more easily manipulated by the action code.
 * \param L      The Lua scripting engine state.
 * \param sess   The session will contain the unloaded objects.
 * \param funcs  The unloading "interface", providing an opportunity to mock for automated testing.
 * \returns OKAY if successful.
 */
int unload_all(lua_State* L, Session sess, struct UnloadFuncs* funcs)
{
	int si, sn, z = OKAY;

	assert(L);
	assert(sess);
	assert(funcs);
	assert(funcs->unload_solution);
	assert(funcs->unload_project);
	assert(funcs->unload_block);

	/* iterate over the list of solutions */
	lua_getglobal(L, SOLUTIONS_KEY);
	sn = luaL_getn(L, -1);
	for (si = 1; z == OKAY && si <= sn; ++si)
	{
		/* add a new solution to the master list */
		Solution sln = solution_create();
		session_add_solution(sess, sln);

		/* get the scripted solution object from the solutions list */
		lua_rawgeti(L, -1, si);

		/* unload the solution fields, then configuration blocks, then projects */
		z = funcs->unload_solution(L, sln);
		if (z == OKAY)
		{
			Blocks blocks = solution_get_blocks(sln);
			z = unload_blocks(L, funcs, blocks);
		}
		if (z == OKAY)
		{
			z = unload_projects(L, funcs, sln);
		}

		/* remove solution object from stack */
		lua_pop(L, 1);
	}

	/* remove list of solutions from stack */
	lua_pop(L, 1);
	return z;
}


static int unload_projects(lua_State* L, struct UnloadFuncs* funcs, Solution sln)
{
	int pi, pn, z = OKAY;

	/* iterate over list of projects from the solution */
	lua_getfield(L, -1, PROJECTS_KEY);
	pn = luaL_getn(L, -1);
	for (pi = 1; z == OKAY && pi <= pn; ++pi)
	{
		/* add a new project to the master list */
		Project prj = project_create();
		solution_add_project(sln, prj);

		/* get the scripted project object from the solutions list */
		lua_rawgeti(L, -1, pi);
	
		/* unload the project fields, then configuration blocks */
		z = funcs->unload_project(L, prj);
		if (z == OKAY)
		{
			Blocks blocks = project_get_blocks(prj);
			z = unload_blocks(L, funcs, blocks);
		}
		
		/* remove project object from stack */
		lua_pop(L, 1);
	}

	/* remove list of projects from stack */
	lua_pop(L, 1);
	return z;
}


static int unload_blocks(lua_State* L, struct UnloadFuncs* funcs, Blocks blocks)
{
	int ci, cn, z = OKAY;
	
	/* iterate over the list configuration blocks from the solution */
	lua_getfield(L, -1, BLOCKS_KEY);
	cn = luaL_getn(L, -1);
	for (ci = 1; z == OKAY && ci <= cn; ++ci)
	{
		Block blk = block_create();
		blocks_add(blocks, blk);

		/* unload the configuration block fields */
		lua_rawgeti(L, -1, ci);
		z = funcs->unload_block(L, blk);

		/* remove the configuration block object from the stack */
		lua_pop(L, 1);
	}

	/* remove the list of blocks from the stack */
	lua_pop(L, 1);
	return z;
}


int unload_fields(lua_State* L, Fields fields, struct FieldInfo* info)
{
	const char* value;
	int fi;

	for (fi = 0; info[fi].name != NULL; ++fi)
	{
		Strings values = strings_create();

		lua_getfield(L, -1, info[fi].name);
		if (lua_istable(L, -1))
		{
			int i, n;
			n = luaL_getn(L, -1);
			for (i = 1; i <= n; ++i)
			{
				lua_rawgeti(L, -1, i);
				value = lua_tostring(L, -1);
				if (value != NULL)
				{
					strings_add(values, value);
				}
				lua_pop(L, 1);
			}
		}
		else
		{
			value = lua_tostring(L, -1);
			if (value != NULL)
			{
				strings_add(values, value);
			}
		}

		/* remove the field value from the top of the stack */
		lua_pop(L, 1);

		/* store the field values */
		fields_set_values(fields, fi, values);
	}

	return OKAY;
}


/**
 * Unload information from the scripting environment for a particular solution.
 * \param L      The Lua scripting engine state.
 * \param sln    The solution object to be populated.
 * \returns OKAY if successful.
 */
int unload_solution(lua_State* L, Solution sln)
{
	return unload_fields(L, solution_get_fields(sln), SolutionFieldInfo);
}


/**
 * Unload information from the scripting environment for a particular project.
 * \param   L      The Lua scripting engine state.
 * \param   prj    The project object to be populated.
 * \returns OKAY if successful.
 */
int unload_project(lua_State* L, Project prj)
{
	return unload_fields(L, project_get_fields(prj), ProjectFieldInfo);
}


/**
 * Unload information from the scripting environment for a particular configuration.
 * \param   L      The Lua scripting engine state.
 * \param   blk    The configuration block to be populated.
 * \returns OKAY if successful.
 */
int unload_block(lua_State* L, Block blk)
{
	return unload_fields(L, block_get_fields(blk), BlockFieldInfo);
}