-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfastbuild_dependency_resolver.lua
163 lines (129 loc) · 4.3 KB
/
fastbuild_dependency_resolver.lua
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
--
-- actions/fastbuild/fastbuild.lua
-- Extend the existing exporters with support for FASTBuild
-- Copyright (c) 2017-2017 Daniel Penkała
--
local p = premake
p.fastbuild.dependency_resolver = { }
local tree = p.tree
local project = p.project
local workspace = p.workspace
local m = p.fastbuild.dependency_resolver
m.resolved = { }
m.groups = { }
---
-- Calls the function for each project in the workspace in dependency relative order
---
function m.eachproject(wks, func)
for _, prj in pairs(m.allprojects(wks)) do
func(prj)
end
end
function m.eachgroup(wks, func)
for _, prj in pairs(m.allgroups(wks)) do
func(name, group)
end
end
---
-- Returns a list of projects in dependency relative order
---
function m.allprojects(wks)
if not m.resolved[wks.name] then
m.resolved[wks.name] = m.projectsResolved(wks)
end
return m.resolved[wks.name]
end
function m.allgroups(wks)
if not m.groups[wks.name] then
m.groups[wks.name] = m.groupsResolved(wks)
end
return m.groups[wks.name]
end
---
-- Removes duplicate entries from a list, always pushing the first occurance into the resulting list
---
local function remove_duplicates(list)
local helper = { }
local result_list = { }
for _, prj in pairs(list) do
if not helper[prj] then
helper[prj] = true
table.insert(result_list, prj)
end
end
return result_list
end
---
-- Given a project add it and all it's dependencies to the given list starting with it's dependencies
---
local function add_project_to_list(list, proj)
local dependencies = project.getdependencies(proj, 'all')
table.foreachi(dependencies, function(dependency)
add_project_to_list(list, dependency)
end)
table.insert(list, proj)
end
---
-- Returns a list of projects in dependency relative order
---
function m.projectsResolved(wks)
local projects_list = { }
local tr = workspace.grouptree(wks)
-- Traverse the tree and append each project and its dependencies to a list. (dependiencies go first)
tree.traverse(tr, {
onleaf = function(node)
local proj = node.project
local dependencies = project.getdependencies(proj, 'all')
-- Add each dependency to the list
add_project_to_list(projects_list, proj)
end,
})
-- Remove duplicates
return remove_duplicates(projects_list)
end
---
-- Returns a list of projects in dependency relative order
---
function m.groupsResolved(wks)
local solution_groups = { }
local groups_stack = { "" }
local current_group_at = 1
local function current_group()
return groups_stack[current_group_at]
end
-- Default group
solution_groups[current_group()] = { }
local tr = workspace.grouptree(wks)
-- Traverse the tree and append each project and its dependencies to a list. (dependiencies go first)
tree.traverse(tr, {
onleaf = function(node)
table.insert(solution_groups[current_group()], node.project)
end,
onbranchenter = function(node, depth)
local group_name = current_group() .. "/" .. node.name
solution_groups[group_name] = solution_groups[group_name] or { }
current_group_at = current_group_at + 1
groups_stack[current_group_at] = group_name
end,
onbranchexit = function(node, depth)
current_group_at = current_group_at - 1
end
})
-- For each group
local result_groups = { }
for name, group in pairs(solution_groups) do
if name ~= "" then
name = name:sub(2)
end
table.insert(result_groups, {
name = name,
projects = group
})
end
-- Sort the table
table.sort(result_groups, function(g1, g2)
return g1.name < g2.name
end)
-- Remove duplicates
return result_groups
end