forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateRuntimeConfigurationFiles.cs
241 lines (194 loc) · 8.41 KB
/
GenerateRuntimeConfigurationFiles.cs
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
// 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.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using NuGet.Frameworks;
using NuGet.ProjectModel;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Generates the $(project).runtimeconfig.json and optionally $(project).runtimeconfig.dev.json files
/// for a project.
/// </summary>
public class GenerateRuntimeConfigurationFiles : TaskBase
{
[Required]
public string AssetsFilePath { get; set; }
[Required]
public string TargetFramework { get; set; }
[Required]
public string TargetFrameworkMoniker { get; set; }
[Required]
public string RuntimeConfigPath { get; set; }
public string RuntimeConfigDevPath { get; set; }
public string RuntimeIdentifier { get; set; }
public string PlatformLibraryName { get; set; }
public string UserRuntimeConfig { get; set; }
public ITaskItem[] HostConfigurationOptions { get; set; }
public ITaskItem[] AdditionalProbingPaths { get; set; }
public bool IsSelfContained { get; set; }
List<ITaskItem> _filesWritten = new List<ITaskItem>();
[Output]
public ITaskItem[] FilesWritten
{
get { return _filesWritten.ToArray(); }
}
protected override void ExecuteCore()
{
bool writeDevRuntimeConfig = !string.IsNullOrEmpty(RuntimeConfigDevPath);
if (AdditionalProbingPaths?.Any() == true && !writeDevRuntimeConfig)
{
Log.LogWarning(Strings.SkippingAdditionalProbingPaths);
}
LockFile lockFile = new LockFileCache(BuildEngine4).GetLockFile(AssetsFilePath);
ProjectContext projectContext = lockFile.CreateProjectContext(
NuGetUtils.ParseFrameworkName(TargetFrameworkMoniker),
RuntimeIdentifier,
PlatformLibraryName,
IsSelfContained);
WriteRuntimeConfig(projectContext);
if (writeDevRuntimeConfig)
{
WriteDevRuntimeConfig(projectContext);
}
}
private void WriteRuntimeConfig(ProjectContext projectContext)
{
RuntimeConfig config = new RuntimeConfig();
config.RuntimeOptions = new RuntimeOptions();
AddFramework(config.RuntimeOptions, projectContext);
AddUserRuntimeOptions(config.RuntimeOptions);
// HostConfigurationOptions are added after AddUserRuntimeOptions so if there are
// conflicts the HostConfigurationOptions win. The reasoning is that HostConfigurationOptions
// can be changed using MSBuild properties, which can be specified at build time.
AddHostConfigurationOptions(config.RuntimeOptions);
WriteToJsonFile(RuntimeConfigPath, config);
_filesWritten.Add(new TaskItem(RuntimeConfigPath));
}
private void AddFramework(RuntimeOptions runtimeOptions, ProjectContext projectContext)
{
if (projectContext.IsFrameworkDependent)
{
var platformLibrary = projectContext.PlatformLibrary;
if (platformLibrary != null)
{
RuntimeConfigFramework framework = new RuntimeConfigFramework();
framework.Name = platformLibrary.Name;
framework.Version = platformLibrary.Version.ToNormalizedString();
runtimeOptions.Framework = framework;
runtimeOptions.tfm = TargetFramework;
}
}
}
private void AddUserRuntimeOptions(RuntimeOptions runtimeOptions)
{
if (string.IsNullOrEmpty(UserRuntimeConfig) || !File.Exists(UserRuntimeConfig))
{
return;
}
var rawRuntimeOptions = File.ReadAllText(UserRuntimeConfig);
var runtimeOptionsFromProject = JObject.Parse(rawRuntimeOptions);
foreach (var runtimeOption in runtimeOptionsFromProject)
{
runtimeOptions.RawOptions.Add(runtimeOption.Key, runtimeOption.Value);
}
}
private void AddHostConfigurationOptions(RuntimeOptions runtimeOptions)
{
if (HostConfigurationOptions == null || !HostConfigurationOptions.Any())
{
return;
}
JObject configProperties = GetConfigProperties(runtimeOptions);
foreach (var hostConfigurationOption in HostConfigurationOptions)
{
configProperties[hostConfigurationOption.ItemSpec] = GetConfigPropertyValue(hostConfigurationOption);
}
}
private static JObject GetConfigProperties(RuntimeOptions runtimeOptions)
{
JToken configProperties;
if (!runtimeOptions.RawOptions.TryGetValue("configProperties", out configProperties)
|| configProperties == null
|| configProperties.Type != JTokenType.Object)
{
configProperties = new JObject();
runtimeOptions.RawOptions["configProperties"] = configProperties;
}
return (JObject)configProperties;
}
private static JToken GetConfigPropertyValue(ITaskItem hostConfigurationOption)
{
string valueString = hostConfigurationOption.GetMetadata("Value");
bool boolValue;
if (bool.TryParse(valueString, out boolValue))
{
return new JValue(boolValue);
}
int intValue;
if (int.TryParse(valueString, out intValue))
{
return new JValue(intValue);
}
return new JValue(valueString);
}
private void WriteDevRuntimeConfig(ProjectContext projectContext)
{
RuntimeConfig devConfig = new RuntimeConfig();
devConfig.RuntimeOptions = new RuntimeOptions();
AddAdditionalProbingPaths(devConfig.RuntimeOptions, projectContext);
WriteToJsonFile(RuntimeConfigDevPath, devConfig);
_filesWritten.Add(new TaskItem(RuntimeConfigDevPath));
}
private void AddAdditionalProbingPaths(RuntimeOptions runtimeOptions, ProjectContext projectContext)
{
if (runtimeOptions.AdditionalProbingPaths == null)
{
runtimeOptions.AdditionalProbingPaths = new List<string>();
}
// Add the specified probing paths first so they are probed first
if (AdditionalProbingPaths?.Any() == true)
{
foreach (var additionalProbingPath in AdditionalProbingPaths)
{
runtimeOptions.AdditionalProbingPaths.Add(additionalProbingPath.ItemSpec);
}
}
foreach (var packageFolder in projectContext.LockFile.PackageFolders)
{
// DotNetHost doesn't handle additional probing paths with a trailing slash
runtimeOptions.AdditionalProbingPaths.Add(EnsureNoTrailingDirectorySeparator(packageFolder.Path));
}
}
private static string EnsureNoTrailingDirectorySeparator(string path)
{
if (!string.IsNullOrEmpty(path))
{
char lastChar = path[path.Length - 1];
if (lastChar == Path.DirectorySeparatorChar)
{
path = path.Substring(0, path.Length - 1);
}
}
return path;
}
private static void WriteToJsonFile(string fileName, object value)
{
JsonSerializer serializer = new JsonSerializer();
serializer.ContractResolver = new CamelCasePropertyNamesContractResolver();
serializer.Formatting = Formatting.Indented;
serializer.DefaultValueHandling = DefaultValueHandling.Ignore;
using (JsonTextWriter writer = new JsonTextWriter(new StreamWriter(File.Create(fileName))))
{
serializer.Serialize(writer, value);
}
}
}
}