forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteAppConfigWithSupportedRuntime.cs
171 lines (148 loc) · 6.06 KB
/
WriteAppConfigWithSupportedRuntime.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
// 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.IO;
using System.Linq;
using System.Xml.Linq;
using Microsoft.Build.Framework;
namespace Microsoft.NET.Build.Tasks
{
public sealed class WriteAppConfigWithSupportedRuntime : TaskBase
{
/// <summary>
/// Path to the app.config source file.
/// </summary>
public ITaskItem AppConfigFile { get; set; }
[Required]
public string TargetFrameworkIdentifier { get; set; }
[Required]
public string TargetFrameworkVersion { get; set; }
public string TargetFrameworkProfile { get; set; }
/// <summary>
/// Path to an intermediate file where we can write the input app.config plus the generated startup supportedRuntime
/// </summary>
[Required]
public ITaskItem OutputAppConfigFile { get; set; }
protected override void ExecuteCore()
{
XDocument doc = LoadAppConfig(AppConfigFile);
AddSupportedRuntimeToAppconfig(doc, TargetFrameworkIdentifier, TargetFrameworkVersion, TargetFrameworkProfile);
var fileStream = new FileStream(
OutputAppConfigFile.ItemSpec,
FileMode.Create,
FileAccess.Write,
FileShare.Read);
using (var stream = new StreamWriter(fileStream))
{
doc.Save(stream);
}
}
public static void AddSupportedRuntimeToAppconfig(
XDocument doc,
string targetFrameworkIdentifier,
string targetFrameworkVersion,
string targetFrameworkProfile = null)
{
XElement startupNode = doc.Root
.Nodes()
.OfType<XElement>()
.FirstOrDefault(e => e.Name.LocalName == "startup");
string runtimeVersion = string.Empty;
if (!HasExistingSupportedRuntime(startupNode))
{
if (TryGetSupportRuntimeNode(
targetFrameworkIdentifier,
targetFrameworkVersion,
targetFrameworkProfile,
runtimeVersion,
out XElement supportedRuntime))
{
if (startupNode == null)
{
startupNode = new XElement("startup");
doc.Root.Add(startupNode);
}
startupNode.Add(supportedRuntime);
}
}
}
private static bool HasExistingSupportedRuntime(XElement startupNode)
{
return startupNode != null
&& startupNode.Nodes().OfType<XElement>().Any(e => e.Name.LocalName == "supportedRuntime");
}
//https://github.com/dotnet/docs/blob/main/docs/framework/configure-apps/file-schema/startup/supportedruntime-element.md
private static bool TryGetSupportRuntimeNode(
string targetFrameworkIdentifier,
string targetFrameworkVersion,
string targetFrameworkProfile,
string runtimeVersion,
out XElement supportedRuntime)
{
supportedRuntime = null;
if (targetFrameworkIdentifier == ".NETFramework"
&& Version.TryParse(targetFrameworkVersion.TrimStart('v', 'V'), out Version parsedVersion))
{
if (parsedVersion.Major < 4)
{
string supportedRuntimeVersion = null;
if (parsedVersion.Major == 1 && parsedVersion.Minor >= 0 && parsedVersion.Minor > 1)
{
supportedRuntimeVersion = "v1.0.3705";
}
else if (parsedVersion.Major == 1 && parsedVersion.Minor >= 1)
{
supportedRuntimeVersion = "v1.1.4322";
}
else if (parsedVersion.Major >= 2 && parsedVersion.Major < 4)
{
supportedRuntimeVersion = "v2.0.50727";
}
if (supportedRuntimeVersion == null)
{
return false;
}
supportedRuntime =
new XElement(
"supportedRuntime",
new XAttribute("version", supportedRuntimeVersion));
return true;
}
else if (parsedVersion.Major == 4)
{
string profileInSku = targetFrameworkProfile != null ? $",Profile={targetFrameworkProfile}" : string.Empty;
supportedRuntime =
new XElement(
"supportedRuntime",
new XAttribute("version", "v4.0"),
new XAttribute("sku", $"{targetFrameworkIdentifier},Version={targetFrameworkVersion}{profileInSku}"));
return true;
}
}
return false;
}
/// <summary>
/// Load or create App.Config
/// </summary>
private XDocument LoadAppConfig(ITaskItem appConfigItem)
{
XDocument document;
if (appConfigItem == null)
{
document =
new XDocument(
new XDeclaration("1.0", "utf-8", "true"),
new XElement("configuration"));
}
else
{
document = XDocument.Load(appConfigItem.ItemSpec);
if (document.Root == null || document.Root.Name != "configuration")
{
throw new BuildErrorException(Strings.AppConfigRequiresRootConfiguration);
}
}
return document;
}
}
}