Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions tools/projmgr/include/ProjMgrUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ class ProjMgrUtils {
*/
static const std::string FileTypeFromExtension(const std::string& file);

/**
* @brief normalize line endings for consistent string comparison
* @param input string
* @return output normalized string
*/
static const std::string NormalizeLineEndings(const std::string& in);

protected:
/**
* @brief get filtered list of contexts
Expand Down
3 changes: 2 additions & 1 deletion tools/projmgr/include/ProjMgrYamlEmitter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2024 Arm Limited. All rights reserved.
* Copyright (c) 2020-2025 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -114,6 +114,7 @@ class ProjMgrYamlEmitter {
bool CompareFile(const std::string& filename, const YAML::Node& rootNode);
bool CompareNodes(const YAML::Node& lhs, const YAML::Node& rhs);
bool NeedRebuild(const std::string& filename, const YAML::Node& rootNode);
std::string EraseGeneratedByNode(const std::string& inStr);
};

#endif // PROJMGRYAMLEMITTER_H
15 changes: 14 additions & 1 deletion tools/projmgr/src/ProjMgrUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2021 Arm Limited. All rights reserved.
* Copyright (c) 2020-2025 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -464,3 +464,16 @@ const string ProjMgrUtils::FileTypeFromExtension(const string& file) {
}
return RteUtils::EMPTY_STRING;
}

const std::string ProjMgrUtils::NormalizeLineEndings(const std::string& in) {
string out = in;
size_t pos = 0;
while ((pos = out.find("\r\n", pos)) != std::string::npos) {
out.replace(pos++, 2, "\n");
}
pos = 0;
while ((pos = out.find("\r", pos)) != std::string::npos) {
out.replace(pos, 1, "\n");
}
return out;
}
36 changes: 18 additions & 18 deletions tools/projmgr/src/ProjMgrYamlEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,29 @@ bool ProjMgrYamlEmitter::WriteFile(YAML::Node& rootNode, const std::string& file
return true;
}

string ProjMgrYamlEmitter::EraseGeneratedByNode(const string& inStr) {
size_t startIndex, endIndex;
string outStr = inStr;
startIndex = outStr.find(YAML_GENERATED_BY, 0);
endIndex = outStr.find('\n', startIndex);
if (startIndex != std::string::npos && endIndex != std::string::npos) {
outStr = outStr.erase(startIndex, endIndex - startIndex);
}
return outStr;
};

bool ProjMgrYamlEmitter::CompareFile(const string& filename, const YAML::Node& rootNode) {
if (!RteFsUtils::Exists(filename)) {
string inBuffer;
if (!RteFsUtils::Exists(filename) || !RteFsUtils::ReadFile(filename, inBuffer)) {
return false;
}
const YAML::Node& yamlRoot = YAML::LoadFile(filename);

// emit and load rootNode to ensure both comparison sides have the same formatting
YAML::Emitter emitter;
return CompareNodes(yamlRoot, YAML::Load((emitter << rootNode).c_str()));
const auto& outBuffer = string((emitter << rootNode).c_str()) + '\n';
return ProjMgrUtils::NormalizeLineEndings(EraseGeneratedByNode(inBuffer)) ==
ProjMgrUtils::NormalizeLineEndings(EraseGeneratedByNode(outBuffer));
}

bool ProjMgrYamlEmitter::CompareNodes(const YAML::Node& lhs, const YAML::Node& rhs) {
auto eraseGenNode = [](const string& inStr) {
size_t startIndex, endIndex;
string outStr = inStr;
startIndex = outStr.find(YAML_GENERATED_BY, 0);
endIndex = outStr.find('\n', startIndex);
if (startIndex != std::string::npos && endIndex != std::string::npos) {
outStr = outStr.erase(startIndex, endIndex - startIndex);
}
return outStr;
};

YAML::Emitter lhsEmitter, rhsEmitter;
string lhsData, rhsData;

Expand All @@ -118,8 +118,8 @@ bool ProjMgrYamlEmitter::CompareNodes(const YAML::Node& lhs, const YAML::Node& r
rhsEmitter << rhs;

// remove generated-by node from the string
lhsData = eraseGenNode(lhsEmitter.c_str());
rhsData = eraseGenNode(rhsEmitter.c_str());
lhsData = EraseGeneratedByNode(lhsEmitter.c_str());
rhsData = EraseGeneratedByNode(rhsEmitter.c_str());

return (lhsData == rhsData) ? true : false;
}
Expand Down
10 changes: 9 additions & 1 deletion tools/projmgr/test/src/ProjMgrUtilsUnitTests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2024 Arm Limited. All rights reserved.
* Copyright (c) 2020-2025 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -531,3 +531,11 @@ TEST_F(ProjMgrUtilsUnitTests, ULLToHex) {
EXPECT_EQ("0xDEADBEEF", ProjMgrUtils::ULLToHex(3735928559));
EXPECT_EQ("0xFFFFFFFF", ProjMgrUtils::ULLToHex(4294967295));
}

TEST_F(ProjMgrUtilsUnitTests, NormalizeLineEndings) {
EXPECT_EQ("abc\ndef\n", ProjMgrUtils::NormalizeLineEndings("abc\r\ndef\r\n"));
EXPECT_EQ("abc\ndef\n", ProjMgrUtils::NormalizeLineEndings("abc\rdef\r"));
EXPECT_EQ("abc\ndef\n", ProjMgrUtils::NormalizeLineEndings("abc\rdef\n"));
EXPECT_EQ("abc\ndef\n", ProjMgrUtils::NormalizeLineEndings("abc\ndef\r"));
EXPECT_EQ("abc\ndef\n", ProjMgrUtils::NormalizeLineEndings("abc\ndef\n"));
}
Loading