Skip to content

Commit bdcb93c

Browse files
authored
[projmgr] Extend commands to handle specified contexts
1 parent 770742b commit bdcb93c

File tree

6 files changed

+192
-132
lines changed

6 files changed

+192
-132
lines changed

tools/projmgr/include/ProjMgr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class ProjMgr {
8484
bool RunListContexts(void);
8585
bool RunListGenerators(void);
8686
bool PopulateContexts(void);
87+
bool CheckContext(void);
8788
};
8889

8990
#endif // PROJMGR_H

tools/projmgr/include/ProjMgrWorker.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,31 +202,34 @@ class ProjMgrWorker {
202202
* @param filter words to filter results
203203
* @return true if executed successfully
204204
*/
205-
bool ListPacks(std::vector<std::string>& packs, const std::string& filter = RteUtils::EMPTY_STRING);
205+
bool ListPacks(std::vector<std::string>& packs, const std::string& contextName, const std::string& filter = RteUtils::EMPTY_STRING);
206206

207207
/**
208208
* @brief list available devices
209209
* @param reference to list of devices
210+
* @param reference to context name
210211
* @param filter words to filter results
211212
* @return true if executed successfully
212213
*/
213-
bool ListDevices(std::vector<std::string>& devices, const std::string& filter = RteUtils::EMPTY_STRING);
214+
bool ListDevices(std::vector<std::string>& devices, const std::string& contextName, const std::string& filter = RteUtils::EMPTY_STRING);
214215

215216
/**
216217
* @brief list available components
217218
* @param reference to list of components
219+
* @param reference to context name
218220
* @param filter words to filter results
219221
* @return true if executed successfully
220222
*/
221-
bool ListComponents(std::vector<std::string>& components, const std::string& filter = RteUtils::EMPTY_STRING);
223+
bool ListComponents(std::vector<std::string>& components, const std::string& contextName, const std::string& filter = RteUtils::EMPTY_STRING);
222224

223225
/**
224226
* @brief list available dependencies
225227
* @param reference to list of dependencies
228+
* @param reference to context name
226229
* @param filter words to filter results
227230
* @return true if executed successfully
228231
*/
229-
bool ListDependencies(std::vector<std::string>& dependencies, const std::string& filter = RteUtils::EMPTY_STRING);
232+
bool ListDependencies(std::vector<std::string>& dependencies, const std::string& contextName, const std::string& filter = RteUtils::EMPTY_STRING);
230233

231234
/**
232235
* @brief list contexts

tools/projmgr/src/ProjMgr.cpp

Lines changed: 69 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,30 @@ bool ProjMgr::PopulateContexts(void) {
250250
return true;
251251
}
252252

253+
bool ProjMgr::CheckContext(void) {
254+
vector<string> contexts;
255+
m_worker.ListContexts(contexts);
256+
if (m_context.empty()) {
257+
if (contexts.size() == 1) {
258+
m_context = contexts.front();
259+
} else {
260+
ProjMgrLogger::Error("context was not specified");
261+
return false;
262+
}
263+
} else {
264+
if (find(contexts.begin(), contexts.end(), m_context) == contexts.end()) {
265+
ProjMgrLogger::Error("context '" + m_context + "' was not found");
266+
return false;
267+
}
268+
}
269+
return true;
270+
}
271+
253272
bool ProjMgr::RunConvert(void) {
254273
// Parse all input files and populate contexts inputs
255274
if (!PopulateContexts()) {
256275
return false;
257276
}
258-
259277
// Process contexts
260278
map<string, ContextItem>* contexts = nullptr;
261279
m_worker.GetContexts(contexts);
@@ -265,9 +283,18 @@ bool ProjMgr::RunConvert(void) {
265283
return false;
266284
}
267285
}
268-
286+
// Check selected context
287+
if (!m_context.empty()) {
288+
if (contexts->find(m_context) == contexts->end()) {
289+
ProjMgrLogger::Error("context '" + m_context + "' was not found");
290+
return false;
291+
}
292+
}
269293
// Generate Cprjs
270294
for (auto& [contextName, contextItem] : *contexts) {
295+
if (!m_context.empty() && m_context != contextName) {
296+
continue;
297+
}
271298
error_code ec;
272299
const string& directory = m_outputDir.empty() ? contextItem.directories.cproject : m_outputDir + "/" + contextName;
273300
const string& filename = fs::weakly_canonical(directory + "/" + contextName + ".cprj", ec).generic_string();
@@ -287,8 +314,18 @@ bool ProjMgr::RunConvert(void) {
287314
}
288315

289316
bool ProjMgr::RunListPacks(void) {
317+
if (!m_csolutionFile.empty() || !m_cprojectFile.empty()) {
318+
// Parse all input files and create contexts
319+
if (!PopulateContexts()) {
320+
return false;
321+
}
322+
// Check context
323+
if (!CheckContext()) {
324+
return false;
325+
}
326+
}
290327
vector<string> packs;
291-
if (!m_worker.ListPacks(packs, m_filter)) {
328+
if (!m_worker.ListPacks(packs, m_context, m_filter)) {
292329
ProjMgrLogger::Error("processing pack list failed");
293330
return false;
294331
}
@@ -299,19 +336,18 @@ bool ProjMgr::RunListPacks(void) {
299336
}
300337

301338
bool ProjMgr::RunListDevices(void) {
302-
if (!m_cprojectFile.empty()) {
303-
if (!m_parser.ParseCproject(m_cprojectFile, m_checkSchema, true)) {
339+
if (!m_csolutionFile.empty() || !m_cprojectFile.empty()) {
340+
// Parse all input files and create contexts
341+
if (!PopulateContexts()) {
304342
return false;
305343
}
306-
ContextItem context;
307-
ContextDesc descriptor;
308-
if (!m_worker.AddContexts(m_parser, descriptor, m_cprojectFile)) {
309-
ProjMgrLogger::Error(m_cprojectFile, "adding project context failed");
344+
// Check context
345+
if (!CheckContext()) {
310346
return false;
311347
}
312348
}
313349
vector<string> devices;
314-
if (!m_worker.ListDevices(devices, m_filter)) {
350+
if (!m_worker.ListDevices(devices, m_context, m_filter)) {
315351
ProjMgrLogger::Error("processing devices list failed");
316352
return false;
317353
}
@@ -322,17 +358,18 @@ bool ProjMgr::RunListDevices(void) {
322358
}
323359

324360
bool ProjMgr::RunListComponents(void) {
325-
if (!m_cprojectFile.empty()) {
326-
if (!m_parser.ParseCproject(m_cprojectFile, m_checkSchema, true)) {
361+
if (!m_csolutionFile.empty() || !m_cprojectFile.empty()) {
362+
// Parse all input files and create contexts
363+
if (!PopulateContexts()) {
327364
return false;
328365
}
329-
ContextDesc descriptor;
330-
if (!m_worker.AddContexts(m_parser, descriptor, m_cprojectFile)) {
366+
// Check context
367+
if (!CheckContext()) {
331368
return false;
332369
}
333370
}
334371
vector<string> components;
335-
if (!m_worker.ListComponents(components, m_filter)) {
372+
if (!m_worker.ListComponents(components, m_context, m_filter)) {
336373
ProjMgrLogger::Error("processing components list failed");
337374
return false;
338375
}
@@ -343,19 +380,16 @@ bool ProjMgr::RunListComponents(void) {
343380
}
344381

345382
bool ProjMgr::RunListDependencies(void) {
346-
if (m_cprojectFile.empty()) {
347-
ProjMgrLogger::Error("cproject.yml file was not specified");
348-
return false;
349-
}
350-
if (!m_parser.ParseCproject(m_cprojectFile, m_checkSchema, true)) {
383+
// Parse all input files and create contexts
384+
if (!PopulateContexts()) {
351385
return false;
352386
}
353-
ContextDesc descriptor;
354-
if (!m_worker.AddContexts(m_parser, descriptor, m_cprojectFile)) {
387+
// Check context
388+
if (!CheckContext()) {
355389
return false;
356390
}
357391
vector<string> dependencies;
358-
if (!m_worker.ListDependencies(dependencies, m_filter)) {
392+
if (!m_worker.ListDependencies(dependencies, m_context, m_filter)) {
359393
ProjMgrLogger::Error("processing dependencies list failed");
360394
return false;
361395
}
@@ -366,28 +400,10 @@ bool ProjMgr::RunListDependencies(void) {
366400
}
367401

368402
bool ProjMgr::RunListContexts(void) {
369-
if (m_csolutionFile.empty()) {
370-
ProjMgrLogger::Error("csolution.yml file was not specified");
371-
return false;
372-
}
373-
if (!m_parser.ParseCsolution(m_csolutionFile, m_checkSchema)) {
403+
// Parse all input files and create contexts
404+
if (!PopulateContexts()) {
374405
return false;
375406
}
376-
for (const auto& cproject : m_parser.GetCsolution().cprojects) {
377-
error_code ec;
378-
string const& cprojectFile = fs::canonical(m_rootDir + "/" + cproject, ec).generic_string();
379-
if (!m_parser.ParseCproject(cprojectFile, m_checkSchema)) {
380-
return false;
381-
}
382-
}
383-
for (auto& descriptor : m_parser.GetCsolution().contexts) {
384-
error_code ec;
385-
const string& cprojectFile = fs::path(descriptor.cproject).is_absolute() ?
386-
descriptor.cproject : fs::canonical(m_rootDir + "/" + descriptor.cproject, ec).generic_string();
387-
if (!m_worker.AddContexts(m_parser, descriptor, cprojectFile)) {
388-
return false;
389-
}
390-
}
391407
vector<string> contexts;
392408
if (!m_worker.ListContexts(contexts, m_filter)) {
393409
ProjMgrLogger::Error("processing contexts list failed");
@@ -404,24 +420,10 @@ bool ProjMgr::RunListGenerators(void) {
404420
if (!PopulateContexts()) {
405421
return false;
406422
}
407-
408-
// Process context
409-
vector<string> contexts;
410-
m_worker.ListContexts(contexts);
411-
if (m_context.empty()) {
412-
if (contexts.size() == 1) {
413-
m_context = contexts.front();
414-
} else {
415-
ProjMgrLogger::Error("context was not specified");
416-
return false;
417-
}
418-
} else {
419-
if (find(contexts.begin(), contexts.end(), m_context) == contexts.end()) {
420-
ProjMgrLogger::Error("context '" + m_context + "' was not found");
421-
return false;
422-
}
423+
// Check context
424+
if (!CheckContext()) {
425+
return false;
423426
}
424-
425427
// Get generators
426428
vector<string> generators;
427429
if (!m_worker.ListGenerators(m_context, generators)) {
@@ -434,38 +436,22 @@ bool ProjMgr::RunListGenerators(void) {
434436
}
435437

436438
bool ProjMgr::RunCodeGenerator(void) {
437-
// Parse all input files and create contexts
438-
if (!PopulateContexts()) {
439-
return false;
440-
}
441-
442439
// Check input options
443440
if (m_codeGenerator.empty()) {
444441
ProjMgrLogger::Error("generator identifier was not specified");
445442
return false;
446443
}
447-
448-
// Process context
449-
vector<string> contexts;
450-
m_worker.ListContexts(contexts);
451-
if (m_context.empty()) {
452-
if (contexts.size() == 1) {
453-
m_context = contexts.front();
454-
} else {
455-
ProjMgrLogger::Error("context was not specified");
456-
return false;
457-
}
458-
} else {
459-
if (find(contexts.begin(), contexts.end(), m_context) == contexts.end()) {
460-
ProjMgrLogger::Error("context '" + m_context + "' was not found");
461-
return false;
462-
}
444+
// Parse all input files and create contexts
445+
if (!PopulateContexts()) {
446+
return false;
447+
}
448+
// Check context
449+
if (!CheckContext()) {
450+
return false;
463451
}
464-
465452
// Run code generator
466453
if (!m_worker.ExecuteGenerator(m_context, m_codeGenerator)) {
467454
return false;
468455
}
469-
470456
return true;
471457
}

0 commit comments

Comments
 (0)