FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SharpGenTools/SharpGen/Config/ConfigFile.cs at main · SharpGenTools/SharpGenTools · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
SharpGenTools
/
SharpGenTools
Public
Notifications
You must be signed in to change notification settings
Fork
60
Star
536
Code
Issues
44
Pull requests
1
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SharpGenTools
/
SharpGen
/
Config
/
ConfigFile.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
482 lines (405 loc) · 16.4 KB
Breadcrumbs
SharpGenTools
/
SharpGen
/
Config
/
ConfigFile.cs
Copy path
File metadata and controls
482 lines (405 loc) · 16.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using
System
;
using
System
.
Collections
;
using
System
.
Collections
.
Generic
;
using
System
.
Globalization
;
using
System
.
IO
;
using
System
.
Linq
;
using
System
.
Reflection
;
using
System
.
Text
.
RegularExpressions
;
using
System
.
Threading
;
using
System
.
Xml
;
using
System
.
Xml
.
Serialization
;
using
SharpGen
.
Logging
;
namespace
SharpGen
.
Config
;
/// <summary>
/// Config File.
/// </summary>
[
XmlRoot
(
"config"
,
Namespace
=
XmlNamespace
)
]
public
partial
class
ConfigFile
{
internal
const
string
XmlNamespace
=
"urn:SharpGen.Config"
;
private
static
readonly
Lazy
<
XmlSerializer
>
Serializer
=
new
(
static
(
)
=>
new
XmlSerializer
(
typeof
(
ConfigFile
)
)
,
LazyThreadSafetyMode
.
PublicationOnly
)
;
private
static
readonly
Lazy
<
XmlSerializerNamespaces
>
XmlNamespaces
=
new
(
static
(
)
=>
new
XmlSerializerNamespaces
(
new
XmlQualifiedName
[
]
{
new
(
string
.
Empty
,
XmlNamespace
)
}
)
,
LazyThreadSafetyMode
.
PublicationOnly
)
;
/// <summary>
/// Gets dynamic variables used by dynamic variable substitution #(MyVariable)
/// </summary>
/// <value>The dynamic variables.</value>
[
XmlIgnore
]
public
Dictionary
<
string
,
string
>
DynamicVariables
{
get
;
private
set
;
}
=
new
Dictionary
<
string
,
string
>
(
)
;
/// <summary>
/// Gets or sets the parent of this mapping file.
/// </summary>
/// <value>The parent.</value>
[
XmlIgnore
]
public
ConfigFile
Parent
{
get
;
set
;
}
/// <summary>
/// Gets or sets the path of this MappingFile. If not null, used when saving this file.
/// </summary>
/// <value>The path.</value>
[
XmlIgnore
]
public
string
FilePath
{
get
;
set
;
}
[
XmlIgnore
]
public
string
AbsoluteFilePath
{
get
{
if
(
FilePath
==
null
)
return
null
;
if
(
Path
.
IsPathRooted
(
FilePath
)
)
return
FilePath
;
if
(
Parent
?
.
AbsoluteFilePath
!=
null
)
return
Path
.
Combine
(
Path
.
GetDirectoryName
(
Parent
.
AbsoluteFilePath
)
,
FilePath
)
;
return
Path
.
GetFullPath
(
Path
.
Combine
(
"."
,
FilePath
)
)
;
}
}
[
XmlAttribute
(
"id"
)
]
public
string
Id
{
get
=>
id
??=
Guid
.
NewGuid
(
)
.
ToString
(
)
;
set
=>
id
=
value
;
}
[
XmlElement
(
"depends"
)
]
public
List
<
string
>
Depends
{
get
;
set
;
}
=
new
List
<
string
>
(
)
;
[
XmlElement
(
"namespace"
)
]
public
string
Namespace
{
get
;
set
;
}
[
XmlElement
(
"assembly"
)
]
public
string
Assembly
{
// The assembly attribute is no longer supported, but we leave the option
// so that config files don't break when the option is specified.
get
=>
string
.
Empty
;
set
{
}
}
[
XmlElement
(
"var"
)
]
public
List
<
KeyValue
>
Variables
{
get
;
set
;
}
=
new
List
<
KeyValue
>
(
)
;
[
XmlElement
(
"file"
)
]
public
List
<
string
>
Files
{
get
;
set
;
}
=
new
List
<
string
>
(
)
;
[
XmlIgnore
]
public
List
<
ConfigFile
>
References
{
get
;
set
;
}
=
new
List
<
ConfigFile
>
(
)
;
[
XmlElement
(
"sdk"
)
]
public
List
<
SdkRule
>
Sdks
{
get
;
set
;
}
=
new
List
<
SdkRule
>
(
)
;
[
XmlElement
(
"include-dir"
)
]
public
List
<
IncludeDirRule
>
IncludeDirs
{
get
;
set
;
}
=
new
List
<
IncludeDirRule
>
(
)
;
[
XmlElement
(
"include-prolog"
)
]
public
List
<
string
>
IncludeProlog
{
get
;
set
;
}
=
new
List
<
string
>
(
)
;
[
XmlElement
(
"include"
)
]
public
List
<
IncludeRule
>
Includes
{
get
;
set
;
}
=
new
List
<
IncludeRule
>
(
)
;
[
XmlArray
(
"naming"
)
,
XmlArrayItem
(
typeof
(
NamingRuleShort
)
)
]
public
List
<
NamingRule
>
Naming
{
get
;
set
;
}
=
new
List
<
NamingRule
>
(
)
;
[
XmlElement
(
"context-set"
)
]
public
List
<
ContextSetRule
>
ContextSets
{
get
;
set
;
}
[
XmlArray
(
"extension"
)
]
[
XmlArrayItem
(
typeof
(
ContextRule
)
)
]
[
XmlArrayItem
(
typeof
(
ClearContextRule
)
)
]
[
XmlArrayItem
(
typeof
(
CreateExtensionRule
)
)
]
[
XmlArrayItem
(
typeof
(
CreateCppExtensionRule
)
)
]
[
XmlArrayItem
(
typeof
(
DefineExtensionRule
)
)
]
[
XmlArrayItem
(
typeof
(
ConstantRule
)
)
]
public
List
<
ExtensionBaseRule
>
Extension
{
get
;
set
;
}
=
new
List
<
ExtensionBaseRule
>
(
)
;
[
XmlIgnore
]
public
string
ExtensionId
=>
Id
+
"-ext"
;
/// <summary>
/// Gets the name of the extension header file.
/// </summary>
[
XmlIgnore
]
public
string
ExtensionFileName
=>
ExtensionId
+
".h"
;
/// <summary>
/// Gets the name of this configs' primary header file.
/// </summary>
[
XmlIgnore
]
public
string
HeaderFileName
=>
Id
+
".h"
;
[
XmlArray
(
"bindings"
)
]
public
List
<
BindRule
>
Bindings
{
get
;
set
;
}
=
new
List
<
BindRule
>
(
)
;
[
XmlArray
(
"mapping"
)
]
[
XmlArrayItem
(
typeof
(
MappingRule
)
)
]
[
XmlArrayItem
(
typeof
(
MoveRule
)
)
]
[
XmlArrayItem
(
typeof
(
RemoveRule
)
)
]
[
XmlArrayItem
(
typeof
(
ContextRule
)
)
]
[
XmlArrayItem
(
typeof
(
ClearContextRule
)
)
]
public
List
<
ConfigBaseRule
>
Mappings
{
get
;
set
;
}
=
new
List
<
ConfigBaseRule
>
(
)
;
/// <summary>
/// Finds all dependencies ConfigFile from this instance.
/// </summary>
/// <param name="dependencyListOutput">The dependencies set to fill.</param>
public
void
FindAllDependencies
(
ISet
<
ConfigFile
>
dependencyListOutput
)
{
ConfigFile
ConfigSelector
(
string
dependConfigFileId
)
=>
GetRoot
(
)
.
mapIdToFile
[
dependConfigFileId
]
;
foreach
(
var
linkedConfig
in
Depends
.
Select
(
ConfigSelector
)
)
{
dependencyListOutput
.
Add
(
linkedConfig
)
;
linkedConfig
.
FindAllDependencies
(
dependencyListOutput
)
;
}
}
/// <summary>
/// Finds a context set by Id.
/// </summary>
/// <param name="contextSetId">The context set id.</param>
/// <returns></returns>
public
ContextSetRule
FindContextSetById
(
string
contextSetId
)
{
return
ContextSets
?
.
FirstOrDefault
(
contextSetRule
=>
contextSetRule
.
Id
==
contextSetId
)
;
}
/// <summary>
/// Expands all dynamic variables used inside Bindings and Mappings tags.
/// </summary>
/// <param name="expandDynamicVariable">if set to <c>true</c> expand dynamic variables.</param>
public
void
ExpandVariables
(
bool
expandDynamicVariable
,
Logger
logger
)
{
ExpandVariables
(
Variables
,
expandDynamicVariable
,
logger
)
;
ExpandVariables
(
Includes
,
expandDynamicVariable
,
logger
)
;
ExpandVariables
(
IncludeDirs
,
expandDynamicVariable
,
logger
)
;
ExpandVariables
(
Bindings
,
expandDynamicVariable
,
logger
)
;
ExpandVariables
(
Mappings
,
expandDynamicVariable
,
logger
)
;
// Do it recursively
foreach
(
var
configFile
in
References
)
configFile
.
ExpandVariables
(
expandDynamicVariable
,
logger
)
;
}
/// <summary>
/// Iterate on all objects and sub-objects to expand dynamic variable
/// </summary>
/// <param name="objectToExpand">The object to expand.</param>
/// <returns>the expanded object</returns>
private
object
ExpandVariables
(
object
objectToExpand
,
bool
expandDynamicVariable
,
Logger
logger
)
{
switch
(
objectToExpand
)
{
case
null
:
return
null
;
case
string
str
:
return
ExpandString
(
str
,
expandDynamicVariable
,
logger
)
;
case
IList
list
:
{
for
(
int
i
=
0
,
count
=
list
.
Count
;
i
<
count
;
i
++
)
list
[
i
]
=
ExpandVariables
(
list
[
i
]
,
expandDynamicVariable
,
logger
)
;
return
list
;
}
}
var
type
=
objectToExpand
.
GetType
(
)
;
if
(
!
type
.
GetTypeInfo
(
)
.
IsPrimitive
)
{
foreach
(
var
propertyInfo
in
type
.
GetRuntimeProperties
(
)
)
{
if
(
propertyInfo
.
GetCustomAttributes
<
XmlIgnoreAttribute
>
(
false
)
.
Any
(
)
)
continue
;
// Check that this field is "ShouldSerializable"
var
method
=
type
.
GetRuntimeMethod
(
"ShouldSerialize"
+
propertyInfo
.
Name
,
Type
.
EmptyTypes
)
;
if
(
method
is
not
null
&&
!
(
bool
)
method
.
Invoke
(
objectToExpand
,
null
)
)
continue
;
var
oldValue
=
propertyInfo
.
GetValue
(
objectToExpand
,
null
)
;
var
newValue
=
ExpandVariables
(
oldValue
,
expandDynamicVariable
,
logger
)
;
if
(
!
ReferenceEquals
(
oldValue
,
newValue
)
)
propertyInfo
.
SetValue
(
objectToExpand
,
newValue
,
null
)
;
}
}
return
objectToExpand
;
}
/// <summary>
/// Gets a variable value. Value is expanded if it contains any reference to other variables.
/// </summary>
/// <param name="variableName">Name of the variable.</param>
/// <returns>the value of this variable</returns>
private
string
GetVariable
(
string
variableName
,
Logger
logger
)
{
foreach
(
var
keyValue
in
Variables
)
{
if
(
keyValue
.
Name
==
variableName
)
return
ExpandString
(
keyValue
.
Value
,
false
,
logger
)
;
}
return
Parent
?
.
GetVariable
(
variableName
,
logger
)
;
}
/// <summary>
/// Regex used to expand variable
/// </summary>
private
static
readonly
Regex
ReplaceVariableRegex
=
new
(
@"\$\(([a-zA-Z_][\w_]*)\)"
,
RegexOptions
.
Compiled
)
;
private
static
readonly
Regex
ReplaceDynamicVariableRegex
=
new
(
@"#\(([a-zA-Z_][\w_]*)\)"
,
RegexOptions
.
Compiled
)
;
/// <summary>
/// Expands a string using environment variable and variables defined in mapping files.
/// </summary>
/// <param name="str">The string to expand.</param>
/// <returns>the expanded string</returns>
public
string
ExpandString
(
string
str
,
bool
expandDynamicVariable
,
Logger
logger
)
{
var
result
=
str
;
// Perform Config Variable substitution
if
(
ReplaceVariableRegex
.
Match
(
result
)
.
Success
)
{
result
=
ReplaceVariableRegex
.
Replace
(
result
,
match
=>
{
var
name
=
match
.
Groups
[
1
]
.
Value
;
var
localResult
=
GetVariable
(
name
,
logger
)
??
Environment
.
GetEnvironmentVariable
(
name
)
;
if
(
localResult
is
not
null
)
return
localResult
;
logger
.
Error
(
LoggingCodes
.
UnknownVariable
,
"Unable to substitute config/environment variable $({0}). Variable is not defined"
,
name
)
;
return
string
.
Empty
;
}
)
;
}
// Perform Dynamic Variable substitution
if
(
expandDynamicVariable
&&
ReplaceDynamicVariableRegex
.
Match
(
result
)
.
Success
)
{
result
=
ReplaceDynamicVariableRegex
.
Replace
(
result
,
match
=>
{
var
name
=
match
.
Groups
[
1
]
.
Value
;
if
(
GetRoot
(
)
.
DynamicVariables
.
TryGetValue
(
name
,
out
var
localResult
)
)
return
localResult
.
Trim
(
'"'
)
;
logger
.
Error
(
LoggingCodes
.
UnknownDynamicVariable
,
"Unable to substitute dynamic variable #({0}). Variable is not defined"
,
name
)
;
return
string
.
Empty
;
}
)
;
}
return
result
;
}
private
void
PostLoad
(
ConfigFile
parent
,
string
file
,
string
[
]
macros
,
IReadOnlyCollection
<
KeyValue
>
variables
,
Logger
logger
)
{
FilePath
=
file
;
Parent
=
parent
;
if
(
AbsoluteFilePath
!=
null
)
{
Variables
.
Add
(
new
KeyValue
(
"THIS_CONFIG_PATH"
,
Path
.
GetDirectoryName
(
AbsoluteFilePath
)
)
)
;
}
Variables
.
AddRange
(
variables
)
;
// Load all dependencies
foreach
(
var
dependFile
in
Files
)
{
var
dependFilePath
=
ExpandString
(
dependFile
,
false
,
logger
)
;
if
(
!
Path
.
IsPathRooted
(
dependFilePath
)
&&
AbsoluteFilePath
!=
null
)
dependFilePath
=
Path
.
Combine
(
Path
.
GetDirectoryName
(
AbsoluteFilePath
)
,
dependFilePath
)
;
var
subMapping
=
Load
(
this
,
dependFilePath
,
macros
,
variables
,
logger
)
;
if
(
subMapping
!=
null
)
{
subMapping
.
FilePath
=
dependFile
;
References
.
Add
(
subMapping
)
;
}
}
// Clear all depends file
Files
.
Clear
(
)
;
// Add this mapping file
GetRoot
(
)
.
mapIdToFile
.
Add
(
Id
,
this
)
;
}
public
IReadOnlyCollection
<
ConfigFile
>
ConfigFilesLoaded
=>
GetRoot
(
)
.
mapIdToFile
.
Values
;
/// <summary>
/// Loads the specified config file attached to a parent config file.
/// </summary>
/// <returns>The loaded config</returns>
private
static
ConfigFile
Load
(
ConfigFile
parent
,
string
file
,
string
[
]
macros
,
IReadOnlyCollection
<
KeyValue
>
variables
,
Logger
logger
)
{
if
(
!
File
.
Exists
(
file
)
)
{
logger
.
Error
(
LoggingCodes
.
ConfigNotFound
,
"Configuration file {0} not found."
,
file
)
;
return
null
;
}
try
{
logger
.
PushLocation
(
file
)
;
using
MemoryStream
preprocessedStream
=
new
(
)
;
{
using
var
configStream
=
File
.
OpenRead
(
file
)
;
using
var
configReader
=
XmlReader
.
Create
(
configStream
)
;
Preprocessor
.
Preprocess
(
configReader
,
preprocessedStream
,
macros
)
;
}
preprocessedStream
.
Position
=
0
;
if
(
Serializer
.
Value
.
Deserialize
(
preprocessedStream
)
is
ConfigFile
config
)
{
config
.
PostLoad
(
parent
,
file
,
macros
,
variables
,
logger
)
;
return
config
;
}
}
catch
(
Exception
ex
)
{
logger
.
Error
(
LoggingCodes
.
UnableToParseConfig
,
"Unable to parse file [{0}]"
,
ex
,
file
)
;
}
finally
{
logger
.
PopLocation
(
)
;
}
return
null
;
}
public
ConfigFile
GetRoot
(
)
{
var
root
=
this
;
while
(
root
.
Parent
!=
null
)
root
=
root
.
Parent
;
return
root
;
}
private
readonly
Dictionary
<
string
,
ConfigFile
>
mapIdToFile
=
new
Dictionary
<
string
,
ConfigFile
>
(
)
;
private
string
id
;
private
void
Verify
(
Logger
logger
)
{
Depends
.
Remove
(
""
)
;
// TODO: verify Depends
foreach
(
var
depend
in
Depends
)
{
if
(
GetRoot
(
)
.
mapIdToFile
.
ContainsKey
(
depend
)
)
continue
;
logger
.
Error
(
LoggingCodes
.
MissingConfigDependency
,
"Unable to resolve dependency [{0}] for config file [{1}]"
,
depend
,
Id
)
;
}
foreach
(
var
includeDir
in
IncludeDirs
)
{
includeDir
.
Path
=
ExpandString
(
includeDir
.
Path
,
false
,
logger
)
;
if
(
includeDir
.
Path
.
StartsWith
(
"="
)
||
Directory
.
Exists
(
includeDir
.
Path
)
)
continue
;
logger
.
Error
(
LoggingCodes
.
IncludeDirectoryNotFound
,
"Include directory {0} from config file [{1}] not found"
,
includeDir
.
Path
,
Id
)
;
}
// Verify all dependencies
foreach
(
var
mappingFile
in
References
)
mappingFile
.
Verify
(
logger
)
;
}
public
void
Load
(
string
file
,
string
[
]
macros
,
Logger
logger
,
params
KeyValue
[
]
variables
)
{
PostLoad
(
null
,
file
,
macros
,
variables
,
logger
)
;
Verify
(
logger
)
;
ExpandVariables
(
false
,
logger
)
;
}
public
void
Write
(
string
file
)
{
using
var
output
=
File
.
Create
(
file
)
;
Write
(
output
)
;
}
/// <summary>
/// Writes this MappingFile to the disk.
/// </summary>
/// <param name="writer">The writer.</param>
public
void
Write
(
Stream
writer
)
{
Serializer
.
Value
.
Serialize
(
writer
,
this
,
XmlNamespaces
.
Value
)
;
}
/// <inheritdoc />
[
ExcludeFromCodeCoverage
]
public
override
string
ToString
(
)
{
return
string
.
Format
(
CultureInfo
.
InvariantCulture
,
"config {0}"
,
Id
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL