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
145 lines (130 loc) · 4.39 KB
/
Copy pathProjMgrYamlEmitter.cpp
File metadata and controls
145 lines (130 loc) · 4.39 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
/*
* Copyright (c) 2020-2026 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) {
string inBuffer;
if (!RteFsUtils::Exists(filename) || !RteFsUtils::ReadFile(filename, inBuffer)) {
return false;
}
YAML::Emitter emitter;
const auto& outBuffer = string((emitter << rootNode).c_str()) + '\n';
return RteUtils::EnsureLf(inBuffer) ==
RteUtils::EnsureLf(outBuffer);
}
bool ProjMgrYamlEmitter::CompareNodes(const YAML::Node& lhs, const YAML::Node& rhs) {
YAML::Emitter lhsEmitter, rhsEmitter;
string lhsData, rhsData;
// convert nodes into string
lhsEmitter << lhs;
rhsEmitter << rhs;
// remove generated-by node from the string
lhsData = lhsEmitter.c_str();
rhsData = 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;
}