forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateAppHost.cs
146 lines (123 loc) · 6 KB
/
CreateAppHost.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Build.Framework;
using Microsoft.NET.HostModel;
using Microsoft.NET.HostModel.AppHost;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Creates the runtime host to be used for an application.
/// This embeds the application DLL path into the apphost and performs additional customizations as requested.
/// </summary>
public class CreateAppHost : TaskBase
{
/// <summary>
/// The number of additional retries to attempt for creating the apphost.
/// <summary>
/// <remarks>
/// The default is no retries because internally the `HostWriter` attempts to retry
/// on different I/O operations. Users can optionally retry at the task level if desired.
/// </remarks>
public const int DefaultRetries = 0;
/// The default delay, in milliseconds, for each retry attempt for creating the apphost.
/// </summary>
public const int DefaultRetryDelayMilliseconds = 1000;
[Required]
public string AppHostSourcePath { get; set; }
[Required]
public string AppHostDestinationPath { get; set; }
[Required]
public string AppBinaryName { get; set; }
[Required]
public string IntermediateAssembly { get; set; }
public bool WindowsGraphicalUserInterface { get; set; }
public int Retries { get; set; } = DefaultRetries;
public int RetryDelayMilliseconds { get; set; } = DefaultRetryDelayMilliseconds;
public bool EnableMacOSCodeSign { get; set; } = false;
public bool DisableCetCompat { get; set; } = false;
public ITaskItem[] DotNetSearchLocations { get; set; }
public string AppRelativeDotNet { get; set; } = null;
protected override void ExecuteCore()
{
try
{
var isGUI = WindowsGraphicalUserInterface;
var resourcesAssembly = IntermediateAssembly;
int attempts = 0;
while (true)
{
try
{
HostWriter.DotNetSearchOptions options = null;
if (DotNetSearchLocations?.Length > 0)
{
HostWriter.DotNetSearchOptions.SearchLocation searchLocation = default;
foreach (var locationItem in DotNetSearchLocations)
{
if (Enum.TryParse(locationItem.ItemSpec, out HostWriter.DotNetSearchOptions.SearchLocation location)
&& Enum.IsDefined(typeof(HostWriter.DotNetSearchOptions.SearchLocation), location))
{
searchLocation |= location;
}
else
{
throw new BuildErrorException(Strings.InvalidAppHostDotNetSearch, locationItem.ItemSpec);
}
}
options = new HostWriter.DotNetSearchOptions()
{
Location = searchLocation,
AppRelativeDotNet = AppRelativeDotNet
};
}
HostWriter.CreateAppHost(appHostSourceFilePath: AppHostSourcePath,
appHostDestinationFilePath: AppHostDestinationPath,
appBinaryFilePath: AppBinaryName,
windowsGraphicalUserInterface: isGUI,
assemblyToCopyResourcesFrom: resourcesAssembly,
enableMacOSCodeSign: EnableMacOSCodeSign,
disableCetCompat: DisableCetCompat,
dotNetSearchOptions: options);
return;
}
catch (Exception ex) when (ex is IOException ||
ex is UnauthorizedAccessException ||
(ex is AggregateException && (ex.InnerException is IOException || ex.InnerException is UnauthorizedAccessException)))
{
if (Retries < 0 || attempts == Retries)
{
throw;
}
++attempts;
string message = ex.Message;
if (ex is AggregateException)
{
message = ex.InnerException.Message;
}
Log.LogWarning(
string.Format(Strings.AppHostCreationFailedWithRetry,
attempts,
Retries + 1,
message));
if (RetryDelayMilliseconds > 0)
{
Thread.Sleep(RetryDelayMilliseconds);
}
}
}
}
catch (AppNameTooLongException ex)
{
throw new BuildErrorException(Strings.FileNameIsTooLong, ex.LongName);
}
catch (AppHostSigningException ex)
{
throw new BuildErrorException(Strings.AppHostSigningFailed, ex.Message, ex.ExitCode.ToString());
}
catch (PlaceHolderNotFoundInAppHostException ex)
{
throw new BuildErrorException(Strings.AppHostHasBeenModified, AppHostSourcePath, BitConverter.ToString(ex.MissingPattern));
}
}
}
}