-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathGenerateToolsSettingsFile.cs
More file actions
107 lines (85 loc) · 4.08 KB
/
GenerateToolsSettingsFile.cs
File metadata and controls
107 lines (85 loc) · 4.08 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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using Microsoft.Build.Framework;
namespace Microsoft.NET.Build.Tasks
{
public class GenerateToolsSettingsFile : TaskBase
{
[Required]
public string EntryPointRelativePath { get; set; }
[Required]
public string CommandName { get; set; }
public string CommandRunner { get; set; }
public string RuntimeIdentifier { get; set; }
public string ToolPackageId { get; set; }
public string ToolPackageVersion { get; set; }
public ITaskItem[] ToolPackageRuntimeIdentifiers { get; set; }
[Required]
public string ToolsSettingsFilePath { get; set; }
protected override void ExecuteCore()
{
GenerateDocument(EntryPointRelativePath, CommandName, CommandRunner, RuntimeIdentifier, ToolPackageId, ToolPackageVersion, ToolPackageRuntimeIdentifiers)
.Save(ToolsSettingsFilePath);
}
internal static XDocument GenerateDocument(string entryPointRelativePath, string commandName, string commandRunner, string runtimeIdentifier,
string toolPackageId, string toolPackageVersion, ITaskItem[] toolPackageRuntimeIdentifiers)
{
// Format version should bump whenever the format changes such that it will break old consumers
int formatVersion = 1;
if (string.IsNullOrEmpty(commandRunner))
{
commandRunner = "dotnet";
}
if (commandRunner != "dotnet")
{
if (commandRunner == "executable")
{
formatVersion = 2;
}
else
{
throw new BuildErrorException(
string.Format(
Strings.UnsupportedToolCommandRunner,
commandRunner));
}
}
XElement runtimeIdentifierPackagesNode = null;
XElement commandNode = new XElement("Command",
new XAttribute("Name", commandName));
// Only generate RuntimeIdentifierPackages node for the primary package, when RuntimeIdentifier isn't set
if (string.IsNullOrEmpty(runtimeIdentifier) && (toolPackageRuntimeIdentifiers?.Any() ?? false))
{
formatVersion = 2;
runtimeIdentifierPackagesNode = new XElement("RuntimeIdentifierPackages");
foreach (var runtimeIdentifierPackage in toolPackageRuntimeIdentifiers)
{
string toolPackageRuntimeIdentifier = runtimeIdentifierPackage.ItemSpec;
var packageNode = new XElement("RuntimeIdentifierPackage");
packageNode.Add(new XAttribute("RuntimeIdentifier", toolPackageRuntimeIdentifier));
string ridPackageId = toolPackageId + "." + toolPackageRuntimeIdentifier;
packageNode.Add(new XAttribute("Id", ridPackageId));
runtimeIdentifierPackagesNode.Add(packageNode);
}
}
else
{
// EntryPoint and Runner are only set in packages with tool implementation, not in primary packages
// when there are RID-specific tool packages
commandNode.Add(new XAttribute("EntryPoint", entryPointRelativePath),
new XAttribute("Runner", commandRunner));
}
var dotnetCliToolNode = new XElement("DotNetCliTool",
new XAttribute("Version", formatVersion),
new XElement("Commands", commandNode));
if (runtimeIdentifierPackagesNode != null)
{
dotnetCliToolNode.Add(runtimeIdentifierPackagesNode);
}
return new XDocument(
new XDeclaration(version: null, encoding: null, standalone: null),
dotnetCliToolNode);
}
}
}