-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
runtime_config.cpp
220 lines (191 loc) · 5.71 KB
/
runtime_config.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include "../pal/pal.h"
#include "../pal/trace.h"
#include "../pal/pal_utils.h"
#include "cpprest/json.h"
#include "runtime_config.h"
#include <cassert>
runtime_config_t::runtime_config_t(const pal::string_t& path, const pal::string_t& dev_path)
: m_patch_roll_fwd(true)
, m_prerelease_roll_fwd(false)
, m_path(path)
, m_dev_path(dev_path)
, m_portable(false)
{
m_valid = ensure_parsed();
trace::verbose(_X("Runtime config [%s] is valid=[%d]"), path.c_str(), m_valid);
}
bool runtime_config_t::parse_opts(const json_value& opts)
{
// Note: both runtime_config and dev_runtime_config call into the function.
// runtime_config will override whatever dev_runtime_config populated.
if (opts.is_null())
{
return true;
}
const auto& opts_obj = opts.as_object();
auto properties = opts_obj.find(_X("configProperties"));
if (properties != opts_obj.end())
{
const auto& prop_obj = properties->second.as_object();
for (const auto& property : prop_obj)
{
m_properties[property.first] = property.second.is_string()
? property.second.as_string()
: property.second.to_string();
}
}
auto probe_paths = opts_obj.find(_X("additionalProbingPaths"));
if (probe_paths != opts_obj.end())
{
if (probe_paths->second.is_string())
{
m_probe_paths.insert(m_probe_paths.begin(), probe_paths->second.as_string());
}
else
{
const auto& arr = probe_paths->second.as_array();
for (auto iter = arr.rbegin(); iter != arr.rend(); iter++)
{
m_probe_paths.push_front(iter->as_string());
}
}
}
auto patch_roll_fwd = opts_obj.find(_X("applyPatches"));
if (patch_roll_fwd != opts_obj.end())
{
m_patch_roll_fwd = patch_roll_fwd->second.as_bool();
}
auto prerelease_roll_fwd = opts_obj.find(_X("preReleaseRollForward"));
if (prerelease_roll_fwd != opts_obj.end())
{
m_prerelease_roll_fwd = prerelease_roll_fwd->second.as_bool();
}
auto framework = opts_obj.find(_X("framework"));
if (framework == opts_obj.end())
{
return true;
}
m_portable = true;
const auto& fx_obj = framework->second.as_object();
m_fx_name = fx_obj.at(_X("name")).as_string();
m_fx_ver = fx_obj.at(_X("version")).as_string();
return true;
}
bool runtime_config_t::ensure_dev_config_parsed()
{
trace::verbose(_X("Attempting to read dev runtime config: %s"), m_dev_path.c_str());
pal::string_t retval;
if (!pal::file_exists(m_dev_path))
{
// Not existing is not an error.
return true;
}
pal::ifstream_t file(m_dev_path);
if (!file.good())
{
trace::verbose(_X("File stream not good %s"), m_dev_path.c_str());
return false;
}
if (skip_utf8_bom(&file))
{
trace::verbose(_X("UTF-8 BOM skipped while reading [%s]"), m_dev_path.c_str());
}
try
{
const auto root = json_value::parse(file);
const auto& json = root.as_object();
const auto iter = json.find(_X("runtimeOptions"));
if (iter != json.end())
{
parse_opts(iter->second);
}
}
catch (const std::exception& je)
{
pal::string_t jes;
(void) pal::utf8_palstring(je.what(), &jes);
trace::error(_X("A JSON parsing exception occurred in [%s]: %s"), m_dev_path.c_str(), jes.c_str());
return false;
}
return true;
}
bool runtime_config_t::ensure_parsed()
{
trace::verbose(_X("Attempting to read runtime config: %s"), m_path.c_str());
if (!ensure_dev_config_parsed())
{
trace::verbose(_X("Did not successfully parse the runtimeconfig.dev.json"));
}
pal::string_t retval;
if (!pal::file_exists(m_path))
{
// Not existing is not an error.
return true;
}
pal::ifstream_t file(m_path);
if (!file.good())
{
trace::verbose(_X("File stream not good %s"), m_path.c_str());
return false;
}
if (skip_utf8_bom(&file))
{
trace::verbose(_X("UTF-8 BOM skipped while reading [%s]"), m_path.c_str());
}
try
{
const auto root = json_value::parse(file);
const auto& json = root.as_object();
const auto iter = json.find(_X("runtimeOptions"));
if (iter != json.end())
{
parse_opts(iter->second);
}
}
catch (const std::exception& je)
{
pal::string_t jes;
(void) pal::utf8_palstring(je.what(), &jes);
trace::error(_X("A JSON parsing exception occurred in [%s]: %s"), m_path.c_str(), jes.c_str());
return false;
}
return true;
}
const pal::string_t& runtime_config_t::get_fx_name() const
{
assert(m_valid);
return m_fx_name;
}
const pal::string_t& runtime_config_t::get_fx_version() const
{
assert(m_valid);
return m_fx_ver;
}
bool runtime_config_t::get_patch_roll_fwd() const
{
assert(m_valid);
return m_patch_roll_fwd;
}
bool runtime_config_t::get_prerelease_roll_fwd() const
{
assert(m_valid);
return m_prerelease_roll_fwd;
}
bool runtime_config_t::get_portable() const
{
return m_portable;
}
const std::list<pal::string_t>& runtime_config_t::get_probe_paths() const
{
return m_probe_paths;
}
void runtime_config_t::config_kv(std::vector<pal::string_t>* keys, std::vector<pal::string_t>* values) const
{
for (const auto& kv : m_properties)
{
keys->push_back(kv.first);
values->push_back(kv.second);
}
}