forked from Open-CMSIS-Pack/devtools
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProjMgrYamlEmitter.cpp
More file actions
156 lines (139 loc) · 4.79 KB
/
Copy pathProjMgrYamlEmitter.cpp
File metadata and controls
156 lines (139 loc) · 4.79 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
/*
* Copyright (c) 2020-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "ProductInfo.h"
#include "ProjMgrLogger.h"
#include "ProjMgrYamlEmitter.h"
#include "ProjMgrYamlParser.h"
#include "ProjMgrYamlSchemaChecker.h"
#include "ProjMgrWorker.h"
#include "RteFsUtils.h"
#include <filesystem>
#include <fstream>
using namespace std;
ProjMgrYamlEmitter::ProjMgrYamlEmitter(ProjMgrParser* parser, ProjMgrWorker* worker) :
m_parser(parser),
m_worker(worker),
m_checkSchema(false) {
// Reserved
}
ProjMgrYamlEmitter::~ProjMgrYamlEmitter(void) {
// Reserved
}
void ProjMgrYamlEmitter::SetCheckSchema(bool checkSchema) {
m_checkSchema = checkSchema;
}
void ProjMgrYamlEmitter::SetOutputDir(const std::string& outputDir) {
m_outputDir = outputDir;
}
bool ProjMgrYamlEmitter::WriteFile(YAML::Node& rootNode, const std::string& filename, const std::string& context, bool allowUpdate) {
// Compare yaml contents
if (RteFsUtils::IsDirectory(filename)) {
ProjMgrLogger::Get().Error("file cannot be written", context, filename);
return false;
}
else if (rootNode.size() == 0) {
// Remove file as nothing to write.
if (RteFsUtils::Exists(filename)) {
RteFsUtils::RemoveFile(filename);
ProjMgrLogger::Get().Info("file has been removed", context, filename);
}
else {
ProjMgrLogger::Get().Info("file skipped", context, filename);
}
}
else if (!CompareFile(filename, rootNode)) {
if (!allowUpdate) {
ProjMgrLogger::Get().Error("file not allowed to be updated", context, filename);
return false;
}
if (!RteFsUtils::MakeSureFilePath(filename)) {
ProjMgrLogger::Get().Error("destination directory cannot be created", context, filename);
return false;
}
ofstream fileStream(filename);
if (!fileStream) {
ProjMgrLogger::Get().Error("file cannot be written", context, filename);
return false;
}
YAML::Emitter emitter;
emitter.SetNullFormat(YAML::EmptyNull);
emitter.SetIntBase(YAML::Hex);
emitter << rootNode;
fileStream << emitter.c_str();
fileStream << endl;
fileStream << flush;
fileStream.close();
ProjMgrLogger::Get().Info("file generated successfully", context, filename);
// Check generated file schema
if (m_checkSchema && !ProjMgrYamlSchemaChecker().Validate(filename)) {
return false;
}
}
else {
ProjMgrLogger::Get().Info("file is already up-to-date", context, filename);
}
return true;
}
bool ProjMgrYamlEmitter::CompareFile(const string& filename, const YAML::Node& rootNode) {
if (!RteFsUtils::Exists(filename)) {
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()));
}
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;
// convert nodes into string
lhsEmitter << lhs;
rhsEmitter << rhs;
// remove generated-by node from the string
lhsData = eraseGenNode(lhsEmitter.c_str());
rhsData = eraseGenNode(rhsEmitter.c_str());
return (lhsData == rhsData) ? true : false;
}
bool ProjMgrYamlEmitter::NeedRebuild(const string& filename, const YAML::Node& newFile) {
if (!RteFsUtils::Exists(filename) || !RteFsUtils::IsRegularFile(filename)) {
return false;
}
const YAML::Node& oldFile = YAML::LoadFile(filename);
if (newFile[YAML_BUILD].IsDefined()) {
// cbuild.yml
if (newFile[YAML_BUILD][YAML_COMPILER].IsDefined() && oldFile[YAML_BUILD][YAML_COMPILER].IsDefined()) {
// compare compiler
return !CompareNodes(newFile[YAML_BUILD][YAML_COMPILER], oldFile[YAML_BUILD][YAML_COMPILER]);
}
}
else if (newFile[YAML_BUILD_IDX].IsDefined()) {
// cbuild-idx.yml
if (newFile[YAML_BUILD_IDX][YAML_CBUILDS].IsDefined() && oldFile[YAML_BUILD_IDX][YAML_CBUILDS].IsDefined()) {
// compare cbuilds
size_t size = newFile[YAML_BUILD_IDX][YAML_CBUILDS].size();
if (size != oldFile[YAML_BUILD_IDX][YAML_CBUILDS].size()) {
return true;
}
for (size_t i = 0; i < size; i++) {
if (!CompareNodes(newFile[YAML_BUILD_IDX][YAML_CBUILDS][i][YAML_CBUILD],
oldFile[YAML_BUILD_IDX][YAML_CBUILDS][i][YAML_CBUILD])) {
return true;
}
}
}
}
return false;
}