Skip to content

Commit bdd4786

Browse files
authored
[projmgr] Add list templates command
1 parent 8f41f87 commit bdd4786

6 files changed

Lines changed: 156 additions & 1 deletion

File tree

test/packs/ARM/RteTest_DFP/0.2.0/ARM.RteTest_DFP.pdsc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@
567567
<template name="Board2" type="Board" file="Templates/board2.csolution.yml" condition="BoardTest2">
568568
<description>"Test board Template two"</description>
569569
</template>
570-
<template name="Board3" type="Board" file="Templates/board3.csolution.yml" condition="BoardTest3">
570+
<template name="Board3" type="Board" path="Templates" file="board3.csolution.yml" copy-to="Template3" condition="BoardTest3">
571571
<description>"Test board Template three"</description>
572572
</template>
573573
<clayer name="TestVariant" type="TestVariant" file="Layers/testvariant.clayer.yml">

tools/projmgr/include/ProjMgr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ class ProjMgr {
193193
bool RunListConfigs();
194194
bool RunListDependencies();
195195
bool RunListExamples();
196+
bool RunListTemplates();
196197
bool RunListContexts();
197198
bool RunListTargetSets();
198199
bool RunListGenerators();

tools/projmgr/include/ProjMgrWorker.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,24 @@ struct ExampleItem {
315315
std::vector<std::string> keywords;
316316
};
317317

318+
/**
319+
* @brief template item containing
320+
* name of example
321+
* description
322+
* path to the directory that contains the template
323+
* path to the *.csolution.yml file
324+
* path to copy the template into
325+
* pack identifier
326+
*/
327+
struct TemplateItem {
328+
std::string name;
329+
std::string description;
330+
std::string path;
331+
std::string file;
332+
std::string copyTo;
333+
std::string pack;
334+
};
335+
318336
/**
319337
* @brief debugger type
320338
* name of debug configuration
@@ -589,6 +607,14 @@ class ProjMgrWorker {
589607
*/
590608
bool ListExamples(std::vector<std::string>& examples, const std::string& filter = RteUtils::EMPTY_STRING);
591609

610+
/**
611+
* @brief list available csolution templates
612+
* @param reference to list of csolution templates
613+
* @param filter words to filter results
614+
* @return true if executed successfully
615+
*/
616+
bool ListTemplates(std::vector<std::string>& templates, const std::string& filter = RteUtils::EMPTY_STRING);
617+
592618
/**
593619
* @brief list contexts
594620
* @param reference to list of contexts
@@ -1146,6 +1172,7 @@ class ProjMgrWorker {
11461172
std::vector<ExampleItem> CollectExamples(ContextItem& context);
11471173
std::vector<RteBoard*> GetCompatibleBoards(ContextItem& context);
11481174
bool IsBoardListCompatible(const std::vector<RteBoard*> compatibleBoards, const Collection<RteItem*>& boards);
1175+
std::vector<TemplateItem> CollectTemplates(ContextItem& context);
11491176
};
11501177

11511178
#endif // PROJMGRWORKER_H

tools/projmgr/src/ProjMgr.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Commands:\n\
3131
list devices Print list of available device names\n\
3232
list environment Print list of environment configurations\n\
3333
list examples Print list of examples\n\
34+
list templates Print list of templates\n\
3435
list generators Print list of code generators of a given context\n\
3536
list layers Print list of available, referenced and compatible layers\n\
3637
list packs Print list of used packs from the pack repository\n\
@@ -183,6 +184,7 @@ int ProjMgr::ParseCommandLine(int argc, char** argv) {
183184
{"list components", { true, {context, contextSet, activeTargetSet, debug, filter, load, quiet, schemaCheck, toolchain, verbose}}},
184185
{"list dependencies", { false, {context, contextSet, activeTargetSet, debug, filter, load, quiet, schemaCheck, toolchain, verbose}}},
185186
{"list examples", { false, {context, contextSet, activeTargetSet, debug, filter, load, quiet, schemaCheck, toolchain, verbose}}},
187+
{"list templates", { false, {context, contextSet, activeTargetSet, debug, filter, load, quiet, schemaCheck, toolchain, verbose}}},
186188
{"list contexts", { false, {debug, filter, quiet, schemaCheck, verbose, ymlOrder}}},
187189
{"list target-sets", { false, {debug, filter, quiet, schemaCheck, verbose}}},
188190
{"list generators", { false, {context, contextSet, activeTargetSet, debug, load, quiet, schemaCheck, toolchain, verbose}}},
@@ -376,6 +378,10 @@ int ProjMgr::ProcessCommands() {
376378
if (!RunListExamples()) {
377379
return ErrorCode::ERROR;
378380
}
381+
} else if (m_args == "templates") {
382+
if (!RunListTemplates()) {
383+
return ErrorCode::ERROR;
384+
}
379385
} else if (m_args == "contexts") {
380386
if (!RunListContexts()) {
381387
return ErrorCode::ERROR;
@@ -938,6 +944,31 @@ bool ProjMgr::RunListExamples(void) {
938944
return true;
939945
}
940946

947+
bool ProjMgr::RunListTemplates(void) {
948+
if (!m_csolutionFile.empty()) {
949+
// Parse all input files and create contexts
950+
if (!PopulateContexts()) {
951+
return false;
952+
}
953+
}
954+
955+
// Parse context selection
956+
if (!ParseAndValidateContexts()) {
957+
return false;
958+
}
959+
960+
vector<string> csolutionTemplates;
961+
if (!m_worker.ListTemplates(csolutionTemplates, m_filter)) {
962+
ProjMgrLogger::Get().Error("processing templates list failed");
963+
return false;
964+
}
965+
966+
for (const auto& csolutionTemplate : csolutionTemplates) {
967+
ProjMgrLogger::out() << csolutionTemplate << endl;
968+
}
969+
return true;
970+
}
971+
941972
bool ProjMgr::RunListContexts(void) {
942973
// Parse all input files and create contexts
943974
if (!PopulateContexts()) {

tools/projmgr/src/ProjMgrWorker.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4348,6 +4348,57 @@ bool ProjMgrWorker::ListExamples(vector<string>& examples, const string& filter)
43484348
return true;
43494349
}
43504350

4351+
std::vector<TemplateItem> ProjMgrWorker::CollectTemplates(ContextItem& context) {
4352+
std::vector<TemplateItem> templates;
4353+
const auto& rteTemplates = context.rteFilteredModel->GetProjectDescriptors();
4354+
for (const auto& rteTemplate : rteTemplates) {
4355+
TemplateItem csolutionTemplate;
4356+
csolutionTemplate.name = rteTemplate->GetName();
4357+
csolutionTemplate.description = rteTemplate->GetDescription();
4358+
csolutionTemplate.pack = rteTemplate->GetPackageID(true);
4359+
csolutionTemplate.path = rteTemplate->GetPathString();
4360+
RteFsUtils::NormalizePath(csolutionTemplate.path, rteTemplate->GetAbsolutePackagePath());
4361+
csolutionTemplate.file = rteTemplate->GetFileString();
4362+
RteFsUtils::NormalizePath(csolutionTemplate.file, csolutionTemplate.path);
4363+
csolutionTemplate.copyTo = rteTemplate->GetCopyToString();
4364+
templates.push_back(csolutionTemplate);
4365+
}
4366+
return templates;
4367+
}
4368+
4369+
bool ProjMgrWorker::ListTemplates(vector<string>& templates, const string& filter) {
4370+
const auto& selectedContext = m_selectedContexts.front();
4371+
ContextItem& context = m_contexts[selectedContext];
4372+
if (!LoadPacks(context)) {
4373+
return false;
4374+
}
4375+
if (!selectedContext.empty()) {
4376+
if (!ProcessPrecedences(context, BoardOrDevice::Both)) {
4377+
return false;
4378+
}
4379+
}
4380+
if (!SetTargetAttributes(context, context.targetAttributes)) {
4381+
return false;
4382+
}
4383+
const auto& collectedTemplates = CollectTemplates(context);
4384+
for (const auto& templateItem : collectedTemplates) {
4385+
if (!filter.empty() && templateItem.name.find(filter) == string::npos) {
4386+
continue;
4387+
}
4388+
string templateStr = templateItem.name + " (" + templateItem.pack + ")";
4389+
if (m_verbose) {
4390+
templateStr += "\n description: " + templateItem.description;
4391+
templateStr += "\n path: " + templateItem.path;
4392+
templateStr += "\n file: " + templateItem.file;
4393+
if (!templateItem.copyTo.empty()) {
4394+
templateStr += "\n copy-to: " + templateItem.copyTo;
4395+
}
4396+
}
4397+
templates.push_back(templateStr);
4398+
}
4399+
return true;
4400+
}
4401+
43514402
bool ProjMgrWorker::FormatValidationResults(set<string>& results, const ContextItem& context) {
43524403
for (const auto& validation : context.validationResults) {
43534404
string resultStr = RteItem::ConditionResultToString(validation.result) + " " + validation.id;

tools/projmgr/test/src/ProjMgrUnitTests.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6964,6 +6964,51 @@ PreIncludeEnvFolder@1.0.0\n\
69646964
EXPECT_TRUE(outStr.empty());
69656965
}
69666966

6967+
6968+
TEST_F(ProjMgrUnitTests, ListTemplates) {
6969+
char* argv[7];
6970+
StdStreamRedirect streamRedirect;
6971+
6972+
// list all templates
6973+
argv[1] = (char*)"list";
6974+
argv[2] = (char*)"templates";
6975+
EXPECT_EQ(0, RunProjMgr(3, argv, 0));
6976+
auto outStr = streamRedirect.GetOutString();
6977+
EXPECT_STREQ(outStr.c_str(), "\
6978+
Board1Template (ARM::RteTest_DFP@0.2.0)\n\
6979+
Board2 (ARM::RteTest_DFP@0.2.0)\n\
6980+
Board3 (ARM::RteTest_DFP@0.2.0)\n\
6981+
");
6982+
6983+
// test filter
6984+
argv[3] = (char*)"--filter";
6985+
argv[4] = (char*)"Board1";
6986+
streamRedirect.ClearStringStreams();
6987+
EXPECT_EQ(0, RunProjMgr(5, argv, 0));
6988+
outStr = streamRedirect.GetOutString();
6989+
EXPECT_STREQ(outStr.c_str(), "\
6990+
Board1Template (ARM::RteTest_DFP@0.2.0)\n\
6991+
");
6992+
6993+
// list board's compatible template
6994+
const string& csolution = testinput_folder + "/Examples/solution.csolution.yml";
6995+
const string expected =
6996+
argv[3] = (char*)csolution.c_str();
6997+
argv[4] = (char*)"--active";
6998+
argv[5] = (char*)"TestBoard";
6999+
argv[6] = (char*)"--verbose";
7000+
streamRedirect.ClearStringStreams();
7001+
EXPECT_EQ(0, RunProjMgr(7, argv, 0));
7002+
outStr = streamRedirect.GetOutString();
7003+
EXPECT_TRUE(regex_search(outStr, regex("\
7004+
Board3 \\(ARM::RteTest_DFP@0.2.0\\)\n\
7005+
description: \"Test board Template three\"\n\
7006+
path: .*/ARM/RteTest_DFP/0.2.0/Templates\n\
7007+
file: .*/ARM/RteTest_DFP/0.2.0/Templates/board3.csolution.yml\n\
7008+
copy-to: Template3\n\
7009+
")));
7010+
}
7011+
69677012
TEST_F(ProjMgrUnitTests, ConvertActiveTargetSet) {
69687013
char* argv[6];
69697014
StdStreamRedirect streamRedirect;

0 commit comments

Comments
 (0)