Skip to content

Commit 5aff765

Browse files
Validate created-for node against cmsis-toolbox manifest version
1 parent 4570155 commit 5aff765

File tree

7 files changed

+119
-1
lines changed

7 files changed

+119
-1
lines changed

libs/rtefsutils/include/RteFsUtils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,14 @@ class RteFsUtils
392392
*/
393393
static const std::string& FileCategoryFromExtension(const std::string& file);
394394

395+
/**
396+
* @brief find file using regular expression (non recursively)
397+
* @param search path
398+
* @param regular expression
399+
* @param path to the file found
400+
* @return true if file is found successfully, false otherwise
401+
*/
402+
static bool FindFileWithPattern(const std::string& searchPath, const std::string& pattern, std::string& file);
395403
};
396404

397405
#endif // RteFsUtils_H

libs/rtefsutils/src/RteFsUtils.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,4 +867,28 @@ const string& RteFsUtils::FileCategoryFromExtension(const string& file) {
867867
return OTHER;
868868
}
869869

870+
bool RteFsUtils::FindFileWithPattern(const std::string& searchDir,
871+
const std::string& pattern, std::string& file)
872+
{
873+
try {
874+
std::regex fileRegex(pattern);
875+
876+
// Iterate through the directory (not recursively)
877+
for (const auto& entry : fs::directory_iterator(searchDir)) {
878+
// Check only regular files
879+
if (entry.is_regular_file()) {
880+
const std::string fileName = entry.path().filename().string();
881+
if (std::regex_match(fileName, fileRegex)) {
882+
file = fileName;
883+
return true; // Return on match found
884+
}
885+
}
886+
}
887+
}
888+
catch (const std::exception& e) {
889+
std::cout << "error: " << e.what() << std::endl;
890+
}
891+
return false; // No matching file found
892+
}
893+
870894
// End of RteFsUtils.cpp

libs/rtefsutils/test/src/RteFsUtilsTest.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,4 +1202,35 @@ TEST_F(RteFsUtilsTest, GetAbsPathFromLocalUrl) {
12021202
EXPECT_EQ(absoluteFilename, RteFsUtils::GetAbsPathFromLocalUrl(testUrlOmittedHost));
12031203
}
12041204

1205+
TEST_F(RteFsUtilsTest, FindFileWithPattern) {
1206+
const string& testdir = dirnameBase + "/FindFileWithPattern";
1207+
const string& fileName = "manifest_1.2.3.yml";
1208+
const string& filePath = testdir + "/" + fileName;
1209+
RteFsUtils::CreateDirectories(testdir);
1210+
RteFsUtils::CreateTextFile(filePath, "");
1211+
string discoveredFile;
1212+
EXPECT_EQ(true, RteFsUtils::FindFileWithPattern(
1213+
testdir, "manifest_(\\d+\\.\\d+\\.\\d+)(.*).yml", discoveredFile));
1214+
EXPECT_EQ(fileName, discoveredFile);
1215+
RteFsUtils::RemoveDir(testdir);
1216+
}
1217+
1218+
TEST_F(RteFsUtilsTest, FindFileWithPattern_NoMatch) {
1219+
const string& testdir = dirnameBase + "/FindFileWithPattern";
1220+
RteFsUtils::CreateDirectories(testdir);
1221+
string discoveredFile;
1222+
1223+
EXPECT_EQ(false, RteFsUtils::FindFileWithPattern(testdir, "manifest_(\\d+\\.\\d+\\.\\d+)(.*).yml", discoveredFile));
1224+
RteFsUtils::RemoveDir(testdir);
1225+
EXPECT_TRUE(discoveredFile.empty());
1226+
}
1227+
1228+
TEST_F(RteFsUtilsTest, FindFileWithPattern_InvalidSearchPath) {
1229+
const string& testdir = dirnameBase + "/FindFileWithPattern";
1230+
string discoveredFile;
1231+
1232+
EXPECT_EQ(false, RteFsUtils::FindFileWithPattern(testdir, "manifest_(\\d+\\.\\d+\\.\\d+)(.*).yml", discoveredFile));
1233+
RteFsUtils::RemoveDir(testdir);
1234+
EXPECT_TRUE(discoveredFile.empty());
1235+
}
12051236
// end of RteFsUtilsTest.cpp

tools/projmgr/include/ProjMgr.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ class ProjMgr {
110110
*/
111111
bool GetCdefaultFile();
112112

113+
/**
114+
* @brief get cmsis-toolbox version from manifest file
115+
* @return version as string
116+
*/
117+
std::string GetToolboxVersion(const std::string& manifestFilePath);
113118
protected:
114119
ProjMgrParser m_parser;
115120
ProjMgrExtGenerator m_extGenerator;

tools/projmgr/src/ProjMgr.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,12 @@ bool ProjMgr::ValidateCreatedFor(const string& createdFor) {
10591059
});
10601060
if (toolName == "cmsis-toolbox") {
10611061
const string version = string(sm[2]) + '.' + string(sm[3]) + '.' + string(sm[4]);
1062-
const string currentVersion = string(VERSION_STRING) + ':' + string(VERSION_STRING);
1062+
string cmsisToolboxDir = ProjMgrKernel::Get()->GetCmsisToolboxDir();
1063+
string currentVersion = GetToolboxVersion(cmsisToolboxDir);
1064+
if (currentVersion.empty()) {
1065+
return true;
1066+
}
1067+
currentVersion += (":" + currentVersion);
10631068
if (VersionCmp::RangeCompare(version, currentVersion) <= 0) {
10641069
return true;
10651070
} else {
@@ -1072,3 +1077,20 @@ bool ProjMgr::ValidateCreatedFor(const string& createdFor) {
10721077
}
10731078
return true;
10741079
}
1080+
1081+
string ProjMgr::GetToolboxVersion(const string& toolboxDir) {
1082+
// Find file non recursively under given search directory
1083+
string manifestFilePattern = "manifest_(\\d+\\.\\d+\\.\\d+)(.*).yml";
1084+
string manifestFile;
1085+
1086+
if (!RteFsUtils::FindFileWithPattern(toolboxDir, manifestFilePattern, manifestFile)) {
1087+
ProjMgrLogger::Get().Warn("manifest file does not exist", "", toolboxDir);
1088+
return "";
1089+
}
1090+
1091+
// Extract the version from filename and match it against the expected pattern
1092+
static const regex regEx = regex(manifestFilePattern);
1093+
smatch matchResult;
1094+
regex_match(manifestFile, matchResult, regEx);
1095+
return matchResult[1].str();
1096+
}

tools/projmgr/test/src/ProjMgrTestEnv.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ void ProjMgrTestEnv::SetUp() {
102102
RteFsUtils::RemoveDir(bin_folder);
103103
}
104104
RteFsUtils::CreateDirectories(bin_folder);
105+
// add dummy manifest file
106+
string manifestFile = string(PROJMGRUNITTESTS_BIN_PATH) + "/../manifest_0.0.0.yml";
107+
if (RteFsUtils::Exists(manifestFile)) {
108+
RteFsUtils::RemoveFile(manifestFile);
109+
}
110+
RteFsUtils::CreateTextFile(manifestFile, RteUtils::EMPTY_STRING);
105111

106112
// copy local pack
107113
string srcPackPath, destPackPath;

tools/projmgr/test/src/ProjMgrUnitTests.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6503,3 +6503,25 @@ TEST_F(ProjMgrUnitTests, ReportPacksUnused) {
65036503
EXPECT_EQ(1, cbuild2["build-idx"]["cbuilds"][0]["packs-unused"].size());
65046504
EXPECT_EQ("ARM::[email protected]", cbuild2["build-idx"]["cbuilds"][0]["packs-unused"][0]["pack"].as<string>());
65056505
}
6506+
6507+
TEST_F(ProjMgrUnitTests, GetToolboxVersion) {
6508+
const string& testdir = testoutput_folder + "/toolbox_version";
6509+
string fileName = "manifest_1.test2.3.yml";
6510+
string filePath = testdir + "/" + fileName;
6511+
RteFsUtils::CreateDirectories(testdir);
6512+
RteFsUtils::CreateTextFile(filePath, "");
6513+
6514+
StdStreamRedirect streamRedirect;
6515+
EXPECT_EQ("", GetToolboxVersion(testdir));
6516+
auto errStr = streamRedirect.GetErrorString();
6517+
EXPECT_TRUE(errStr.find("manifest file does not exist") != string::npos);
6518+
6519+
streamRedirect.ClearStringStreams();
6520+
fileName = "manifest_1.2.3.yml";
6521+
filePath = testdir + "/" + fileName;
6522+
RteFsUtils::CreateDirectories(testdir);
6523+
RteFsUtils::CreateTextFile(filePath, "");
6524+
EXPECT_EQ("1.2.3", GetToolboxVersion(testdir));
6525+
6526+
RteFsUtils::RemoveDir(testdir);
6527+
}

0 commit comments

Comments
 (0)