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: 0f1ad74f547a399b91b444a83892f3f02f8cb5d5 (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
/**
 * \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_solution_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 slns   An array to contain the list of unloaded solutions.
 * \param funcs  The unloading "interface", providing an opportunity to mock for automated testing.
 * \returns OKAY if successful.
 */
int unload_all(lua_State* L, Array slns, struct UnloadFuncs* funcs)
{
	int si, sn, z = OKAY;

	assert(L);
	assert(slns);
	assert(funcs);
	assert(funcs->unload_solution);

	/* iterate over the list of solutions */
	lua_getglobal(L, SOLUTIONS_KEY);
	sn = luaL_getn(L, -1);
	for (si = 1; z == OKAY && si <= sn; ++si)
	{
		Solution sln = solution_create();
		array_add(slns, sln);
		lua_rawgeti(L, -1, si);

		/* hardcoded a standard set of configurations for now */
		solution_add_config_name(sln, "Debug");
		solution_add_config_name(sln, "Release");

		/* extract the project fields */
		z = funcs->unload_solution(L, sln);
		if (z == OKAY)
		{
			z = unload_solution_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_solution_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)
	{
		Project prj = project_create();
		solution_add_project(sln, prj);

		/* unload the project fields */
		lua_rawgeti(L, -1, pi);
		z = funcs->unload_project(L, prj);
		
		/* remove project object from stack */
		lua_pop(L, 1);
	}

	/* remove list of projects from 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);
}