-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathMergeRuntimeConfigFiles.cs
More file actions
72 lines (58 loc) · 2.77 KB
/
Copy pathMergeRuntimeConfigFiles.cs
File metadata and controls
72 lines (58 loc) · 2.77 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.IO;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Build.Framework;
#nullable enable
namespace Xamarin.MacDev.Tasks {
// This task merges the 'runtimeOptions.configProperties' from a '*.runtimeconfig.dev.json' file on top of
// the ones from the main '*.runtimeconfig.json' file, writing the merged result to an output file.
//
// We do this because we hand the runtime configuration directly to the runtime (bypassing hostfxr), and
// hostfxr is what would normally merge the dev file (which contains Debug-only switches such as Hot Reload)
// into the main runtime configuration. The dev values win, matching hostfxr's behavior.
public class MergeRuntimeConfigFiles : XamarinTask {
[Required]
public string RuntimeConfigFile { get; set; } = "";
public string? RuntimeConfigDevFile { get; set; }
[Required]
public string OutputFile { get; set; } = "";
static readonly JsonDocumentOptions documentOptions = new JsonDocumentOptions {
AllowTrailingCommas = true,
CommentHandling = JsonCommentHandling.Skip,
};
public override bool Execute ()
{
var mainNode = JsonNode.Parse (File.ReadAllText (RuntimeConfigFile), documentOptions: documentOptions);
if (mainNode is not JsonObject mainObject) {
Log.LogError (MSBStrings.E7185 /* The runtime configuration file '{0}' is not a valid JSON object. */, RuntimeConfigFile);
return false;
}
if (!string.IsNullOrEmpty (RuntimeConfigDevFile) && File.Exists (RuntimeConfigDevFile)) {
var devNode = JsonNode.Parse (File.ReadAllText (RuntimeConfigDevFile!), documentOptions: documentOptions);
var devConfigProperties = (devNode as JsonObject)? ["runtimeOptions"]? ["configProperties"] as JsonObject;
if (devConfigProperties is not null && devConfigProperties.Count > 0) {
var runtimeOptions = mainObject ["runtimeOptions"] as JsonObject;
if (runtimeOptions is null) {
runtimeOptions = new JsonObject ();
mainObject ["runtimeOptions"] = runtimeOptions;
}
var configProperties = runtimeOptions ["configProperties"] as JsonObject;
if (configProperties is null) {
configProperties = new JsonObject ();
runtimeOptions ["configProperties"] = configProperties;
}
foreach (var property in devConfigProperties) {
configProperties [property.Key] = property.Value is null ? null : JsonNode.Parse (property.Value.ToJsonString ());
}
}
}
var outputDirectory = Path.GetDirectoryName (OutputFile);
if (!string.IsNullOrEmpty (outputDirectory))
Directory.CreateDirectory (outputDirectory!);
File.WriteAllText (OutputFile, mainObject.ToJsonString (new JsonSerializerOptions { WriteIndented = true }));
return !Log.HasLoggedErrors;
}
}
}