-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildsManager.cpp
More file actions
337 lines (270 loc) · 10.2 KB
/
Copy pathBuildsManager.cpp
File metadata and controls
337 lines (270 loc) · 10.2 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "runcpp2/BuildsManager.hpp"
#include "ssLogger/ssLog.hpp"
#include <sstream>
#include <cassert>
#if INTERNAL_RUNCPP2_UNIT_TESTS
#include "Tests/BuildsManager/MockComponents.hpp"
#else
#define CO_NO_OVERRIDE 1
#include "CppOverride.hpp"
#endif
namespace runcpp2
{
bool BuildsManager::ParseMappings(const std::string& mappingsContent)
{
ssLOG_FUNC_DEBUG();
std::error_code e;
std::istringstream iss(mappingsContent);
std::string line;
//Read each line in the mappings file, and extract the mappings
while(std::getline(iss, line))
{
if(line.empty())
continue;
std::size_t foundComma = line.find(",");
if(foundComma == std::string::npos)
{
ssLOG_ERROR("Failed to parse line: " << line);
return false;
}
std::string scriptPath = line.substr(0, foundComma);
if(!ghc::filesystem::path(scriptPath).is_absolute())
{
ssLOG_ERROR("Script path " << scriptPath << " is not absolute");
return false;
}
if(foundComma == line.size() - 1)
{
ssLOG_ERROR("End of line reached, mapped path not found");
ssLOG_ERROR("Failed to parse line" << line);
return false;
}
std::string mappedPath = line.substr(foundComma + 1, line.size() - foundComma - 1);
if(!ghc::filesystem::path(mappedPath).is_relative())
{
ssLOG_ERROR("Mapped path " << mappedPath << " is not relative");
return false;
}
ssLOG_DEBUG("BuildDirectory / mappedPath: " <<
ghc::filesystem::path(BuildDirectory / mappedPath).string());
//We don't add the mapped path to record if it doesn't exist
if(!ghc::filesystem::exists(BuildDirectory / mappedPath, e))
continue;
Mappings[scriptPath] = mappedPath;
ReverseMappings[mappedPath] = scriptPath;
}
return true;
}
std::string BuildsManager::ProcessPath(const std::string& path)
{
std::string processedPath;
for(int i = 0; i < path.size(); ++i)
{
if(path[i] == '\\')
processedPath += '/';
else
processedPath += path[i];
}
return processedPath;
};
BuildsManager::BuildsManager(const ghc::filesystem::path& configDirectory) :
ConfigDirectory(configDirectory),
Mappings(),
BuildDirectory(configDirectory / "CachedBuilds"),
MappingsFile(BuildDirectory / "Mappings.csv"),
Initialized(false)
{}
BuildsManager::BuildsManager(const BuildsManager& other)
{
*this = other;
}
BuildsManager& BuildsManager::operator=(const BuildsManager& other)
{
ConfigDirectory = other.ConfigDirectory;
Mappings = other.Mappings;
BuildDirectory = other.BuildDirectory;
MappingsFile = other.MappingsFile;
Initialized = other.Initialized;
return *this;
}
BuildsManager::~BuildsManager()
{}
bool BuildsManager::Initialize()
{
ssLOG_FUNC_DEBUG();
std::error_code e;
ssLOG_INFO("MappingsFile: " << MappingsFile.string());
ssLOG_INFO("BuildDirectory: " << BuildDirectory.string());
//Read the build mappings if exist, if not create it.
if(ghc::filesystem::exists(MappingsFile, e))
{
std::ifstream inputFile(MappingsFile);
if (!inputFile.is_open())
{
ssLOG_ERROR("Failed to open file: " << MappingsFile.string());
return false;
}
std::stringstream buffer;
buffer << inputFile.rdbuf();
std::string source(buffer.str());
inputFile.close();
if(!ParseMappings(source))
return false;
}
else
{
//Create the Builds directory
if(!ghc::filesystem::exists(BuildDirectory, e))
{
if(!ghc::filesystem::create_directories(BuildDirectory, e))
{
ssLOG_ERROR("Failed to create directory " << BuildDirectory.string());
return false;
}
}
std::ofstream newFile(MappingsFile);
if(!newFile.is_open())
{
ssLOG_ERROR("Failed to open file: " << MappingsFile.string());
return false;
}
newFile.close();
}
Initialized = true;
return true;
}
bool BuildsManager::CreateBuildMapping(const ghc::filesystem::path& scriptPath)
{
CO_INSERT_MEMBER_IMPL(OverrideInstance, bool, (scriptPath));
ssLOG_FUNC_DEBUG();
if(!Initialized)
return false;
if(scriptPath.is_relative())
{
ssLOG_ERROR("Cannot create build mapping with relative path");
return false;
}
ghc::filesystem::path cleanScriptPath = scriptPath.lexically_normal();
std::string processScriptPathStr = ProcessPath(cleanScriptPath.string());
if(Mappings.count(processScriptPathStr) > 0)
return true;
//Create build folder for script
constexpr int MAX_TRIES = 1000;
std::size_t scriptPathHash;
std::string scriptBuildPathStr;
int counter = 0;
do
{
scriptPathHash = std::hash<std::string>{}(processScriptPathStr + std::to_string(counter));
scriptBuildPathStr = "./" + std::to_string(scriptPathHash);
if(ReverseMappings.count(scriptBuildPathStr) == 0)
break;
counter++;
}
while(counter < MAX_TRIES);
if(counter == MAX_TRIES)
{
ssLOG_ERROR("Failed to get unique hash for " << cleanScriptPath.string() <<
" after " << MAX_TRIES << " attempts");
return false;
}
ghc::filesystem::path scriptBuildPath = BuildDirectory / scriptBuildPathStr;
std::error_code e;
//Removing existing build folder if exists
if(ghc::filesystem::exists(scriptBuildPath, e))
{
if(!ghc::filesystem::remove_all(scriptBuildPath, e))
{
ssLOG_ERROR("Failed to remove " << scriptBuildPath);
return false;
}
}
//Create the build folder we need
if(!ghc::filesystem::create_directories(scriptBuildPath, e))
{
ssLOG_ERROR("Failed to create directory " << scriptBuildPath.string());
return false;
}
ssLOG_INFO("Build path " << scriptBuildPath << " for " << cleanScriptPath);
Mappings[processScriptPathStr] = scriptBuildPathStr;
ReverseMappings[scriptBuildPathStr] = processScriptPathStr;
return true;
}
bool BuildsManager::RemoveBuildMapping(const ghc::filesystem::path& scriptPath)
{
if(!Initialized)
return false;
if(scriptPath.is_relative())
{
ssLOG_ERROR("Cannot have build mapping with relative path");
return false;
}
ghc::filesystem::path cleanScriptPath = scriptPath.lexically_normal();
std::string processScriptPathStr = ProcessPath(cleanScriptPath.string());
if(Mappings.count(processScriptPathStr) == 0)
return true;
ReverseMappings.erase(Mappings.at(processScriptPathStr));
Mappings.erase(processScriptPathStr);
assert(ReverseMappings.size() == Mappings.size());
return true;
}
bool BuildsManager::HasBuildMapping(const ghc::filesystem::path& scriptPath)
{
if(!Initialized)
return false;
if(scriptPath.is_relative())
{
ssLOG_ERROR("Cannot have build mapping with relative path");
return false;
}
ghc::filesystem::path cleanScriptPath = scriptPath.lexically_normal();
return Mappings.count(ProcessPath(cleanScriptPath.string()));
}
bool BuildsManager::GetBuildMapping(const ghc::filesystem::path& scriptPath,
ghc::filesystem::path& outPath)
{
if(!Initialized)
return false;
if(scriptPath.is_relative())
{
ssLOG_ERROR("Cannot have build mapping with relative path");
return false;
}
ghc::filesystem::path cleanScriptPath = scriptPath.lexically_normal();
//If it doesn't exist, create the mapping
if(!Mappings.count(ProcessPath(cleanScriptPath.string())))
{
if(!CreateBuildMapping(cleanScriptPath))
return false;
}
outPath = BuildDirectory.string() + "/" + Mappings.at(ProcessPath(cleanScriptPath.string()));
return true;
}
bool BuildsManager::RemoveAllBuildsMappings()
{
if(!Initialized)
return false;
Mappings.clear();
ReverseMappings.clear();
return true;
}
bool BuildsManager::SaveBuildsMappings()
{
if(!Initialized)
return false;
std::ofstream buildsMappings(MappingsFile);
if(!buildsMappings.is_open())
{
ssLOG_ERROR("Failed to open file: " << MappingsFile.string());
return false;
}
for(std::unordered_map<std::string, std::string>::iterator it = Mappings.begin();
it != Mappings.end(); ++it)
{
ssLOG_DEBUG("Writing mapping: " << it->first << "," << it->second);
buildsMappings << it->first << "," << it->second << std::endl;
}
buildsMappings.close();
return true;
}
}