-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjMgrYamlSchemaChecker.cpp
121 lines (107 loc) · 3.28 KB
/
ProjMgrYamlSchemaChecker.cpp
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
/*
* Copyright (c) 2020-2022 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "ProjMgrYamlSchemaChecker.h"
#include "ProjMgrLogger.h"
#include "CrossPlatformUtils.h"
#include "RteFsUtils.h"
#include <string>
using namespace std;
ProjMgrYamlSchemaChecker::ProjMgrYamlSchemaChecker(void) {
// Reserved
}
ProjMgrYamlSchemaChecker::~ProjMgrYamlSchemaChecker(void) {
// Reserved
}
bool ProjMgrYamlSchemaChecker::Validate(const string& input,
ProjMgrYamlSchemaChecker::FileType type)
{
// Check if the input file exist
if (!RteFsUtils::Exists(input)) {
ProjMgrLogger::Error(input, " file doesn't exist");
return false;
}
// Get schema file path
string schemaFile;
if(!GetSchemaFile(schemaFile, type)) {
ProjMgrLogger::Warn(input, "yaml schemas were not found, file cannot be validated");
return true;
}
ClearErrors();
// Validate schema
bool result = ValidateFile(input, schemaFile);
for (auto& err : GetErrors()) {
ProjMgrLogger::Error(err.m_file, err.m_line, err.m_col, err.m_msg);
}
return result;
}
bool ProjMgrYamlSchemaChecker::GetSchemaFile(string& schemaFile, const ProjMgrYamlSchemaChecker::FileType& type) {
schemaFile = RteUtils::EMPTY_STRING;
// Get the schema file name
string schemaFileName;
switch (type)
{
case ProjMgrYamlSchemaChecker::FileType::DEFAULT:
schemaFileName = "cdefault.schema.json";
break;
case ProjMgrYamlSchemaChecker::FileType::SOLUTION:
schemaFileName = "csolution.schema.json";
break;
case ProjMgrYamlSchemaChecker::FileType::PROJECT:
schemaFileName = "cproject.schema.json";
break;
case ProjMgrYamlSchemaChecker::FileType::LAYER:
schemaFileName = "clayer.schema.json";
break;
case ProjMgrYamlSchemaChecker::FileType::BUILD:
schemaFileName = "cbuild.schema.json";
break;
case ProjMgrYamlSchemaChecker::FileType::BUILDIDX:
schemaFileName = "cbuild-idx.schema.json";
break;
case ProjMgrYamlSchemaChecker::FileType::BUILDSET:
schemaFileName = "cbuildset.schema.json";
break;
case ProjMgrYamlSchemaChecker::FileType::GENERATOR:
schemaFileName = "generator.schema.json";
break;
case ProjMgrYamlSchemaChecker::FileType::BUILDGEN:
schemaFileName = "cbuild-gen.schema.json";
break;
case ProjMgrYamlSchemaChecker::FileType::BUILDGENIDX:
schemaFileName = "cbuild-gen-idx.schema.json";
break;
case ProjMgrYamlSchemaChecker::FileType::GENERATOR_IMPORT:
schemaFileName = "cgen.schema.json";
break;
default:
ProjMgrLogger::Error("Unknown file type");
return false;
}
// Get current exe path
std::error_code ec;
string exePath = RteUtils::ExtractFilePath(
CrossPlatformUtils::GetExecutablePath(ec), true);
if (ec) {
ProjMgrLogger::Error(ec.message());
return false;
}
// Search schema in priority order
vector<string> relSearchOrder = { "./", "../etc/", "../../etc/" };
string schemaFilePath;
for (auto& relPath : relSearchOrder) {
schemaFilePath = exePath + relPath + schemaFileName;
if (RteFsUtils::Exists(schemaFilePath)) {
schemaFile = fs::canonical(schemaFilePath, ec).generic_string();
if (ec) {
ProjMgrLogger::Error(ec.message());
return false;
}
return true;
}
}
return false;
}
// end of ProjMgrYamlSchemaChecker